[ 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 object $wpdb
 565       *
 566       * @param bool $for_editing Whether or not the field is for editing.
 567       * @return array
 568       */
 569  	public function get_children( $for_editing = false ) {
 570          global $wpdb;
 571  
 572          // This is done here so we don't have problems with sql injection.
 573          if ( empty( $for_editing ) && ( 'asc' === $this->order_by ) ) {
 574              $sort_sql = 'ORDER BY name ASC';
 575          } elseif ( empty( $for_editing ) && ( 'desc' === $this->order_by ) ) {
 576              $sort_sql = 'ORDER BY name DESC';
 577          } else {
 578              $sort_sql = 'ORDER BY option_order ASC';
 579          }
 580  
 581          // This eliminates a problem with getting all fields when there is no
 582          // id for the object.
 583          if ( empty( $this->id ) ) {
 584              $parent_id = -1;
 585          } else {
 586              $parent_id = $this->id;
 587          }
 588  
 589          $bp  = buddypress();
 590          $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 );
 591  
 592          $children = $wpdb->get_results( $sql );
 593  
 594          /**
 595           * Filters the found children for a field.
 596           *
 597           * @since 1.2.5
 598           * @since 3.0.0 Added the `$this` parameter.
 599           *
 600           * @param object            $children    Found children for a field.
 601           * @param bool              $for_editing Whether or not the field is for editing.
 602           * @param BP_XProfile_Field $this        Field object
 603           */
 604          return apply_filters( 'bp_xprofile_field_get_children', $children, $for_editing, $this );
 605      }
 606  
 607      /**
 608       * Delete all field children for this field.
 609       *
 610       * @since 1.2.0
 611       *
 612       * @global object $wpdb
 613       */
 614  	public function delete_children() {
 615          global $wpdb;
 616  
 617          $bp  = buddypress();
 618          $sql = $wpdb->prepare( "DELETE FROM {$bp->profile->table_name_fields} WHERE parent_id = %d", $this->id );
 619  
 620          $wpdb->query( $sql );
 621      }
 622  
 623      /**
 624       * Gets the member types to which this field should be available.
 625       *
 626       * Will not return inactive member types, even if associated metadata is found.
 627       *
 628       * 'null' is a special pseudo-type, which represents users that do not have a member type.
 629       *
 630       * @since 2.4.0
 631       *
 632       * @return array Array of member type names.
 633       */
 634  	public function get_member_types() {
 635          if ( ! is_null( $this->member_types ) ) {
 636              return $this->member_types;
 637          }
 638  
 639          $raw_types = bp_xprofile_get_meta( $this->id, 'field', 'member_type', false );
 640  
 641          // If `$raw_types` is not an array, it probably means this is a new field (id=0).
 642          if ( ! is_array( $raw_types ) ) {
 643              $raw_types = array();
 644          }
 645  
 646          // If '_none' is found in the array, it overrides all types.
 647          $types = array();
 648          if ( ! in_array( '_none', $raw_types ) ) {
 649              $registered_types = bp_get_member_types();
 650  
 651              // Eliminate invalid member types saved in the database.
 652              foreach ( $raw_types as $raw_type ) {
 653                  // 'null' is a special case - it represents users without a type.
 654                  if ( 'null' === $raw_type || isset( $registered_types[ $raw_type ] ) ) {
 655                      $types[] = $raw_type;
 656                  }
 657              }
 658  
 659              // If no member types have been saved, interpret as *all* member types.
 660              if ( empty( $types ) ) {
 661                  $types = array_values( $registered_types );
 662  
 663                  // + the "null" type, ie users without a type.
 664                  $types[] = 'null';
 665              }
 666          }
 667  
 668          /**
 669           * Filters the member types to which an XProfile object should be applied.
 670           *
 671           * @since 2.4.0
 672           *
 673           * @param array             $types Member types.
 674           * @param BP_XProfile_Field $field Field object.
 675           */
 676          $this->member_types = apply_filters( 'bp_xprofile_field_member_types', $types, $this );
 677  
 678          return $this->member_types;
 679      }
 680  
 681      /**
 682       * Sets the member types for this field.
 683       *
 684       * @since 2.4.0
 685       *
 686       * @param array $member_types Array of member types. Can include 'null' (users with no type) in addition to any
 687       *                            registered types.
 688       * @param bool  $append       Whether to append to existing member types. If false, all existing member type
 689       *                            associations will be deleted before adding your `$member_types`. Default false.
 690       * @return array Member types for the current field, after being saved.
 691       */
 692  	public function set_member_types( $member_types, $append = false ) {
 693          // Unset invalid member types.
 694          $types = array();
 695          foreach ( $member_types as $member_type ) {
 696              // 'null' is a special case - it represents users without a type.
 697              if ( 'null' === $member_type || bp_get_member_type_object( $member_type ) ) {
 698                  $types[] = $member_type;
 699              }
 700          }
 701  
 702          // When `$append` is false, delete all existing types before adding new ones.
 703          if ( ! $append ) {
 704              bp_xprofile_delete_meta( $this->id, 'field', 'member_type' );
 705  
 706              /*
 707               * We interpret an empty array as disassociating the field from all types. This is
 708               * represented internally with the '_none' flag.
 709               */
 710              if ( empty( $types ) ) {
 711                  bp_xprofile_add_meta( $this->id, 'field', 'member_type', '_none' );
 712              }
 713          }
 714  
 715          /*
 716           * Unrestricted fields are represented in the database as having no 'member_type'.
 717           * We detect whether a field is being set to unrestricted by checking whether the
 718           * list of types passed to the method is the same as the list of registered types,
 719           * plus the 'null' pseudo-type.
 720           */
 721          $_rtypes  = bp_get_member_types();
 722          $rtypes   = array_values( $_rtypes );
 723          $rtypes[] = 'null';
 724  
 725          sort( $types );
 726          sort( $rtypes );
 727  
 728          // Only save if this is a restricted field.
 729          if ( $types !== $rtypes ) {
 730              // Save new types.
 731              foreach ( $types as $type ) {
 732                  bp_xprofile_add_meta( $this->id, 'field', 'member_type', $type );
 733              }
 734          }
 735  
 736          // Reset internal cache of member types.
 737          $this->member_types = null;
 738  
 739          /**
 740           * Fires after a field's member types have been updated.
 741           *
 742           * @since 2.4.0
 743           *
 744           * @param BP_XProfile_Field $this Field object.
 745           */
 746          do_action( 'bp_xprofile_field_set_member_type', $this );
 747  
 748          // Refetch fresh items from the database.
 749          return $this->get_member_types();
 750      }
 751  
 752      /**
 753       * Gets a label representing the field's member types.
 754       *
 755       * This label is displayed alongside the field's name on the Profile Fields Dashboard panel.
 756       *
 757       * @since 2.4.0
 758       *
 759       * @return string
 760       */
 761  	public function get_member_type_label() {
 762          // Field 1 is always displayed to everyone, so never gets a label.
 763          if ( 1 == $this->id ) {
 764              return '';
 765          }
 766  
 767          // Return an empty string if no member types are registered.
 768          $all_types = bp_get_member_types();
 769          if ( empty( $all_types ) ) {
 770              return '';
 771          }
 772  
 773          $member_types = $this->get_member_types();
 774  
 775          // If the field applies to all member types, show no message.
 776          $all_types[] = 'null';
 777          if ( array_values( $all_types ) == $member_types ) {
 778              return '';
 779          }
 780  
 781          $label = '';
 782          if ( ! empty( $member_types ) ) {
 783              $has_null = false;
 784              $member_type_labels = array();
 785              foreach ( $member_types as $member_type ) {
 786                  if ( 'null' === $member_type ) {
 787                      $has_null = true;
 788                      continue;
 789                  } else {
 790                      $mt_obj = bp_get_member_type_object( $member_type );
 791                      $member_type_labels[] = $mt_obj->labels['name'];
 792                  }
 793              }
 794  
 795              // Alphabetical sort.
 796              natcasesort( $member_type_labels );
 797              $member_type_labels = array_values( $member_type_labels );
 798  
 799              // Add the 'null' option to the end of the list.
 800              if ( $has_null ) {
 801                  $member_type_labels[] = __( 'Users with no member type', 'buddypress' );
 802              }
 803  
 804              /* translators: %s: comma separated list of member types */
 805              $label = sprintf( __( '(Member types: %s)', 'buddypress' ), implode( ', ', array_map( 'esc_html', $member_type_labels ) ) );
 806          } else {
 807              $label = '<span class="member-type-none-notice">' . __( '(Unavailable to all members)', 'buddypress' ) . '</span>';
 808          }
 809  
 810          return $label;
 811      }
 812  
 813      /**
 814       * Get the field's default visibility setting.
 815       *
 816       * Lazy-loaded to reduce overhead.
 817       *
 818       * Defaults to 'public' if no visibility setting is found in the database.
 819       *
 820       * @since 2.4.0
 821       *
 822       * @return string
 823       */
 824  	public function get_default_visibility() {
 825          if ( ! isset( $this->default_visibility ) ) {
 826              $this->default_visibility = 'public';
 827              $this->visibility         = '';
 828  
 829              if ( isset( $this->type_obj->visibility ) && $this->type_obj->visibility ) {
 830                  $this->visibility = $this->type_obj->visibility;
 831              }
 832  
 833              if ( $this->field_type_supports( 'allow_custom_visibility' ) ) {
 834                  $this->visibility = bp_xprofile_get_meta( $this->id, 'field', 'default_visibility' );
 835              }
 836  
 837              if ( $this->visibility ) {
 838                  $this->default_visibility = $this->visibility;
 839              }
 840          }
 841  
 842          return $this->default_visibility;
 843      }
 844  
 845      /**
 846       * Get whether the field's default visibility can be overridden by users.
 847       *
 848       * Lazy-loaded to reduce overhead.
 849       *
 850       * Defaults to 'allowed'.
 851       *
 852       * @since 4.4.0
 853       *
 854       * @return string 'disabled' or 'allowed'.
 855       */
 856  	public function get_allow_custom_visibility() {
 857          if ( ! isset( $this->allow_custom_visibility ) ) {
 858              $allow_custom_visibility = bp_xprofile_get_meta( $this->id, 'field', 'allow_custom_visibility' );
 859  
 860              if ( 'disabled' === $allow_custom_visibility ) {
 861                  $this->allow_custom_visibility = 'disabled';
 862              } else {
 863                  $this->allow_custom_visibility = 'allowed';
 864              }
 865          }
 866  
 867          return $this->allow_custom_visibility;
 868      }
 869  
 870      /**
 871       * Get the field's signup position.
 872       *
 873       * @since 8.0.0
 874       *
 875       * @return int the field's signup position.
 876       *             0 if the field has not been added to the signup form.
 877       */
 878  	public function get_signup_position() {
 879          if ( ! isset( $this->signup_position ) ) {
 880              $this->signup_position = (int) bp_xprofile_get_meta( $this->id, 'field', 'signup_position' );
 881          }
 882  
 883          return $this->signup_position;
 884      }
 885  
 886      /**
 887       * Get whether the field values should be auto-linked to a directory search.
 888       *
 889       * Lazy-loaded to reduce overhead.
 890       *
 891       * Defaults to true for multi and default fields, false for single fields.
 892       *
 893       * @since 2.5.0
 894       *
 895       * @return bool
 896       */
 897  	public function get_do_autolink() {
 898          if ( ! isset( $this->do_autolink ) ) {
 899              $do_autolink = bp_xprofile_get_meta( $this->id, 'field', 'do_autolink' );
 900  
 901              if ( '' === $do_autolink ) {
 902                  $this->do_autolink = $this->type_obj->supports_options;
 903              } else {
 904                  $this->do_autolink = 'on' === $do_autolink;
 905              }
 906          }
 907  
 908          /**
 909           * Filters the autolink property of the field.
 910           *
 911           * @since 6.0.0
 912           *
 913           * @param bool              $do_autolink The autolink property of the field.
 914           * @param BP_XProfile_Field $this Field object.
 915           */
 916          return apply_filters( 'bp_xprofile_field_do_autolink', $this->do_autolink, $this );
 917      }
 918  
 919      /* Static Methods ********************************************************/
 920  
 921      /**
 922       * Get the type for provided field ID.
 923       *
 924       * @param int $field_id Field ID to get type of.
 925       * @return bool|null|string
 926       */
 927  	public static function get_type( $field_id = 0 ) {
 928          global $wpdb;
 929  
 930          // Bail if no field ID.
 931          if ( empty( $field_id ) ) {
 932              return false;
 933          }
 934  
 935          $bp   = buddypress();
 936          $sql  = $wpdb->prepare( "SELECT type FROM {$bp->profile->table_name_fields} WHERE id = %d", $field_id );
 937          $type = $wpdb->get_var( $sql );
 938  
 939          // Return field type.
 940          if ( ! empty( $type ) ) {
 941              return $type;
 942          }
 943  
 944          return false;
 945      }
 946  
 947      /**
 948       * Delete all fields in a field group.
 949       *
 950       * @since 1.2.0
 951       *
 952       * @global object $wpdb
 953       *
 954       * @param int $group_id ID of the field group to delete fields from.
 955       * @return boolean
 956       */
 957  	public static function delete_for_group( $group_id = 0 ) {
 958          global $wpdb;
 959  
 960          // Bail if no group ID.
 961          if ( empty( $group_id ) ) {
 962              return false;
 963          }
 964  
 965          $bp      = buddypress();
 966          $sql     = $wpdb->prepare( "DELETE FROM {$bp->profile->table_name_fields} WHERE group_id = %d", $group_id );
 967          $deleted = $wpdb->get_var( $sql );
 968  
 969          // Return true if fields were deleted.
 970          if ( false !== $deleted ) {
 971              return true;
 972          }
 973  
 974          return false;
 975      }
 976  
 977      /**
 978       * Get field ID from field name.
 979       *
 980       * @since 1.5.0
 981       *
 982       * @global object $wpdb
 983       *
 984       * @param string $field_name Name of the field to query the ID for.
 985       * @return int|null Field ID on success; null on failure.
 986       */
 987  	public static function get_id_from_name( $field_name = '' ) {
 988          global $wpdb;
 989  
 990          $bp = buddypress();
 991  
 992          if ( empty( $bp->profile->table_name_fields ) || empty( $field_name ) ) {
 993              return false;
 994          }
 995  
 996          $id = bp_core_get_incremented_cache( $field_name, 'bp_xprofile_fields_by_name' );
 997          if ( false === $id ) {
 998              $sql = $wpdb->prepare( "SELECT id FROM {$bp->profile->table_name_fields} WHERE name = %s AND parent_id = 0", $field_name );
 999              $id = $wpdb->get_var( $sql );
1000              bp_core_set_incremented_cache( $field_name, 'bp_xprofile_fields_by_name', $id );
1001          }
1002  
1003          return is_numeric( $id ) ? (int) $id : $id;
1004      }
1005  
1006      /**
1007       * Update field position and/or field group when relocating.
1008       *
1009       * @since 1.5.0
1010       *
1011       * @global object $wpdb
1012       *
1013       * @param int      $field_id       ID of the field to update.
1014       * @param int|null $position       Field position to update.
1015       * @param int|null $field_group_id ID of the field group.
1016       * @return boolean
1017       */
1018  	public static function update_position( $field_id, $position = null, $field_group_id = null ) {
1019          global $wpdb;
1020  
1021          // Bail if invalid position or field group.
1022          if ( ! is_numeric( $position ) || ! is_numeric( $field_group_id ) ) {
1023              return false;
1024          }
1025  
1026          // Get table name and field parent.
1027          $table_name = buddypress()->profile->table_name_fields;
1028          $sql        = $wpdb->prepare( "UPDATE {$table_name} SET field_order = %d, group_id = %d WHERE id = %d", $position, $field_group_id, $field_id );
1029          $parent     = $wpdb->query( $sql );
1030  
1031          $retval = false;
1032  
1033          // Update $field_id with new $position and $field_group_id.
1034          if ( ! empty( $parent ) && ! is_wp_error( $parent ) ) {
1035  
1036              // Update any children of this $field_id.
1037              $sql = $wpdb->prepare( "UPDATE {$table_name} SET group_id = %d WHERE parent_id = %d", $field_group_id, $field_id );
1038              $wpdb->query( $sql );
1039  
1040              // Invalidate profile field and group query cache.
1041              wp_cache_delete( $field_id, 'bp_xprofile_fields' );
1042  
1043              $retval = $parent;
1044          }
1045  
1046          bp_core_reset_incrementor( 'bp_xprofile_groups' );
1047  
1048          return $retval;
1049      }
1050  
1051      /**
1052       * Gets the IDs of fields applicable for a given member type or array of member types.
1053       *
1054       * @since 2.4.0
1055       *
1056       * @param string|array $member_types Member type or array of member types. Use 'any' to return unrestricted
1057       *                                   fields (those available for anyone, regardless of member type).
1058       * @return array Multi-dimensional array, with field IDs as top-level keys, and arrays of member types
1059       *               associated with each field as values.
1060       */
1061  	public static function get_fields_for_member_type( $member_types ) {
1062          global $wpdb;
1063  
1064          $fields = array();
1065  
1066          if ( empty( $member_types ) ) {
1067              $member_types = array( 'any' );
1068          } elseif ( ! is_array( $member_types ) ) {
1069              $member_types = array( $member_types );
1070          }
1071  
1072          $bp = buddypress();
1073  
1074          // Pull up all recorded field member type data.
1075          $mt_meta = wp_cache_get( 'field_member_types', 'bp_xprofile' );
1076          if ( false === $mt_meta ) {
1077              $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'" );
1078              wp_cache_set( 'field_member_types', $mt_meta, 'bp_xprofile' );
1079          }
1080  
1081          // Keep track of all fields with recorded member_type metadata.
1082          $all_recorded_field_ids = wp_list_pluck( $mt_meta, 'object_id' );
1083  
1084          // Sort member_type matches in arrays, keyed by field_id.
1085          foreach ( $mt_meta as $_mt_meta ) {
1086              if ( ! isset( $fields[ $_mt_meta->object_id ] ) ) {
1087                  $fields[ $_mt_meta->object_id ] = array();
1088              }
1089  
1090              $fields[ $_mt_meta->object_id ][] = $_mt_meta->meta_value;
1091          }
1092  
1093          /*
1094           * Filter out fields that don't match any passed types, or those marked '_none'.
1095           * The 'any' type is implicitly handled here: it will match no types.
1096           */
1097          foreach ( $fields as $field_id => $field_types ) {
1098              if ( ! array_intersect( $field_types, $member_types ) ) {
1099                  unset( $fields[ $field_id ] );
1100              }
1101          }
1102  
1103          // Any fields with no member_type metadata are available to all member types.
1104          if ( ! in_array( '_none', $member_types ) ) {
1105              if ( ! empty( $all_recorded_field_ids ) ) {
1106                  $all_recorded_field_ids_sql = implode( ',', array_map( 'absint', $all_recorded_field_ids ) );
1107                  $unrestricted_field_ids = $wpdb->get_col( "SELECT id FROM {$bp->profile->table_name_fields} WHERE id NOT IN ({$all_recorded_field_ids_sql})" );
1108              } else {
1109                  $unrestricted_field_ids = $wpdb->get_col( "SELECT id FROM {$bp->profile->table_name_fields}" );
1110              }
1111  
1112              // Append the 'null' pseudo-type.
1113              $all_member_types   = bp_get_member_types();
1114              $all_member_types   = array_values( $all_member_types );
1115              $all_member_types[] = 'null';
1116  
1117              foreach ( $unrestricted_field_ids as $unrestricted_field_id ) {
1118                  $fields[ $unrestricted_field_id ] = $all_member_types;
1119              }
1120          }
1121  
1122          return $fields;
1123      }
1124  
1125      /**
1126       * Validate form field data on submission.
1127       *
1128       * @since 2.2.0
1129       *
1130       * @global $message
1131       *
1132       * @return boolean
1133       */
1134  	public static function admin_validate() {
1135          global $message;
1136  
1137          // Check field name.
1138          if ( ! isset( $_POST['title'] ) || ( '' === $_POST['title'] ) ) {
1139              $message = esc_html__( 'Profile fields must have a name.', 'buddypress' );
1140              return false;
1141          }
1142  
1143          // Check field requirement.
1144          if ( ! isset( $_POST['required'] ) ) {
1145              $message = esc_html__( 'Profile field requirement is missing.', 'buddypress' );
1146              return false;
1147          }
1148  
1149          // Check field type.
1150          if ( empty( $_POST['fieldtype'] ) ) {
1151              $message = esc_html__( 'Profile field type is missing.', 'buddypress' );
1152              return false;
1153          }
1154  
1155          // Check that field is of valid type.
1156          if ( ! in_array( $_POST['fieldtype'], array_keys( bp_xprofile_get_field_types() ), true ) ) {
1157              /* translators: %s: field type name */
1158              $message = sprintf( esc_html__( 'The profile field type %s is not registered.', 'buddypress' ), '<code>' . esc_attr( $_POST['fieldtype'] ) . '</code>' );
1159              return false;
1160          }
1161  
1162          // Get field type so we can check for and validate any field options.
1163          $field_type = bp_xprofile_create_field_type( $_POST['fieldtype'] );
1164  
1165          // Field type requires options.
1166          if ( true === $field_type->supports_options ) {
1167  
1168              // Build the field option key.
1169              $option_name = sanitize_key( $_POST['fieldtype'] ) . '_option';
1170  
1171              // Check for missing or malformed options.
1172              if ( empty( $_POST[ $option_name ] ) || ! is_array( $_POST[ $option_name ] ) ) {
1173                  $message = esc_html__( 'These field options are invalid.', 'buddypress' );
1174                  return false;
1175              }
1176  
1177              // Trim out empty field options.
1178              $field_values  = array_values( $_POST[ $option_name ] );
1179              $field_options = array_map( 'sanitize_text_field', $field_values );
1180              $field_count   = count( $field_options );
1181  
1182              // Check for missing or malformed options.
1183              if ( 0 === $field_count ) {
1184                  /* translators: %s: field type name */
1185                  $message = sprintf( esc_html__( '%s require at least one option.', 'buddypress' ), $field_type->name );
1186                  return false;
1187              }
1188  
1189              // If only one option exists, it cannot be an empty string.
1190              if ( ( 1 === $field_count ) && ( '' === $field_options[0] ) ) {
1191                  /* translators: %s: field type name */
1192                  $message = sprintf( esc_html__( '%s require at least one option.', 'buddypress' ), $field_type->name );
1193                  return false;
1194              }
1195          }
1196  
1197          return true;
1198      }
1199  
1200      /**
1201       * Save miscellaneous settings for this field.
1202       *
1203       * Some field types have type-specific settings, which are saved here.
1204       *
1205       * @since 2.7.0
1206       *
1207       * @param array $settings Array of settings.
1208       */
1209  	public function admin_save_settings( $settings ) {
1210          return $this->type_obj->admin_save_settings( $this->id, $settings );
1211      }
1212  
1213      /**
1214       * Populates the items for radio buttons, checkboxes, and dropdown boxes.
1215       */
1216  	public function render_admin_form_children() {
1217          foreach ( array_keys( bp_xprofile_get_field_types() ) as $field_type ) {
1218              $type_obj = bp_xprofile_create_field_type( $field_type );
1219              $type_obj->admin_new_field_html( $this );
1220          }
1221      }
1222  
1223      /**
1224       * Oupput the admin form for this field.
1225       *
1226       * @since 1.9.0
1227       *
1228       * @param string $message Message to display.
1229       */
1230  	public function render_admin_form( $message = '' ) {
1231  
1232          // Users Admin URL.
1233          $users_url = bp_get_admin_url( 'users.php' );
1234  
1235          // Add New.
1236          if ( empty( $this->id ) ) {
1237              $title  = __( 'Add New Field', 'buddypress' );
1238              $button    = __( 'Save',          'buddypress' );
1239              $action = add_query_arg( array(
1240                  'page'     => 'bp-profile-setup',
1241                  'mode'     => 'add_field',
1242                  'group_id' => (int) $this->group_id
1243              ), $users_url . '#tabs-' . (int) $this->group_id );
1244  
1245              if ( !empty( $_POST['saveField'] ) ) {
1246                  $this->name        = $_POST['title'];
1247                  $this->description = $_POST['description'];
1248                  $this->is_required = $_POST['required'];
1249                  $this->type        = $_POST['fieldtype'];
1250                  $this->field_order = $_POST['field_order'];
1251  
1252                  if ( ! empty( $_POST["sort_order_{$this->type}"] ) ) {
1253                      $this->order_by = $_POST["sort_order_{$this->type}"];
1254                  }
1255              }
1256  
1257          // Edit.
1258          } else {
1259              $title  = __( 'Edit Field', 'buddypress' );
1260              $button    = __( 'Update',     'buddypress' );
1261              $action = add_query_arg( array(
1262                  'page'     => 'bp-profile-setup',
1263                  'mode'     => 'edit_field',
1264                  'group_id' => (int) $this->group_id,
1265                  'field_id' => (int) $this->id
1266              ), $users_url . '#tabs-' . (int) $this->group_id );
1267          } ?>
1268  
1269          <div class="wrap">
1270  
1271              <h1 class="wp-heading-inline"><?php echo esc_html( $title ); ?></h1>
1272              <hr class="wp-header-end">
1273  
1274              <?php if ( !empty( $message ) ) : ?>
1275  
1276                  <div id="message" class="error fade notice is-dismissible">
1277                      <p><?php echo esc_html( $message ); ?></p>
1278                  </div>
1279  
1280              <?php endif; ?>
1281  
1282              <form id="bp-xprofile-add-field" action="<?php echo esc_url( $action ); ?>" method="post">
1283                  <div id="poststuff">
1284                      <div id="post-body" class="metabox-holder columns-<?php echo ( 1 == get_current_screen()->get_columns() ) ? '1' : '2'; ?>">
1285                          <div id="post-body-content">
1286  
1287                              <?php
1288  
1289                              // Output the name & description fields.
1290                              $this->name_and_description(); ?>
1291  
1292                          </div><!-- #post-body-content -->
1293  
1294                          <div id="postbox-container-1" class="postbox-container">
1295  
1296                              <?php
1297  
1298                              // Output the sumbit metabox.
1299                              $this->submit_metabox( $button );
1300  
1301                              // Output the required metabox.
1302                              $this->required_metabox();
1303  
1304                              // Output signup position metabox.
1305                              $this->signup_position_metabox();
1306  
1307                              // Output the Member Types metabox.
1308                              $this->member_type_metabox();
1309  
1310                              // Output the field visibility metaboxes.
1311                              $this->visibility_metabox();
1312  
1313                              // Output the autolink metabox.
1314                              $this->autolink_metabox();
1315  
1316  
1317                              /**
1318                               * Fires after XProfile Field sidebar metabox.
1319                               *
1320                               * @since 2.2.0
1321                               *
1322                               * @param BP_XProfile_Field $this Current XProfile field.
1323                               */
1324                              do_action( 'xprofile_field_after_sidebarbox', $this ); ?>
1325  
1326                          </div>
1327  
1328                          <div id="postbox-container-2" class="postbox-container">
1329  
1330                              <?php
1331  
1332                              /**
1333                               * Fires before XProfile Field content metabox.
1334                               *
1335                               * @since 2.3.0
1336                               *
1337                               * @param BP_XProfile_Field $this Current XProfile field.
1338                               */
1339                              do_action( 'xprofile_field_before_contentbox', $this );
1340  
1341                              // Output the field attributes metabox.
1342                              $this->type_metabox();
1343  
1344                              // Output hidden inputs for default field.
1345                              $this->default_field_hidden_inputs();
1346  
1347                              /**
1348                               * Fires after XProfile Field content metabox.
1349                               *
1350                               * @since 2.2.0
1351                               *
1352                               * @param BP_XProfile_Field $this Current XProfile field.
1353                               */
1354                              do_action( 'xprofile_field_after_contentbox', $this ); ?>
1355  
1356                          </div>
1357                      </div><!-- #post-body -->
1358                  </div><!-- #poststuff -->
1359              </form>
1360          </div>
1361  
1362      <?php
1363      }
1364  
1365      /**
1366       * Gets field type supports.
1367       *
1368       * @since 8.0.0
1369       *
1370       * @return bool[] Supported features.
1371       */
1372  	public function get_field_type_supports() {
1373          $supports = array(
1374              'switch_fieldtype'        => true,
1375              'required'                => true,
1376              'do_autolink'             => true,
1377              'allow_custom_visibility' => true,
1378              'member_types'            => true,
1379              'signup_position'         => true,
1380          );
1381  
1382          if ( isset( $this->type_obj ) && $this->type_obj ) {
1383              $field_type = $this->type_obj;
1384  
1385              if ( isset( $field_type::$supported_features ) ) {
1386                  $supports = array_merge( $supports, $field_type::$supported_features );
1387              }
1388          }
1389  
1390          return $supports;
1391      }
1392  
1393      /**
1394       * Checks whether the field type supports the requested feature.
1395       *
1396       * @since 8.0.0
1397       *
1398       * @param string $support The name of the feature.
1399       * @return boolean True if the field type supports the feature. False otherwise.
1400       */
1401  	public function field_type_supports( $support = '' ) {
1402          $retval   = true;
1403          $features = $this->get_field_type_supports();
1404  
1405          if ( isset( $features[ $support ] ) ) {
1406              $retval = $features[ $support ];
1407          }
1408  
1409          return $retval;
1410      }
1411  
1412      /**
1413       * Private method used to display the submit metabox.
1414       *
1415       * @since 2.3.0
1416       *
1417       * @param string $button_text Text to put on button.
1418       */
1419  	private function submit_metabox( $button_text = '' ) {
1420  
1421          // Setup the URL for deleting
1422          $users_url  = bp_get_admin_url( 'users.php' );
1423          $cancel_url = add_query_arg( array(
1424              'page' => 'bp-profile-setup'
1425          ), $users_url );
1426  
1427  
1428          // Delete.
1429          if ( $this->can_delete ) {
1430              $delete_url = wp_nonce_url( add_query_arg( array(
1431                  'page'     => 'bp-profile-setup',
1432                  'mode'     => 'delete_field',
1433                  'field_id' => (int) $this->id
1434              ), $users_url ), 'bp_xprofile_delete_field-' . $this->id, 'bp_xprofile_delete_field' );
1435          }
1436          /**
1437           * Fires before XProfile Field submit metabox.
1438           *
1439           * @since 2.1.0
1440           *
1441           * @param BP_XProfile_Field $this Current XProfile field.
1442           */
1443          do_action( 'xprofile_field_before_submitbox', $this ); ?>
1444  
1445          <div id="submitdiv" class="postbox">
1446              <h2><?php esc_html_e( 'Submit', 'buddypress' ); ?></h2>
1447              <div class="inside">
1448                  <div id="submitcomment" class="submitbox">
1449                      <div id="major-publishing-actions">
1450  
1451                          <?php
1452  
1453                          /**
1454                           * Fires at the beginning of the XProfile Field publishing actions section.
1455                           *
1456                           * @since 2.1.0
1457                           *
1458                           * @param BP_XProfile_Field $this Current XProfile field.
1459                           */
1460                          do_action( 'xprofile_field_submitbox_start', $this ); ?>
1461  
1462                          <input type="hidden" name="field_order" id="field_order" value="<?php echo esc_attr( $this->field_order ); ?>" />
1463  
1464                          <?php if ( ! empty( $button_text ) ) : ?>
1465  
1466                              <div id="publishing-action">
1467                                  <input type="submit" name="saveField" value="<?php echo esc_attr( $button_text ); ?>" class="button-primary" />
1468                              </div>
1469  
1470                          <?php endif; ?>
1471  
1472                          <div id="delete-action">
1473                              <?php if ( ! empty( $this->id ) && isset( $delete_url ) ) : ?>
1474                                  <a href="<?php echo esc_url( $delete_url ); ?>" class="submitdelete deletion"><?php esc_html_e( 'Delete', 'buddypress' ); ?></a>
1475                              <?php endif; ?>
1476  
1477                              <div><a href="<?php echo esc_url( $cancel_url ); ?>" class="deletion"><?php esc_html_e( 'Cancel', 'buddypress' ); ?></a></div>
1478                          </div>
1479  
1480                          <?php wp_nonce_field( 'xprofile_delete_option' ); ?>
1481  
1482                          <div class="clear"></div>
1483                      </div>
1484                  </div>
1485              </div>
1486          </div>
1487  
1488          <?php
1489  
1490          /**
1491           * Fires after XProfile Field submit metabox.
1492           *
1493           * @since 2.1.0
1494           *
1495           * @param BP_XProfile_Field $this Current XProfile field.
1496           */
1497          do_action( 'xprofile_field_after_submitbox', $this );
1498      }
1499  
1500      /**
1501       * Private method used to output field name and description fields.
1502       *
1503       * @since 2.3.0
1504       */
1505  	private function name_and_description() {
1506      ?>
1507  
1508          <div id="titlediv">
1509              <div class="titlewrap">
1510                  <label id="title-prompt-text" for="title"><?php echo esc_html_x( 'Name (required)', 'XProfile admin edit field', 'buddypress' ); ?></label>
1511                  <input type="text" name="title" id="title" value="<?php echo esc_attr( $this->name ); ?>" autocomplete="off" />
1512              </div>
1513          </div>
1514  
1515          <div class="postbox">
1516              <h2><?php echo esc_html_x( 'Description', 'XProfile admin edit field', 'buddypress' ); ?></h2>
1517              <div class="inside">
1518                  <label for="description" class="screen-reader-text"><?php
1519                      /* translators: accessibility text */
1520                      esc_html_e( 'Add description', 'buddypress' );
1521                  ?></label>
1522                  <textarea name="description" id="description" rows="8" cols="60"><?php echo esc_textarea( $this->description ); ?></textarea>
1523              </div>
1524          </div>
1525  
1526      <?php
1527      }
1528  
1529      /**
1530       * Private method used to output field Member Type metabox.
1531       *
1532       * @since 2.4.0
1533       *
1534       * @return void If default field or if the field does not support the feature.
1535       */
1536  	private function member_type_metabox() {
1537  
1538          // The primary field is for all, so bail.
1539          if ( true === $this->is_default_field() || ! $this->field_type_supports( 'member_types' ) ) {
1540              return;
1541          }
1542  
1543          // Bail when no member types are registered.
1544          if ( ! $member_types = bp_get_member_types( array(), 'objects' ) ) {
1545              return;
1546          }
1547  
1548          $field_member_types = $this->get_member_types();
1549  
1550          ?>
1551  
1552          <div id="field-type-member-types" class="postbox">
1553              <h2><?php _e( 'Member Types', 'buddypress' ); ?></h2>
1554              <div class="inside">
1555                  <p class="description"><?php _e( 'This field should be available to:', 'buddypress' ); ?></p>
1556  
1557                  <ul>
1558                      <?php foreach ( $member_types as $member_type ) : ?>
1559                      <li>
1560                          <label for="member-type-<?php echo $member_type->labels['name']; ?>">
1561                              <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 ) ); ?>/>
1562                              <?php echo $member_type->labels['name']; ?>
1563                          </label>
1564                      </li>
1565                      <?php endforeach; ?>
1566  
1567                      <li>
1568                          <label for="member-type-none">
1569                              <input name="member-types[]" id="member-type-none" class="member-type-selector" type="checkbox" value="null" <?php checked( in_array( 'null', $field_member_types ) ); ?>/>
1570                              <?php _e( 'Users with no member type', 'buddypress' ); ?>
1571                          </label>
1572                      </li>
1573  
1574                  </ul>
1575                  <p class="description member-type-none-notice<?php if ( ! empty( $field_member_types ) ) : ?> hide<?php endif; ?>"><?php _e( 'Unavailable to all members.', 'buddypress' ) ?></p>
1576              </div>
1577  
1578              <input type="hidden" name="has-member-types" value="1" />
1579          </div>
1580  
1581          <?php
1582      }
1583  
1584      /**
1585       * Private method used to output field visibility metaboxes.
1586       *
1587       * @since 2.3.0
1588       *
1589       * @return void If default field or if the field does not support the feature.
1590       */
1591  	private function visibility_metabox() {
1592  
1593          // Default field and field types not supporting the feature cannot have custom visibility.
1594          if ( true === $this->is_default_field() || ! $this->field_type_supports( 'allow_custom_visibility' ) ) {
1595              return;
1596          } ?>
1597  
1598          <div class="postbox" id="field-type-visibiliy-metabox">
1599              <h2><label for="default-visibility"><?php esc_html_e( 'Visibility', 'buddypress' ); ?></label></h2>
1600              <div class="inside">
1601                  <div>
1602                      <select name="default-visibility" id="default-visibility">
1603  
1604                          <?php foreach( bp_xprofile_get_visibility_levels() as $level ) : ?>
1605  
1606                              <option value="<?php echo esc_attr( $level['id'] ); ?>" <?php selected( $this->get_default_visibility(), $level['id'] ); ?>>
1607                                  <?php echo esc_html( $level['label'] ); ?>
1608                              </option>
1609  
1610                          <?php endforeach ?>
1611  
1612                      </select>
1613                  </div>
1614  
1615                  <div>
1616                      <ul>
1617                          <li>
1618                              <input type="radio" id="allow-custom-visibility-allowed" name="allow-custom-visibility" value="allowed" <?php checked( $this->get_allow_custom_visibility(), 'allowed' ); ?> />
1619                              <label for="allow-custom-visibility-allowed"><?php esc_html_e( 'Allow members to override', 'buddypress' ); ?></label>
1620                          </li>
1621                          <li>
1622                              <input type="radio" id="allow-custom-visibility-disabled" name="allow-custom-visibility" value="disabled" <?php checked( $this->get_allow_custom_visibility(), 'disabled' ); ?> />
1623                              <label for="allow-custom-visibility-disabled"><?php esc_html_e( 'Enforce field visibility', 'buddypress' ); ?></label>
1624                          </li>
1625                      </ul>
1626                  </div>
1627              </div>
1628          </div>
1629  
1630          <?php
1631      }
1632  
1633      /**
1634       * Output the metabox for setting if field is required or not.
1635       *
1636       * @since 2.3.0
1637       *
1638       * @return void If default field or if the field does not support the feature.
1639       */
1640  	private function required_metabox() {
1641  
1642          // Default field and field types not supporting the feature cannot be required.
1643          if ( true === $this->is_default_field() || ! $this->field_type_supports( 'required' ) ) {
1644              return;
1645          } ?>
1646  
1647          <div class="postbox" id="field-type-required-metabox">
1648              <h2><label for="required"><?php esc_html_e( 'Requirement', 'buddypress' ); ?></label></h2>
1649              <div class="inside">
1650                  <select name="required" id="required">
1651                      <option value="0"<?php selected( $this->is_required, '0' ); ?>><?php esc_html_e( 'Not Required', 'buddypress' ); ?></option>
1652                      <option value="1"<?php selected( $this->is_required, '1' ); ?>><?php esc_html_e( 'Required',     'buddypress' ); ?></option>
1653                  </select>
1654              </div>
1655          </div>
1656  
1657      <?php
1658      }
1659  
1660      /**
1661       * Private method used to output autolink metabox.
1662       *
1663       * @since 2.5.0
1664       *
1665       * @return void If the field does not support the feature.
1666       */
1667  	private function autolink_metabox() {
1668  
1669          // Field types not supporting the feature cannot use autolink.
1670          if ( ! $this->field_type_supports( 'do_autolink' ) ) {
1671              return;
1672          } ?>
1673  
1674          <div class="postbox" id="field-type-autolink-metabox">
1675              <h2><?php esc_html_e( 'Autolink', 'buddypress' ); ?></h2>
1676              <div class="inside">
1677                  <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>
1678  
1679                  <p>
1680                      <label for="do-autolink" class="screen-reader-text"><?php
1681                          /* translators: accessibility text */
1682                          esc_html_e( 'Autolink status for this field', 'buddypress' );
1683                      ?></label>
1684                      <select name="do_autolink" id="do-autolink">
1685                          <option value="on" <?php selected( $this->get_do_autolink() ); ?>><?php esc_html_e( 'Enabled', 'buddypress' ); ?></option>
1686                          <option value="" <?php selected( $this->get_do_autolink(), false ); ?>><?php esc_html_e( 'Disabled', 'buddypress' ); ?></option>
1687                      </select>
1688                  </p>
1689              </div>
1690          </div>
1691  
1692          <?php
1693      }
1694  
1695      /**
1696       * Output the metabox for setting what type of field this is.
1697       *
1698       * @since 2.3.0
1699       *
1700       * @return void If default field.
1701       */
1702  	private function type_metabox() {
1703  
1704          // Default field cannot change type.
1705          if ( true === $this->is_default_field() ) {
1706              return;
1707          }
1708          ?>
1709  
1710          <div class="postbox">
1711              <h2><label for="fieldtype"><?php esc_html_e( 'Type', 'buddypress'); ?></label></h2>
1712              <div class="inside" aria-live="polite" aria-atomic="true" aria-relevant="all">
1713                  <?php if ( ! $this->field_type_supports( 'switch_fieldtype' ) ) : ?>
1714                      <input type="text" disabled="true" value="<?php echo esc_attr( $this->type_obj->name ); ?>">
1715                      <input type="hidden" name="fieldtype" id="fieldtype" value="<?php echo esc_attr( $this->type ); ?>">
1716  
1717                  <?php else : ?>
1718                      <select name="fieldtype" id="fieldtype" onchange="show_options(this.value)">
1719  
1720                          <?php bp_xprofile_admin_form_field_types( $this->type ); ?>
1721  
1722                      </select>
1723                  <?php endif; ?>
1724  
1725                  <?php
1726  
1727                  // Deprecated filter, don't use. Go look at {@link BP_XProfile_Field_Type::admin_new_field_html()}.
1728                  do_action( 'xprofile_field_additional_options', $this );
1729  
1730                  $this->render_admin_form_children(); ?>
1731  
1732              </div>
1733          </div>
1734  
1735      <?php
1736      }
1737  
1738      /**
1739       * Output the metabox for setting the field's position into the signup form.
1740       *
1741       * @since 8.0.0
1742       *
1743       * @return void If default field or if the field does not support the feature.
1744       */
1745  	private function signup_position_metabox() {
1746          // Field types not supporting the feature cannot be added to signups form.
1747          if ( ! $this->field_type_supports( 'signup_position' ) || true === $this->is_default_field() ) {
1748              return;
1749          }
1750  
1751          $next_signup_position = 1;
1752          $signup_position      = $this->get_signup_position();
1753  
1754          if ( 0 === $signup_position ) {
1755              $signup_fields_order = bp_xprofile_get_signup_field_ids();
1756              $next_signup_position = count( $signup_fields_order ) + 1;
1757          } else {
1758              $next_signup_position = $signup_position;
1759          }
1760          ?>
1761  
1762          <div class="postbox" id="field-signup-position-metabox">
1763              <h2><label for="default-visibility"><?php esc_html_e( 'Signups', 'buddypress' ); ?></label></h2>
1764              <div class="inside">
1765                  <div>
1766                      <ul>
1767                          <li>
1768                              <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 ); ?> />
1769                              <label for="has-signup-position"><?php esc_html_e( 'Use the field into the registration form.', 'buddypress' ); ?></label>
1770                          </li>
1771                      </ul>
1772                  </div>
1773              </div>
1774          </div>
1775          <?php
1776      }
1777  
1778      /**
1779       * Output hidden fields used by default field.
1780       *
1781       * @since 2.3.0
1782       *
1783       * @return void If not default field.
1784       */
1785  	private function default_field_hidden_inputs() {
1786  
1787          // Nonce.
1788          wp_nonce_field( 'bp_xprofile_admin_field', 'bp_xprofile_admin_field' );
1789  
1790          // Init default field hidden inputs.
1791          $default_field_hidden_inputs = array();
1792          $hidden_fields = array(
1793              'required' => array(
1794                  'name'  => 'required',
1795                  'id'    => 'required',
1796                  'value' => '0',
1797              ),
1798              'default_visibility' => array(
1799                  'name'  => 'default-visibility',
1800                  'id'    => 'default-visibility',
1801                  'value' => $this->get_default_visibility(),
1802              ),
1803              'allow_custom_visibility' => array(
1804                  'name'  => 'allow-custom-visibility',
1805                  'id'    => 'allow-custom-visibility',
1806                  'value' => 'disabled',
1807              ),
1808              'do_autolink' => array(
1809                  'name'  => 'do_autolink',
1810                  'id'    => 'do-autolink',
1811                  'value' => '',
1812              ),
1813          );
1814  
1815          // Field 1 is the fullname field, which is required.
1816          if ( true === $this->is_default_field() ) {
1817              $default_field_required          = $hidden_fields['required'];
1818              $default_field_required['value'] = '1';
1819  
1820              $default_field_hidden_inputs = array(
1821                  $default_field_required,
1822                  array(
1823                      'name'  => 'fieldtype',
1824                      'id'    => 'fieldtype',
1825                      'value' => 'textbox',
1826                  ),
1827                  array(
1828                      'name'  => 'signup-position',
1829                      'id'    => 'has-signup-position',
1830                      'value' => $this->get_signup_position(),
1831                  ),
1832              );
1833          }
1834  
1835          $supports = $this->get_field_type_supports();
1836          if ( $supports ) {
1837              foreach ( $supports as $feature => $support ) {
1838                  if ( true === $support || in_array( $feature, array( 'switch_fieldtype', 'member_types' ), true ) ) {
1839                      continue;
1840                  }
1841  
1842                  $default_field_hidden_inputs[] = $hidden_fields[ $feature ];
1843  
1844                  if ( 'allow_custom_visibility' === $feature ) {
1845                      $default_field_hidden_inputs[] = $hidden_fields['default_visibility'];
1846                  }
1847              }
1848          }
1849  
1850          if ( ! $default_field_hidden_inputs ) {
1851              return;
1852          }
1853  
1854          foreach ( $default_field_hidden_inputs as $default_field_hidden_input ) {
1855              printf(
1856                  '<input type="hidden" name="%1$s" id="%2$s" value="%3$s"/>',
1857                  esc_attr( $default_field_hidden_input['name'] ),
1858                  esc_attr( $default_field_hidden_input['id'] ),
1859                  esc_attr( $default_field_hidden_input['value'] )
1860              );
1861          }
1862      }
1863  
1864      /**
1865       * Return if a field ID is the default field.
1866       *
1867       * @since 2.3.0
1868       *
1869       * @param int $field_id ID of field to check.
1870       * @return bool
1871       */
1872  	private function is_default_field( $field_id = 0 ) {
1873  
1874          // Fallback to current field ID if none passed.
1875          if ( empty( $field_id ) ) {
1876              $field_id = $this->id;
1877          }
1878  
1879          // Compare & return.
1880          return (bool) ( 1 === (int) $field_id );
1881      }
1882  }


Generated: Wed Jun 23 01:01:38 2021 Cross-referenced by PHPXref 0.7.1