BP_XProfile_Group

Class to help set up XProfile Groups.

Defined (1)

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

/bp-xprofile/classes/class-bp-xprofile-group.php  
  1. class BP_XProfile_Group { 
  2.  
  3. /** 
  4. * Field group ID. 
  5. * @since 1.1.0 
  6. * @var int ID of field group. 
  7. */ 
  8. public $id = null; 
  9.  
  10. /** 
  11. * Field group name. 
  12. * @since 1.1.0 
  13. * @var string Name of field group. 
  14. */ 
  15. public $name; 
  16.  
  17. /** 
  18. * Field group Description. 
  19. * @since 1.1.0 
  20. * @var string Description of field group. 
  21. */ 
  22. public $description; 
  23.  
  24. /** 
  25. * Group deletion boolean. 
  26. * @since 1.1.0 
  27. * @var bool Can this group be deleted? 
  28. */ 
  29. public $can_delete; 
  30.  
  31. /** 
  32. * Group order. 
  33. * @since 1.1.0 
  34. * @var int Group order relative to other groups. 
  35. */ 
  36. public $group_order; 
  37.  
  38. /** 
  39. * Group fields. 
  40. * @since 1.1.0 
  41. * @var array Fields of group. 
  42. */ 
  43. public $fields; 
  44.  
  45. /** 
  46. * Initialize and/or populate profile field group. 
  47. * @since 1.1.0 
  48. * @param int|null $id Field group ID. 
  49. */ 
  50. public function __construct( $id = null ) { 
  51. if ( ! empty( $id ) ) { 
  52. $this->populate( $id ); 
  53.  
  54. /** 
  55. * Populate a profile field group. 
  56. * @since 1.0.0 
  57. * @global $wpdb $wpdb 
  58. * @param int $id Field group ID. 
  59. * @return boolean 
  60. */ 
  61. public function populate( $id ) { 
  62.  
  63. // Get this group. 
  64. $group = self::get( array( 
  65. 'profile_group_id' => $id 
  66. ) ); 
  67.  
  68. // Bail if group not found. 
  69. if ( empty( $group ) ) { 
  70. return false; 
  71.  
  72. // Get the first array element. 
  73. $group = reset( $group ); 
  74.  
  75. // Set object properties. 
  76. $this->id = $group->id; 
  77. $this->name = $group->name; 
  78. $this->description = $group->description; 
  79. $this->can_delete = $group->can_delete; 
  80. $this->group_order = $group->group_order; 
  81.  
  82. /** 
  83. * Save a profile field group. 
  84. * @since 1.1.0 
  85. * @global object $wpdb 
  86. * @return boolean 
  87. */ 
  88. public function save() { 
  89. global $wpdb; 
  90.  
  91. // Filter the field group attributes. 
  92. $this->name = apply_filters( 'xprofile_group_name_before_save', $this->name, $this->id ); 
  93. $this->description = apply_filters( 'xprofile_group_description_before_save', $this->description, $this->id ); 
  94.  
  95. /** 
  96. * Fires before the current group instance gets saved. 
  97. * Please use this hook to filter the properties above. Each part will be passed in. 
  98. * @since 1.0.0 
  99. * @param BP_XProfile_Group $this Current instance of the group being saved. Passed by reference. 
  100. */ 
  101. do_action_ref_array( 'xprofile_group_before_save', array( &$this ) ); 
  102.  
  103. $bp = buddypress(); 
  104.  
  105. // Update or insert. 
  106. if ( ! empty( $this->id ) ) { 
  107. $sql = $wpdb->prepare( "UPDATE {$bp->profile->table_name_groups} SET name = %s, description = %s WHERE id = %d", $this->name, $this->description, $this->id ); 
  108. } else { 
  109. $sql = $wpdb->prepare( "INSERT INTO {$bp->profile->table_name_groups} (name, description, can_delete) VALUES (%s, %s, 1)", $this->name, $this->description ); 
  110.  
  111. // Attempt to insert or update. 
  112. $query = $wpdb->query( $sql ); 
  113.  
  114. // Bail if query fails. If `$query` is 0, it means the save was successful, but no fields were updated. 
  115. if ( false === $query || is_wp_error( $query ) ) { 
  116. return false; 
  117.  
  118. // If not set, update the ID in the group object. 
  119. if ( empty( $this->id ) ) { 
  120. $this->id = $wpdb->insert_id; 
  121.  
  122. /** 
  123. * Fires after the current group instance gets saved. 
  124. * @since 1.0.0 
  125. * @param BP_XProfile_Group $this Current instance of the group being saved. Passed by reference. 
  126. */ 
  127. do_action_ref_array( 'xprofile_group_after_save', array( &$this ) ); 
  128.  
  129. return $this->id; 
  130.  
  131. /** 
  132. * Delete a profile field group 
  133. * @since 1.1.0 
  134. * @global object $wpdb 
  135. * @return boolean 
  136. */ 
  137. public function delete() { 
  138. global $wpdb; 
  139.  
  140. // Bail if field group cannot be deleted. 
  141. if ( empty( $this->can_delete ) ) { 
  142. return false; 
  143.  
  144. /** 
  145. * Fires before the current group instance gets deleted. 
  146. * @since 2.0.0 
  147. * @param BP_XProfile_Group $this Current instance of the group being deleted. Passed by reference. 
  148. */ 
  149. do_action_ref_array( 'xprofile_group_before_delete', array( &$this ) ); 
  150.  
  151. $bp = buddypress(); 
  152. $sql = $wpdb->prepare( "DELETE FROM {$bp->profile->table_name_groups} WHERE id = %d", $this->id ); 
  153. $deleted = $wpdb->query( $sql ); 
  154.  
  155. // Delete field group. 
  156. if ( empty( $deleted ) || is_wp_error( $deleted ) ) { 
  157. return false; 
  158.  
  159. // Remove the group's fields. 
  160. if ( BP_XProfile_Field::delete_for_group( $this->id ) ) { 
  161.  
  162. // Remove profile data for the groups fields. 
  163. for ( $i = 0, $count = count( $this->fields ); $i < $count; ++$i ) { 
  164. BP_XProfile_ProfileData::delete_for_field( $this->fields[$i]->id ); 
  165.  
  166. /** 
  167. * Fires after the current group instance gets deleted. 
  168. * @since 2.0.0 
  169. * @param BP_XProfile_Group $this Current instance of the group being deleted. Passed by reference. 
  170. */ 
  171. do_action_ref_array( 'xprofile_group_after_delete', array( &$this ) ); 
  172.  
  173. return true; 
  174.  
  175. /** Static Methods ********************************************************/ 
  176.  
  177. /** 
  178. * Populates the BP_XProfile_Group object with profile field groups, fields,  
  179. * and field data. 
  180. * @since 1.2.0 
  181. * @global object $wpdb WordPress DB access object. 
  182. * @param array $args { 
  183. * Array of optional arguments: 
  184. * @type int $profile_group_id Limit results to a single profile group. 
  185. * @type int $user_id Required if you want to load a specific user's data. 
  186. * Default: displayed user's ID. 
  187. * @type array|string $member_type Limit fields by those restricted to a given member type, or array of 
  188. * member types. If `$user_id` is provided, the value of `$member_type` 
  189. * will be overridden by the member types of the provided user. The 
  190. * special value of 'any' will return only those fields that are 
  191. * unrestricted by member type - i.e., those applicable to any type. 
  192. * @type bool $hide_empty_groups True to hide groups that don't have any fields. Default: false. 
  193. * @type bool $hide_empty_fields True to hide fields where the user has not provided data. 
  194. * Default: false. 
  195. * @type bool $fetch_fields Whether to fetch each group's fields. Default: false. 
  196. * @type bool $fetch_field_data Whether to fetch data for each field. Requires a $user_id. 
  197. * Default: false. 
  198. * @type array $exclude_groups Comma-separated list or array of group IDs to exclude. 
  199. * @type array $exclude_fields Comma-separated list or array of field IDs to exclude. 
  200. * @type bool $update_meta_cache Whether to pre-fetch xprofilemeta for all retrieved groups, fields,  
  201. * and data. Default: true. 
  202. * } 
  203. * @return array $groups 
  204. */ 
  205. public static function get( $args = array() ) { 
  206. global $wpdb; 
  207.  
  208. // Parse arguments. 
  209. $r = wp_parse_args( $args, array( 
  210. 'profile_group_id' => false,  
  211. 'user_id' => bp_displayed_user_id(),  
  212. 'member_type' => false,  
  213. 'hide_empty_groups' => false,  
  214. 'hide_empty_fields' => false,  
  215. 'fetch_fields' => false,  
  216. 'fetch_field_data' => false,  
  217. 'fetch_visibility_level' => false,  
  218. 'exclude_groups' => false,  
  219. 'exclude_fields' => false,  
  220. 'update_meta_cache' => true,  
  221. ) ); 
  222.  
  223. // Keep track of object IDs for cache-priming. 
  224. $object_ids = array( 
  225. 'group' => array(),  
  226. 'field' => array(),  
  227. 'data' => array(),  
  228. ); 
  229.  
  230. // WHERE. 
  231. if ( ! empty( $r['profile_group_id'] ) ) { 
  232. $where_sql = $wpdb->prepare( 'WHERE g.id = %d', $r['profile_group_id'] ); 
  233. } elseif ( $r['exclude_groups'] ) { 
  234. $exclude = join( ', ', wp_parse_id_list( $r['exclude_groups'] ) ); 
  235. $where_sql = "WHERE g.id NOT IN ({$exclude})"; 
  236. } else { 
  237. $where_sql = ''; 
  238.  
  239. $bp = buddypress(); 
  240.  
  241. // Include or exclude empty groups. 
  242. if ( ! empty( $r['hide_empty_groups'] ) ) { 
  243. $group_ids = $wpdb->get_col( "SELECT DISTINCT g.id FROM {$bp->profile->table_name_groups} g INNER JOIN {$bp->profile->table_name_fields} f ON g.id = f.group_id {$where_sql} ORDER BY g.group_order ASC" ); 
  244. } else { 
  245. $group_ids = $wpdb->get_col( "SELECT DISTINCT g.id FROM {$bp->profile->table_name_groups} g {$where_sql} ORDER BY g.group_order ASC" ); 
  246.  
  247. // Get all group data. 
  248. $groups = self::get_group_data( $group_ids ); 
  249.  
  250. // Bail if not also getting fields. 
  251. if ( empty( $r['fetch_fields'] ) ) { 
  252. return $groups; 
  253.  
  254. // Get the group ids from the groups we found. 
  255. $group_ids = wp_list_pluck( $groups, 'id' ); 
  256.  
  257. // Store for meta cache priming. 
  258. $object_ids['group'] = $group_ids; 
  259.  
  260. // Bail if no groups found. 
  261. if ( empty( $group_ids ) ) { 
  262. return $groups; 
  263.  
  264. // Setup IN query from group IDs. 
  265. $group_ids_in = implode( ', ', (array) $group_ids ); 
  266.  
  267. // Support arrays and comma-separated strings. 
  268. $exclude_fields_cs = wp_parse_id_list( $r['exclude_fields'] ); 
  269.  
  270. // Visibility - Handled here so as not to be overridden by sloppy use of the 
  271. // exclude_fields parameter. See bp_xprofile_get_hidden_fields_for_user(). 
  272. $hidden_user_fields = bp_xprofile_get_hidden_fields_for_user( $r['user_id'] ); 
  273. $exclude_fields_cs = array_merge( $exclude_fields_cs, $hidden_user_fields ); 
  274. $exclude_fields_cs = implode( ', ', $exclude_fields_cs ); 
  275.  
  276. // Set up NOT IN query for excluded field IDs. 
  277. if ( ! empty( $exclude_fields_cs ) ) { 
  278. $exclude_fields_sql = "AND id NOT IN ({$exclude_fields_cs})"; 
  279. } else { 
  280. $exclude_fields_sql = ''; 
  281.  
  282. // Set up IN query for included field IDs. 
  283. $include_field_ids = array(); 
  284.  
  285. // Member-type restrictions. 
  286. if ( bp_get_member_types() ) { 
  287. if ( $r['user_id'] || false !== $r['member_type'] ) { 
  288. $member_types = $r['member_type']; 
  289. if ( $r['user_id'] ) { 
  290. $member_types = bp_get_member_type( $r['user_id'], false ); 
  291. if ( empty( $member_types ) ) { 
  292. $member_types = array( 'null' ); 
  293.  
  294. $member_types_fields = BP_XProfile_Field::get_fields_for_member_type( $member_types ); 
  295. $include_field_ids += array_keys( $member_types_fields ); 
  296.  
  297. $in_sql = ''; 
  298. if ( ! empty( $include_field_ids ) ) { 
  299. $include_field_ids_cs = implode( ', ', array_map( 'intval', $include_field_ids ) ); 
  300. $in_sql = " AND id IN ({$include_field_ids_cs}) "; 
  301.  
  302. // Fetch the fields. 
  303. $field_ids = $wpdb->get_col( "SELECT id FROM {$bp->profile->table_name_fields} WHERE group_id IN ( {$group_ids_in} ) AND parent_id = 0 {$exclude_fields_sql} {$in_sql} ORDER BY field_order" ); 
  304.  
  305. foreach( $groups as $group ) { 
  306. $group->fields = array(); 
  307.  
  308. // Bail if no fields. 
  309. if ( empty( $field_ids ) ) { 
  310. return $groups; 
  311.  
  312. $field_ids = array_map( 'intval', $field_ids ); 
  313.  
  314. // Prime the field cache. 
  315. $uncached_field_ids = bp_get_non_cached_ids( $field_ids, 'bp_xprofile_fields' ); 
  316. if ( ! empty( $uncached_field_ids ) ) { 
  317. $_uncached_field_ids = implode( ', ', array_map( 'intval', $uncached_field_ids ) ); 
  318. $uncached_fields = $wpdb->get_results( "SELECT * FROM {$bp->profile->table_name_fields} WHERE id IN ({$_uncached_field_ids})" ); 
  319. foreach ( $uncached_fields as $uncached_field ) { 
  320. $fid = intval( $uncached_field->id ); 
  321. wp_cache_set( $fid, $uncached_field, 'bp_xprofile_fields' ); 
  322.  
  323. // Pull field objects from the cache. 
  324. $fields = array(); 
  325. foreach ( $field_ids as $field_id ) { 
  326. $fields[] = xprofile_get_field( $field_id, null, false ); 
  327.  
  328. // Store field IDs for meta cache priming. 
  329. $object_ids['field'] = $field_ids; 
  330.  
  331. // Maybe fetch field data. 
  332. if ( ! empty( $r['fetch_field_data'] ) ) { 
  333.  
  334. // Get field data for user ID. 
  335. if ( ! empty( $field_ids ) && ! empty( $r['user_id'] ) ) { 
  336. $field_data = BP_XProfile_ProfileData::get_data_for_user( $r['user_id'], $field_ids ); 
  337.  
  338. // Remove data-less fields, if necessary. 
  339. if ( ! empty( $r['hide_empty_fields'] ) && ! empty( $field_ids ) && ! empty( $field_data ) ) { 
  340.  
  341. // Loop through the results and find the fields that have data. 
  342. foreach( (array) $field_data as $data ) { 
  343.  
  344. // Empty fields may contain a serialized empty array. 
  345. $maybe_value = maybe_unserialize( $data->value ); 
  346.  
  347. // Valid field values of 0 or '0' get caught by empty(), so we have an extra check for these. See #BP5731. 
  348. if ( ( ! empty( $maybe_value ) || '0' == $maybe_value ) && false !== $key = array_search( $data->field_id, $field_ids ) ) { 
  349.  
  350. // Fields that have data get removed from the list. 
  351. unset( $field_ids[ $key ] ); 
  352.  
  353. // The remaining members of $field_ids are empty. Remove them. 
  354. foreach( $fields as $field_key => $field ) { 
  355. if ( in_array( $field->id, $field_ids ) ) { 
  356. unset( $fields[ $field_key ] ); 
  357.  
  358. // Reset indexes. 
  359. $fields = array_values( $fields ); 
  360.  
  361. // Field data was found. 
  362. if ( ! empty( $fields ) && ! empty( $field_data ) && ! is_wp_error( $field_data ) ) { 
  363.  
  364. // Loop through fields. 
  365. foreach( (array) $fields as $field_key => $field ) { 
  366.  
  367. // Loop through the data in each field. 
  368. foreach( (array) $field_data as $data ) { 
  369.  
  370. // Assign correct data value to the field. 
  371. if ( $field->id == $data->field_id ) { 
  372. $fields[ $field_key ]->data = new stdClass; 
  373. $fields[ $field_key ]->data->value = $data->value; 
  374. $fields[ $field_key ]->data->id = $data->id; 
  375.  
  376. // Store for meta cache priming. 
  377. $object_ids['data'][] = $data->id; 
  378.  
  379. // Prime the meta cache, if necessary. 
  380. if ( ! empty( $r['update_meta_cache'] ) ) { 
  381. bp_xprofile_update_meta_cache( $object_ids ); 
  382.  
  383. // Maybe fetch visibility levels. 
  384. if ( ! empty( $r['fetch_visibility_level'] ) ) { 
  385. $fields = self::fetch_visibility_level( $r['user_id'], $fields ); 
  386.  
  387. // Merge the field array back in with the group array. 
  388. foreach( (array) $groups as $group ) { 
  389. // Indexes may have been shifted after previous deletions, so we get a 
  390. // fresh one each time through the loop. 
  391. $index = array_search( $group, $groups ); 
  392.  
  393. foreach( (array) $fields as $field ) { 
  394. if ( $group->id === $field->group_id ) { 
  395. $groups[ $index ]->fields[] = $field; 
  396.  
  397. // When we unset fields above, we may have created empty groups. 
  398. // Remove them, if necessary. 
  399. if ( empty( $group->fields ) && ! empty( $r['hide_empty_groups'] ) ) { 
  400. unset( $groups[ $index ] ); 
  401.  
  402. // Reset indexes. 
  403. $groups = array_values( $groups ); 
  404.  
  405. return $groups; 
  406.  
  407. /** 
  408. * Get data about a set of groups, based on IDs. 
  409. * @since 2.0.0 
  410. * @param array $group_ids Array of IDs. 
  411. * @return array 
  412. */ 
  413. protected static function get_group_data( $group_ids ) { 
  414. global $wpdb; 
  415.  
  416. // Bail if no group IDs are passed. 
  417. if ( empty( $group_ids ) ) { 
  418. return array(); 
  419.  
  420. // Setup empty arrays. 
  421. $groups = array(); 
  422. $uncached_gids = array(); 
  423.  
  424. // Loop through groups and look for cached & uncached data. 
  425. foreach ( $group_ids as $group_id ) { 
  426.  
  427. // If cached data is found, use it. 
  428. $group_data = wp_cache_get( $group_id, 'bp_xprofile_groups' ); 
  429. if ( false !== $group_data ) { 
  430. $groups[ $group_id ] = $group_data; 
  431.  
  432. // Otherwise leave a placeholder so we don't lose the order. 
  433. } else { 
  434. $groups[ $group_id ] = ''; 
  435.  
  436. // Add to the list of items to be queried. 
  437. $uncached_gids[] = $group_id; 
  438.  
  439. // Fetch uncached data from the DB if necessary. 
  440. if ( ! empty( $uncached_gids ) ) { 
  441.  
  442. // Setup IN query for uncached group data. 
  443. $uncached_gids_sql = implode( ', ', wp_parse_id_list( $uncached_gids ) ); 
  444.  
  445. // Get table name to query. 
  446. $table_name = buddypress()->profile->table_name_groups; 
  447.  
  448. // Fetch data, preserving order. 
  449. $queried_gdata = $wpdb->get_results( "SELECT * FROM {$table_name} WHERE id IN ({$uncached_gids_sql}) ORDER BY FIELD( id, {$uncached_gids_sql} )"); 
  450.  
  451. // Make sure query returned valid data. 
  452. if ( ! empty( $queried_gdata ) && ! is_wp_error( $queried_gdata ) ) { 
  453.  
  454. // Put queried data into the placeholders created earlier,  
  455. // and add it to the cache. 
  456. foreach ( (array) $queried_gdata as $gdata ) { 
  457.  
  458. // Add group to groups array. 
  459. $groups[ $gdata->id ] = $gdata; 
  460.  
  461. // Cache previously uncached group data. 
  462. wp_cache_set( $gdata->id, $gdata, 'bp_xprofile_groups' ); 
  463.  
  464. // Integer casting. 
  465. foreach ( (array) $groups as $key => $data ) { 
  466. $groups[ $key ]->id = (int) $groups[ $key ]->id; 
  467. $groups[ $key ]->group_order = (int) $groups[ $key ]->group_order; 
  468. $groups[ $key ]->can_delete = (int) $groups[ $key ]->can_delete; 
  469.  
  470. // Reset indexes & return. 
  471. return array_values( $groups ); 
  472.  
  473. /** 
  474. * Validate field group when form submitted. 
  475. * @since 1.0.0 
  476. * @global string $message 
  477. * @return boolean 
  478. */ 
  479. public static function admin_validate() { 
  480. global $message; 
  481.  
  482. // Validate Form. 
  483. if ( empty( $_POST['group_name'] ) ) { 
  484. $message = __( 'Please make sure you give the group a name.', 'buddypress' ); 
  485. return false; 
  486. } else { 
  487. return true; 
  488.  
  489. /** 
  490. * Update field group position. 
  491. * @since 1.5.0 
  492. * @global $wpdb $wpdb 
  493. * @param int $field_group_id ID of the group the field belongs to. 
  494. * @param int $position Field group position. 
  495. * @return boolean 
  496. */ 
  497. public static function update_position( $field_group_id, $position ) { 
  498. global $wpdb; 
  499.  
  500. if ( ! is_numeric( $position ) ) { 
  501. return false; 
  502.  
  503. // Purge profile field group cache. 
  504. wp_cache_delete( 'all', 'bp_xprofile_groups' ); 
  505.  
  506. $bp = buddypress(); 
  507.  
  508. return $wpdb->query( $wpdb->prepare( "UPDATE {$bp->profile->table_name_groups} SET group_order = %d WHERE id = %d", $position, $field_group_id ) ); 
  509.  
  510. /** 
  511. * Fetch the field visibility level for the fields returned by the query. 
  512. * @since 1.6.0 
  513. * @param int $user_id The profile owner's user_id. 
  514. * @param array $fields The database results returned by the get() query. 
  515. * @return array $fields The database results, with field_visibility added 
  516. */ 
  517. public static function fetch_visibility_level( $user_id = 0, $fields = array() ) { 
  518.  
  519. // Get the user's visibility level preferences. 
  520. $visibility_levels = bp_get_user_meta( $user_id, 'bp_xprofile_visibility_levels', true ); 
  521.  
  522. foreach( (array) $fields as $key => $field ) { 
  523.  
  524. // Does the admin allow this field to be customized? 
  525. $visibility = bp_xprofile_get_meta( $field->id, 'field', 'allow_custom_visibility' ); 
  526. $allow_custom = (bool) ( 'disabled' !== $visibility ); 
  527.  
  528. // Look to see if the user has set the visibility for this field. 
  529. if ( ( true === $allow_custom ) && isset( $visibility_levels[ $field->id ] ) ) { 
  530. $field_visibility = $visibility_levels[ $field->id ]; 
  531.  
  532. // If no admin-set default is saved, fall back on a global default. 
  533. } else { 
  534. $fallback_visibility = bp_xprofile_get_meta( $field->id, 'field', 'default_visibility' ); 
  535.  
  536. /** 
  537. * Filters the XProfile default visibility level for a field. 
  538. * @since 1.6.0 
  539. * @param string $value Default visibility value. 
  540. */ 
  541. $field_visibility = ! empty( $fallback_visibility ) 
  542. ? $fallback_visibility 
  543. : apply_filters( 'bp_xprofile_default_visibility_level', 'public' ); 
  544.  
  545. $fields[ $key ]->visibility_level = $field_visibility; 
  546.  
  547. return $fields; 
  548.  
  549. /** 
  550. * Fetch the admin-set preferences for all fields. 
  551. * @since 1.6.0 
  552. * @return array $default_visibility_levels An array, keyed by field_id, of default 
  553. * visibility level + allow_custom 
  554. * (whether the admin allows this 
  555. * field to be set by user) 
  556. */ 
  557. public static function fetch_default_visibility_levels() { 
  558. global $wpdb; 
  559.  
  560. $default_visibility_levels = wp_cache_get( 'default_visibility_levels', 'bp_xprofile' ); 
  561.  
  562. if ( false === $default_visibility_levels ) { 
  563. $bp = buddypress(); 
  564.  
  565. $levels = $wpdb->get_results( "SELECT object_id, meta_key, meta_value FROM {$bp->profile->table_name_meta} WHERE object_type = 'field' AND ( meta_key = 'default_visibility' OR meta_key = 'allow_custom_visibility' )" ); 
  566.  
  567. // Arrange so that the field id is the key and the visibility level the value. 
  568. $default_visibility_levels = array(); 
  569. foreach ( $levels as $level ) { 
  570. switch ( $level->meta_key ) { 
  571. case 'default_visibility' : 
  572. $default_visibility_levels[ $level->object_id ]['default'] = $level->meta_value; 
  573. break; 
  574. case 'allow_custom_visibility' : 
  575. $default_visibility_levels[ $level->object_id ]['allow_custom'] = $level->meta_value; 
  576. break; 
  577.  
  578. wp_cache_set( 'default_visibility_levels', $default_visibility_levels, 'bp_xprofile' ); 
  579.  
  580. return $default_visibility_levels; 
  581.  
  582. /** Admin Output **********************************************************/ 
  583.  
  584. /** 
  585. * Output the admin area field group form. 
  586. * @since 1.0.0 
  587. * @global string $message 
  588. */ 
  589. public function render_admin_form() { 
  590. global $message; 
  591.  
  592. // New field group. 
  593. if ( empty( $this->id ) ) { 
  594. $title = __( 'Add New Field Group', 'buddypress' ); 
  595. $action = add_query_arg( array( 'page' => 'bp-profile-setup', 'mode' => 'add_group' ), 'users.php' ); 
  596. $button = __( 'Save', 'buddypress' ); 
  597.  
  598. // Existing field group. 
  599. } else { 
  600. $title = __( 'Edit Field Group', 'buddypress' ); 
  601. $action = add_query_arg( array( 'page' => 'bp-profile-setup', 'mode' => 'edit_group', 'group_id' => $this->id ), 'users.php' ); 
  602. $button = __( 'Update', 'buddypress' ); 
  603. } ?> 
  604.  
  605. <div class="wrap"> 
  606.  
  607. <h1><?php echo esc_html( $title ); ?></h1> 
  608.  
  609. <?php if ( ! empty( $message ) ) : ?> 
  610.  
  611. <div id="message" class="error fade"> 
  612. <p><?php echo esc_html( $message ); ?></p> 
  613. </div> 
  614.  
  615. <?php endif; ?> 
  616.  
  617. <form id="bp-xprofile-add-field-group" action="<?php echo esc_url( $action ); ?>" method="post"> 
  618. <div id="poststuff"> 
  619. <div id="post-body" class="metabox-holder columns-<?php echo ( 1 == get_current_screen()->get_columns() ) ? '1' : '2'; ?>"> 
  620. <div id="post-body-content"> 
  621. <div id="titlediv"> 
  622. <div class="titlewrap"> 
  623. <label id="title-prompt-text" for="title"><?php esc_html_e( 'Field Group Name (required)', 'buddypress') ?></label> 
  624. <input type="text" name="group_name" id="title" value="<?php echo esc_attr( $this->name ); ?>" autocomplete="off" /> 
  625. </div> 
  626. </div> 
  627. <div class="postbox"> 
  628. <h2><?php esc_html_e( 'Field Group Description', 'buddypress' ); ?></h2> 
  629. <div class="inside"> 
  630. <label for="group_description" class="screen-reader-text"><?php 
  631. /** translators: accessibility text */ 
  632. esc_html_e( 'Add description', 'buddypress' ); 
  633. ?></label> 
  634. <textarea name="group_description" id="group_description" rows="8" cols="60"><?php echo esc_textarea( $this->description ); ?></textarea> 
  635. </div> 
  636. </div> 
  637.  
  638. <?php 
  639.  
  640. /** 
  641. * Fires after the XProfile group description field is rendered in wp-admin. 
  642. * @since 2.6.0 
  643. * @param BP_XProfile_Group $this Current XProfile group. 
  644. */ 
  645. do_action( 'xprofile_group_admin_after_description', $this ); ?> 
  646.  
  647. </div><!-- #post-body-content --> 
  648.  
  649. <div id="postbox-container-1" class="postbox-container"> 
  650.  
  651. <?php 
  652.  
  653. /** 
  654. * Fires before XProfile Group submit metabox. 
  655. * @since 2.1.0 
  656. * @param BP_XProfile_Group $this Current XProfile group. 
  657. */ 
  658. do_action( 'xprofile_group_before_submitbox', $this ); ?> 
  659.  
  660. <div id="submitdiv" class="postbox"> 
  661. <h2><?php _e( 'Submit', 'buddypress' ); ?></h2> 
  662. <div class="inside"> 
  663. <div id="submitcomment" class="submitbox"> 
  664. <div id="major-publishing-actions"> 
  665.  
  666. <?php 
  667.  
  668. // Nonce fields 
  669. wp_nonce_field( 'bp_xprofile_admin_group', 'bp_xprofile_admin_group' ); 
  670.  
  671. /** 
  672. * Fires at the beginning of the XProfile Group publishing actions section. 
  673. * @since 2.1.0 
  674. * @param BP_XProfile_Group $this Current XProfile group. 
  675. */ 
  676. do_action( 'xprofile_group_submitbox_start', $this ); ?> 
  677.  
  678. <input type="hidden" name="group_order" id="group_order" value="<?php echo esc_attr( $this->group_order ); ?>" /> 
  679. <div id="publishing-action"> 
  680. <input type="submit" name="save_group" value="<?php echo esc_attr( $button ); ?>" class="button-primary"/> 
  681. </div> 
  682. <div id="delete-action"> 
  683. <a href="users.php?page=bp-profile-setup" class="deletion"><?php _e( 'Cancel', 'buddypress' ); ?></a> 
  684. </div> 
  685. <div class="clear"></div> 
  686. </div> 
  687. </div> 
  688. </div> 
  689. </div> 
  690.  
  691. <?php 
  692.  
  693. /** 
  694. * Fires after XProfile Group submit metabox. 
  695. * @since 2.1.0 
  696. * @param BP_XProfile_Group $this Current XProfile group. 
  697. */ 
  698. do_action( 'xprofile_group_after_submitbox', $this ); ?> 
  699.  
  700. </div> 
  701. </div> 
  702. </div> 
  703. </form> 
  704. </div> 
  705.  
  706. <?php