- Timestamp:
- 08/09/2024 01:13:58 PM (4 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/bp-xprofile/classes/class-bp-xprofile-group.php
r13890 r14000 91 91 92 92 // Get this group. 93 $group = self::get( array( 94 'profile_group_id' => $id, 95 ) ); 93 $group = self::get( 94 array( 95 'profile_group_id' => $id, 96 ) 97 ); 96 98 97 99 // Bail if group not found. … … 124 126 125 127 // Filter the field group attributes. 126 $this->name = apply_filters( 'xprofile_group_name_before_save', $this->name,$this->id );128 $this->name = apply_filters( 'xprofile_group_name_before_save', $this->name, $this->id ); 127 129 $this->description = apply_filters( 'xprofile_group_description_before_save', $this->description, $this->id ); 128 130 … … 240 242 * @since 8.0.0 Introduced `$hide_field_types` & `$signup_fields_only` arguments. 241 243 * @since 11.0.0 `$profile_group_id` accepts an array of profile group ids. 244 * @since 15.0.0 Introduced the `cache_results` parameter. 242 245 * 243 246 * @global wpdb $wpdb WordPress database object. … … 246 249 * Array of optional arguments. 247 250 * 248 * @type int|int[]|bool $profile_group_id Limit results to a single profile group or a comma-separated list or array of249 * profile group ids. Default: false.250 * @type int $user_id Required if you want to load a specific user's data.251 * Default: displayed user's ID.252 * @type array|string $member_type Limit fields by those restricted to a given member type, or array of253 * member types. If `$user_id` is provided, the value of `$member_type`254 * will be overridden by the member types of the provided user. The255 * special value of 'any' will return only those fields that are256 * unrestricted by member type - i.e., those applicable to any type.257 * @type bool $hide_empty_groups True to hide groups that don't have any fields. Default: false.258 * @type bool $hide_empty_fields True to hide fields where the user has not provided data.259 * 260 * @type bool $fetch_fields Whether to fetch each group's fields. Default: false.261 * @type bool $fetch_field_data Whether to fetch data for each field. Requires a $user_id.262 * Default: false.263 * @type int[]|bool $exclude_ groups Comma-separated list or array of groupIDs to exclude.264 * @type int[]|bool $exclude_fields Comma-separated list or array of field IDs to exclude.265 * @type string[] $hide_field_types List of field types to hide form loop. Default: empty array.266 * @type bool $ signup_fields_only Whether to only return signup fields. Default: false.267 * @type bool $update_meta_cache Whether to pre-fetch xprofilemeta for all retrieved groups, fields,268 * and data. Default: true.251 * @type int|int[]|bool $profile_group_id Limit results to a single profile group or a comma-separated list or array of 252 * profile group ids. Default: false. 253 * @type int $user_id Required if you want to load a specific user's data. 254 * Default: displayed user's ID. 255 * @type array|string $member_type Limit fields by those restricted to a given member type, or array of 256 * member types. If `$user_id` is provided, the value of `$member_type` 257 * will be overridden by the member types of the provided user. The 258 * special value of 'any' will return only those fields that are 259 * unrestricted by member type - i.e., those applicable to any type. 260 * @type bool $hide_empty_groups True to hide groups that don't have any fields. Default: false. 261 * @type bool $hide_empty_fields True to hide fields where the user has not provided data. Default: false. 262 * @type bool $fetch_visibility_level Whether to fetch the visibility level for each field. Default: false. 263 * @type bool $fetch_fields Whether to fetch each group's fields. Default: false. 264 * @type bool $fetch_field_data Whether to fetch data for each field. Requires a $user_id. Default: false. 265 * @type int[]|bool $exclude_groups Comma-separated list or array of group IDs to exclude. 266 * @type int[]|bool $exclude_fields Comma-separated list or array of field IDs to exclude. 267 * @type string[] $hide_field_types List of field types to hide form loop. Default: empty array. 268 * @type bool $signup_fields_only Whether to only return signup fields. Default: false. 269 * @type bool $cache_results Whether to cache the XProfile group information. Default: true. 270 * @type bool $update_meta_cache Whether to pre-fetch xprofilemeta for all retrieved groups, fields, 271 * and data. Default: true. 269 272 * } 270 273 * @return array … … 286 289 'fetch_visibility_level' => false, 287 290 'exclude_groups' => false, 291 'cache_results' => true, 288 292 'exclude_fields' => false, 289 293 'hide_field_types' => array(), … … 305 309 306 310 // Get all group data. 307 $groups = self::get_group_data( $group_ids );311 $groups = self::get_group_data( $group_ids, $r ); 308 312 309 313 // Bail if not also getting fields. … … 337 341 338 342 // Prime the field cache. 339 $uncached_field_ids = bp_get_non_cached_ids( $field_ids, 'bp_xprofile_fields' ); 340 if ( ! empty( $uncached_field_ids ) ) { 341 $_uncached_field_ids = implode( ',', array_map( 'intval', $uncached_field_ids ) ); 342 $uncached_fields = $wpdb->get_results( "SELECT * FROM {$bp->profile->table_name_fields} WHERE id IN ({$_uncached_field_ids})" ); 343 foreach ( $uncached_fields as $uncached_field ) { 344 $fid = intval( $uncached_field->id ); 345 wp_cache_set( $fid, $uncached_field, 'bp_xprofile_fields' ); 343 if ( $r['cache_results'] ) { 344 $uncached_field_ids = bp_get_non_cached_ids( $field_ids, 'bp_xprofile_fields' ); 345 if ( ! empty( $uncached_field_ids ) ) { 346 $_uncached_field_ids = implode( ',', array_map( 'intval', $uncached_field_ids ) ); 347 $uncached_fields = $wpdb->get_results( "SELECT * FROM {$bp->profile->table_name_fields} WHERE id IN ({$_uncached_field_ids})" ); 348 foreach ( $uncached_fields as $uncached_field ) { 349 $fid = intval( $uncached_field->id ); 350 wp_cache_set( $fid, $uncached_field, 'bp_xprofile_fields' ); 351 } 346 352 } 347 353 } … … 386 392 $maybe_value = maybe_unserialize( $data->value ); 387 393 394 $key = array_search( $data->field_id, $field_ids, true ); 395 388 396 // Valid field values of 0 or '0' get caught by empty(), so we have an extra check for these. See #BP5731. 389 if ( ( ! empty( $maybe_value ) || '0' == $maybe_value ) && false !== $key = array_search( $data->field_id, $field_ids )) {397 if ( ( ( ! empty( $maybe_value ) || '0' === $maybe_value ) ) && false !== $key ) { 390 398 391 399 // Fields that have data get removed from the list. … … 396 404 // The remaining members of $field_ids are empty. Remove them. 397 405 foreach ( $fields as $field_key => $field ) { 398 if ( in_array( $field->id, $field_ids ) ) {406 if ( in_array( $field->id, $field_ids, true ) ) { 399 407 unset( $fields[ $field_key ] ); 400 408 } … … 415 423 416 424 // Assign correct data value to the field. 417 if ( $field->id == $data->field_id ) {418 $fields[ $field_key ]->data = new stdClass ;425 if ( $field->id === $data->field_id ) { 426 $fields[ $field_key ]->data = new stdClass(); 419 427 $fields[ $field_key ]->data->value = $data->value; 420 428 $fields[ $field_key ]->data->id = $data->id; … … 442 450 // Indexes may have been shifted after previous deletions, so we get a 443 451 // fresh one each time through the loop. 444 $index = array_search( $group, $groups );452 $index = array_search( $group, $groups, true ); 445 453 446 454 foreach ( (array) $fields as $field ) { … … 468 476 * @since 5.0.0 469 477 * @since 11.0.0 `$profile_group_id` accepts an array of profile group ids. 478 * @since 15.0.0 Introduced the `cache_results` parameter. 470 479 * 471 480 * @global wpdb $wpdb WordPress database object. … … 475 484 * 476 485 * @type int|int[]|bool $profile_group_id Limit results to a single profile group or a comma-separated list or array of 477 * profile group ids. Default: false.486 * profile group ids. Default: false. 478 487 * @type int[] $exclude_groups Comma-separated list or array of group IDs to exclude. Default: false. 479 488 * @type bool $hide_empty_groups True to hide groups that don't have any fields. Default: false. 489 * @type bool $cache_results Whether to cache the XProfile group information. Default: true. 480 490 * } 481 491 * @return array … … 484 494 global $wpdb; 485 495 486 $r = array_merge( 496 $r = bp_parse_args( 497 $args, 487 498 array( 488 499 'profile_group_id' => false, 489 500 'exclude_groups' => false, 490 501 'hide_empty_groups' => false, 491 ),492 $args502 'cache_results' => true, 503 ) 493 504 ); 494 505 … … 512 523 } 513 524 514 $cached = bp_core_get_incremented_cache( $group_ids_sql, 'bp_xprofile_groups' ); 515 if ( false === $cached ) { 525 if ( $r['cache_results'] ) { 526 $cached = bp_core_get_incremented_cache( $group_ids_sql, 'bp_xprofile_groups' ); 527 if ( false === $cached ) { 528 $group_ids = $wpdb->get_col( $group_ids_sql ); 529 bp_core_set_incremented_cache( $group_ids_sql, 'bp_xprofile_groups', $group_ids ); 530 } else { 531 $group_ids = $cached; 532 } 533 } else { 516 534 $group_ids = $wpdb->get_col( $group_ids_sql ); 517 bp_core_set_incremented_cache( $group_ids_sql, 'bp_xprofile_groups', $group_ids );518 } else {519 $group_ids = $cached;520 535 } 521 536 … … 527 542 * 528 543 * @since 5.0.0 544 * @since 15.0.0 Added support for the `cache_results` parameter. 529 545 * 530 546 * @global wpdb $wpdb WordPress database object. 531 547 * 532 * @param array $group_ids Array of group IDs. 533 * @param array $args { 534 * Array of optional arguments: 535 * @type array $exclude_fields Comma-separated list or array of field IDs to exclude. 536 * Default empty. 537 * @type int $user_id Limit results to fields associated with a given user's 538 * member type. Default empty. 539 * @type array|string $member_type Limit fields by those restricted to a given member type, or array of 540 * member types. If `$user_id` is provided, the value of `$member_type` 541 * is honored. 542 * } 543 * @return array 548 * @param int[] $group_ids Array of group IDs. 549 * @param array $args Optional. See {@link BP_XProfile_Group::get()}. 550 * @return int[] 544 551 */ 545 552 public static function get_group_field_ids( $group_ids, $args = array() ) { 546 553 global $wpdb; 547 554 548 $r = array_merge( 555 $r = bp_parse_args( 556 $args, 549 557 array( 550 558 'exclude_fields' => false, 551 'user_id' => false,552 'member_type' => false,553 ),554 $args559 'user_id' => false, 560 'member_type' => false, 561 'cache_results' => true, 562 ) 555 563 ); 556 564 … … 561 569 $group_ids = array( 0 ); 562 570 } 571 563 572 $group_ids_in = implode( ',', array_map( 'intval', $group_ids ) ); 564 573 … … 594 603 595 604 $member_types_fields = BP_XProfile_Field::get_fields_for_member_type( $member_types ); 596 $include_field_ids += array_keys( $member_types_fields );605 $include_field_ids += array_keys( $member_types_fields ); 597 606 } 598 607 } … … 601 610 if ( ! empty( $include_field_ids ) ) { 602 611 $include_field_ids_cs = implode( ',', array_map( 'intval', $include_field_ids ) ); 603 $in_sql = " AND id IN ({$include_field_ids_cs}) ";612 $in_sql = " AND id IN ({$include_field_ids_cs}) "; 604 613 } 605 614 606 615 $field_ids_sql = "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"; 607 616 608 $cached = bp_core_get_incremented_cache( $field_ids_sql, 'bp_xprofile_groups' ); 609 if ( false === $cached ) { 617 if ( $r['cache_results'] ) { 618 $cached = bp_core_get_incremented_cache( $field_ids_sql, 'bp_xprofile_groups' ); 619 if ( false === $cached ) { 620 $field_ids = $wpdb->get_col( $field_ids_sql ); 621 bp_core_set_incremented_cache( $field_ids_sql, 'bp_xprofile_groups', $field_ids ); 622 } else { 623 $field_ids = $cached; 624 } 625 } else { 610 626 $field_ids = $wpdb->get_col( $field_ids_sql ); 611 bp_core_set_incremented_cache( $field_ids_sql, 'bp_xprofile_groups', $field_ids );612 } else {613 $field_ids = $cached;614 627 } 615 628 … … 621 634 * 622 635 * @since 2.0.0 636 * @since 15.0.0 Added additional `$r` parameter. 623 637 * 624 638 * @global wpdb $wpdb WordPress database object. 625 639 * 626 640 * @param array $group_ids Array of IDs. 641 * @param array $r Optional. See {@link BP_XProfile_Group::get()}. 627 642 * @return array 628 643 */ 629 protected static function get_group_data( $group_ids ) {644 protected static function get_group_data( $group_ids, $r = array() ) { 630 645 global $wpdb; 631 646 … … 634 649 return array(); 635 650 } 651 652 $r = bp_parse_args( 653 $r, 654 array( 'cache_results' => true ) 655 ); 636 656 637 657 // Setup empty arrays. … … 647 667 $groups[ $group_id ] = $group_data; 648 668 649 // Otherwise leave a placeholder so we don't lose the order.669 // Otherwise leave a placeholder so we don't lose the order. 650 670 } else { 651 671 $groups[ $group_id ] = ''; … … 666 686 667 687 // Fetch data, preserving order. 668 $queried_gdata = $wpdb->get_results( "SELECT * FROM {$table_name} WHERE id IN ({$uncached_gids_sql}) ORDER BY FIELD( id, {$uncached_gids_sql} )" );688 $queried_gdata = $wpdb->get_results( "SELECT * FROM {$table_name} WHERE id IN ({$uncached_gids_sql}) ORDER BY FIELD( id, {$uncached_gids_sql} )" ); 669 689 670 690 // Make sure query returned valid data. … … 679 699 680 700 // Cache previously uncached group data. 681 wp_cache_set( $gdata->id, $gdata, 'bp_xprofile_groups' ); 701 if ( $r['cache_results'] ) { 702 wp_cache_set( $gdata->id, $gdata, 'bp_xprofile_groups' ); 703 } 682 704 } 683 705 } … … 767 789 $field_visibility = $visibility_levels[ $field->id ]; 768 790 769 // If no admin-set default is saved, fall back on a global default.791 // If no admin-set default is saved, fall back on a global default. 770 792 } else { 771 793 $fallback_visibility = bp_xprofile_get_meta( $field->id, 'field', 'default_visibility' ); … … 815 837 foreach ( $levels as $level ) { 816 838 switch ( $level->meta_key ) { 817 case 'default_visibility' 818 $default_visibility_levels[ $level->object_id ]['default'] 839 case 'default_visibility': 840 $default_visibility_levels[ $level->object_id ]['default'] = $level->meta_value; 819 841 break; 820 case 'allow_custom_visibility' 842 case 'allow_custom_visibility': 821 843 $default_visibility_levels[ $level->object_id ]['allow_custom'] = $level->meta_value; 822 844 break; … … 846 868 847 869 // URL to cancel to. 848 $cancel_url = add_query_arg( array( 849 'page' => 'bp-profile-setup', 850 ), $users_url ); 870 $cancel_url = add_query_arg( 871 array( 872 'page' => 'bp-profile-setup', 873 ), 874 $users_url 875 ); 851 876 852 877 // New field group. 853 878 if ( empty( $this->id ) ) { 854 $title 855 $button = __( 'Save','buddypress' );856 $action 879 $title = __( 'Add New Field Group', 'buddypress' ); 880 $button = __( 'Save', 'buddypress' ); 881 $action = add_query_arg( 857 882 array( 858 883 'page' => 'bp-profile-setup', … … 863 888 $description = ''; 864 889 865 // Existing field group.890 // Existing field group. 866 891 } else { 867 892 $title = __( 'Edit Field Group', 'buddypress' ); 868 $button = __( 'Update','buddypress' );869 $action 893 $button = __( 'Update', 'buddypress' ); 894 $action = add_query_arg( 870 895 array( 871 896 'page' => 'bp-profile-setup', … … 879 904 if ( $this->can_delete ) { 880 905 // Delete Group URL. 881 $delete_url = wp_nonce_url( add_query_arg( array( 882 'page' => 'bp-profile-setup', 883 'mode' => 'delete_group', 884 'group_id' => (int) $this->id, 885 ), $users_url ), 'bp_xprofile_delete_group' ); 906 $delete_url = wp_nonce_url( 907 add_query_arg( 908 array( 909 'page' => 'bp-profile-setup', 910 'mode' => 'delete_group', 911 'group_id' => (int) $this->id, 912 ), 913 $users_url 914 ), 915 'bp_xprofile_delete_group' 916 ); 886 917 } 887 918 } ?> … … 906 937 <div id="titlediv"> 907 938 <div class="titlewrap"> 908 <label id="title-prompt-text" for="title"><?php esc_html_e( 'Field Group Name (required)', 'buddypress' )?></label>939 <label id="title-prompt-text" for="title"><?php esc_html_e( 'Field Group Name (required)', 'buddypress' ); ?></label> 909 940 <input type="text" name="group_name" id="title" value="<?php echo esc_attr( $this->name ); ?>" autocomplete="off" /> 910 941 </div> … … 932 963 * @param BP_XProfile_Group $field_group Current instance of the field group. 933 964 */ 934 do_action( 'xprofile_group_admin_after_description', $this ); ?> 965 do_action( 'xprofile_group_admin_after_description', $this ); 966 ?> 935 967 936 968 </div><!-- #post-body-content --> … … 947 979 * @param BP_XProfile_Group $field_group Current instance of the field group. 948 980 */ 949 do_action( 'xprofile_group_before_submitbox', $this ); ?> 981 do_action( 'xprofile_group_before_submitbox', $this ); 982 ?> 950 983 951 984 <div id="submitdiv" class="postbox"> … … 967 1000 * @param BP_XProfile_Group $field_group Current instance of the field group. 968 1001 */ 969 do_action( 'xprofile_group_submitbox_start', $this ); ?> 1002 do_action( 'xprofile_group_submitbox_start', $this ); 1003 ?> 970 1004 971 1005 <input type="hidden" name="group_order" id="group_order" value="<?php echo esc_attr( $this->group_order ); ?>" /> … … 995 1029 * @param BP_XProfile_Group $field_group Current instance of the field group. 996 1030 */ 997 do_action( 'xprofile_group_after_submitbox', $this ); ?> 1031 do_action( 'xprofile_group_after_submitbox', $this ); 1032 ?> 998 1033 999 1034 </div> … … 1003 1038 </div> 1004 1039 1005 <?php1040 <?php 1006 1041 } 1007 1042 }
Note: See TracChangeset
for help on using the changeset viewer.