BP_XProfile_Field

Class to help set up XProfile fields.

Defined (1)

The class is defined in the following location(s).

/bp-xprofile/classes/class-bp-xprofile-field.php  
  1. class BP_XProfile_Field { 
  2.  
  3. /** 
  4. * Field ID. 
  5. * @since 1.0.0 
  6. * @var int ID of field. 
  7. */ 
  8. public $id; 
  9.  
  10. /** 
  11. * Field group ID. 
  12. * @since 1.0.0 
  13. * @var int Field group ID for field. 
  14. */ 
  15. public $group_id; 
  16.  
  17. /** 
  18. * Field parent ID. 
  19. * @since 1.0.0 
  20. * @var int Parent ID of field. 
  21. */ 
  22. public $parent_id; 
  23.  
  24. /** 
  25. * Field type. 
  26. * @since 1.0.0 
  27. * @var string Field type. 
  28. */ 
  29. public $type; 
  30.  
  31. /** 
  32. * Field name. 
  33. * @since 1.0.0 
  34. * @var string Field name. 
  35. */ 
  36. public $name; 
  37.  
  38. /** 
  39. * Field description. 
  40. * @since 1.0.0 
  41. * @var string Field description. 
  42. */ 
  43. public $description; 
  44.  
  45. /** 
  46. * Required field? 
  47. * @since 1.0.0 
  48. * @var bool Is field required to be filled out? 
  49. */ 
  50. public $is_required; 
  51.  
  52. /** 
  53. * Deletable field? 
  54. * @since 1.0.0 
  55. * @var int Can field be deleted? 
  56. */ 
  57. public $can_delete = '1'; 
  58.  
  59. /** 
  60. * Field position. 
  61. * @since 1.0.0 
  62. * @var int Field position. 
  63. */ 
  64. public $field_order; 
  65.  
  66. /** 
  67. * Option order. 
  68. * @since 1.0.0 
  69. * @var int Option order. 
  70. */ 
  71. public $option_order; 
  72.  
  73. /** 
  74. * Order child fields. 
  75. * @since 1.0.0 
  76. * @var string Order child fields by. 
  77. */ 
  78. public $order_by; 
  79.  
  80. /** 
  81. * Is this the default option? 
  82. * @since 1.0.0 
  83. * @var bool Is this the default option for this field? 
  84. */ 
  85. public $is_default_option; 
  86.  
  87. /** 
  88. * Field data visibility. 
  89. * @since 1.9.0 
  90. * @since 2.4.0 Property marked protected. Now accessible by magic method or by `get_default_visibility()`. 
  91. * @var string Default field data visibility. 
  92. */ 
  93. protected $default_visibility; 
  94.  
  95. /** 
  96. * Is the visibility able to be modified? 
  97. * @since 2.3.0 
  98. * @since 2.4.0 Property marked protected. Now accessible by magic method or by `get_allow_custom_visibility()`. 
  99. * @var string Members are allowed/disallowed to modify data visibility. 
  100. */ 
  101. protected $allow_custom_visibility; 
  102.  
  103. /** 
  104. * Whether values from this field are autolinked to directory searches. 
  105. * @since 2.5.0 
  106. * @var bool 
  107. */ 
  108. public $do_autolink; 
  109.  
  110. /** 
  111. * Field type option. 
  112. * @since 2.0.0 
  113. * @var BP_XProfile_Field_Type Field type object used for validation. 
  114. */ 
  115. public $type_obj = null; 
  116.  
  117. /** 
  118. * Field data for user ID. 
  119. * @since 2.0.0 
  120. * @var BP_XProfile_ProfileData Field data for user ID. 
  121. */ 
  122. public $data; 
  123.  
  124. /** 
  125. * Member types to which the profile field should be applied. 
  126. * @since 2.4.0 
  127. * @var array Array of member types. 
  128. */ 
  129. protected $member_types; 
  130.  
  131. /** 
  132. * Initialize and/or populate profile field. 
  133. * @since 1.1.0 
  134. * @param int|null $id Field ID. 
  135. * @param int|null $user_id User ID. 
  136. * @param bool $get_data Get data. 
  137. */ 
  138. public function __construct( $id = null, $user_id = null, $get_data = true ) { 
  139.  
  140. if ( ! empty( $id ) ) { 
  141. $this->populate( $id, $user_id, $get_data ); 
  142.  
  143. // Initialise the type obj to prevent fatals when creating new profile fields. 
  144. } else { 
  145. $this->type_obj = bp_xprofile_create_field_type( 'textbox' ); 
  146. $this->type_obj->field_obj = $this; 
  147.  
  148. /** 
  149. * Populate a profile field object. 
  150. * @since 1.1.0 
  151. * @global object $wpdb 
  152. * @global object $userdata 
  153. * @param int $id Field ID. 
  154. * @param int|null $user_id User ID. 
  155. * @param bool $get_data Get data. 
  156. */ 
  157. public function populate( $id, $user_id = null, $get_data = true ) { 
  158. global $wpdb, $userdata; 
  159.  
  160. if ( empty( $user_id ) ) { 
  161. $user_id = isset( $userdata->ID ) ? $userdata->ID : 0; 
  162.  
  163. $field = wp_cache_get( $id, 'bp_xprofile_fields' ); 
  164. if ( false === $field ) { 
  165. $bp = buddypress(); 
  166.  
  167. $field = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$bp->profile->table_name_fields} WHERE id = %d", $id ) ); 
  168.  
  169. if ( ! $field ) { 
  170. return false; 
  171.  
  172. wp_cache_add( $id, $field, 'bp_xprofile_fields' ); 
  173.  
  174. $this->fill_data( $field ); 
  175.  
  176. if ( ! empty( $get_data ) && ! empty( $user_id ) ) { 
  177. $this->data = $this->get_field_data( $user_id ); 
  178.  
  179. /** 
  180. * Retrieve a `BP_XProfile_Field` instance. 
  181. * @since 2.4.0 
  182. * @static 
  183. * @param int $field_id ID of the field. 
  184. * @return BP_XProfile_Field|false Field object if found, otherwise false. 
  185. */ 
  186. public static function get_instance( $field_id ) { 
  187. global $wpdb; 
  188.  
  189. $field_id = (int) $field_id; 
  190. if ( ! $field_id ) { 
  191. return false; 
  192.  
  193. return new self( $field_id ); 
  194.  
  195. /** 
  196. * Fill object vars based on data passed to the method. 
  197. * @since 2.4.0 
  198. * @param array|object $args Array or object representing the `BP_XProfile_Field` properties. 
  199. * Generally, this is a row from the fields database table. 
  200. */ 
  201. public function fill_data( $args ) { 
  202. if ( is_object( $args ) ) { 
  203. $args = (array) $args; 
  204.  
  205. $int_fields = array( 
  206. 'id', 'is_required', 'group_id', 'parent_id', 'is_default_option',  
  207. 'field_order', 'option_order', 'can_delete' 
  208. ); 
  209.  
  210. foreach ( $args as $k => $v ) { 
  211. if ( 'name' === $k || 'description' === $k ) { 
  212. $v = stripslashes( $v ); 
  213.  
  214. // Cast numeric strings as integers. 
  215. if ( true === in_array( $k, $int_fields ) ) { 
  216. $v = (int) $v; 
  217.  
  218. $this->{$k} = $v; 
  219.  
  220. // Create the field type and store a reference back to this object. 
  221. $this->type_obj = bp_xprofile_create_field_type( $this->type ); 
  222. $this->type_obj->field_obj = $this; 
  223.  
  224. /** 
  225. * Magic getter. 
  226. * @since 2.4.0 
  227. * @param string $key Property name. 
  228. * @return mixed 
  229. */ 
  230. public function __get( $key ) { 
  231. switch ( $key ) { 
  232. case 'default_visibility' : 
  233. return $this->get_default_visibility(); 
  234. break; 
  235.  
  236. case 'allow_custom_visibility' : 
  237. return $this->get_allow_custom_visibility(); 
  238. break; 
  239.  
  240. /** 
  241. * Magic issetter. 
  242. * @since 2.4.0 
  243. * @param string $key Property name. 
  244. * @return bool 
  245. */ 
  246. public function __isset( $key ) { 
  247. switch ( $key ) { 
  248. // Backward compatibility for when these were public methods. 
  249. case 'allow_custom_visibility' : 
  250. case 'default_visibility' : 
  251. return true; 
  252. break; 
  253.  
  254. /** 
  255. * Delete a profile field. 
  256. * @since 1.1.0 
  257. * @global object $wpdb 
  258. * @param boolean $delete_data Whether or not to delete data. 
  259. * @return boolean 
  260. */ 
  261. public function delete( $delete_data = false ) { 
  262. global $wpdb; 
  263.  
  264. // Prevent deletion if no ID is present. 
  265. // Prevent deletion by url when can_delete is false. 
  266. // Prevent deletion of option 1 since this invalidates fields with options. 
  267. if ( empty( $this->id ) || empty( $this->can_delete ) || ( $this->parent_id && $this->option_order == 1 ) ) { 
  268. return false; 
  269.  
  270. $bp = buddypress(); 
  271. $sql = $wpdb->prepare( "DELETE FROM {$bp->profile->table_name_fields} WHERE id = %d OR parent_id = %d", $this->id, $this->id ); 
  272.  
  273. if ( ! $wpdb->query( $sql ) ) { 
  274. return false; 
  275.  
  276. // Delete the data in the DB for this field. 
  277. if ( true === $delete_data ) { 
  278. BP_XProfile_ProfileData::delete_for_field( $this->id ); 
  279.  
  280. return true; 
  281.  
  282. /** 
  283. * Save a profile field. 
  284. * @since 1.1.0 
  285. * @global object $wpdb 
  286. * @return boolean 
  287. */ 
  288. public function save() { 
  289. global $wpdb; 
  290.  
  291. $bp = buddypress(); 
  292.  
  293. $this->group_id = apply_filters( 'xprofile_field_group_id_before_save', $this->group_id, $this->id ); 
  294. $this->parent_id = apply_filters( 'xprofile_field_parent_id_before_save', $this->parent_id, $this->id ); 
  295. $this->type = apply_filters( 'xprofile_field_type_before_save', $this->type, $this->id ); 
  296. $this->name = apply_filters( 'xprofile_field_name_before_save', $this->name, $this->id ); 
  297. $this->description = apply_filters( 'xprofile_field_description_before_save', $this->description, $this->id ); 
  298. $this->is_required = apply_filters( 'xprofile_field_is_required_before_save', $this->is_required, $this->id ); 
  299. $this->order_by = apply_filters( 'xprofile_field_order_by_before_save', $this->order_by, $this->id ); 
  300. $this->field_order = apply_filters( 'xprofile_field_field_order_before_save', $this->field_order, $this->id ); 
  301. $this->option_order = apply_filters( 'xprofile_field_option_order_before_save', $this->option_order, $this->id ); 
  302. $this->can_delete = apply_filters( 'xprofile_field_can_delete_before_save', $this->can_delete, $this->id ); 
  303. $this->type_obj = bp_xprofile_create_field_type( $this->type ); 
  304.  
  305. /** 
  306. * Fires before the current field instance gets saved. 
  307. * Please use this hook to filter the properties above. Each part will be passed in. 
  308. * @since 1.0.0 
  309. * @param BP_XProfile_Field $this Current instance of the field being saved. 
  310. */ 
  311. do_action_ref_array( 'xprofile_field_before_save', array( $this ) ); 
  312.  
  313. $is_new_field = is_null( $this->id ); 
  314.  
  315. if ( ! $is_new_field ) { 
  316. $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 ); 
  317. } else { 
  318. $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 ); 
  319.  
  320. /** 
  321. * Check for null so field options can be changed without changing any 
  322. * other part of the field. The described situation will return 0 here. 
  323. */ 
  324. if ( $wpdb->query( $sql ) !== null ) { 
  325.  
  326. if ( $is_new_field ) { 
  327. $this->id = $wpdb->insert_id; 
  328.  
  329. // Only do this if we are editing an existing field. 
  330. if ( ! $is_new_field ) { 
  331.  
  332. /** 
  333. * Remove any radio or dropdown options for this 
  334. * field. They will be re-added if needed. 
  335. * This stops orphan options if the user changes a 
  336. * field from a radio button field to a text box. 
  337. */ 
  338. $this->delete_children(); 
  339.  
  340. /** 
  341. * Check to see if this is a field with child options. 
  342. * We need to add the options to the db, if it is. 
  343. */ 
  344. if ( $this->type_obj->supports_options ) { 
  345.  
  346. $parent_id = $this->id; 
  347.  
  348. // Allow plugins to filter the field's child options (i.e. the items in a selectbox). 
  349. $post_option = ! empty( $_POST["{$this->type}_option"] ) ? $_POST["{$this->type}_option"] : ''; 
  350. $post_default = ! empty( $_POST["isDefault_{$this->type}_option"] ) ? $_POST["isDefault_{$this->type}_option"] : ''; 
  351.  
  352. /** 
  353. * Filters the submitted field option value before saved. 
  354. * @since 1.5.0 
  355. * @param string $post_option Submitted option value. 
  356. * @param BP_XProfile_Field $type Current field type being saved for. 
  357. */ 
  358. $options = apply_filters( 'xprofile_field_options_before_save', $post_option, $this->type ); 
  359.  
  360. /** 
  361. * Filters the default field option value before saved. 
  362. * @since 1.5.0 
  363. * @param string $post_default Default option value. 
  364. * @param BP_XProfile_Field $type Current field type being saved for. 
  365. */ 
  366. $defaults = apply_filters( 'xprofile_field_default_before_save', $post_default, $this->type ); 
  367.  
  368. $counter = 1; 
  369. if ( !empty( $options ) ) { 
  370. foreach ( (array) $options as $option_key => $option_value ) { 
  371. $is_default = 0; 
  372.  
  373. if ( is_array( $defaults ) ) { 
  374. if ( isset( $defaults[ $option_key ] ) ) { 
  375. $is_default = 1; 
  376. } else { 
  377. if ( (int) $defaults == $option_key ) { 
  378. $is_default = 1; 
  379.  
  380. if ( '' != $option_value ) { 
  381. $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 ); 
  382. if ( ! $wpdb->query( $sql ) ) { 
  383. return false; 
  384.  
  385. $counter++; 
  386.  
  387. /** 
  388. * Fires after the current field instance gets saved. 
  389. * @since 1.0.0 
  390. * @param BP_XProfile_Field $this Current instance of the field being saved. 
  391. */ 
  392. do_action_ref_array( 'xprofile_field_after_save', array( $this ) ); 
  393.  
  394. // Recreate type_obj in case someone changed $this->type via a filter. 
  395. $this->type_obj = bp_xprofile_create_field_type( $this->type ); 
  396. $this->type_obj->field_obj = $this; 
  397.  
  398. return $this->id; 
  399. } else { 
  400. return false; 
  401.  
  402. /** 
  403. * Get field data for a user ID. 
  404. * @since 1.2.0 
  405. * @param int $user_id ID of the user to get field data for. 
  406. * @return BP_XProfile_ProfileData 
  407. */ 
  408. public function get_field_data( $user_id = 0 ) { 
  409. return new BP_XProfile_ProfileData( $this->id, $user_id ); 
  410.  
  411. /** 
  412. * Get all child fields for this field ID. 
  413. * @since 1.2.0 
  414. * @global object $wpdb 
  415. * @param bool $for_editing Whether or not the field is for editing. 
  416. * @return array 
  417. */ 
  418. public function get_children( $for_editing = false ) { 
  419. global $wpdb; 
  420.  
  421. // This is done here so we don't have problems with sql injection. 
  422. if ( empty( $for_editing ) && ( 'asc' === $this->order_by ) ) { 
  423. $sort_sql = 'ORDER BY name ASC'; 
  424. } elseif ( empty( $for_editing ) && ( 'desc' === $this->order_by ) ) { 
  425. $sort_sql = 'ORDER BY name DESC'; 
  426. } else { 
  427. $sort_sql = 'ORDER BY option_order ASC'; 
  428.  
  429. // This eliminates a problem with getting all fields when there is no 
  430. // id for the object. 
  431. if ( empty( $this->id ) ) { 
  432. $parent_id = -1; 
  433. } else { 
  434. $parent_id = $this->id; 
  435.  
  436. $bp = buddypress(); 
  437. $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 ); 
  438.  
  439. $children = $wpdb->get_results( $sql ); 
  440.  
  441. /** 
  442. * Filters the found children for a field. 
  443. * @since 1.2.5 
  444. * @param object $children Found children for a field. 
  445. * @param bool $for_editing Whether or not the field is for editing. 
  446. */ 
  447. return apply_filters( 'bp_xprofile_field_get_children', $children, $for_editing ); 
  448.  
  449. /** 
  450. * Delete all field children for this field. 
  451. * @since 1.2.0 
  452. * @global object $wpdb 
  453. */ 
  454. public function delete_children() { 
  455. global $wpdb; 
  456.  
  457. $bp = buddypress(); 
  458. $sql = $wpdb->prepare( "DELETE FROM {$bp->profile->table_name_fields} WHERE parent_id = %d", $this->id ); 
  459.  
  460. $wpdb->query( $sql ); 
  461.  
  462. /** 
  463. * Gets the member types to which this field should be available. 
  464. * Will not return inactive member types, even if associated metadata is found. 
  465. * 'null' is a special pseudo-type, which represents users that do not have a member type. 
  466. * @since 2.4.0 
  467. * @return array Array of member type names. 
  468. */ 
  469. public function get_member_types() { 
  470. if ( ! is_null( $this->member_types ) ) { 
  471. return $this->member_types; 
  472.  
  473. $raw_types = bp_xprofile_get_meta( $this->id, 'field', 'member_type', false ); 
  474.  
  475. // If `$raw_types` is not an array, it probably means this is a new field (id=0). 
  476. if ( ! is_array( $raw_types ) ) { 
  477. $raw_types = array(); 
  478.  
  479. // If '_none' is found in the array, it overrides all types. 
  480. $types = array(); 
  481. if ( ! in_array( '_none', $raw_types ) ) { 
  482. $registered_types = bp_get_member_types(); 
  483.  
  484. // Eliminate invalid member types saved in the database. 
  485. foreach ( $raw_types as $raw_type ) { 
  486. // 'null' is a special case - it represents users without a type. 
  487. if ( 'null' === $raw_type || isset( $registered_types[ $raw_type ] ) ) { 
  488. $types[] = $raw_type; 
  489.  
  490. // If no member types have been saved, intepret as *all* member types. 
  491. if ( empty( $types ) ) { 
  492. $types = array_values( $registered_types ); 
  493.  
  494. // + the "null" type, ie users without a type. 
  495. $types[] = 'null'; 
  496.  
  497. /** 
  498. * Filters the member types to which an XProfile object should be applied. 
  499. * @since 2.4.0 
  500. * @param array $types Member types. 
  501. * @param BP_XProfile_Field $field Field object. 
  502. */ 
  503. $this->member_types = apply_filters( 'bp_xprofile_field_member_types', $types, $this ); 
  504.  
  505. return $this->member_types; 
  506.  
  507. /** 
  508. * Sets the member types for this field. 
  509. * @since 2.4.0 
  510. * @param array $member_types Array of member types. Can include 'null' (users with no type) in addition to any 
  511. * registered types. 
  512. * @param bool $append Whether to append to existing member types. If false, all existing member type 
  513. * associations will be deleted before adding your `$member_types`. Default false. 
  514. * @return array Member types for the current field, after being saved. 
  515. */ 
  516. public function set_member_types( $member_types, $append = false ) { 
  517. // Unset invalid member types. 
  518. $types = array(); 
  519. foreach ( $member_types as $member_type ) { 
  520. // 'null' is a special case - it represents users without a type. 
  521. if ( 'null' === $member_type || bp_get_member_type_object( $member_type ) ) { 
  522. $types[] = $member_type; 
  523.  
  524. // When `$append` is false, delete all existing types before adding new ones. 
  525. if ( ! $append ) { 
  526. bp_xprofile_delete_meta( $this->id, 'field', 'member_type' ); 
  527.  
  528. /** 
  529. * We interpret an empty array as disassociating the field from all types. This is 
  530. * represented internally with the '_none' flag. 
  531. */ 
  532. if ( empty( $types ) ) { 
  533. bp_xprofile_add_meta( $this->id, 'field', 'member_type', '_none' ); 
  534.  
  535. /** 
  536. * Unrestricted fields are represented in the database as having no 'member_type'. 
  537. * We detect whether a field is being set to unrestricted by checking whether the 
  538. * list of types passed to the method is the same as the list of registered types,  
  539. * plus the 'null' pseudo-type. 
  540. */ 
  541. $_rtypes = bp_get_member_types(); 
  542. $rtypes = array_values( $_rtypes ); 
  543. $rtypes[] = 'null'; 
  544.  
  545. sort( $types ); 
  546. sort( $rtypes ); 
  547.  
  548. // Only save if this is a restricted field. 
  549. if ( $types !== $rtypes ) { 
  550. // Save new types. 
  551. foreach ( $types as $type ) { 
  552. bp_xprofile_add_meta( $this->id, 'field', 'member_type', $type ); 
  553.  
  554. // Reset internal cache of member types. 
  555. $this->member_types = null; 
  556.  
  557. /** 
  558. * Fires after a field's member types have been updated. 
  559. * @since 2.4.0 
  560. * @param BP_XProfile_Field $this Field object. 
  561. */ 
  562. do_action( 'bp_xprofile_field_set_member_type', $this ); 
  563.  
  564. // Refetch fresh items from the database. 
  565. return $this->get_member_types(); 
  566.  
  567. /** 
  568. * Gets a label representing the field's member types. 
  569. * This label is displayed alongside the field's name on the Profile Fields Dashboard panel. 
  570. * @since 2.4.0 
  571. * @return string 
  572. */ 
  573. public function get_member_type_label() { 
  574. // Field 1 is always displayed to everyone, so never gets a label. 
  575. if ( 1 == $this->id ) { 
  576. return ''; 
  577.  
  578. // Return an empty string if no member types are registered. 
  579. $all_types = bp_get_member_types(); 
  580. if ( empty( $all_types ) ) { 
  581. return ''; 
  582.  
  583. $member_types = $this->get_member_types(); 
  584.  
  585. // If the field applies to all member types, show no message. 
  586. $all_types[] = 'null'; 
  587. if ( array_values( $all_types ) == $member_types ) { 
  588. return ''; 
  589.  
  590. $label = ''; 
  591. if ( ! empty( $member_types ) ) { 
  592. $has_null = false; 
  593. $member_type_labels = array(); 
  594. foreach ( $member_types as $member_type ) { 
  595. if ( 'null' === $member_type ) { 
  596. $has_null = true; 
  597. continue; 
  598. } else { 
  599. $mt_obj = bp_get_member_type_object( $member_type ); 
  600. $member_type_labels[] = $mt_obj->labels['name']; 
  601.  
  602. // Alphabetical sort. 
  603. natcasesort( $member_type_labels ); 
  604. $member_type_labels = array_values( $member_type_labels ); 
  605.  
  606. // Add the 'null' option to the end of the list. 
  607. if ( $has_null ) { 
  608. $member_type_labels[] = __( 'Users with no member type', 'buddypress' ); 
  609.  
  610. $label = sprintf( __( '(Member types: %s)', 'buddypress' ), implode( ', ', array_map( 'esc_html', $member_type_labels ) ) ); 
  611. } else { 
  612. $label = '<span class="member-type-none-notice">' . __( '(Unavailable to all members)', 'buddypress' ) . '</span>'; 
  613.  
  614. return $label; 
  615.  
  616. /** 
  617. * Get the field's default visibility setting. 
  618. * Lazy-loaded to reduce overhead. 
  619. * Defaults to 'public' if no visibility setting is found in the database. 
  620. * @since 2.4.0 
  621. * @return string 
  622. */ 
  623. public function get_default_visibility() { 
  624. if ( ! isset( $this->default_visibility ) ) { 
  625. $this->default_visibility = bp_xprofile_get_meta( $this->id, 'field', 'default_visibility' ); 
  626.  
  627. if ( ! $this->default_visibility ) { 
  628. $this->default_visibility = 'public'; 
  629.  
  630. return $this->default_visibility; 
  631.  
  632. /** 
  633. * Get whether the field's default visibility can be overridden by users. 
  634. * Lazy-loaded to reduce overhead. 
  635. * Defaults to 'allowed'. 
  636. * @since 4.4.0 
  637. * @return string 'disabled' or 'allowed'. 
  638. */ 
  639. public function get_allow_custom_visibility() { 
  640. if ( ! isset( $this->allow_custom_visibility ) ) { 
  641. $allow_custom_visibility = bp_xprofile_get_meta( $this->id, 'field', 'allow_custom_visibility' ); 
  642.  
  643. if ( 'disabled' === $allow_custom_visibility ) { 
  644. $this->allow_custom_visibility = 'disabled'; 
  645. } else { 
  646. $this->allow_custom_visibility = 'allowed'; 
  647.  
  648. return $this->allow_custom_visibility; 
  649.  
  650. /** 
  651. * Get whether the field values should be auto-linked to a directory search. 
  652. * Lazy-loaded to reduce overhead. 
  653. * Defaults to true for multi and default fields, false for single fields. 
  654. * @since 2.5.0 
  655. * @return bool 
  656. */ 
  657. public function get_do_autolink() { 
  658. if ( ! isset( $this->do_autolink ) ) { 
  659. $do_autolink = bp_xprofile_get_meta( $this->id, 'field', 'do_autolink' ); 
  660.  
  661. if ( '' === $do_autolink ) { 
  662. $this->do_autolink = $this->type_obj->supports_options; 
  663. } else { 
  664. $this->do_autolink = 'on' === $do_autolink; 
  665.  
  666. return $this->do_autolink; 
  667.  
  668. /** Static Methods ********************************************************/ 
  669.  
  670. /** 
  671. * Get the type for provided field ID. 
  672. * @param int $field_id Field ID to get type of. 
  673. * @return bool|null|string 
  674. */ 
  675. public static function get_type( $field_id = 0 ) { 
  676. global $wpdb; 
  677.  
  678. // Bail if no field ID. 
  679. if ( empty( $field_id ) ) { 
  680. return false; 
  681.  
  682. $bp = buddypress(); 
  683. $sql = $wpdb->prepare( "SELECT type FROM {$bp->profile->table_name_fields} WHERE id = %d", $field_id ); 
  684. $type = $wpdb->get_var( $sql ); 
  685.  
  686. // Return field type. 
  687. if ( ! empty( $type ) ) { 
  688. return $type; 
  689.  
  690. return false; 
  691.  
  692. /** 
  693. * Delete all fields in a field group. 
  694. * @since 1.2.0 
  695. * @global object $wpdb 
  696. * @param int $group_id ID of the field group to delete fields from. 
  697. * @return boolean 
  698. */ 
  699. public static function delete_for_group( $group_id = 0 ) { 
  700. global $wpdb; 
  701.  
  702. // Bail if no group ID. 
  703. if ( empty( $group_id ) ) { 
  704. return false; 
  705.  
  706. $bp = buddypress(); 
  707. $sql = $wpdb->prepare( "DELETE FROM {$bp->profile->table_name_fields} WHERE group_id = %d", $group_id ); 
  708. $deleted = $wpdb->get_var( $sql ); 
  709.  
  710. // Return true if fields were deleted. 
  711. if ( false !== $deleted ) { 
  712. return true; 
  713.  
  714. return false; 
  715.  
  716. /** 
  717. * Get field ID from field name. 
  718. * @since 1.5.0 
  719. * @global object $wpdb 
  720. * @param string $field_name Name of the field to query the ID for. 
  721. * @return int|null Field ID on success; null on failure. 
  722. */ 
  723. public static function get_id_from_name( $field_name = '' ) { 
  724. global $wpdb; 
  725.  
  726. $bp = buddypress(); 
  727.  
  728. if ( empty( $bp->profile->table_name_fields ) || empty( $field_name ) ) { 
  729. return false; 
  730.  
  731. $sql = $wpdb->prepare( "SELECT id FROM {$bp->profile->table_name_fields} WHERE name = %s AND parent_id = 0", $field_name ); 
  732.  
  733. $query = $wpdb->get_var( $sql ); 
  734.  
  735. return is_numeric( $query ) ? (int) $query : $query; 
  736.  
  737. /** 
  738. * Update field position and/or field group when relocating. 
  739. * @since 1.5.0 
  740. * @global object $wpdb 
  741. * @param int $field_id ID of the field to update. 
  742. * @param int|null $position Field position to update. 
  743. * @param int|null $field_group_id ID of the field group. 
  744. * @return boolean 
  745. */ 
  746. public static function update_position( $field_id, $position = null, $field_group_id = null ) { 
  747. global $wpdb; 
  748.  
  749. // Bail if invalid position or field group. 
  750. if ( ! is_numeric( $position ) || ! is_numeric( $field_group_id ) ) { 
  751. return false; 
  752.  
  753. // Get table name and field parent. 
  754. $table_name = buddypress()->profile->table_name_fields; 
  755. $sql = $wpdb->prepare( "UPDATE {$table_name} SET field_order = %d, group_id = %d WHERE id = %d", $position, $field_group_id, $field_id ); 
  756. $parent = $wpdb->query( $sql ); 
  757.  
  758. // Update $field_id with new $position and $field_group_id. 
  759. if ( ! empty( $parent ) && ! is_wp_error( $parent ) ) { 
  760.  
  761. // Update any children of this $field_id. 
  762. $sql = $wpdb->prepare( "UPDATE {$table_name} SET group_id = %d WHERE parent_id = %d", $field_group_id, $field_id ); 
  763. $wpdb->query( $sql ); 
  764.  
  765. // Invalidate profile field cache. 
  766. wp_cache_delete( $field_id, 'bp_xprofile_fields' ); 
  767.  
  768. return $parent; 
  769.  
  770. return false; 
  771.  
  772. /** 
  773. * Gets the IDs of fields applicable for a given member type or array of member types. 
  774. * @since 2.4.0 
  775. * @param string|array $member_types Member type or array of member types. Use 'any' to return unrestricted 
  776. * fields (those available for anyone, regardless of member type). 
  777. * @return array Multi-dimensional array, with field IDs as top-level keys, and arrays of member types 
  778. * associated with each field as values. 
  779. */ 
  780. public static function get_fields_for_member_type( $member_types ) { 
  781. global $wpdb; 
  782.  
  783. $fields = array(); 
  784.  
  785. if ( empty( $member_types ) ) { 
  786. $member_types = array( 'any' ); 
  787. } elseif ( ! is_array( $member_types ) ) { 
  788. $member_types = array( $member_types ); 
  789.  
  790. $bp = buddypress(); 
  791.  
  792. // Pull up all recorded field member type data. 
  793. $mt_meta = wp_cache_get( 'field_member_types', 'bp_xprofile' ); 
  794. if ( false === $mt_meta ) { 
  795. $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'" ); 
  796. wp_cache_set( 'field_member_types', $mt_meta, 'bp_xprofile' ); 
  797.  
  798. // Keep track of all fields with recorded member_type metadata. 
  799. $all_recorded_field_ids = wp_list_pluck( $mt_meta, 'object_id' ); 
  800.  
  801. // Sort member_type matches in arrays, keyed by field_id. 
  802. foreach ( $mt_meta as $_mt_meta ) { 
  803. if ( ! isset( $fields[ $_mt_meta->object_id ] ) ) { 
  804. $fields[ $_mt_meta->object_id ] = array(); 
  805.  
  806. $fields[ $_mt_meta->object_id ][] = $_mt_meta->meta_value; 
  807.  
  808. /** 
  809. * Filter out fields that don't match any passed types, or those marked '_none'. 
  810. * The 'any' type is implicitly handled here: it will match no types. 
  811. */ 
  812. foreach ( $fields as $field_id => $field_types ) { 
  813. if ( ! array_intersect( $field_types, $member_types ) ) { 
  814. unset( $fields[ $field_id ] ); 
  815.  
  816. // Any fields with no member_type metadata are available to all member types. 
  817. if ( ! in_array( '_none', $member_types ) ) { 
  818. if ( ! empty( $all_recorded_field_ids ) ) { 
  819. $all_recorded_field_ids_sql = implode( ', ', array_map( 'absint', $all_recorded_field_ids ) ); 
  820. $unrestricted_field_ids = $wpdb->get_col( "SELECT id FROM {$bp->profile->table_name_fields} WHERE id NOT IN ({$all_recorded_field_ids_sql})" ); 
  821. } else { 
  822. $unrestricted_field_ids = $wpdb->get_col( "SELECT id FROM {$bp->profile->table_name_fields}" ); 
  823.  
  824. // Append the 'null' pseudo-type. 
  825. $all_member_types = bp_get_member_types(); 
  826. $all_member_types = array_values( $all_member_types ); 
  827. $all_member_types[] = 'null'; 
  828.  
  829. foreach ( $unrestricted_field_ids as $unrestricted_field_id ) { 
  830. $fields[ $unrestricted_field_id ] = $all_member_types; 
  831.  
  832. return $fields; 
  833.  
  834. /** 
  835. * Validate form field data on sumbission. 
  836. * @since 2.2.0 
  837. * @global $message 
  838. * @return boolean 
  839. */ 
  840. public static function admin_validate() { 
  841. global $message; 
  842.  
  843. // Check field name. 
  844. if ( ! isset( $_POST['title'] ) || ( '' === $_POST['title'] ) ) { 
  845. $message = esc_html__( 'Profile fields must have a name.', 'buddypress' ); 
  846. return false; 
  847.  
  848. // Check field requirement. 
  849. if ( ! isset( $_POST['required'] ) ) { 
  850. $message = esc_html__( 'Profile field requirement is missing.', 'buddypress' ); 
  851. return false; 
  852.  
  853. // Check field type. 
  854. if ( empty( $_POST['fieldtype'] ) ) { 
  855. $message = esc_html__( 'Profile field type is missing.', 'buddypress' ); 
  856. return false; 
  857.  
  858. // Check that field is of valid type. 
  859. if ( ! in_array( $_POST['fieldtype'], array_keys( bp_xprofile_get_field_types() ), true ) ) { 
  860. $message = sprintf( esc_html__( 'The profile field type %s is not registered.', 'buddypress' ), '<code>' . esc_attr( $_POST['fieldtype'] ) . '</code>' ); 
  861. return false; 
  862.  
  863. // Get field type so we can check for and lavidate any field options. 
  864. $field_type = bp_xprofile_create_field_type( $_POST['fieldtype'] ); 
  865.  
  866. // Field type requires options. 
  867. if ( true === $field_type->supports_options ) { 
  868.  
  869. // Build the field option key. 
  870. $option_name = sanitize_key( $_POST['fieldtype'] ) . '_option'; 
  871.  
  872. // Check for missing or malformed options. 
  873. if ( empty( $_POST[ $option_name ] ) || ! is_array( $_POST[ $option_name ] ) ) { 
  874. $message = esc_html__( 'These field options are invalid.', 'buddypress' ); 
  875. return false; 
  876.  
  877. // Trim out empty field options. 
  878. $field_values = array_values( $_POST[ $option_name ] ); 
  879. $field_options = array_map( 'sanitize_text_field', $field_values ); 
  880. $field_count = count( $field_options ); 
  881.  
  882. // Check for missing or malformed options. 
  883. if ( 0 === $field_count ) { 
  884. $message = sprintf( esc_html__( '%s require at least one option.', 'buddypress' ), $field_type->name ); 
  885. return false; 
  886.  
  887. // If only one option exists, it cannot be an empty string. 
  888. if ( ( 1 === $field_count ) && ( '' === $field_options[0] ) ) { 
  889. $message = sprintf( esc_html__( '%s require at least one option.', 'buddypress' ), $field_type->name ); 
  890. return false; 
  891.  
  892. return true; 
  893.  
  894. /** 
  895. * Save miscellaneous settings for this field. 
  896. * Some field types have type-specific settings, which are saved here. 
  897. * @since 2.7.0 
  898. * @param array $settings Array of settings. 
  899. */ 
  900. public function admin_save_settings( $settings ) { 
  901. return $this->type_obj->admin_save_settings( $this->id, $settings ); 
  902.  
  903. /** 
  904. * Populates the items for radio buttons, checkboxes, and dropdown boxes. 
  905. */ 
  906. public function render_admin_form_children() { 
  907. foreach ( array_keys( bp_xprofile_get_field_types() ) as $field_type ) { 
  908. $type_obj = bp_xprofile_create_field_type( $field_type ); 
  909. $type_obj->admin_new_field_html( $this ); 
  910.  
  911. /** 
  912. * Oupput the admin form for this field. 
  913. * @since 1.9.0 
  914. * @param string $message Message to display. 
  915. */ 
  916. public function render_admin_form( $message = '' ) { 
  917. if ( empty( $this->id ) ) { 
  918. $title = __( 'Add New Field', 'buddypress' ); 
  919. $action = "users.php?page=bp-profile-setup&group_id=" . $this->group_id . "&mode=add_field#tabs-" . $this->group_id; 
  920. $button = __( 'Save', 'buddypress' ); 
  921.  
  922. if ( !empty( $_POST['saveField'] ) ) { 
  923. $this->name = $_POST['title']; 
  924. $this->description = $_POST['description']; 
  925. $this->is_required = $_POST['required']; 
  926. $this->type = $_POST['fieldtype']; 
  927. $this->field_order = $_POST['field_order']; 
  928.  
  929. if ( ! empty( $_POST["sort_order_{$this->type}"] ) ) { 
  930. $this->order_by = $_POST["sort_order_{$this->type}"]; 
  931. } else { 
  932. $title = __( 'Edit Field', 'buddypress' ); 
  933. $action = "users.php?page=bp-profile-setup&mode=edit_field&group_id=" . $this->group_id . "&field_id=" . $this->id . "#tabs-" . $this->group_id; 
  934. $button = __( 'Update', 'buddypress' ); 
  935. } ?> 
  936.  
  937. <div class="wrap"> 
  938.  
  939. <h1><?php echo esc_html( $title ); ?></h1> 
  940.  
  941. <?php if ( !empty( $message ) ) : ?> 
  942.  
  943. <div id="message" class="error fade"> 
  944. <p><?php echo esc_html( $message ); ?></p> 
  945. </div> 
  946.  
  947. <?php endif; ?> 
  948.  
  949. <form id="bp-xprofile-add-field" action="<?php echo esc_url( $action ); ?>" method="post"> 
  950. <div id="poststuff"> 
  951. <div id="post-body" class="metabox-holder columns-<?php echo ( 1 == get_current_screen()->get_columns() ) ? '1' : '2'; ?>"> 
  952. <div id="post-body-content"> 
  953.  
  954. <?php 
  955.  
  956. // Output the name & description fields. 
  957. $this->name_and_description(); ?> 
  958.  
  959. </div><!-- #post-body-content --> 
  960.  
  961. <div id="postbox-container-1" class="postbox-container"> 
  962.  
  963. <?php 
  964.  
  965. // Output the sumbit metabox. 
  966. $this->submit_metabox( $button ); 
  967.  
  968. // Output the required metabox. 
  969. $this->required_metabox(); 
  970.  
  971. // Output the Member Types metabox. 
  972. $this->member_type_metabox(); 
  973.  
  974. // Output the field visibility metaboxes. 
  975. $this->visibility_metabox(); 
  976.  
  977. // Output the autolink metabox. 
  978. $this->autolink_metabox(); 
  979.  
  980.  
  981. /** 
  982. * Fires after XProfile Field sidebar metabox. 
  983. * @since 2.2.0 
  984. * @param BP_XProfile_Field $this Current XProfile field. 
  985. */ 
  986. do_action( 'xprofile_field_after_sidebarbox', $this ); ?> 
  987.  
  988. </div> 
  989.  
  990. <div id="postbox-container-2" class="postbox-container"> 
  991.  
  992. <?php 
  993.  
  994. /** 
  995. * Fires before XProfile Field content metabox. 
  996. * @since 2.3.0 
  997. * @param BP_XProfile_Field $this Current XProfile field. 
  998. */ 
  999. do_action( 'xprofile_field_before_contentbox', $this ); 
  1000.  
  1001. // Output the field attributes metabox. 
  1002. $this->type_metabox(); 
  1003.  
  1004. // Output hidden inputs for default field. 
  1005. $this->default_field_hidden_inputs(); 
  1006.  
  1007. /** 
  1008. * Fires after XProfile Field content metabox. 
  1009. * @since 2.2.0 
  1010. * @param BP_XProfile_Field $this Current XProfile field. 
  1011. */ 
  1012. do_action( 'xprofile_field_after_contentbox', $this ); ?> 
  1013.  
  1014. </div> 
  1015. </div><!-- #post-body --> 
  1016. </div><!-- #poststuff --> 
  1017. </form> 
  1018. </div> 
  1019.  
  1020. <?php 
  1021.  
  1022. /** 
  1023. * Private method used to display the submit metabox. 
  1024. * @since 2.3.0 
  1025. * @param string $button_text Text to put on button. 
  1026. */ 
  1027. private function submit_metabox( $button_text = '' ) { 
  1028.  
  1029. /** 
  1030. * Fires before XProfile Field submit metabox. 
  1031. * @since 2.1.0 
  1032. * @param BP_XProfile_Field $this Current XProfile field. 
  1033. */ 
  1034. do_action( 'xprofile_field_before_submitbox', $this ); ?> 
  1035.  
  1036. <div id="submitdiv" class="postbox"> 
  1037. <h2><?php esc_html_e( 'Submit', 'buddypress' ); ?></h2> 
  1038. <div class="inside"> 
  1039. <div id="submitcomment" class="submitbox"> 
  1040. <div id="major-publishing-actions"> 
  1041.  
  1042. <?php 
  1043.  
  1044. /** 
  1045. * Fires at the beginning of the XProfile Field publishing actions section. 
  1046. * @since 2.1.0 
  1047. * @param BP_XProfile_Field $this Current XProfile field. 
  1048. */ 
  1049. do_action( 'xprofile_field_submitbox_start', $this ); ?> 
  1050.  
  1051. <input type="hidden" name="field_order" id="field_order" value="<?php echo esc_attr( $this->field_order ); ?>" /> 
  1052.  
  1053. <?php if ( ! empty( $button_text ) ) : ?> 
  1054.  
  1055. <div id="publishing-action"> 
  1056. <input type="submit" name="saveField" value="<?php echo esc_attr( $button_text ); ?>" class="button-primary" /> 
  1057. </div> 
  1058.  
  1059. <?php endif; ?> 
  1060.  
  1061. <div id="delete-action"> 
  1062. <a href="users.php?page=bp-profile-setup" class="deletion"><?php esc_html_e( 'Cancel', 'buddypress' ); ?></a> 
  1063. </div> 
  1064.  
  1065. <?php wp_nonce_field( 'xprofile_delete_option' ); ?> 
  1066.  
  1067. <div class="clear"></div> 
  1068. </div> 
  1069. </div> 
  1070. </div> 
  1071. </div> 
  1072.  
  1073. <?php 
  1074.  
  1075. /** 
  1076. * Fires after XProfile Field submit metabox. 
  1077. * @since 2.1.0 
  1078. * @param BP_XProfile_Field $this Current XProfile field. 
  1079. */ 
  1080. do_action( 'xprofile_field_after_submitbox', $this ); 
  1081.  
  1082. /** 
  1083. * Private method used to output field name and description fields. 
  1084. * @since 2.3.0 
  1085. */ 
  1086. private function name_and_description() { 
  1087. ?> 
  1088.  
  1089. <div id="titlediv"> 
  1090. <div class="titlewrap"> 
  1091. <label id="title-prompt-text" for="title"><?php echo esc_html_x( 'Name (required)', 'XProfile admin edit field', 'buddypress' ); ?></label> 
  1092. <input type="text" name="title" id="title" value="<?php echo esc_attr( $this->name ); ?>" autocomplete="off" /> 
  1093. </div> 
  1094. </div> 
  1095.  
  1096. <div class="postbox"> 
  1097. <h2><?php echo esc_html_x( 'Description', 'XProfile admin edit field', 'buddypress' ); ?></h2> 
  1098. <div class="inside"> 
  1099. <label for="description" class="screen-reader-text"><?php 
  1100. /** translators: accessibility text */ 
  1101. esc_html_e( 'Add description', 'buddypress' ); 
  1102. ?></label> 
  1103. <textarea name="description" id="description" rows="8" cols="60"><?php echo esc_textarea( $this->description ); ?></textarea> 
  1104. </div> 
  1105. </div> 
  1106.  
  1107. <?php 
  1108.  
  1109. /** 
  1110. * Private method used to output field Member Type metabox. 
  1111. * @since 2.4.0 
  1112. */ 
  1113. private function member_type_metabox() { 
  1114.  
  1115. // The primary field is for all, so bail. 
  1116. if ( 1 === (int) $this->id ) { 
  1117. return; 
  1118.  
  1119. // Bail when no member types are registered. 
  1120. if ( ! $member_types = bp_get_member_types( array(), 'objects' ) ) { 
  1121. return; 
  1122.  
  1123. $field_member_types = $this->get_member_types(); 
  1124.  
  1125. ?> 
  1126.  
  1127. <div id="member-types-div" class="postbox"> 
  1128. <h2><?php _e( 'Member Types', 'buddypress' ); ?></h2> 
  1129. <div class="inside"> 
  1130. <p class="description"><?php _e( 'This field should be available to:', 'buddypress' ); ?></p> 
  1131.  
  1132. <ul> 
  1133. <?php foreach ( $member_types as $member_type ) : ?> 
  1134. <li> 
  1135. <label for="member-type-<?php echo $member_type->labels['name']; ?>"> 
  1136. <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 ) ); ?>/> 
  1137. <?php echo $member_type->labels['name']; ?> 
  1138. </label> 
  1139. </li> 
  1140. <?php endforeach; ?> 
  1141.  
  1142. <li> 
  1143. <label for="member-type-none"> 
  1144. <input name="member-types[]" id="member-type-none" class="member-type-selector" type="checkbox" value="null" <?php checked( in_array( 'null', $field_member_types ) ); ?>/> 
  1145. <?php _e( 'Users with no member type', 'buddypress' ); ?> 
  1146. </label> 
  1147. </li> 
  1148.  
  1149. </ul> 
  1150. <p class="description member-type-none-notice<?php if ( ! empty( $field_member_types ) ) : ?> hide<?php endif; ?>"><?php _e( 'Unavailable to all members.', 'buddypress' ) ?></p> 
  1151. </div> 
  1152.  
  1153. <input type="hidden" name="has-member-types" value="1" /> 
  1154. </div> 
  1155.  
  1156. <?php 
  1157.  
  1158. /** 
  1159. * Private method used to output field visibility metaboxes. 
  1160. * @since 2.3.0 
  1161. * @return void If default field id 1. 
  1162. */ 
  1163. private function visibility_metabox() { 
  1164.  
  1165. // Default field cannot have custom visibility. 
  1166. if ( true === $this->is_default_field() ) { 
  1167. return; 
  1168. } ?> 
  1169.  
  1170. <div class="postbox"> 
  1171. <h2><label for="default-visibility"><?php esc_html_e( 'Visibility', 'buddypress' ); ?></label></h2> 
  1172. <div class="inside"> 
  1173. <div> 
  1174. <select name="default-visibility" id="default-visibility"> 
  1175.  
  1176. <?php foreach( bp_xprofile_get_visibility_levels() as $level ) : ?> 
  1177.  
  1178. <option value="<?php echo esc_attr( $level['id'] ); ?>" <?php selected( $this->get_default_visibility(), $level['id'] ); ?>> 
  1179. <?php echo esc_html( $level['label'] ); ?> 
  1180. </option> 
  1181.  
  1182. <?php endforeach ?> 
  1183.  
  1184. </select> 
  1185. </div> 
  1186.  
  1187. <div> 
  1188. <ul> 
  1189. <li> 
  1190. <input type="radio" id="allow-custom-visibility-allowed" name="allow-custom-visibility" value="allowed" <?php checked( $this->get_allow_custom_visibility(), 'allowed' ); ?> /> 
  1191. <label for="allow-custom-visibility-allowed"><?php esc_html_e( 'Allow members to override', 'buddypress' ); ?></label> 
  1192. </li> 
  1193. <li> 
  1194. <input type="radio" id="allow-custom-visibility-disabled" name="allow-custom-visibility" value="disabled" <?php checked( $this->get_allow_custom_visibility(), 'disabled' ); ?> /> 
  1195. <label for="allow-custom-visibility-disabled"><?php esc_html_e( 'Enforce field visibility', 'buddypress' ); ?></label> 
  1196. </li> 
  1197. </ul> 
  1198. </div> 
  1199. </div> 
  1200. </div> 
  1201.  
  1202. <?php 
  1203.  
  1204. /** 
  1205. * Output the metabox for setting if field is required or not. 
  1206. * @since 2.3.0 
  1207. * @return void If default field. 
  1208. */ 
  1209. private function required_metabox() { 
  1210.  
  1211. // Default field is always required. 
  1212. if ( true === $this->is_default_field() ) { 
  1213. return; 
  1214. } ?> 
  1215.  
  1216. <div class="postbox"> 
  1217. <h2><label for="required"><?php esc_html_e( 'Requirement', 'buddypress' ); ?></label></h2> 
  1218. <div class="inside"> 
  1219. <select name="required" id="required"> 
  1220. <option value="0"<?php selected( $this->is_required, '0' ); ?>><?php esc_html_e( 'Not Required', 'buddypress' ); ?></option> 
  1221. <option value="1"<?php selected( $this->is_required, '1' ); ?>><?php esc_html_e( 'Required', 'buddypress' ); ?></option> 
  1222. </select> 
  1223. </div> 
  1224. </div> 
  1225.  
  1226. <?php 
  1227.  
  1228. /** 
  1229. * Private method used to output autolink metabox. 
  1230. * @since 2.5.0 
  1231. * @return void If default field id 1. 
  1232. */ 
  1233. private function autolink_metabox() { 
  1234. ?> 
  1235.  
  1236. <div class="postbox"> 
  1237. <h2><?php esc_html_e( 'Autolink', 'buddypress' ); ?></h2> 
  1238. <div class="inside"> 
  1239. <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> 
  1240.  
  1241. <p> 
  1242. <label for="do-autolink" class="screen-reader-text"><?php 
  1243. /** translators: accessibility text */ 
  1244. esc_html_e( 'Autolink status for this field', 'buddypress' ); 
  1245. ?></label> 
  1246. <select name="do_autolink" id="do-autolink"> 
  1247. <option value="on" <?php selected( $this->get_do_autolink() ); ?>><?php esc_html_e( 'Enabled', 'buddypress' ); ?></option> 
  1248. <option value="" <?php selected( $this->get_do_autolink(), false ); ?>><?php esc_html_e( 'Disabled', 'buddypress' ); ?></option> 
  1249. </select> 
  1250. </p> 
  1251. </div> 
  1252. </div> 
  1253.  
  1254. <?php 
  1255.  
  1256. /** 
  1257. * Output the metabox for setting what type of field this is. 
  1258. * @since 2.3.0 
  1259. * @return void If default field. 
  1260. */ 
  1261. private function type_metabox() { 
  1262.  
  1263. // Default field cannot change type. 
  1264. if ( true === $this->is_default_field() ) { 
  1265. return; 
  1266. } ?> 
  1267.  
  1268. <div class="postbox"> 
  1269. <h2><label for="fieldtype"><?php esc_html_e( 'Type', 'buddypress'); ?></label></h2> 
  1270. <div class="inside" aria-live="polite" aria-atomic="true" aria-relevant="all"> 
  1271. <select name="fieldtype" id="fieldtype" onchange="show_options(this.value)"> 
  1272.  
  1273. <?php bp_xprofile_admin_form_field_types( $this->type ); ?> 
  1274.  
  1275. </select> 
  1276.  
  1277. <?php 
  1278.  
  1279. // Deprecated filter, don't use. Go look at {@link BP_XProfile_Field_Type::admin_new_field_html()}. 
  1280. do_action( 'xprofile_field_additional_options', $this ); 
  1281.  
  1282. $this->render_admin_form_children(); ?> 
  1283.  
  1284. </div> 
  1285. </div> 
  1286.  
  1287. <?php 
  1288.  
  1289. /** 
  1290. * Output hidden fields used by default field. 
  1291. * @since 2.3.0 
  1292. * @return void If not default field. 
  1293. */ 
  1294. private function default_field_hidden_inputs() { 
  1295.  
  1296. // Nonce 
  1297. wp_nonce_field( 'bp_xprofile_admin_field', 'bp_xprofile_admin_field' ); 
  1298.  
  1299. // Field 1 is the fullname field, which cannot have custom visibility. 
  1300. if ( false === $this->is_default_field() ) { 
  1301. return; 
  1302. } ?> 
  1303.  
  1304. <input type="hidden" name="required" id="required" value="1" /> 
  1305. <input type="hidden" name="fieldtype" id="fieldtype" value="textbox" /> 
  1306.  
  1307. <?php 
  1308.  
  1309. /** 
  1310. * Return if a field ID is the default field. 
  1311. * @since 2.3.0 
  1312. * @param int $field_id ID of field to check. 
  1313. * @return bool 
  1314. */ 
  1315. private function is_default_field( $field_id = 0 ) { 
  1316.  
  1317. // Fallback to current field ID if none passed. 
  1318. if ( empty( $field_id ) ) { 
  1319. $field_id = $this->id; 
  1320.  
  1321. // Compare & return. 
  1322. return (bool) ( 1 === (int) $field_id );