- Timestamp:
- 10/01/2015 04:18:13 AM (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/bp-xprofile/classes/class-bp-xprofile-group.php
r10142 r10163 1 1 <?php 2 2 /** 3 * BuddyPress XProfile Classes 3 * BuddyPress XProfile Classes. 4 4 * 5 5 * @package BuddyPress … … 13 13 14 14 /** 15 * Field group ID. 16 * 15 17 * @since 1.1.0 16 18 * … … 20 22 21 23 /** 24 * Field group name. 25 * 22 26 * @since 1.1.0 23 27 * … … 27 31 28 32 /** 33 * Field group Description. 34 * 29 35 * @since 1.1.0 30 36 * … … 34 40 35 41 /** 42 * Group deletion boolean. 43 * 36 44 * @since 1.1.0 37 45 * … … 41 49 42 50 /** 51 * Group order. 52 * 43 53 * @since 1.1.0 44 54 * … … 48 58 49 59 /** 60 * Group fields. 61 * 50 62 * @since 1.1.0 51 63 * … … 55 67 56 68 /** 57 * Initialize and/or populate profile field group 69 * Initialize and/or populate profile field group. 58 70 * 59 71 * @since 1.1.0 60 72 * 61 * @param int $id 62 * @param int $user_id 63 * @param bool $get_data 73 * @param int|null $id Field group ID. 64 74 */ 65 75 public function __construct( $id = null ) { … … 70 80 71 81 /** 72 * Populate a profile field group 82 * Populate a profile field group. 73 83 * 74 84 * @since 1.0.0 75 85 * 76 86 * @global $wpdb $wpdb 77 * @param int $id 87 * 88 * @param int $id Field group ID. 78 89 * 79 90 * @return boolean … … 81 92 public function populate( $id ) { 82 93 83 // Get this group 94 // Get this group. 84 95 $group = self::get( array( 85 96 'profile_group_id' => $id 86 97 ) ); 87 98 88 // Bail if group not found 99 // Bail if group not found. 89 100 if ( empty( $group ) ) { 90 101 return false; 91 102 } 92 103 93 // Get the first array element 104 // Get the first array element. 94 105 $group = reset( $group ); 95 106 96 // Set object properties 107 // Set object properties. 97 108 $this->id = $group->id; 98 109 $this->name = $group->name; … … 103 114 104 115 /** 105 * Save a profile field group 116 * Save a profile field group. 106 117 * 107 118 * @since 1.1.0 … … 114 125 global $wpdb; 115 126 116 // Filter the field group attributes 127 // Filter the field group attributes. 117 128 $this->name = apply_filters( 'xprofile_group_name_before_save', $this->name, $this->id ); 118 129 $this->description = apply_filters( 'xprofile_group_description_before_save', $this->description, $this->id ); … … 125 136 * @since 1.0.0 126 137 * 127 * @param BP_XProfile_Group Current instance of the group being saved. Passed by reference.138 * @param BP_XProfile_Group $this Current instance of the group being saved. Passed by reference. 128 139 */ 129 140 do_action_ref_array( 'xprofile_group_before_save', array( &$this ) ); … … 131 142 $bp = buddypress(); 132 143 133 // Update or insert 144 // Update or insert. 134 145 if ( ! empty( $this->id ) ) { 135 146 $sql = $wpdb->prepare( "UPDATE {$bp->profile->table_name_groups} SET name = %s, description = %s WHERE id = %d", $this->name, $this->description, $this->id ); … … 138 149 } 139 150 140 // Attempt to insert or update 151 // Attempt to insert or update. 141 152 $query = $wpdb->query( $sql ); 142 153 … … 146 157 } 147 158 148 // If not set, update the ID in the group object 159 // If not set, update the ID in the group object. 149 160 if ( empty( $this->id ) ) { 150 161 $this->id = $wpdb->insert_id; … … 156 167 * @since 1.0.0 157 168 * 158 * @param BP_XProfile_Group Current instance of the group being saved. Passed by reference.169 * @param BP_XProfile_Group $this Current instance of the group being saved. Passed by reference. 159 170 */ 160 171 do_action_ref_array( 'xprofile_group_after_save', array( &$this ) ); … … 174 185 global $wpdb; 175 186 176 // Bail if field group cannot be deleted 187 // Bail if field group cannot be deleted. 177 188 if ( empty( $this->can_delete ) ) { 178 189 return false; … … 184 195 * @since 2.0.0 185 196 * 186 * @param BP_XProfile_Group Current instance of the group being deleted. Passed by reference.197 * @param BP_XProfile_Group $this Current instance of the group being deleted. Passed by reference. 187 198 */ 188 199 do_action_ref_array( 'xprofile_group_before_delete', array( &$this ) ); … … 192 203 $deleted = $wpdb->query( $sql ); 193 204 194 // Delete field group 205 // Delete field group. 195 206 if ( empty( $deleted ) || is_wp_error( $deleted ) ) { 196 207 return false; … … 200 211 if ( BP_XProfile_Field::delete_for_group( $this->id ) ) { 201 212 202 // Remove profile data for the groups fields 213 // Remove profile data for the groups fields. 203 214 for ( $i = 0, $count = count( $this->fields ); $i < $count; ++$i ) { 204 215 BP_XProfile_ProfileData::delete_for_field( $this->fields[$i]->id ); … … 211 222 * @since 2.0.0 212 223 * 213 * @param BP_XProfile_Group Current instance of the group being deleted. Passed by reference.224 * @param BP_XProfile_Group $this Current instance of the group being deleted. Passed by reference. 214 225 */ 215 226 do_action_ref_array( 'xprofile_group_after_delete', array( &$this ) ); … … 226 237 * @package BuddyPress XProfile 227 238 * 228 * @global $wpdb WordPress DB access object.239 * @global object $wpdb WordPress DB access object. 229 240 * 230 241 * @param array $args { 231 * Array of optional arguments:232 * @type int $profile_group_idLimit results to a single profile group.233 * @type int $user_idRequired if you want to load a specific user's data.234 * Default: displayed user's ID.235 * @type array|string $member_type Limit fields by those restricted to a given member type, or array of236 * member types. If `$user_id` is provided, the value of `$member_type`237 * will be overridden by the member types of the provided user. The238 * special value of 'any' will return only those fields that are239 * unrestricted by member type - i.e., those applicable to any type.240 * @type bool $hide_empty_groupsTrue to hide groups that don't have any fields. Default: false.241 * @type bool $hide_empty_fieldsTrue to hide fields where the user has not provided data.242 * Default: false.243 * @type bool $fetch_fieldsWhether to fetch each group's fields. Default: false.244 * @type bool $fetch_field_dataWhether to fetch data for each field. Requires a $user_id.245 * Default: false.246 * @type array $exclude_groupsComma-separated list or array of group IDs to exclude.247 * @type array $exclude_fieldsComma-separated list or array of field IDs to exclude.248 * @type bool $update_meta_cacheWhether to pre-fetch xprofilemeta for all retrieved groups, fields,249 * and data. Default: true.242 * Array of optional arguments: 243 * @type int $profile_group_id Limit results to a single profile group. 244 * @type int $user_id Required if you want to load a specific user's data. 245 * Default: displayed user's ID. 246 * @type array|string $member_type Limit fields by those restricted to a given member type, or array of 247 * member types. If `$user_id` is provided, the value of `$member_type` 248 * will be overridden by the member types of the provided user. The 249 * special value of 'any' will return only those fields that are 250 * unrestricted by member type - i.e., those applicable to any type. 251 * @type bool $hide_empty_groups True to hide groups that don't have any fields. Default: false. 252 * @type bool $hide_empty_fields True to hide fields where the user has not provided data. 253 * Default: false. 254 * @type bool $fetch_fields Whether to fetch each group's fields. Default: false. 255 * @type bool $fetch_field_data Whether to fetch data for each field. Requires a $user_id. 256 * Default: false. 257 * @type array $exclude_groups Comma-separated list or array of group IDs to exclude. 258 * @type array $exclude_fields Comma-separated list or array of field IDs to exclude. 259 * @type bool $update_meta_cache Whether to pre-fetch xprofilemeta for all retrieved groups, fields, 260 * and data. Default: true. 250 261 * } 251 262 * @return array $groups … … 254 265 global $wpdb; 255 266 256 // Parse arguments 267 // Parse arguments. 257 268 $r = wp_parse_args( $args, array( 258 269 'profile_group_id' => false, … … 269 280 ) ); 270 281 271 // Keep track of object IDs for cache-priming 282 // Keep track of object IDs for cache-priming. 272 283 $object_ids = array( 273 284 'group' => array(), … … 276 287 ); 277 288 278 // WHERE 289 // WHERE. 279 290 if ( ! empty( $r['profile_group_id'] ) ) { 280 291 $where_sql = $wpdb->prepare( 'WHERE g.id = %d', $r['profile_group_id'] ); … … 288 299 $bp = buddypress(); 289 300 290 // Include or exclude empty groups 301 // Include or exclude empty groups. 291 302 if ( ! empty( $r['hide_empty_groups'] ) ) { 292 303 $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" ); … … 295 306 } 296 307 297 // Get all group data 308 // Get all group data. 298 309 $groups = self::get_group_data( $group_ids ); 299 310 300 // Bail if not also getting fields 311 // Bail if not also getting fields. 301 312 if ( empty( $r['fetch_fields'] ) ) { 302 313 return $groups; 303 314 } 304 315 305 // Get the group ids from the groups we found 316 // Get the group ids from the groups we found. 306 317 $group_ids = wp_list_pluck( $groups, 'id' ); 307 318 308 // Store for meta cache priming 319 // Store for meta cache priming. 309 320 $object_ids['group'] = $group_ids; 310 321 311 // Bail if no groups found S322 // Bail if no groups found. 312 323 if ( empty( $group_ids ) ) { 313 324 return $groups; 314 325 } 315 326 316 // Setup IN query from group IDs 327 // Setup IN query from group IDs. 317 328 $group_ids_in = implode( ',', (array) $group_ids ); 318 329 319 // Support arrays and comma-separated strings 330 // Support arrays and comma-separated strings. 320 331 $exclude_fields_cs = wp_parse_id_list( $r['exclude_fields'] ); 321 332 322 333 // Visibility - Handled here so as not to be overridden by sloppy use of the 323 // exclude_fields parameter. See bp_xprofile_get_hidden_fields_for_user() 334 // exclude_fields parameter. See bp_xprofile_get_hidden_fields_for_user(). 324 335 $hidden_user_fields = bp_xprofile_get_hidden_fields_for_user( $r['user_id'] ); 325 336 $exclude_fields_cs = array_merge( $exclude_fields_cs, $hidden_user_fields ); … … 358 369 } 359 370 360 // Fetch the fields 371 // Fetch the fields. 361 372 $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} {$in_sql} ORDER BY field_order" ); 362 373 363 374 $field_ids = wp_list_pluck( $fields, 'id' ); 364 375 365 // Store field IDs for meta cache priming 376 // Store field IDs for meta cache priming. 366 377 $object_ids['field'] = $field_ids; 367 378 368 // Bail if no fields 379 // Bail if no fields. 369 380 if ( empty( $fields ) ) { 370 381 return $groups; 371 382 } 372 383 373 // Maybe fetch field data 384 // Maybe fetch field data. 374 385 if ( ! empty( $r['fetch_field_data'] ) ) { 375 386 376 // Get field data for user ID 387 // Get field data for user ID. 377 388 if ( ! empty( $field_ids ) && ! empty( $r['user_id'] ) ) { 378 389 $field_data = BP_XProfile_ProfileData::get_data_for_user( $r['user_id'], $field_ids ); 379 390 } 380 391 381 // Remove data-less fields, if necessary 392 // Remove data-less fields, if necessary. 382 393 if ( ! empty( $r['hide_empty_fields'] ) && ! empty( $field_ids ) && ! empty( $field_data ) ) { 383 394 … … 385 396 foreach( (array) $field_data as $data ) { 386 397 387 // Empty fields may contain a serialized empty array 398 // Empty fields may contain a serialized empty array. 388 399 $maybe_value = maybe_unserialize( $data->value ); 389 400 390 // Valid field values of 0 or '0' get caught by empty(), so we have an extra check for these. See #BP5731 401 // Valid field values of 0 or '0' get caught by empty(), so we have an extra check for these. See #BP5731. 391 402 if ( ( ! empty( $maybe_value ) || '0' == $maybe_value ) && false !== $key = array_search( $data->field_id, $field_ids ) ) { 392 403 393 // Fields that have data get removed from the list 404 // Fields that have data get removed from the list. 394 405 unset( $field_ids[ $key ] ); 395 406 } … … 403 414 } 404 415 405 // Reset indexes 416 // Reset indexes. 406 417 $fields = array_values( $fields ); 407 418 } 408 419 409 // Field data was found 420 // Field data was found. 410 421 if ( ! empty( $fields ) && ! empty( $field_data ) && ! is_wp_error( $field_data ) ) { 411 422 412 // Loop through fields 423 // Loop through fields. 413 424 foreach( (array) $fields as $field_key => $field ) { 414 425 415 // Loop through the data in each field 426 // Loop through the data in each field. 416 427 foreach( (array) $field_data as $data ) { 417 428 418 // Assign correct data value to the field 429 // Assign correct data value to the field. 419 430 if ( $field->id == $data->field_id ) { 420 431 $fields[ $field_key ]->data = new stdClass; … … 423 434 } 424 435 425 // Store for meta cache priming 436 // Store for meta cache priming. 426 437 $object_ids['data'][] = $data->id; 427 438 } … … 430 441 } 431 442 432 // Prime the meta cache, if necessary 443 // Prime the meta cache, if necessary. 433 444 if ( ! empty( $r['update_meta_cache'] ) ) { 434 445 bp_xprofile_update_meta_cache( $object_ids ); 435 446 } 436 447 437 // Maybe fetch visibility levels 448 // Maybe fetch visibility levels. 438 449 if ( ! empty( $r['fetch_visibility_level'] ) ) { 439 450 $fields = self::fetch_visibility_level( $r['user_id'], $fields ); 440 451 } 441 452 442 // Merge the field array back in with the group array 453 // Merge the field array back in with the group array. 443 454 foreach( (array) $groups as $group ) { 444 455 445 456 // Indexes may have been shifted after previous deletions, so we get a 446 // fresh one each time through the loop 457 // fresh one each time through the loop. 447 458 $index = array_search( $group, $groups ); 448 459 … … 459 470 } 460 471 461 // Reset indexes 472 // Reset indexes. 462 473 $groups = array_values( $groups ); 463 474 } … … 472 483 * 473 484 * @param array $group_ids Array of IDs. 485 * 474 486 * @return array 475 487 */ … … 477 489 global $wpdb; 478 490 479 // Bail if no group IDs are passed 491 // Bail if no group IDs are passed. 480 492 if ( empty( $group_ids ) ) { 481 493 return array(); 482 494 } 483 495 484 // Setup empty arrays 496 // Setup empty arrays. 485 497 $groups = array(); 486 498 $uncached_gids = array(); 487 499 488 // Loop through groups and look for cached & uncached data 500 // Loop through groups and look for cached & uncached data. 489 501 foreach ( $group_ids as $group_id ) { 490 502 491 // If cached data is found, use it 503 // If cached data is found, use it. 492 504 $group_data = wp_cache_get( $group_id, 'bp_xprofile_groups' ); 493 505 if ( false !== $group_data ) { 494 506 $groups[ $group_id ] = $group_data; 495 507 496 // Otherwise leave a placeholder so we don't lose the order 508 // Otherwise leave a placeholder so we don't lose the order. 497 509 } else { 498 510 $groups[ $group_id ] = ''; 499 511 500 // Add to the list of items to be queried 512 // Add to the list of items to be queried. 501 513 $uncached_gids[] = $group_id; 502 514 } 503 515 } 504 516 505 // Fetch uncached data from the DB if necessary 517 // Fetch uncached data from the DB if necessary. 506 518 if ( ! empty( $uncached_gids ) ) { 507 519 508 // Setup IN query for uncached group data 520 // Setup IN query for uncached group data. 509 521 $uncached_gids_sql = implode( ',', wp_parse_id_list( $uncached_gids ) ); 510 522 511 // Get table name to query 523 // Get table name to query. 512 524 $table_name = buddypress()->profile->table_name_groups; 513 525 514 // Fetch data, preserving order 526 // Fetch data, preserving order. 515 527 $queried_gdata = $wpdb->get_results( "SELECT * FROM {$table_name} WHERE id IN ({$uncached_gids_sql}) ORDER BY FIELD( id, {$uncached_gids_sql} )"); 516 528 517 // Make sure query returned valid data 529 // Make sure query returned valid data. 518 530 if ( ! empty( $queried_gdata ) && ! is_wp_error( $queried_gdata ) ) { 519 531 520 532 // Put queried data into the placeholders created earlier, 521 // and add it to the cache 533 // and add it to the cache. 522 534 foreach ( (array) $queried_gdata as $gdata ) { 523 535 524 // Add group to groups array 536 // Add group to groups array. 525 537 $groups[ $gdata->id ] = $gdata; 526 538 527 // Cache previously uncached group data 539 // Cache previously uncached group data. 528 540 wp_cache_set( $gdata->id, $gdata, 'bp_xprofile_groups' ); 529 541 } … … 531 543 } 532 544 533 // Reset indexes & return 545 // Reset indexes & return. 534 546 return array_values( $groups ); 535 547 } 536 548 537 549 /** 538 * Validate field group when form submitted 550 * Validate field group when form submitted. 539 551 * 540 552 * @since 1.0.0 … … 547 559 global $message; 548 560 549 // Validate Form 561 // Validate Form. 550 562 if ( empty( $_POST['group_name'] ) ) { 551 563 $message = __( 'Please make sure you give the group a name.', 'buddypress' ); … … 557 569 558 570 /** 559 * Update field group position 571 * Update field group position. 560 572 * 561 573 * @since 1.5.0 562 574 * 563 575 * @global $wpdb $wpdb 564 * @param int $field_group_id 565 * @param int $position 576 * @param int $field_group_id ID of the group the field belongs to. 577 * @param int $position Field group position. 566 578 * 567 579 * @return boolean … … 574 586 } 575 587 576 // Purge profile field group cache 588 // Purge profile field group cache. 577 589 wp_cache_delete( 'all', 'bp_xprofile_groups' ); 578 590 … … 583 595 584 596 /** 585 * Fetch the field visibility level for the fields returned by the query 597 * Fetch the field visibility level for the fields returned by the query. 586 598 * 587 599 * @since 1.6.0 588 600 * 589 * @param int $user_id The profile owner's user_id 590 * @param array $fields The database results returned by the get() query 601 * @param int $user_id The profile owner's user_id. 602 * @param array $fields The database results returned by the get() query. 603 * 591 604 * @return array $fields The database results, with field_visibility added 592 605 */ 593 606 public static function fetch_visibility_level( $user_id = 0, $fields = array() ) { 594 607 595 // Get the user's visibility level preferences 608 // Get the user's visibility level preferences. 596 609 $visibility_levels = bp_get_user_meta( $user_id, 'bp_xprofile_visibility_levels', true ); 597 610 … … 602 615 $allow_custom = (bool) ( 'disabled' !== $visibility ); 603 616 604 // Look to see if the user has set the visibility for this field 617 // Look to see if the user has set the visibility for this field. 605 618 if ( ( true === $allow_custom ) && isset( $visibility_levels[ $field->id ] ) ) { 606 619 $field_visibility = $visibility_levels[ $field->id ]; 607 620 608 // If no admin-set default is saved, fall back on a global default 621 // If no admin-set default is saved, fall back on a global default. 609 622 } else { 610 623 $fallback_visibility = bp_xprofile_get_meta( $field->id, 'field', 'default_visibility' ); … … 633 646 * @since 1.6.0 634 647 * 635 * @return array $default_visibility_levels An array, keyed by 636 * field_id, of default visibility level + allow_custom 637 * (whether the admin allows this field to be set by user) 648 * @return array $default_visibility_levels An array, keyed by field_id, of default 649 * visibility level + allow_custom 650 * (whether the admin allows this 651 * field to be set by user) 638 652 */ 639 653 public static function fetch_default_visibility_levels() { … … 647 661 $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' )" ); 648 662 649 // Arrange so that the field id is the key and the visibility level the value 663 // Arrange so that the field id is the key and the visibility level the value. 650 664 $default_visibility_levels = array(); 651 665 foreach ( $levels as $level ) { … … 669 683 670 684 /** 671 * Output the admin area field group form 685 * Output the admin area field group form. 672 686 * 673 687 * @since 1.0.0 … … 678 692 global $message; 679 693 680 // New field group 694 // New field group. 681 695 if ( empty( $this->id ) ) { 682 696 $title = __( 'Add New Field Group', 'buddypress' ); … … 684 698 $button = __( 'Save', 'buddypress' ); 685 699 686 // Existing field group 700 // Existing field group. 687 701 } else { 688 702 $title = __( 'Edit Field Group', 'buddypress' );
Note: See TracChangeset
for help on using the changeset viewer.