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