[ 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          // Initialise 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, intepret 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          return $this->do_autolink;
 866      }
 867  
 868      /* Static Methods ********************************************************/
 869  
 870      /**
 871       * Get the type for provided field ID.
 872       *
 873       * @param int $field_id Field ID to get type of.
 874       * @return bool|null|string
 875       */
 876  	public static function get_type( $field_id = 0 ) {
 877          global $wpdb;
 878  
 879          // Bail if no field ID.
 880          if ( empty( $field_id ) ) {
 881              return false;
 882          }
 883  
 884          $bp   = buddypress();
 885          $sql  = $wpdb->prepare( "SELECT type FROM {$bp->profile->table_name_fields} WHERE id = %d", $field_id );
 886          $type = $wpdb->get_var( $sql );
 887  
 888          // Return field type.
 889          if ( ! empty( $type ) ) {
 890              return $type;
 891          }
 892  
 893          return false;
 894      }
 895  
 896      /**
 897       * Delete all fields in a field group.
 898       *
 899       * @since 1.2.0
 900       *
 901       * @global object $wpdb
 902       *
 903       * @param int $group_id ID of the field group to delete fields from.
 904       * @return boolean
 905       */
 906  	public static function delete_for_group( $group_id = 0 ) {
 907          global $wpdb;
 908  
 909          // Bail if no group ID.
 910          if ( empty( $group_id ) ) {
 911              return false;
 912          }
 913  
 914          $bp      = buddypress();
 915          $sql     = $wpdb->prepare( "DELETE FROM {$bp->profile->table_name_fields} WHERE group_id = %d", $group_id );
 916          $deleted = $wpdb->get_var( $sql );
 917  
 918          // Return true if fields were deleted.
 919          if ( false !== $deleted ) {
 920              return true;
 921          }
 922  
 923          return false;
 924      }
 925  
 926      /**
 927       * Get field ID from field name.
 928       *
 929       * @since 1.5.0
 930       *
 931       * @global object $wpdb
 932       *
 933       * @param string $field_name Name of the field to query the ID for.
 934       * @return int|null Field ID on success; null on failure.
 935       */
 936  	public static function get_id_from_name( $field_name = '' ) {
 937          global $wpdb;
 938  
 939          $bp = buddypress();
 940  
 941          if ( empty( $bp->profile->table_name_fields ) || empty( $field_name ) ) {
 942              return false;
 943          }
 944  
 945          $id = bp_core_get_incremented_cache( $field_name, 'bp_xprofile_fields_by_name' );
 946          if ( false === $id ) {
 947              $sql = $wpdb->prepare( "SELECT id FROM {$bp->profile->table_name_fields} WHERE name = %s AND parent_id = 0", $field_name );
 948              $id = $wpdb->get_var( $sql );
 949              bp_core_set_incremented_cache( $field_name, 'bp_xprofile_fields_by_name', $id );
 950          }
 951  
 952          return is_numeric( $id ) ? (int) $id : $id;
 953      }
 954  
 955      /**
 956       * Update field position and/or field group when relocating.
 957       *
 958       * @since 1.5.0
 959       *
 960       * @global object $wpdb
 961       *
 962       * @param int      $field_id       ID of the field to update.
 963       * @param int|null $position       Field position to update.
 964       * @param int|null $field_group_id ID of the field group.
 965       * @return boolean
 966       */
 967  	public static function update_position( $field_id, $position = null, $field_group_id = null ) {
 968          global $wpdb;
 969  
 970          // Bail if invalid position or field group.
 971          if ( ! is_numeric( $position ) || ! is_numeric( $field_group_id ) ) {
 972              return false;
 973          }
 974  
 975          // Get table name and field parent.
 976          $table_name = buddypress()->profile->table_name_fields;
 977          $sql        = $wpdb->prepare( "UPDATE {$table_name} SET field_order = %d, group_id = %d WHERE id = %d", $position, $field_group_id, $field_id );
 978          $parent     = $wpdb->query( $sql );
 979  
 980          $retval = false;
 981  
 982          // Update $field_id with new $position and $field_group_id.
 983          if ( ! empty( $parent ) && ! is_wp_error( $parent ) ) {
 984  
 985              // Update any children of this $field_id.
 986              $sql = $wpdb->prepare( "UPDATE {$table_name} SET group_id = %d WHERE parent_id = %d", $field_group_id, $field_id );
 987              $wpdb->query( $sql );
 988  
 989              // Invalidate profile field and group query cache.
 990              wp_cache_delete( $field_id, 'bp_xprofile_fields' );
 991  
 992              $retval = $parent;
 993          }
 994  
 995          bp_core_reset_incrementor( 'bp_xprofile_groups' );
 996  
 997          return $retval;
 998      }
 999  
1000      /**
1001       * Gets the IDs of fields applicable for a given member type or array of member types.
1002       *
1003       * @since 2.4.0
1004       *
1005       * @param string|array $member_types Member type or array of member types. Use 'any' to return unrestricted
1006       *                                   fields (those available for anyone, regardless of member type).
1007       * @return array Multi-dimensional array, with field IDs as top-level keys, and arrays of member types
1008       *               associated with each field as values.
1009       */
1010  	public static function get_fields_for_member_type( $member_types ) {
1011          global $wpdb;
1012  
1013          $fields = array();
1014  
1015          if ( empty( $member_types ) ) {
1016              $member_types = array( 'any' );
1017          } elseif ( ! is_array( $member_types ) ) {
1018              $member_types = array( $member_types );
1019          }
1020  
1021          $bp = buddypress();
1022  
1023          // Pull up all recorded field member type data.
1024          $mt_meta = wp_cache_get( 'field_member_types', 'bp_xprofile' );
1025          if ( false === $mt_meta ) {
1026              $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'" );
1027              wp_cache_set( 'field_member_types', $mt_meta, 'bp_xprofile' );
1028          }
1029  
1030          // Keep track of all fields with recorded member_type metadata.
1031          $all_recorded_field_ids = wp_list_pluck( $mt_meta, 'object_id' );
1032  
1033          // Sort member_type matches in arrays, keyed by field_id.
1034          foreach ( $mt_meta as $_mt_meta ) {
1035              if ( ! isset( $fields[ $_mt_meta->object_id ] ) ) {
1036                  $fields[ $_mt_meta->object_id ] = array();
1037              }
1038  
1039              $fields[ $_mt_meta->object_id ][] = $_mt_meta->meta_value;
1040          }
1041  
1042          /*
1043           * Filter out fields that don't match any passed types, or those marked '_none'.
1044           * The 'any' type is implicitly handled here: it will match no types.
1045           */
1046          foreach ( $fields as $field_id => $field_types ) {
1047              if ( ! array_intersect( $field_types, $member_types ) ) {
1048                  unset( $fields[ $field_id ] );
1049              }
1050          }
1051  
1052          // Any fields with no member_type metadata are available to all member types.
1053          if ( ! in_array( '_none', $member_types ) ) {
1054              if ( ! empty( $all_recorded_field_ids ) ) {
1055                  $all_recorded_field_ids_sql = implode( ',', array_map( 'absint', $all_recorded_field_ids ) );
1056                  $unrestricted_field_ids = $wpdb->get_col( "SELECT id FROM {$bp->profile->table_name_fields} WHERE id NOT IN ({$all_recorded_field_ids_sql})" );
1057              } else {
1058                  $unrestricted_field_ids = $wpdb->get_col( "SELECT id FROM {$bp->profile->table_name_fields}" );
1059              }
1060  
1061              // Append the 'null' pseudo-type.
1062              $all_member_types   = bp_get_member_types();
1063              $all_member_types   = array_values( $all_member_types );
1064              $all_member_types[] = 'null';
1065  
1066              foreach ( $unrestricted_field_ids as $unrestricted_field_id ) {
1067                  $fields[ $unrestricted_field_id ] = $all_member_types;
1068              }
1069          }
1070  
1071          return $fields;
1072      }
1073  
1074      /**
1075       * Validate form field data on sumbission.
1076       *
1077       * @since 2.2.0
1078       *
1079       * @global $message
1080       *
1081       * @return boolean
1082       */
1083  	public static function admin_validate() {
1084          global $message;
1085  
1086          // Check field name.
1087          if ( ! isset( $_POST['title'] ) || ( '' === $_POST['title'] ) ) {
1088              $message = esc_html__( 'Profile fields must have a name.', 'buddypress' );
1089              return false;
1090          }
1091  
1092          // Check field requirement.
1093          if ( ! isset( $_POST['required'] ) ) {
1094              $message = esc_html__( 'Profile field requirement is missing.', 'buddypress' );
1095              return false;
1096          }
1097  
1098          // Check field type.
1099          if ( empty( $_POST['fieldtype'] ) ) {
1100              $message = esc_html__( 'Profile field type is missing.', 'buddypress' );
1101              return false;
1102          }
1103  
1104          // Check that field is of valid type.
1105          if ( ! in_array( $_POST['fieldtype'], array_keys( bp_xprofile_get_field_types() ), true ) ) {
1106              $message = sprintf( esc_html__( 'The profile field type %s is not registered.', 'buddypress' ), '<code>' . esc_attr( $_POST['fieldtype'] ) . '</code>' );
1107              return false;
1108          }
1109  
1110          // Get field type so we can check for and lavidate any field options.
1111          $field_type = bp_xprofile_create_field_type( $_POST['fieldtype'] );
1112  
1113          // Field type requires options.
1114          if ( true === $field_type->supports_options ) {
1115  
1116              // Build the field option key.
1117              $option_name = sanitize_key( $_POST['fieldtype'] ) . '_option';
1118  
1119              // Check for missing or malformed options.
1120              if ( empty( $_POST[ $option_name ] ) || ! is_array( $_POST[ $option_name ] ) ) {
1121                  $message = esc_html__( 'These field options are invalid.', 'buddypress' );
1122                  return false;
1123              }
1124  
1125              // Trim out empty field options.
1126              $field_values  = array_values( $_POST[ $option_name ] );
1127              $field_options = array_map( 'sanitize_text_field', $field_values );
1128              $field_count   = count( $field_options );
1129  
1130              // Check for missing or malformed options.
1131              if ( 0 === $field_count ) {
1132                  $message = sprintf( esc_html__( '%s require at least one option.', 'buddypress' ), $field_type->name );
1133                  return false;
1134              }
1135  
1136              // If only one option exists, it cannot be an empty string.
1137              if ( ( 1 === $field_count ) && ( '' === $field_options[0] ) ) {
1138                  $message = sprintf( esc_html__( '%s require at least one option.', 'buddypress' ), $field_type->name );
1139                  return false;
1140              }
1141          }
1142  
1143          return true;
1144      }
1145  
1146      /**
1147       * Save miscellaneous settings for this field.
1148       *
1149       * Some field types have type-specific settings, which are saved here.
1150       *
1151       * @since 2.7.0
1152       *
1153       * @param array $settings Array of settings.
1154       */
1155  	public function admin_save_settings( $settings ) {
1156          return $this->type_obj->admin_save_settings( $this->id, $settings );
1157      }
1158  
1159      /**
1160       * Populates the items for radio buttons, checkboxes, and dropdown boxes.
1161       */
1162  	public function render_admin_form_children() {
1163          foreach ( array_keys( bp_xprofile_get_field_types() ) as $field_type ) {
1164              $type_obj = bp_xprofile_create_field_type( $field_type );
1165              $type_obj->admin_new_field_html( $this );
1166          }
1167      }
1168  
1169      /**
1170       * Oupput the admin form for this field.
1171       *
1172       * @since 1.9.0
1173       *
1174       * @param string $message Message to display.
1175       */
1176  	public function render_admin_form( $message = '' ) {
1177  
1178          // Users Admin URL
1179          $users_url = bp_get_admin_url( 'users.php' );
1180  
1181          // Add New
1182          if ( empty( $this->id ) ) {
1183              $title  = __( 'Add New Field', 'buddypress' );
1184              $button    = __( 'Save',          'buddypress' );
1185              $action = add_query_arg( array(
1186                  'page'     => 'bp-profile-setup',
1187                  'mode'     => 'add_field',
1188                  'group_id' => (int) $this->group_id
1189              ), $users_url . '#tabs-' . (int) $this->group_id );
1190  
1191              if ( !empty( $_POST['saveField'] ) ) {
1192                  $this->name        = $_POST['title'];
1193                  $this->description = $_POST['description'];
1194                  $this->is_required = $_POST['required'];
1195                  $this->type        = $_POST['fieldtype'];
1196                  $this->field_order = $_POST['field_order'];
1197  
1198                  if ( ! empty( $_POST["sort_order_{$this->type}"] ) ) {
1199                      $this->order_by = $_POST["sort_order_{$this->type}"];
1200                  }
1201              }
1202  
1203          // Edit
1204          } else {
1205              $title  = __( 'Edit Field', 'buddypress' );
1206              $button    = __( 'Update',     'buddypress' );
1207              $action = add_query_arg( array(
1208                  'page'     => 'bp-profile-setup',
1209                  'mode'     => 'edit_field',
1210                  'group_id' => (int) $this->group_id,
1211                  'field_id' => (int) $this->id
1212              ), $users_url . '#tabs-' . (int) $this->group_id );
1213          } ?>
1214  
1215          <div class="wrap">
1216  
1217              <h1><?php echo esc_html( $title ); ?></h1>
1218  
1219              <?php if ( !empty( $message ) ) : ?>
1220  
1221                  <div id="message" class="error fade">
1222                      <p><?php echo esc_html( $message ); ?></p>
1223                  </div>
1224  
1225              <?php endif; ?>
1226  
1227              <form id="bp-xprofile-add-field" action="<?php echo esc_url( $action ); ?>" method="post">
1228                  <div id="poststuff">
1229                      <div id="post-body" class="metabox-holder columns-<?php echo ( 1 == get_current_screen()->get_columns() ) ? '1' : '2'; ?>">
1230                          <div id="post-body-content">
1231  
1232                              <?php
1233  
1234                              // Output the name & description fields.
1235                              $this->name_and_description(); ?>
1236  
1237                          </div><!-- #post-body-content -->
1238  
1239                          <div id="postbox-container-1" class="postbox-container">
1240  
1241                              <?php
1242  
1243                              // Output the sumbit metabox.
1244                              $this->submit_metabox( $button );
1245  
1246                              // Output the required metabox.
1247                              $this->required_metabox();
1248  
1249                              // Output the Member Types metabox.
1250                              $this->member_type_metabox();
1251  
1252                              // Output the field visibility metaboxes.
1253                              $this->visibility_metabox();
1254  
1255                              // Output the autolink metabox.
1256                              $this->autolink_metabox();
1257  
1258  
1259                              /**
1260                               * Fires after XProfile Field sidebar metabox.
1261                               *
1262                               * @since 2.2.0
1263                               *
1264                               * @param BP_XProfile_Field $this Current XProfile field.
1265                               */
1266                              do_action( 'xprofile_field_after_sidebarbox', $this ); ?>
1267  
1268                          </div>
1269  
1270                          <div id="postbox-container-2" class="postbox-container">
1271  
1272                              <?php
1273  
1274                              /**
1275                               * Fires before XProfile Field content metabox.
1276                               *
1277                               * @since 2.3.0
1278                               *
1279                               * @param BP_XProfile_Field $this Current XProfile field.
1280                               */
1281                              do_action( 'xprofile_field_before_contentbox', $this );
1282  
1283                              // Output the field attributes metabox.
1284                              $this->type_metabox();
1285  
1286                              // Output hidden inputs for default field.
1287                              $this->default_field_hidden_inputs();
1288  
1289                              /**
1290                               * Fires after XProfile Field content metabox.
1291                               *
1292                               * @since 2.2.0
1293                               *
1294                               * @param BP_XProfile_Field $this Current XProfile field.
1295                               */
1296                              do_action( 'xprofile_field_after_contentbox', $this ); ?>
1297  
1298                          </div>
1299                      </div><!-- #post-body -->
1300                  </div><!-- #poststuff -->
1301              </form>
1302          </div>
1303  
1304      <?php
1305      }
1306  
1307      /**
1308       * Private method used to display the submit metabox.
1309       *
1310       * @since 2.3.0
1311       *
1312       * @param string $button_text Text to put on button.
1313       */
1314  	private function submit_metabox( $button_text = '' ) {
1315  
1316          // Setup the URL for deleting
1317          $users_url  = bp_get_admin_url( 'users.php' );
1318          $cancel_url = add_query_arg( array(
1319              'page' => 'bp-profile-setup'
1320          ), $users_url );
1321  
1322          /**
1323           * Fires before XProfile Field submit metabox.
1324           *
1325           * @since 2.1.0
1326           *
1327           * @param BP_XProfile_Field $this Current XProfile field.
1328           */
1329          do_action( 'xprofile_field_before_submitbox', $this ); ?>
1330  
1331          <div id="submitdiv" class="postbox">
1332              <h2><?php esc_html_e( 'Submit', 'buddypress' ); ?></h2>
1333              <div class="inside">
1334                  <div id="submitcomment" class="submitbox">
1335                      <div id="major-publishing-actions">
1336  
1337                          <?php
1338  
1339                          /**
1340                           * Fires at the beginning of the XProfile Field publishing actions section.
1341                           *
1342                           * @since 2.1.0
1343                           *
1344                           * @param BP_XProfile_Field $this Current XProfile field.
1345                           */
1346                          do_action( 'xprofile_field_submitbox_start', $this ); ?>
1347  
1348                          <input type="hidden" name="field_order" id="field_order" value="<?php echo esc_attr( $this->field_order ); ?>" />
1349  
1350                          <?php if ( ! empty( $button_text ) ) : ?>
1351  
1352                              <div id="publishing-action">
1353                                  <input type="submit" name="saveField" value="<?php echo esc_attr( $button_text ); ?>" class="button-primary" />
1354                              </div>
1355  
1356                          <?php endif; ?>
1357  
1358                          <div id="delete-action">
1359                              <a href="<?php echo esc_url( $cancel_url ); ?>" class="deletion"><?php esc_html_e( 'Cancel', 'buddypress' ); ?></a>
1360                          </div>
1361  
1362                          <?php wp_nonce_field( 'xprofile_delete_option' ); ?>
1363  
1364                          <div class="clear"></div>
1365                      </div>
1366                  </div>
1367              </div>
1368          </div>
1369  
1370          <?php
1371  
1372          /**
1373           * Fires after XProfile Field submit metabox.
1374           *
1375           * @since 2.1.0
1376           *
1377           * @param BP_XProfile_Field $this Current XProfile field.
1378           */
1379          do_action( 'xprofile_field_after_submitbox', $this );
1380      }
1381  
1382      /**
1383       * Private method used to output field name and description fields.
1384       *
1385       * @since 2.3.0
1386       */
1387  	private function name_and_description() {
1388      ?>
1389  
1390          <div id="titlediv">
1391              <div class="titlewrap">
1392                  <label id="title-prompt-text" for="title"><?php echo esc_html_x( 'Name (required)', 'XProfile admin edit field', 'buddypress' ); ?></label>
1393                  <input type="text" name="title" id="title" value="<?php echo esc_attr( $this->name ); ?>" autocomplete="off" />
1394              </div>
1395          </div>
1396  
1397          <div class="postbox">
1398              <h2><?php echo esc_html_x( 'Description', 'XProfile admin edit field', 'buddypress' ); ?></h2>
1399              <div class="inside">
1400                  <label for="description" class="screen-reader-text"><?php
1401                      /* translators: accessibility text */
1402                      esc_html_e( 'Add description', 'buddypress' );
1403                  ?></label>
1404                  <textarea name="description" id="description" rows="8" cols="60"><?php echo esc_textarea( $this->description ); ?></textarea>
1405              </div>
1406          </div>
1407  
1408      <?php
1409      }
1410  
1411      /**
1412       * Private method used to output field Member Type metabox.
1413       *
1414       * @since 2.4.0
1415       */
1416  	private function member_type_metabox() {
1417  
1418          // The primary field is for all, so bail.
1419          if ( 1 === (int) $this->id ) {
1420              return;
1421          }
1422  
1423          // Bail when no member types are registered.
1424          if ( ! $member_types = bp_get_member_types( array(), 'objects' ) ) {
1425              return;
1426          }
1427  
1428          $field_member_types = $this->get_member_types();
1429  
1430          ?>
1431  
1432          <div id="member-types-div" class="postbox">
1433              <h2><?php _e( 'Member Types', 'buddypress' ); ?></h2>
1434              <div class="inside">
1435                  <p class="description"><?php _e( 'This field should be available to:', 'buddypress' ); ?></p>
1436  
1437                  <ul>
1438                      <?php foreach ( $member_types as $member_type ) : ?>
1439                      <li>
1440                          <label for="member-type-<?php echo $member_type->labels['name']; ?>">
1441                              <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 ) ); ?>/>
1442                              <?php echo $member_type->labels['name']; ?>
1443                          </label>
1444                      </li>
1445                      <?php endforeach; ?>
1446  
1447                      <li>
1448                          <label for="member-type-none">
1449                              <input name="member-types[]" id="member-type-none" class="member-type-selector" type="checkbox" value="null" <?php checked( in_array( 'null', $field_member_types ) ); ?>/>
1450                              <?php _e( 'Users with no member type', 'buddypress' ); ?>
1451                          </label>
1452                      </li>
1453  
1454                  </ul>
1455                  <p class="description member-type-none-notice<?php if ( ! empty( $field_member_types ) ) : ?> hide<?php endif; ?>"><?php _e( 'Unavailable to all members.', 'buddypress' ) ?></p>
1456              </div>
1457  
1458              <input type="hidden" name="has-member-types" value="1" />
1459          </div>
1460  
1461          <?php
1462      }
1463  
1464      /**
1465       * Private method used to output field visibility metaboxes.
1466       *
1467       * @since 2.3.0
1468       *
1469       * @return void If default field id 1.
1470       */
1471  	private function visibility_metabox() {
1472  
1473          // Default field cannot have custom visibility.
1474          if ( true === $this->is_default_field() ) {
1475              return;
1476          } ?>
1477  
1478          <div class="postbox">
1479              <h2><label for="default-visibility"><?php esc_html_e( 'Visibility', 'buddypress' ); ?></label></h2>
1480              <div class="inside">
1481                  <div>
1482                      <select name="default-visibility" id="default-visibility">
1483  
1484                          <?php foreach( bp_xprofile_get_visibility_levels() as $level ) : ?>
1485  
1486                              <option value="<?php echo esc_attr( $level['id'] ); ?>" <?php selected( $this->get_default_visibility(), $level['id'] ); ?>>
1487                                  <?php echo esc_html( $level['label'] ); ?>
1488                              </option>
1489  
1490                          <?php endforeach ?>
1491  
1492                      </select>
1493                  </div>
1494  
1495                  <div>
1496                      <ul>
1497                          <li>
1498                              <input type="radio" id="allow-custom-visibility-allowed" name="allow-custom-visibility" value="allowed" <?php checked( $this->get_allow_custom_visibility(), 'allowed' ); ?> />
1499                              <label for="allow-custom-visibility-allowed"><?php esc_html_e( 'Allow members to override', 'buddypress' ); ?></label>
1500                          </li>
1501                          <li>
1502                              <input type="radio" id="allow-custom-visibility-disabled" name="allow-custom-visibility" value="disabled" <?php checked( $this->get_allow_custom_visibility(), 'disabled' ); ?> />
1503                              <label for="allow-custom-visibility-disabled"><?php esc_html_e( 'Enforce field visibility', 'buddypress' ); ?></label>
1504                          </li>
1505                      </ul>
1506                  </div>
1507              </div>
1508          </div>
1509  
1510          <?php
1511      }
1512  
1513      /**
1514       * Output the metabox for setting if field is required or not.
1515       *
1516       * @since 2.3.0
1517       *
1518       * @return void If default field.
1519       */
1520  	private function required_metabox() {
1521  
1522          // Default field is always required.
1523          if ( true === $this->is_default_field() ) {
1524              return;
1525          } ?>
1526  
1527          <div class="postbox">
1528              <h2><label for="required"><?php esc_html_e( 'Requirement', 'buddypress' ); ?></label></h2>
1529              <div class="inside">
1530                  <select name="required" id="required">
1531                      <option value="0"<?php selected( $this->is_required, '0' ); ?>><?php esc_html_e( 'Not Required', 'buddypress' ); ?></option>
1532                      <option value="1"<?php selected( $this->is_required, '1' ); ?>><?php esc_html_e( 'Required',     'buddypress' ); ?></option>
1533                  </select>
1534              </div>
1535          </div>
1536  
1537      <?php
1538      }
1539  
1540      /**
1541       * Private method used to output autolink metabox.
1542       *
1543       * @since 2.5.0
1544       *
1545       * @return void If default field id 1.
1546       */
1547  	private function autolink_metabox() {
1548          ?>
1549  
1550          <div class="postbox">
1551              <h2><?php esc_html_e( 'Autolink', 'buddypress' ); ?></h2>
1552              <div class="inside">
1553                  <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>
1554  
1555                  <p>
1556                      <label for="do-autolink" class="screen-reader-text"><?php
1557                          /* translators: accessibility text */
1558                          esc_html_e( 'Autolink status for this field', 'buddypress' );
1559                      ?></label>
1560                      <select name="do_autolink" id="do-autolink">
1561                          <option value="on" <?php selected( $this->get_do_autolink() ); ?>><?php esc_html_e( 'Enabled', 'buddypress' ); ?></option>
1562                          <option value="" <?php selected( $this->get_do_autolink(), false ); ?>><?php esc_html_e( 'Disabled', 'buddypress' ); ?></option>
1563                      </select>
1564                  </p>
1565              </div>
1566          </div>
1567  
1568          <?php
1569      }
1570  
1571      /**
1572       * Output the metabox for setting what type of field this is.
1573       *
1574       * @since 2.3.0
1575       *
1576       * @return void If default field.
1577       */
1578  	private function type_metabox() {
1579  
1580          // Default field cannot change type.
1581          if ( true === $this->is_default_field() ) {
1582              return;
1583          } ?>
1584  
1585          <div class="postbox">
1586              <h2><label for="fieldtype"><?php esc_html_e( 'Type', 'buddypress'); ?></label></h2>
1587              <div class="inside" aria-live="polite" aria-atomic="true" aria-relevant="all">
1588                  <select name="fieldtype" id="fieldtype" onchange="show_options(this.value)">
1589  
1590                      <?php bp_xprofile_admin_form_field_types( $this->type ); ?>
1591  
1592                  </select>
1593  
1594                  <?php
1595  
1596                  // Deprecated filter, don't use. Go look at {@link BP_XProfile_Field_Type::admin_new_field_html()}.
1597                  do_action( 'xprofile_field_additional_options', $this );
1598  
1599                  $this->render_admin_form_children(); ?>
1600  
1601              </div>
1602          </div>
1603  
1604      <?php
1605      }
1606  
1607      /**
1608       * Output hidden fields used by default field.
1609       *
1610       * @since 2.3.0
1611       *
1612       * @return void If not default field.
1613       */
1614  	private function default_field_hidden_inputs() {
1615  
1616          // Nonce
1617          wp_nonce_field( 'bp_xprofile_admin_field', 'bp_xprofile_admin_field' );
1618  
1619          // Field 1 is the fullname field, which cannot have custom visibility.
1620          if ( false === $this->is_default_field() ) {
1621              return;
1622          } ?>
1623  
1624          <input type="hidden" name="required"  id="required"  value="1"       />
1625          <input type="hidden" name="fieldtype" id="fieldtype" value="textbox" />
1626  
1627          <?php
1628      }
1629  
1630      /**
1631       * Return if a field ID is the default field.
1632       *
1633       * @since 2.3.0
1634       *
1635       * @param int $field_id ID of field to check.
1636       * @return bool
1637       */
1638  	private function is_default_field( $field_id = 0 ) {
1639  
1640          // Fallback to current field ID if none passed.
1641          if ( empty( $field_id ) ) {
1642              $field_id = $this->id;
1643          }
1644  
1645          // Compare & return.
1646          return (bool) ( 1 === (int) $field_id );
1647      }
1648  }


Generated: Tue Jul 16 01:01:43 2019 Cross-referenced by PHPXref 0.7.1