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


Generated: Fri Jan 24 01:01:43 2020 Cross-referenced by PHPXref 0.7.1