[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

/src/bp-xprofile/classes/ -> class-bp-xprofile-field.php (source)

   1  <?php
   2  /**
   3   * BuddyPress XProfile Classes.
   4   *
   5   * @package BuddyPress
   6   * @subpackage XProfileClasses
   7   * @since 1.0.0
   8   */
   9  
  10  // Exit if accessed directly.
  11  defined( 'ABSPATH' ) || exit;
  12  
  13  /**
  14   * Class to help set up XProfile fields.
  15   *
  16   * @since 1.0.0
  17   */
  18  class BP_XProfile_Field {
  19  
  20      /**
  21       * Field ID.
  22       *
  23       * @since 1.0.0
  24       * @var int ID of field.
  25       */
  26      public $id;
  27  
  28      /**
  29       * Field group ID.
  30       *
  31       * @since 1.0.0
  32       * @var int Field group ID for field.
  33       */
  34      public $group_id;
  35  
  36      /**
  37       * Field parent ID.
  38       *
  39       * @since 1.0.0
  40       * @var int Parent ID of field.
  41       */
  42      public $parent_id;
  43  
  44      /**
  45       * Field type.
  46       *
  47       * @since 1.0.0
  48       * @var string Field type.
  49       */
  50      public $type;
  51  
  52      /**
  53       * Field name.
  54       *
  55       * @since 1.0.0
  56       * @var string Field name.
  57       */
  58      public $name;
  59  
  60      /**
  61       * Field description.
  62       *
  63       * @since 1.0.0
  64       * @var string Field description.
  65       */
  66      public $description;
  67  
  68      /**
  69       * Required field?
  70       *
  71       * @since 1.0.0
  72       * @var bool Is field required to be filled out?
  73       */
  74      public $is_required;
  75  
  76      /**
  77       * Deletable field?
  78       *
  79       * @since 1.0.0
  80       * @var int Can field be deleted?
  81       */
  82      public $can_delete = '1';
  83  
  84      /**
  85       * Field position.
  86       *
  87       * @since 1.0.0
  88       * @var int Field position.
  89       */
  90      public $field_order;
  91  
  92      /**
  93       * Option order.
  94       *
  95       * @since 1.0.0
  96       * @var int Option order.
  97       */
  98      public $option_order;
  99  
 100      /**
 101       * Order child fields.
 102       *
 103       * @since 1.0.0
 104       * @var string Order child fields by.
 105       */
 106      public $order_by;
 107  
 108      /**
 109       * Is this the default option?
 110       *
 111       * @since 1.0.0
 112       * @var bool Is this the default option for this field?
 113       */
 114      public $is_default_option;
 115  
 116      /**
 117       * Field data visibility.
 118       *
 119       * @since 1.9.0
 120       * @since 2.4.0 Property marked protected. Now accessible by magic method or by `get_default_visibility()`.
 121       * @var string Default field data visibility.
 122       */
 123      protected $default_visibility;
 124  
 125      /**
 126       * Is the visibility able to be modified?
 127       *
 128       * @since 2.3.0
 129       * @since 2.4.0 Property marked protected. Now accessible by magic method or by `get_allow_custom_visibility()`.
 130       * @var string Members are allowed/disallowed to modify data visibility.
 131       */
 132      protected $allow_custom_visibility;
 133  
 134      /**
 135       * Whether values from this field are autolinked to directory searches.
 136       *
 137       * @since 2.5.0
 138       * @var bool
 139       */
 140      public $do_autolink;
 141  
 142      /**
 143       * The signup position of the field into the signups form.
 144       *
 145       * @since 8.0.0
 146       * @var int
 147       */
 148      public $signup_position;
 149  
 150      /**
 151       * Field type option.
 152       *
 153       * @since 2.0.0
 154       * @var BP_XProfile_Field_Type Field type object used for validation.
 155       */
 156      public $type_obj = null;
 157  
 158      /**
 159       * Field data for user ID.
 160       *
 161       * @since 2.0.0
 162       * @var BP_XProfile_ProfileData Field data for user ID.
 163       */
 164      public $data;
 165  
 166      /**
 167       * Member types to which the profile field should be applied.
 168       *
 169       * @since 2.4.0
 170       * @var array Array of member types.
 171       */
 172      protected $member_types;
 173  
 174      /**
 175       * Initialize and/or populate profile field.
 176       *
 177       * @since 1.1.0
 178       *
 179       * @param int|null $id Field ID.
 180       * @param int|null $user_id User ID.
 181       * @param bool     $get_data Get data.
 182       */
 183  	public function __construct( $id = null, $user_id = null, $get_data = true ) {
 184  
 185          if ( ! empty( $id ) ) {
 186              $this->populate( $id, $user_id, $get_data );
 187  
 188          // Initialize the type obj to prevent fatals when creating new profile fields.
 189          } else {
 190              $this->type_obj            = bp_xprofile_create_field_type( 'textbox' );
 191              $this->type_obj->field_obj = $this;
 192          }
 193  
 194          /**
 195           * Fires when the xProfile field object has been constructed.
 196           *
 197           * @since 8.0.0
 198           *
 199           * @param BP_XProfile_Field $this The xProfile field object.
 200           */
 201          do_action( 'bp_xprofile_field', $this );
 202      }
 203  
 204      /**
 205       * Populate a profile field object.
 206       *
 207       * @since 1.1.0
 208       *
 209       * @global object $wpdb
 210       * @global object $userdata
 211       *
 212       * @param int      $id Field ID.
 213       * @param int|null $user_id User ID.
 214       * @param bool     $get_data Get data.
 215       */
 216  	public function populate( $id, $user_id = null, $get_data = true ) {
 217          global $wpdb, $userdata;
 218  
 219          if ( empty( $user_id ) ) {
 220              $user_id = isset( $userdata->ID ) ? $userdata->ID : 0;
 221          }
 222  
 223          $field = wp_cache_get( $id, 'bp_xprofile_fields' );
 224          if ( false === $field ) {
 225              $bp = buddypress();
 226  
 227              $field = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$bp->profile->table_name_fields} WHERE id = %d", $id ) );
 228  
 229              if ( ! $field ) {
 230                  return false;
 231              }
 232  
 233              wp_cache_add( $id, $field, 'bp_xprofile_fields' );
 234          }
 235  
 236          $this->fill_data( $field );
 237  
 238          if ( ! empty( $get_data ) && ! empty( $user_id ) ) {
 239              $this->data = $this->get_field_data( $user_id );
 240          }
 241      }
 242  
 243      /**
 244       * Retrieve a `BP_XProfile_Field` instance.
 245       *
 246       * @since 2.4.0
 247       * @since 2.8.0 Added `$user_id` and `$get_data` parameters.
 248       *
 249       * @static
 250       *
 251       * @param int      $field_id ID of the field.
 252       * @param int|null $user_id  Optional. ID of the user associated with the field.
 253       *                           Ignored if `$get_data` is false. If `$get_data` is
 254       *                           true, but no `$user_id` is provided, defaults to
 255       *                           logged-in user ID.
 256       * @param bool     $get_data Whether to fetch data for the specified `$user_id`.
 257       * @return BP_XProfile_Field|false Field object if found, otherwise false.
 258       */
 259  	public static function get_instance( $field_id, $user_id = null, $get_data = true ) {
 260          global $wpdb;
 261  
 262          $field_id = (int) $field_id;
 263          if ( ! $field_id ) {
 264              return false;
 265          }
 266  
 267          return new self( $field_id, $user_id, $get_data );
 268      }
 269  
 270      /**
 271       * Fill object vars based on data passed to the method.
 272       *
 273       * @since 2.4.0
 274       *
 275       * @param array|object $args Array or object representing the `BP_XProfile_Field` properties.
 276       *                           Generally, this is a row from the fields database table.
 277       */
 278  	public function fill_data( $args ) {
 279          if ( is_object( $args ) ) {
 280              $args = (array) $args;
 281          }
 282  
 283          $int_fields = array(
 284              'id', 'is_required', 'group_id', 'parent_id', 'is_default_option',
 285              'field_order', 'option_order', 'can_delete'
 286          );
 287  
 288          foreach ( $args as $k => $v ) {
 289              if ( 'name' === $k || 'description' === $k ) {
 290                  $v = stripslashes( $v );
 291              }
 292  
 293              // Cast numeric strings as integers.
 294              if ( true === in_array( $k, $int_fields ) ) {
 295                  $v = (int) $v;
 296              }
 297  
 298              $this->{$k} = $v;
 299          }
 300  
 301          // Create the field type and store a reference back to this object.
 302          $this->type_obj            = bp_xprofile_create_field_type( $this->type );
 303          $this->type_obj->field_obj = $this;
 304      }
 305  
 306      /**
 307       * Magic getter.
 308       *
 309       * @since 2.4.0
 310       *
 311       * @param string $key Property name.
 312       * @return string|null
 313       */
 314  	public function __get( $key ) {
 315          switch ( $key ) {
 316              case 'default_visibility' :
 317                  return $this->get_default_visibility();
 318                  break;
 319  
 320              case 'allow_custom_visibility' :
 321                  return $this->get_allow_custom_visibility();
 322                  break;
 323          }
 324      }
 325  
 326      /**
 327       * Magic issetter.
 328       *
 329       * @since 2.4.0
 330       *
 331       * @param string $key Property name.
 332       * @return bool
 333       */
 334  	public function __isset( $key ) {
 335          switch ( $key ) {
 336              // Backward compatibility for when these were public methods.
 337              case 'allow_custom_visibility' :
 338              case 'default_visibility' :
 339                  return true;
 340                  break;
 341          }
 342      }
 343  
 344      /**
 345       * Delete a profile field.
 346       *
 347       * @since 1.1.0
 348       *
 349       * @global object $wpdb
 350       *
 351       * @param boolean $delete_data Whether or not to delete data.
 352       * @return boolean
 353       */
 354  	public function delete( $delete_data = false ) {
 355          global $wpdb;
 356  
 357          // Prevent deletion if no ID is present.
 358          // Prevent deletion by url when can_delete is false.
 359          // Prevent deletion of option 1 since this invalidates fields with options.
 360          if ( empty( $this->id ) || empty( $this->can_delete ) || ( $this->parent_id && $this->option_order == 1 ) ) {
 361              return false;
 362          }
 363  
 364          /**
 365           * Fires before the current field instance gets deleted.
 366           *
 367           * @since 3.0.0
 368           *
 369           * @param BP_XProfile_Field $this        Current instance of the field being deleted. Passed by reference.
 370           * @param bool              $delete_data Whether or not to delete data.
 371           */
 372          do_action_ref_array( 'xprofile_field_before_delete', array( &$this, $delete_data ) );
 373  
 374          $bp  = buddypress();
 375          $sql = $wpdb->prepare( "DELETE FROM {$bp->profile->table_name_fields} WHERE id = %d OR parent_id = %d", $this->id, $this->id );
 376  
 377          if ( ! $wpdb->query( $sql ) ) {
 378              return false;
 379          }
 380  
 381          // Delete all metadata for this field.
 382          bp_xprofile_delete_meta( $this->id, 'field' );
 383  
 384          // Delete the data in the DB for this field.
 385          if ( true === $delete_data ) {
 386              BP_XProfile_ProfileData::delete_for_field( $this->id );
 387          }
 388  
 389          /**
 390           * Fires after the current field instance gets deleted.
 391           *
 392           * @since 3.0.0
 393           *
 394           * @param BP_XProfile_Field $this        Current instance of the field being deleted. Passed by reference.
 395           * @param bool              $delete_data Whether or not to delete data.
 396           */
 397          do_action_ref_array( 'xprofile_field_after_delete', array( &$this, $delete_data ) );
 398  
 399          return true;
 400      }
 401  
 402      /**
 403       * Save a profile field.
 404       *
 405       * @since 1.1.0
 406       *
 407       * @global object $wpdb
 408       *
 409       * @return boolean
 410       */
 411  	public function save() {
 412          global $wpdb;
 413  
 414          $bp = buddypress();
 415  
 416          $this->group_id     = apply_filters( 'xprofile_field_group_id_before_save',     $this->group_id,     $this->id );
 417          $this->parent_id    = apply_filters( 'xprofile_field_parent_id_before_save',    $this->parent_id,    $this->id );
 418          $this->type         = apply_filters( 'xprofile_field_type_before_save',         $this->type,         $this->id );
 419          $this->name         = apply_filters( 'xprofile_field_name_before_save',         $this->name,         $this->id );
 420          $this->description  = apply_filters( 'xprofile_field_description_before_save',  $this->description,  $this->id );
 421          $this->is_required  = apply_filters( 'xprofile_field_is_required_before_save',  $this->is_required,  $this->id );
 422          $this->order_by        = apply_filters( 'xprofile_field_order_by_before_save',     $this->order_by,     $this->id );
 423          $this->field_order  = apply_filters( 'xprofile_field_field_order_before_save',  $this->field_order,  $this->id );
 424          $this->option_order = apply_filters( 'xprofile_field_option_order_before_save', $this->option_order, $this->id );
 425          $this->can_delete   = apply_filters( 'xprofile_field_can_delete_before_save',   $this->can_delete,   $this->id );
 426          $this->type_obj     = bp_xprofile_create_field_type( $this->type );
 427  
 428          /**
 429           * Fires before the current field instance gets saved.
 430           *
 431           * Please use this hook to filter the properties above. Each part will be passed in.
 432           *
 433           * @since 1.0.0
 434           *
 435           * @param BP_XProfile_Field $this Current instance of the field being saved.
 436           */
 437          do_action_ref_array( 'xprofile_field_before_save', array( $this ) );
 438  
 439          $is_new_field = is_null( $this->id );
 440  
 441          if ( ! $is_new_field ) {
 442              $sql = $wpdb->prepare( "UPDATE {$bp->profile->table_name_fields} SET group_id = %d, parent_id = 0, type = %s, name = %s, description = %s, is_required = %d, order_by = %s, field_order = %d, option_order = %d, can_delete = %d, is_default_option = %d WHERE id = %d", $this->group_id, $this->type, $this->name, $this->description, $this->is_required, $this->order_by, $this->field_order, $this->option_order, $this->can_delete, $this->is_default_option, $this->id );
 443          } else {
 444              $sql = $wpdb->prepare( "INSERT INTO {$bp->profile->table_name_fields} (group_id, parent_id, type, name, description, is_required, order_by, field_order, option_order, can_delete, is_default_option ) VALUES ( %d, %d, %s, %s, %s, %d, %s, %d, %d, %d, %d )", $this->group_id, $this->parent_id, $this->type, $this->name, $this->description, $this->is_required, $this->order_by, $this->field_order, $this->option_order, $this->can_delete, $this->is_default_option );
 445          }
 446  
 447          /**
 448           * Check for null so field options can be changed without changing any
 449           * other part of the field. The described situation will return 0 here.
 450           */
 451          if ( $wpdb->query( $sql ) !== null ) {
 452  
 453              if ( $is_new_field ) {
 454                  $this->id = $wpdb->insert_id;
 455              }
 456  
 457              // Only do this if we are editing an existing field.
 458              if ( ! $is_new_field ) {
 459  
 460                  /**
 461                   * Remove any radio or dropdown options for this
 462                   * field. They will be re-added if needed.
 463                   * This stops orphan options if the user changes a
 464                   * field from a radio button field to a text box.
 465                   */
 466                  $this->delete_children();
 467              }
 468  
 469              /**
 470               * Check to see if this is a field with child options.
 471               * We need to add the options to the db, if it is.
 472               */
 473              if ( $this->type_obj->supports_options ) {
 474  
 475                  $parent_id = $this->id;
 476  
 477                  // Allow plugins to filter the field's child options (i.e. the items in a selectbox).
 478                  $post_option  = ! empty( $_POST["{$this->type}_option"]           ) ? $_POST["{$this->type}_option"]           : '';
 479                  $post_default = ! empty( $_POST["isDefault_{$this->type}_option"] ) ? $_POST["isDefault_{$this->type}_option"] : '';
 480  
 481                  /**
 482                   * Filters the submitted field option value before saved.
 483                   *
 484                   * @since 1.5.0
 485                   *
 486                   * @param string            $post_option Submitted option value.
 487                   * @param BP_XProfile_Field $type        Current field type being saved for.
 488                   */
 489                  $options      = apply_filters( 'xprofile_field_options_before_save', $post_option,  $this->type );
 490  
 491                  /**
 492                   * Filters the default field option value before saved.
 493                   *
 494                   * @since 1.5.0
 495                   *
 496                   * @param string            $post_default Default option value.
 497                   * @param BP_XProfile_Field $type         Current field type being saved for.
 498                   */
 499                  $defaults     = apply_filters( 'xprofile_field_default_before_save', $post_default, $this->type );
 500  
 501                  $counter = 1;
 502                  if ( !empty( $options ) ) {
 503                      foreach ( (array) $options as $option_key => $option_value ) {
 504                          $is_default = 0;
 505  
 506                          if ( is_array( $defaults ) ) {
 507                              if ( isset( $defaults[ $option_key ] ) ) {
 508                                  $is_default = 1;
 509                              }
 510                          } else {
 511                              if ( (int) $defaults == $option_key ) {
 512                                  $is_default = 1;
 513                              }
 514                          }
 515  
 516                          if ( '' != $option_value ) {
 517                              $sql = $wpdb->prepare( "INSERT INTO {$bp->profile->table_name_fields} (group_id, parent_id, type, name, description, is_required, option_order, is_default_option) VALUES (%d, %d, 'option', %s, '', 0, %d, %d)", $this->group_id, $parent_id, $option_value, $counter, $is_default );
 518                              if ( ! $wpdb->query( $sql ) ) {
 519                                  return false;
 520                              }
 521                          }
 522  
 523                          $counter++;
 524                      }
 525                  }
 526              }
 527  
 528              /**
 529               * Fires after the current field instance gets saved.
 530               *
 531               * @since 1.0.0
 532               *
 533               * @param BP_XProfile_Field $this Current instance of the field being saved.
 534               */
 535              do_action_ref_array( 'xprofile_field_after_save', array( $this ) );
 536  
 537              // Recreate type_obj in case someone changed $this->type via a filter.
 538              $this->type_obj            = bp_xprofile_create_field_type( $this->type );
 539              $this->type_obj->field_obj = $this;
 540  
 541              return $this->id;
 542          } else {
 543              return false;
 544          }
 545      }
 546  
 547      /**
 548       * Get field data for a user ID.
 549       *
 550       * @since 1.2.0
 551       *
 552       * @param int $user_id ID of the user to get field data for.
 553       * @return BP_XProfile_ProfileData
 554       */
 555  	public function get_field_data( $user_id = 0 ) {
 556          return new BP_XProfile_ProfileData( $this->id, $user_id );
 557      }
 558  
 559      /**
 560       * Get all child fields for this field ID.
 561       *
 562       * @since 1.2.0
 563       *
 564       * @global BuddyPress $bp The one true BuddyPress instance.
 565       * @global wpdb $wpdb WordPress database object.
 566       *
 567       * @param bool $for_editing Whether or not the field is for editing. Default to false.
 568       * @return array
 569       */
 570  	public function get_children( $for_editing = false ) {
 571          global $wpdb;
 572  
 573          // Sanitize 'order_by'.
 574          $order_by = bp_esc_sql_order( $this->order_by );
 575  
 576          // This is done here so we don't have problems with sql injection.
 577          if ( empty( $for_editing ) ) {
 578              $sort_sql = "ORDER BY name {$order_by}";
 579          } else {
 580              $sort_sql = 'ORDER BY option_order ASC';
 581          }
 582  
 583          // This eliminates a problem with getting all fields when there is no
 584          // id for the object.
 585          if ( empty( $this->id ) ) {
 586              $parent_id = -1;
 587          } else {
 588              $parent_id = $this->id;
 589          }
 590  
 591          $bp       = buddypress();
 592          $sql      = $wpdb->prepare( "SELECT * FROM {$bp->profile->table_name_fields} WHERE parent_id = %d AND group_id = %d {$sort_sql}", $parent_id, $this->group_id );
 593          $children = $wpdb->get_results( $sql );
 594  
 595          /**
 596           * Filters the found children for a field.
 597           *
 598           * @since 1.2.5
 599           * @since 3.0.0 Added the `$this` parameter.
 600           *
 601           * @param array             $children    Found children for a field.
 602           * @param bool              $for_editing Whether or not the field is for editing.
 603           * @param BP_XProfile_Field $this        Field object
 604           */
 605          return apply_filters( 'bp_xprofile_field_get_children', $children, $for_editing, $this );
 606      }
 607  
 608      /**
 609       * Delete all field children for this field.
 610       *
 611       * @since 1.2.0
 612       *
 613       * @global object $wpdb
 614       */
 615  	public function delete_children() {
 616          global $wpdb;
 617  
 618          $bp  = buddypress();
 619          $sql = $wpdb->prepare( "DELETE FROM {$bp->profile->table_name_fields} WHERE parent_id = %d", $this->id );
 620  
 621          $wpdb->query( $sql );
 622      }
 623  
 624      /**
 625       * Gets the member types to which this field should be available.
 626       *
 627       * Will not return inactive member types, even if associated metadata is found.
 628       *
 629       * 'null' is a special pseudo-type, which represents users that do not have a member type.
 630       *
 631       * @since 2.4.0
 632       *
 633       * @return array Array of member type names.
 634       */
 635  	public function get_member_types() {
 636          if ( ! is_null( $this->member_types ) ) {
 637              return $this->member_types;
 638          }
 639  
 640          $raw_types = bp_xprofile_get_meta( $this->id, 'field', 'member_type', false );
 641  
 642          // If `$raw_types` is not an array, it probably means this is a new field (id=0).
 643          if ( ! is_array( $raw_types ) ) {
 644              $raw_types = array();
 645          }
 646  
 647          // If '_none' is found in the array, it overrides all types.
 648          $types = array();
 649          if ( ! in_array( '_none', $raw_types ) ) {
 650              $registered_types = bp_get_member_types();
 651  
 652              // Eliminate invalid member types saved in the database.
 653              foreach ( $raw_types as $raw_type ) {
 654                  // 'null' is a special case - it represents users without a type.
 655                  if ( 'null' === $raw_type || isset( $registered_types[ $raw_type ] ) ) {
 656                      $types[] = $raw_type;
 657                  }
 658              }
 659  
 660              // If no member types have been saved, interpret as *all* member types.
 661              if ( empty( $types ) ) {
 662                  $types = array_values( $registered_types );
 663  
 664                  // + the "null" type, ie users without a type.
 665                  $types[] = 'null';
 666              }
 667          }
 668  
 669          /**
 670           * Filters the member types to which an XProfile object should be applied.
 671           *
 672           * @since 2.4.0
 673           *
 674           * @param array             $types Member types.
 675           * @param BP_XProfile_Field $field Field object.
 676           */
 677          $this->member_types = apply_filters( 'bp_xprofile_field_member_types', $types, $this );
 678  
 679          return $this->member_types;
 680      }
 681  
 682      /**
 683       * Sets the member types for this field.
 684       *
 685       * @since 2.4.0
 686       *
 687       * @param array $member_types Array of member types. Can include 'null' (users with no type) in addition to any
 688       *                            registered types.
 689       * @param bool  $append       Whether to append to existing member types. If false, all existing member type
 690       *                            associations will be deleted before adding your `$member_types`. Default false.
 691       * @return array Member types for the current field, after being saved.
 692       */
 693  	public function set_member_types( $member_types, $append = false ) {
 694          // Unset invalid member types.
 695          $types = array();
 696          foreach ( $member_types as $member_type ) {
 697              // 'null' is a special case - it represents users without a type.
 698              if ( 'null' === $member_type || bp_get_member_type_object( $member_type ) ) {
 699                  $types[] = $member_type;
 700              }
 701          }
 702  
 703          // When `$append` is false, delete all existing types before adding new ones.
 704          if ( ! $append ) {
 705              bp_xprofile_delete_meta( $this->id, 'field', 'member_type' );
 706  
 707              /*
 708               * We interpret an empty array as disassociating the field from all types. This is
 709               * represented internally with the '_none' flag.
 710               */
 711              if ( empty( $types ) ) {
 712                  bp_xprofile_add_meta( $this->id, 'field', 'member_type', '_none' );
 713              }
 714          }
 715  
 716          /*
 717           * Unrestricted fields are represented in the database as having no 'member_type'.
 718           * We detect whether a field is being set to unrestricted by checking whether the
 719           * list of types passed to the method is the same as the list of registered types,
 720           * plus the 'null' pseudo-type.
 721           */
 722          $_rtypes  = bp_get_member_types();
 723          $rtypes   = array_values( $_rtypes );
 724          $rtypes[] = 'null';
 725  
 726          sort( $types );
 727          sort( $rtypes );
 728  
 729          // Only save if this is a restricted field.
 730          if ( $types !== $rtypes ) {
 731              // Save new types.
 732              foreach ( $types as $type ) {
 733                  bp_xprofile_add_meta( $this->id, 'field', 'member_type', $type );
 734              }
 735          }
 736  
 737          // Reset internal cache of member types.
 738          $this->member_types = null;
 739  
 740          /**
 741           * Fires after a field's member types have been updated.
 742           *
 743           * @since 2.4.0
 744           *
 745           * @param BP_XProfile_Field $this Field object.
 746           */
 747          do_action( 'bp_xprofile_field_set_member_type', $this );
 748  
 749          // Refetch fresh items from the database.
 750          return $this->get_member_types();
 751      }
 752  
 753      /**
 754       * Gets a label representing the field's member types.
 755       *
 756       * This label is displayed alongside the field's name on the Profile Fields Dashboard panel.
 757       *
 758       * @since 2.4.0
 759       *
 760       * @return string
 761       */
 762  	public function get_member_type_label() {
 763          // Field 1 is always displayed to everyone, so never gets a label.
 764          if ( 1 == $this->id ) {
 765              return '';
 766          }
 767  
 768          // Return an empty string if no member types are registered.
 769          $all_types = bp_get_member_types();
 770          if ( empty( $all_types ) ) {
 771              return '';
 772          }
 773  
 774          $member_types = $this->get_member_types();
 775  
 776          // If the field applies to all member types, show no message.
 777          $all_types[] = 'null';
 778          if ( array_values( $all_types ) == $member_types ) {
 779              return '';
 780          }
 781  
 782          $label = '';
 783          if ( ! empty( $member_types ) ) {
 784              $has_null = false;
 785              $member_type_labels = array();
 786              foreach ( $member_types as $member_type ) {
 787                  if ( 'null' === $member_type ) {
 788                      $has_null = true;
 789                      continue;
 790                  } else {
 791                      $mt_obj = bp_get_member_type_object( $member_type );
 792                      $member_type_labels[] = $mt_obj->labels['name'];
 793                  }
 794              }
 795  
 796              // Alphabetical sort.
 797              natcasesort( $member_type_labels );
 798              $member_type_labels = array_values( $member_type_labels );
 799  
 800              // Add the 'null' option to the end of the list.
 801              if ( $has_null ) {
 802                  $member_type_labels[] = __( 'Users with no member type', 'buddypress' );
 803              }
 804  
 805              /* translators: %s: comma separated list of member types */
 806              $label = sprintf( __( '(Member types: %s)', 'buddypress' ), implode( ', ', array_map( 'esc_html', $member_type_labels ) ) );
 807          } else {
 808              $label = '<span class="member-type-none-notice">' . __( '(Unavailable to all members)', 'buddypress' ) . '</span>';
 809          }
 810  
 811          return $label;
 812      }
 813  
 814      /**
 815       * Get the field's default visibility setting.
 816       *
 817       * Lazy-loaded to reduce overhead.
 818       *
 819       * Defaults to 'public' if no visibility setting is found in the database.
 820       *
 821       * @since 2.4.0
 822       *
 823       * @return string
 824       */
 825  	public function get_default_visibility() {
 826          if ( ! isset( $this->default_visibility ) ) {
 827              $this->default_visibility = 'public';
 828              $this->visibility         = '';
 829  
 830              if ( isset( $this->type_obj->visibility ) && $this->type_obj->visibility ) {
 831                  $this->visibility = $this->type_obj->visibility;
 832              }
 833  
 834              if ( $this->field_type_supports( 'allow_custom_visibility' ) ) {
 835                  $this->visibility = bp_xprofile_get_meta( $this->id, 'field', 'default_visibility' );
 836              }
 837  
 838              if ( $this->visibility ) {
 839                  $this->default_visibility = $this->visibility;
 840              }
 841          }
 842  
 843          return $this->default_visibility;
 844      }
 845  
 846      /**
 847       * Get whether the field's default visibility can be overridden by users.
 848       *
 849       * Lazy-loaded to reduce overhead.
 850       *
 851       * Defaults to 'allowed'.
 852       *
 853       * @since 4.4.0
 854       *
 855       * @return string 'disabled' or 'allowed'.
 856       */
 857  	public function get_allow_custom_visibility() {
 858          if ( ! isset( $this->allow_custom_visibility ) ) {
 859              $allow_custom_visibility = bp_xprofile_get_meta( $this->id, 'field', 'allow_custom_visibility' );
 860  
 861              if ( 'disabled' === $allow_custom_visibility ) {
 862                  $this->allow_custom_visibility = 'disabled';
 863              } else {
 864                  $this->allow_custom_visibility = 'allowed';
 865              }
 866          }
 867  
 868          return $this->allow_custom_visibility;
 869      }
 870  
 871      /**
 872       * Get the field's signup position.
 873       *
 874       * @since 8.0.0
 875       *
 876       * @return int the field's signup position.
 877       *             0 if the field has not been added to the signup form.
 878       */
 879  	public function get_signup_position() {
 880          if ( ! isset( $this->signup_position ) ) {
 881              $this->signup_position = (int) bp_xprofile_get_meta( $this->id, 'field', 'signup_position' );
 882          }
 883  
 884          return $this->signup_position;
 885      }
 886  
 887      /**
 888       * Get whether the field values should be auto-linked to a directory search.
 889       *
 890       * Lazy-loaded to reduce overhead.
 891       *
 892       * Defaults to true for multi and default fields, false for single fields.
 893       *
 894       * @since 2.5.0
 895       *
 896       * @return bool
 897       */
 898  	public function get_do_autolink() {
 899          if ( ! isset( $this->do_autolink ) ) {
 900              $do_autolink = bp_xprofile_get_meta( $this->id, 'field', 'do_autolink' );
 901  
 902              if ( '' === $do_autolink ) {
 903                  $this->do_autolink = $this->type_obj->supports_options;
 904              } else {
 905                  $this->do_autolink = 'on' === $do_autolink;
 906              }
 907          }
 908  
 909          /**
 910           * Filters the autolink property of the field.
 911           *
 912           * @since 6.0.0
 913           *
 914           * @param bool              $do_autolink The autolink property of the field.
 915           * @param BP_XProfile_Field $this Field object.
 916           */
 917          return apply_filters( 'bp_xprofile_field_do_autolink', $this->do_autolink, $this );
 918      }
 919  
 920      /* Static Methods ********************************************************/
 921  
 922      /**
 923       * Get the type for provided field ID.
 924       *
 925       * @param int $field_id Field ID to get type of.
 926       * @return bool|null|string
 927       */
 928  	public static function get_type( $field_id = 0 ) {
 929          global $wpdb;
 930  
 931          // Bail if no field ID.
 932          if ( empty( $field_id ) ) {
 933              return false;
 934          }
 935  
 936          $bp   = buddypress();
 937          $sql  = $wpdb->prepare( "SELECT type FROM {$bp->profile->table_name_fields} WHERE id = %d", $field_id );
 938          $type = $wpdb->get_var( $sql );
 939  
 940          // Return field type.
 941          if ( ! empty( $type ) ) {
 942              return $type;
 943          }
 944  
 945          return false;
 946      }
 947  
 948      /**
 949       * Delete all fields in a field group.
 950       *
 951       * @since 1.2.0
 952       *
 953       * @global object $wpdb
 954       *
 955       * @param int $group_id ID of the field group to delete fields from.
 956       * @return boolean
 957       */
 958  	public static function delete_for_group( $group_id = 0 ) {
 959          global $wpdb;
 960  
 961          // Bail if no group ID.
 962          if ( empty( $group_id ) ) {
 963              return false;
 964          }
 965  
 966          $bp      = buddypress();
 967          $sql     = $wpdb->prepare( "DELETE FROM {$bp->profile->table_name_fields} WHERE group_id = %d", $group_id );
 968          $deleted = $wpdb->get_var( $sql );
 969  
 970          // Return true if fields were deleted.
 971          if ( false !== $deleted ) {
 972              return true;
 973          }
 974  
 975          return false;
 976      }
 977  
 978      /**
 979       * Get field ID from field name.
 980       *
 981       * @since 1.5.0
 982       *
 983       * @global object $wpdb
 984       *
 985       * @param string $field_name Name of the field to query the ID for.
 986       * @return int|null Field ID on success; null on failure.
 987       */
 988  	public static function get_id_from_name( $field_name = '' ) {
 989          global $wpdb;
 990  
 991          $bp = buddypress();
 992  
 993          if ( empty( $bp->profile->table_name_fields ) || empty( $field_name ) ) {
 994              return false;
 995          }
 996  
 997          $id = bp_core_get_incremented_cache( $field_name, 'bp_xprofile_fields_by_name' );
 998          if ( false === $id ) {
 999              $sql = $wpdb->prepare( "SELECT id FROM {$bp->profile->table_name_fields} WHERE name = %s AND parent_id = 0", $field_name );
1000              $id = $wpdb->get_var( $sql );
1001              bp_core_set_incremented_cache( $field_name, 'bp_xprofile_fields_by_name', $id );
1002          }
1003  
1004          return is_numeric( $id ) ? (int) $id : $id;
1005      }
1006  
1007      /**
1008       * Update field position and/or field group when relocating.
1009       *
1010       * @since 1.5.0
1011       *
1012       * @global object $wpdb
1013       *
1014       * @param int      $field_id       ID of the field to update.
1015       * @param int|null $position       Field position to update.
1016       * @param int|null $field_group_id ID of the field group.
1017       * @return boolean
1018       */
1019  	public static function update_position( $field_id, $position = null, $field_group_id = null ) {
1020          global $wpdb;
1021  
1022          // Bail if invalid position or field group.
1023          if ( ! is_numeric( $position ) || ! is_numeric( $field_group_id ) ) {
1024              return false;
1025          }
1026  
1027          // Get table name and field parent.
1028          $table_name = buddypress()->profile->table_name_fields;
1029          $sql        = $wpdb->prepare( "UPDATE {$table_name} SET field_order = %d, group_id = %d WHERE id = %d", $position, $field_group_id, $field_id );
1030          $parent     = $wpdb->query( $sql );
1031  
1032          $retval = false;
1033  
1034          // Update $field_id with new $position and $field_group_id.
1035          if ( ! empty( $parent ) && ! is_wp_error( $parent ) ) {
1036  
1037              // Update any children of this $field_id.
1038              $sql = $wpdb->prepare( "UPDATE {$table_name} SET group_id = %d WHERE parent_id = %d", $field_group_id, $field_id );
1039              $wpdb->query( $sql );
1040  
1041              // Invalidate profile field and group query cache.
1042              wp_cache_delete( $field_id, 'bp_xprofile_fields' );
1043  
1044              $retval = $parent;
1045          }
1046  
1047          bp_core_reset_incrementor( 'bp_xprofile_groups' );
1048  
1049          return $retval;
1050      }
1051  
1052      /**
1053       * Gets the IDs of fields applicable for a given member type or array of member types.
1054       *
1055       * @since 2.4.0
1056       *
1057       * @param string|array $member_types Member type or array of member types. Use 'any' to return unrestricted
1058       *                                   fields (those available for anyone, regardless of member type).
1059       * @return array Multi-dimensional array, with field IDs as top-level keys, and arrays of member types
1060       *               associated with each field as values.
1061       */
1062  	public static function get_fields_for_member_type( $member_types ) {
1063          global $wpdb;
1064  
1065          $fields = array();
1066  
1067          if ( empty( $member_types ) ) {
1068              $member_types = array( 'any' );
1069          } elseif ( ! is_array( $member_types ) ) {
1070              $member_types = array( $member_types );
1071          }
1072  
1073          $bp = buddypress();
1074  
1075          // Pull up all recorded field member type data.
1076          $mt_meta = wp_cache_get( 'field_member_types', 'bp_xprofile' );
1077          if ( false === $mt_meta ) {
1078              $mt_meta = $wpdb->get_results( "SELECT object_id, meta_value FROM {$bp->profile->table_name_meta} WHERE meta_key = 'member_type' AND object_type = 'field'" );
1079              wp_cache_set( 'field_member_types', $mt_meta, 'bp_xprofile' );
1080          }
1081  
1082          // Keep track of all fields with recorded member_type metadata.
1083          $all_recorded_field_ids = wp_list_pluck( $mt_meta, 'object_id' );
1084  
1085          // Sort member_type matches in arrays, keyed by field_id.
1086          foreach ( $mt_meta as $_mt_meta ) {
1087              if ( ! isset( $fields[ $_mt_meta->object_id ] ) ) {
1088                  $fields[ $_mt_meta->object_id ] = array();
1089              }
1090  
1091              $fields[ $_mt_meta->object_id ][] = $_mt_meta->meta_value;
1092          }
1093  
1094          /*
1095           * Filter out fields that don't match any passed types, or those marked '_none'.
1096           * The 'any' type is implicitly handled here: it will match no types.
1097           */
1098          foreach ( $fields as $field_id => $field_types ) {
1099              if ( ! array_intersect( $field_types, $member_types ) ) {
1100                  unset( $fields[ $field_id ] );
1101              }
1102          }
1103  
1104          // Any fields with no member_type metadata are available to all member types.
1105          if ( ! in_array( '_none', $member_types ) ) {
1106              if ( ! empty( $all_recorded_field_ids ) ) {
1107                  $all_recorded_field_ids_sql = implode( ',', array_map( 'absint', $all_recorded_field_ids ) );
1108                  $unrestricted_field_ids = $wpdb->get_col( "SELECT id FROM {$bp->profile->table_name_fields} WHERE id NOT IN ({$all_recorded_field_ids_sql})" );
1109              } else {
1110                  $unrestricted_field_ids = $wpdb->get_col( "SELECT id FROM {$bp->profile->table_name_fields}" );
1111              }
1112  
1113              // Append the 'null' pseudo-type.
1114              $all_member_types   = bp_get_member_types();
1115              $all_member_types   = array_values( $all_member_types );
1116              $all_member_types[] = 'null';
1117  
1118              foreach ( $unrestricted_field_ids as $unrestricted_field_id ) {
1119                  $fields[ $unrestricted_field_id ] = $all_member_types;
1120              }
1121          }
1122  
1123          return $fields;
1124      }
1125  
1126      /**
1127       * Validate form field data on submission.
1128       *
1129       * @since 2.2.0
1130       *
1131       * @global $message
1132       *
1133       * @return boolean
1134       */
1135  	public static function admin_validate() {
1136          global $message;
1137  
1138          // Check field name.
1139          if ( ! isset( $_POST['title'] ) || ( '' === $_POST['title'] ) ) {
1140              $message = esc_html__( 'Profile fields must have a name.', 'buddypress' );
1141              return false;
1142          }
1143  
1144          // Check field requirement.
1145          if ( ! isset( $_POST['required'] ) ) {
1146              $message = esc_html__( 'Profile field requirement is missing.', 'buddypress' );
1147              return false;
1148          }
1149  
1150          // Check field type.
1151          if ( empty( $_POST['fieldtype'] ) ) {
1152              $message = esc_html__( 'Profile field type is missing.', 'buddypress' );
1153              return false;
1154          }
1155  
1156          // Check that field is of valid type.
1157          if ( ! in_array( $_POST['fieldtype'], array_keys( bp_xprofile_get_field_types() ), true ) ) {
1158              /* translators: %s: field type name */
1159              $message = sprintf( esc_html__( 'The profile field type %s is not registered.', 'buddypress' ), '<code>' . esc_attr( $_POST['fieldtype'] ) . '</code>' );
1160              return false;
1161          }
1162  
1163          // Get field type so we can check for and validate any field options.
1164          $field_type = bp_xprofile_create_field_type( $_POST['fieldtype'] );
1165  
1166          // Field type requires options.
1167          if ( true === $field_type->supports_options ) {
1168  
1169              // Build the field option key.
1170              $option_name = sanitize_key( $_POST['fieldtype'] ) . '_option';
1171  
1172              // Check for missing or malformed options.
1173              if ( empty( $_POST[ $option_name ] ) || ! is_array( $_POST[ $option_name ] ) ) {
1174                  $message = esc_html__( 'These field options are invalid.', 'buddypress' );
1175                  return false;
1176              }
1177  
1178              // Trim out empty field options.
1179              $field_values  = array_values( $_POST[ $option_name ] );
1180              $field_options = array_map( 'sanitize_text_field', $field_values );
1181              $field_count   = count( $field_options );
1182  
1183              // Check for missing or malformed options.
1184              if ( 0 === $field_count ) {
1185                  /* translators: %s: field type name */
1186                  $message = sprintf( esc_html__( '%s require at least one option.', 'buddypress' ), $field_type->name );
1187                  return false;
1188              }
1189  
1190              // If only one option exists, it cannot be an empty string.
1191              if ( ( 1 === $field_count ) && ( '' === $field_options[0] ) ) {
1192                  /* translators: %s: field type name */
1193                  $message = sprintf( esc_html__( '%s require at least one option.', 'buddypress' ), $field_type->name );
1194                  return false;
1195              }
1196          }
1197  
1198          return true;
1199      }
1200  
1201      /**
1202       * Save miscellaneous settings for this field.
1203       *
1204       * Some field types have type-specific settings, which are saved here.
1205       *
1206       * @since 2.7.0
1207       *
1208       * @param array $settings Array of settings.
1209       */
1210  	public function admin_save_settings( $settings ) {
1211          return $this->type_obj->admin_save_settings( $this->id, $settings );
1212      }
1213  
1214      /**
1215       * Populates the items for radio buttons, checkboxes, and dropdown boxes.
1216       */
1217  	public function render_admin_form_children() {
1218          foreach ( array_keys( bp_xprofile_get_field_types() ) as $field_type ) {
1219              $type_obj = bp_xprofile_create_field_type( $field_type );
1220              $type_obj->admin_new_field_html( $this );
1221          }
1222      }
1223  
1224      /**
1225       * Oupput the admin form for this field.
1226       *
1227       * @since 1.9.0
1228       *
1229       * @param string $message Message to display.
1230       */
1231  	public function render_admin_form( $message = '' ) {
1232  
1233          // Users Admin URL.
1234          $users_url = bp_get_admin_url( 'users.php' );
1235  
1236          // Add New.
1237          if ( empty( $this->id ) ) {
1238              $title  = __( 'Add New Field', 'buddypress' );
1239              $button    = __( 'Save',          'buddypress' );
1240              $action = add_query_arg( array(
1241                  'page'     => 'bp-profile-setup',
1242                  'mode'     => 'add_field',
1243                  'group_id' => (int) $this->group_id
1244              ), $users_url . '#tabs-' . (int) $this->group_id );
1245  
1246              if ( !empty( $_POST['saveField'] ) ) {
1247                  $this->name        = $_POST['title'];
1248                  $this->description = $_POST['description'];
1249                  $this->is_required = $_POST['required'];
1250                  $this->type        = $_POST['fieldtype'];
1251                  $this->field_order = $_POST['field_order'];
1252  
1253                  if ( ! empty( $_POST["sort_order_{$this->type}"] ) ) {
1254                      $this->order_by = $_POST["sort_order_{$this->type}"];
1255                  }
1256              }
1257  
1258          // Edit.
1259          } else {
1260              $title  = __( 'Edit Field', 'buddypress' );
1261              $button    = __( 'Update',     'buddypress' );
1262              $action = add_query_arg( array(
1263                  'page'     => 'bp-profile-setup',
1264                  'mode'     => 'edit_field',
1265                  'group_id' => (int) $this->group_id,
1266                  'field_id' => (int) $this->id
1267              ), $users_url . '#tabs-' . (int) $this->group_id );
1268          } ?>
1269  
1270          <div class="wrap">
1271  
1272              <h1 class="wp-heading-inline"><?php echo esc_html( $title ); ?></h1>
1273              <hr class="wp-header-end">
1274  
1275              <?php if ( !empty( $message ) ) : ?>
1276  
1277                  <div id="message" class="error fade notice is-dismissible">
1278                      <p><?php echo esc_html( $message ); ?></p>
1279                  </div>
1280  
1281              <?php endif; ?>
1282  
1283              <form id="bp-xprofile-add-field" action="<?php echo esc_url( $action ); ?>" method="post">
1284                  <div id="poststuff">
1285                      <div id="post-body" class="metabox-holder columns-<?php echo ( 1 == get_current_screen()->get_columns() ) ? '1' : '2'; ?>">
1286                          <div id="post-body-content">
1287  
1288                              <?php
1289  
1290                              // Output the name & description fields.
1291                              $this->name_and_description(); ?>
1292  
1293                          </div><!-- #post-body-content -->
1294  
1295                          <div id="postbox-container-1" class="postbox-container">
1296  
1297                              <?php
1298  
1299                              // Output the sumbit metabox.
1300                              $this->submit_metabox( $button );
1301  
1302                              // Output the required metabox.
1303                              $this->required_metabox();
1304  
1305                              // Output signup position metabox.
1306                              $this->signup_position_metabox();
1307  
1308                              // Output the Member Types metabox.
1309                              $this->member_type_metabox();
1310  
1311                              // Output the field visibility metaboxes.
1312                              $this->visibility_metabox();
1313  
1314                              // Output the autolink metabox.
1315                              $this->autolink_metabox();
1316  
1317  
1318                              /**
1319                               * Fires after XProfile Field sidebar metabox.
1320                               *
1321                               * @since 2.2.0
1322                               *
1323                               * @param BP_XProfile_Field $this Current XProfile field.
1324                               */
1325                              do_action( 'xprofile_field_after_sidebarbox', $this ); ?>
1326  
1327                          </div>
1328  
1329                          <div id="postbox-container-2" class="postbox-container">
1330  
1331                              <?php
1332  
1333                              /**
1334                               * Fires before XProfile Field content metabox.
1335                               *
1336                               * @since 2.3.0
1337                               *
1338                               * @param BP_XProfile_Field $this Current XProfile field.
1339                               */
1340                              do_action( 'xprofile_field_before_contentbox', $this );
1341  
1342                              // Output the field attributes metabox.
1343                              $this->type_metabox();
1344  
1345                              // Output hidden inputs for default field.
1346                              $this->default_field_hidden_inputs();
1347  
1348                              /**
1349                               * Fires after XProfile Field content metabox.
1350                               *
1351                               * @since 2.2.0
1352                               *
1353                               * @param BP_XProfile_Field $this Current XProfile field.
1354                               */
1355                              do_action( 'xprofile_field_after_contentbox', $this ); ?>
1356  
1357                          </div>
1358                      </div><!-- #post-body -->
1359                  </div><!-- #poststuff -->
1360              </form>
1361          </div>
1362  
1363      <?php
1364      }
1365  
1366      /**
1367       * Gets field type supports.
1368       *
1369       * @since 8.0.0
1370       *
1371       * @return bool[] Supported features.
1372       */
1373  	public function get_field_type_supports() {
1374          $supports = array(
1375              'switch_fieldtype'        => true,
1376              'required'                => true,
1377              'do_autolink'             => true,
1378              'allow_custom_visibility' => true,
1379              'member_types'            => true,
1380              'signup_position'         => true,
1381          );
1382  
1383          if ( isset( $this->type_obj ) && $this->type_obj ) {
1384              $field_type = $this->type_obj;
1385  
1386              if ( isset( $field_type::$supported_features ) ) {
1387                  $supports = array_merge( $supports, $field_type::$supported_features );
1388              }
1389          }
1390  
1391          return $supports;
1392      }
1393  
1394      /**
1395       * Checks whether the field type supports the requested feature.
1396       *
1397       * @since 8.0.0
1398       *
1399       * @param string $support The name of the feature.
1400       * @return boolean True if the field type supports the feature. False otherwise.
1401       */
1402  	public function field_type_supports( $support = '' ) {
1403          $retval   = true;
1404          $features = $this->get_field_type_supports();
1405  
1406          if ( isset( $features[ $support ] ) ) {
1407              $retval = $features[ $support ];
1408          }
1409  
1410          return $retval;
1411      }
1412  
1413      /**
1414       * Private method used to display the submit metabox.
1415       *
1416       * @since 2.3.0
1417       *
1418       * @param string $button_text Text to put on button.
1419       */
1420  	private function submit_metabox( $button_text = '' ) {
1421  
1422          // Setup the URL for deleting
1423          $users_url  = bp_get_admin_url( 'users.php' );
1424          $cancel_url = add_query_arg( array(
1425              'page' => 'bp-profile-setup'
1426          ), $users_url );
1427  
1428  
1429          // Delete.
1430          if ( $this->can_delete ) {
1431              $delete_url = wp_nonce_url( add_query_arg( array(
1432                  'page'     => 'bp-profile-setup',
1433                  'mode'     => 'delete_field',
1434                  'field_id' => (int) $this->id
1435              ), $users_url ), 'bp_xprofile_delete_field-' . $this->id, 'bp_xprofile_delete_field' );
1436          }
1437          /**
1438           * Fires before XProfile Field submit metabox.
1439           *
1440           * @since 2.1.0
1441           *
1442           * @param BP_XProfile_Field $this Current XProfile field.
1443           */
1444          do_action( 'xprofile_field_before_submitbox', $this ); ?>
1445  
1446          <div id="submitdiv" class="postbox">
1447              <h2><?php esc_html_e( 'Submit', 'buddypress' ); ?></h2>
1448              <div class="inside">
1449                  <div id="submitcomment" class="submitbox">
1450                      <div id="major-publishing-actions">
1451  
1452                          <?php
1453  
1454                          /**
1455                           * Fires at the beginning of the XProfile Field publishing actions section.
1456                           *
1457                           * @since 2.1.0
1458                           *
1459                           * @param BP_XProfile_Field $this Current XProfile field.
1460                           */
1461                          do_action( 'xprofile_field_submitbox_start', $this ); ?>
1462  
1463                          <input type="hidden" name="field_order" id="field_order" value="<?php echo esc_attr( $this->field_order ); ?>" />
1464  
1465                          <?php if ( ! empty( $button_text ) ) : ?>
1466  
1467                              <div id="publishing-action">
1468                                  <input type="submit" name="saveField" value="<?php echo esc_attr( $button_text ); ?>" class="button-primary" />
1469                              </div>
1470  
1471                          <?php endif; ?>
1472  
1473                          <div id="delete-action">
1474                              <?php if ( ! empty( $this->id ) && isset( $delete_url ) ) : ?>
1475                                  <a href="<?php echo esc_url( $delete_url ); ?>" class="submitdelete deletion"><?php esc_html_e( 'Delete', 'buddypress' ); ?></a>
1476                              <?php endif; ?>
1477  
1478                              <div><a href="<?php echo esc_url( $cancel_url ); ?>" class="deletion"><?php esc_html_e( 'Cancel', 'buddypress' ); ?></a></div>
1479                          </div>
1480  
1481                          <?php wp_nonce_field( 'xprofile_delete_option' ); ?>
1482  
1483                          <div class="clear"></div>
1484                      </div>
1485                  </div>
1486              </div>
1487          </div>
1488  
1489          <?php
1490  
1491          /**
1492           * Fires after XProfile Field submit metabox.
1493           *
1494           * @since 2.1.0
1495           *
1496           * @param BP_XProfile_Field $this Current XProfile field.
1497           */
1498          do_action( 'xprofile_field_after_submitbox', $this );
1499      }
1500  
1501      /**
1502       * Private method used to output field name and description fields.
1503       *
1504       * @since 2.3.0
1505       */
1506  	private function name_and_description() {
1507      ?>
1508  
1509          <div id="titlediv">
1510              <div class="titlewrap">
1511                  <label id="title-prompt-text" for="title"><?php echo esc_html_x( 'Name (required)', 'XProfile admin edit field', 'buddypress' ); ?></label>
1512                  <input type="text" name="title" id="title" value="<?php echo esc_attr( $this->name ); ?>" autocomplete="off" />
1513              </div>
1514          </div>
1515  
1516          <div class="postbox">
1517              <h2><?php echo esc_html_x( 'Description', 'XProfile admin edit field', 'buddypress' ); ?></h2>
1518              <div class="inside">
1519                  <label for="description" class="screen-reader-text"><?php
1520                      /* translators: accessibility text */
1521                      esc_html_e( 'Add description', 'buddypress' );
1522                  ?></label>
1523                  <textarea name="description" id="description" rows="8" cols="60"><?php echo esc_textarea( $this->description ); ?></textarea>
1524              </div>
1525          </div>
1526  
1527      <?php
1528      }
1529  
1530      /**
1531       * Private method used to output field Member Type metabox.
1532       *
1533       * @since 2.4.0
1534       *
1535       * @return void If default field or if the field does not support the feature.
1536       */
1537  	private function member_type_metabox() {
1538  
1539          // The primary field is for all, so bail.
1540          if ( true === $this->is_default_field() || ! $this->field_type_supports( 'member_types' ) ) {
1541              return;
1542          }
1543  
1544          // Bail when no member types are registered.
1545          if ( ! $member_types = bp_get_member_types( array(), 'objects' ) ) {
1546              return;
1547          }
1548  
1549          $field_member_types = $this->get_member_types();
1550  
1551          ?>
1552  
1553          <div id="field-type-member-types" class="postbox">
1554              <h2><?php _e( 'Member Types', 'buddypress' ); ?></h2>
1555              <div class="inside">
1556                  <p class="description"><?php _e( 'This field should be available to:', 'buddypress' ); ?></p>
1557  
1558                  <ul>
1559                      <?php foreach ( $member_types as $member_type ) : ?>
1560                      <li>
1561                          <label for="member-type-<?php echo $member_type->labels['name']; ?>">
1562                              <input name="member-types[]" id="member-type-<?php echo $member_type->labels['name']; ?>" class="member-type-selector" type="checkbox" value="<?php echo $member_type->name; ?>" <?php checked( in_array( $member_type->name, $field_member_types ) ); ?>/>
1563                              <?php echo $member_type->labels['name']; ?>
1564                          </label>
1565                      </li>
1566                      <?php endforeach; ?>
1567  
1568                      <li>
1569                          <label for="member-type-none">
1570                              <input name="member-types[]" id="member-type-none" class="member-type-selector" type="checkbox" value="null" <?php checked( in_array( 'null', $field_member_types ) ); ?>/>
1571                              <?php _e( 'Users with no member type', 'buddypress' ); ?>
1572                          </label>
1573                      </li>
1574  
1575                  </ul>
1576                  <p class="description member-type-none-notice<?php if ( ! empty( $field_member_types ) ) : ?> hide<?php endif; ?>"><?php _e( 'Unavailable to all members.', 'buddypress' ) ?></p>
1577              </div>
1578  
1579              <input type="hidden" name="has-member-types" value="1" />
1580          </div>
1581  
1582          <?php
1583      }
1584  
1585      /**
1586       * Private method used to output field visibility metaboxes.
1587       *
1588       * @since 2.3.0
1589       *
1590       * @return void If default field or if the field does not support the feature.
1591       */
1592  	private function visibility_metabox() {
1593  
1594          // Default field and field types not supporting the feature cannot have custom visibility.
1595          if ( true === $this->is_default_field() || ! $this->field_type_supports( 'allow_custom_visibility' ) ) {
1596              return;
1597          } ?>
1598  
1599          <div class="postbox" id="field-type-visibiliy-metabox">
1600              <h2><label for="default-visibility"><?php esc_html_e( 'Visibility', 'buddypress' ); ?></label></h2>
1601              <div class="inside">
1602                  <div>
1603                      <select name="default-visibility" id="default-visibility">
1604  
1605                          <?php foreach( bp_xprofile_get_visibility_levels() as $level ) : ?>
1606  
1607                              <option value="<?php echo esc_attr( $level['id'] ); ?>" <?php selected( $this->get_default_visibility(), $level['id'] ); ?>>
1608                                  <?php echo esc_html( $level['label'] ); ?>
1609                              </option>
1610  
1611                          <?php endforeach ?>
1612  
1613                      </select>
1614                  </div>
1615  
1616                  <div>
1617                      <ul>
1618                          <li>
1619                              <input type="radio" id="allow-custom-visibility-allowed" name="allow-custom-visibility" value="allowed" <?php checked( $this->get_allow_custom_visibility(), 'allowed' ); ?> />
1620                              <label for="allow-custom-visibility-allowed"><?php esc_html_e( 'Allow members to override', 'buddypress' ); ?></label>
1621                          </li>
1622                          <li>
1623                              <input type="radio" id="allow-custom-visibility-disabled" name="allow-custom-visibility" value="disabled" <?php checked( $this->get_allow_custom_visibility(), 'disabled' ); ?> />
1624                              <label for="allow-custom-visibility-disabled"><?php esc_html_e( 'Enforce field visibility', 'buddypress' ); ?></label>
1625                          </li>
1626                      </ul>
1627                  </div>
1628              </div>
1629          </div>
1630  
1631          <?php
1632      }
1633  
1634      /**
1635       * Output the metabox for setting if field is required or not.
1636       *
1637       * @since 2.3.0
1638       *
1639       * @return void If default field or if the field does not support the feature.
1640       */
1641  	private function required_metabox() {
1642  
1643          // Default field and field types not supporting the feature cannot be required.
1644          if ( true === $this->is_default_field() || ! $this->field_type_supports( 'required' ) ) {
1645              return;
1646          } ?>
1647  
1648          <div class="postbox" id="field-type-required-metabox">
1649              <h2><label for="required"><?php esc_html_e( 'Requirement', 'buddypress' ); ?></label></h2>
1650              <div class="inside">
1651                  <select name="required" id="required">
1652                      <option value="0"<?php selected( $this->is_required, '0' ); ?>><?php esc_html_e( 'Not Required', 'buddypress' ); ?></option>
1653                      <option value="1"<?php selected( $this->is_required, '1' ); ?>><?php esc_html_e( 'Required',     'buddypress' ); ?></option>
1654                  </select>
1655              </div>
1656          </div>
1657  
1658      <?php
1659      }
1660  
1661      /**
1662       * Private method used to output autolink metabox.
1663       *
1664       * @since 2.5.0
1665       *
1666       * @return void If the field does not support the feature.
1667       */
1668  	private function autolink_metabox() {
1669  
1670          // Field types not supporting the feature cannot use autolink.
1671          if ( ! $this->field_type_supports( 'do_autolink' ) ) {
1672              return;
1673          } ?>
1674  
1675          <div class="postbox" id="field-type-autolink-metabox">
1676              <h2><?php esc_html_e( 'Autolink', 'buddypress' ); ?></h2>
1677              <div class="inside">
1678                  <p class="description"><?php esc_html_e( 'On user profiles, link this field to a search of the Members directory, using the field value as a search term.', 'buddypress' ); ?></p>
1679  
1680                  <p>
1681                      <label for="do-autolink" class="screen-reader-text"><?php
1682                          /* translators: accessibility text */
1683                          esc_html_e( 'Autolink status for this field', 'buddypress' );
1684                      ?></label>
1685                      <select name="do_autolink" id="do-autolink">
1686                          <option value="on" <?php selected( $this->get_do_autolink() ); ?>><?php esc_html_e( 'Enabled', 'buddypress' ); ?></option>
1687                          <option value="" <?php selected( $this->get_do_autolink(), false ); ?>><?php esc_html_e( 'Disabled', 'buddypress' ); ?></option>
1688                      </select>
1689                  </p>
1690              </div>
1691          </div>
1692  
1693          <?php
1694      }
1695  
1696      /**
1697       * Output the metabox for setting what type of field this is.
1698       *
1699       * @since 2.3.0
1700       *
1701       * @return void If default field.
1702       */
1703  	private function type_metabox() {
1704  
1705          // Default field cannot change type.
1706          if ( true === $this->is_default_field() ) {
1707              return;
1708          }
1709          ?>
1710  
1711          <div class="postbox">
1712              <h2><label for="fieldtype"><?php esc_html_e( 'Type', 'buddypress'); ?></label></h2>
1713              <div class="inside" aria-live="polite" aria-atomic="true" aria-relevant="all">
1714                  <?php if ( ! $this->field_type_supports( 'switch_fieldtype' ) ) : ?>
1715                      <input type="text" disabled="true" value="<?php echo esc_attr( $this->type_obj->name ); ?>">
1716                      <input type="hidden" name="fieldtype" id="fieldtype" value="<?php echo esc_attr( $this->type ); ?>">
1717  
1718                  <?php else : ?>
1719                      <select name="fieldtype" id="fieldtype" onchange="show_options(this.value)">
1720  
1721                          <?php bp_xprofile_admin_form_field_types( $this->type ); ?>
1722  
1723                      </select>
1724                  <?php endif; ?>
1725  
1726                  <?php
1727  
1728                  // Deprecated filter, don't use. Go look at {@link BP_XProfile_Field_Type::admin_new_field_html()}.
1729                  do_action( 'xprofile_field_additional_options', $this );
1730  
1731                  $this->render_admin_form_children(); ?>
1732  
1733              </div>
1734          </div>
1735  
1736      <?php
1737      }
1738  
1739      /**
1740       * Output the metabox for setting the field's position into the signup form.
1741       *
1742       * @since 8.0.0
1743       *
1744       * @return void If default field or if the field does not support the feature.
1745       */
1746  	private function signup_position_metabox() {
1747          // Field types not supporting the feature cannot be added to signups form.
1748          if ( ! $this->field_type_supports( 'signup_position' ) || true === $this->is_default_field() ) {
1749              return;
1750          }
1751  
1752          $next_signup_position = 1;
1753          $signup_position      = $this->get_signup_position();
1754  
1755          if ( 0 === $signup_position ) {
1756              $signup_fields_order = bp_xprofile_get_signup_field_ids();
1757              $next_signup_position = count( $signup_fields_order ) + 1;
1758          } else {
1759              $next_signup_position = $signup_position;
1760          }
1761          ?>
1762  
1763          <div class="postbox" id="field-signup-position-metabox">
1764              <h2><?php esc_html_e( 'Signups', 'buddypress' ); ?></h2>
1765              <div class="inside">
1766                  <div>
1767                      <ul>
1768                          <li>
1769                              <input type="checkbox" id="has-signup-position" name="signup-position" value="<?php echo esc_attr( $next_signup_position ); ?>" <?php checked( $signup_position, $next_signup_position ); ?> />
1770                              <label for="has-signup-position"><?php esc_html_e( 'Use this field in the site registration form.', 'buddypress' ); ?></label>
1771                          </li>
1772                      </ul>
1773                  </div>
1774              </div>
1775          </div>
1776          <?php
1777      }
1778  
1779      /**
1780       * Output hidden fields used by default field.
1781       *
1782       * @since 2.3.0
1783       *
1784       * @return void If not default field.
1785       */
1786  	private function default_field_hidden_inputs() {
1787  
1788          // Nonce.
1789          wp_nonce_field( 'bp_xprofile_admin_field', 'bp_xprofile_admin_field' );
1790  
1791          // Init default field hidden inputs.
1792          $default_field_hidden_inputs = array();
1793          $hidden_fields = array(
1794              'required' => array(
1795                  'name'  => 'required',
1796                  'id'    => 'required',
1797                  'value' => '0',
1798              ),
1799              'default_visibility' => array(
1800                  'name'  => 'default-visibility',
1801                  'id'    => 'default-visibility',
1802                  'value' => $this->get_default_visibility(),
1803              ),
1804              'allow_custom_visibility' => array(
1805                  'name'  => 'allow-custom-visibility',
1806                  'id'    => 'allow-custom-visibility',
1807                  'value' => 'disabled',
1808              ),
1809              'do_autolink' => array(
1810                  'name'  => 'do_autolink',
1811                  'id'    => 'do-autolink',
1812                  'value' => '',
1813              ),
1814          );
1815  
1816          // Field 1 is the fullname field, which is required.
1817          if ( true === $this->is_default_field() ) {
1818              $default_field_required          = $hidden_fields['required'];
1819              $default_field_required['value'] = '1';
1820  
1821              $default_field_hidden_inputs = array(
1822                  $default_field_required,
1823                  array(
1824                      'name'  => 'fieldtype',
1825                      'id'    => 'fieldtype',
1826                      'value' => 'textbox',
1827                  ),
1828                  array(
1829                      'name'  => 'signup-position',
1830                      'id'    => 'has-signup-position',
1831                      'value' => $this->get_signup_position(),
1832                  ),
1833              );
1834          }
1835  
1836          $supports = $this->get_field_type_supports();
1837          if ( $supports ) {
1838              foreach ( $supports as $feature => $support ) {
1839                  if ( true === $support || in_array( $feature, array( 'switch_fieldtype', 'member_types' ), true ) ) {
1840                      continue;
1841                  }
1842  
1843                  $default_field_hidden_inputs[] = $hidden_fields[ $feature ];
1844  
1845                  if ( 'allow_custom_visibility' === $feature ) {
1846                      $default_field_hidden_inputs[] = $hidden_fields['default_visibility'];
1847                  }
1848              }
1849          }
1850  
1851          if ( ! $default_field_hidden_inputs ) {
1852              return;
1853          }
1854  
1855          foreach ( $default_field_hidden_inputs as $default_field_hidden_input ) {
1856              printf(
1857                  '<input type="hidden" name="%1$s" id="%2$s" value="%3$s"/>',
1858                  esc_attr( $default_field_hidden_input['name'] ),
1859                  esc_attr( $default_field_hidden_input['id'] ),
1860                  esc_attr( $default_field_hidden_input['value'] )
1861              );
1862          }
1863      }
1864  
1865      /**
1866       * Return if a field ID is the default field.
1867       *
1868       * @since 2.3.0
1869       *
1870       * @param int $field_id ID of field to check.
1871       * @return bool
1872       */
1873  	private function is_default_field( $field_id = 0 ) {
1874  
1875          // Fallback to current field ID if none passed.
1876          if ( empty( $field_id ) ) {
1877              $field_id = $this->id;
1878          }
1879  
1880          // Compare & return.
1881          return (bool) ( 1 === (int) $field_id );
1882      }
1883  }


Generated: Mon Jan 24 01:01:00 2022 Cross-referenced by PHPXref 0.7.1