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