Changeset 14000
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 } -
trunk/tests/phpunit/testcases/xprofile/class-bp-xprofile-group.php
r13358 r14000 11 11 */ 12 12 public function test_save_should_not_return_false_when_no_fields_have_been_altered() { 13 $g = self::factory()->xprofile_group->create();13 $g = self::factory()->xprofile_group->create(); 14 14 $group = new BP_XProfile_Group( $g ); 15 15 … … 22 22 * @ticket BP7916 23 23 */ 24 public function test_delete () {24 public function test_delete_xprofile_group() { 25 25 $g = self::factory()->xprofile_group->create(); 26 26 … … 43 43 $u = self::factory()->user->create(); 44 44 $g = self::factory()->xprofile_group->create(); 45 $f = self::factory()->xprofile_field->create( array( 46 'field_group_id' => $g, 47 ) ); 45 $f = self::factory()->xprofile_field->create( 46 array( 47 'field_group_id' => $g, 48 ) 49 ); 48 50 49 51 $f_obj = new BP_XProfile_Field( $f ); 50 52 51 53 $fields = array( 52 0 => new stdClass ,53 ); 54 55 $fields[0]->id = $f;56 $fields[0]->name = $f_obj->name;54 0 => new stdClass(), 55 ); 56 57 $fields[0]->id = $f; 58 $fields[0]->name = $f_obj->name; 57 59 $fields[0]->description = $f_obj->description; 58 $fields[0]->type = $f_obj->type;59 $fields[0]->group_id = $f_obj->group_id;60 $fields[0]->type = $f_obj->type; 61 $fields[0]->group_id = $f_obj->group_id; 60 62 $fields[0]->is_required = $f_obj->is_required; 61 $fields[0]->data = new stdClass;63 $fields[0]->data = new stdClass(); 62 64 $fields[0]->data->value = 'foo'; 63 $fields[0]->data->id = 123;65 $fields[0]->data->id = 123; 64 66 65 67 // custom visibility enabled, but no fallback … … 69 71 $found = BP_XProfile_Group::fetch_visibility_level( $u, $fields ); 70 72 71 $expected = $fields;73 $expected = $fields; 72 74 $expected[0]->visibility_level = 'adminsonly'; 73 75 … … 81 83 $found = BP_XProfile_Group::fetch_visibility_level( $u, $fields ); 82 84 83 $expected = $fields;85 $expected = $fields; 84 86 $expected[0]->visibility_level = 'public'; 85 87 … … 93 95 $found = BP_XProfile_Group::fetch_visibility_level( $u, $fields ); 94 96 95 $expected = $fields;97 $expected = $fields; 96 98 $expected[0]->visibility_level = 'adminsonly'; 97 99 … … 107 109 $g3 = self::factory()->xprofile_group->create(); 108 110 109 $all = BP_XProfile_Group::get();111 $all = BP_XProfile_Group::get(); 110 112 $all_results = array_map( 'absint', wp_list_pluck( $all, 'id' ) ); 111 113 112 $e1 = array( $g1, $g2 ); 113 $groups1 = BP_XProfile_Group::get( array( 114 'exclude_groups' => implode( ',', $e1 ), 115 ) ); 114 $e1 = array( $g1, $g2 ); 115 $groups1 = BP_XProfile_Group::get( 116 array( 117 'exclude_groups' => implode( ',', $e1 ), 118 ) 119 ); 116 120 117 121 $r_groups1 = array_map( 'absint', wp_list_pluck( $groups1, 'id' ) ); 118 $found1 = array_diff( $all_results, $r_groups1 );122 $found1 = array_diff( $all_results, $r_groups1 ); 119 123 120 124 $this->assertSame( $e1, array_merge( $found1, array() ) ); 121 125 122 $e2 = array( $g2, $g3 ); 123 $groups2 = BP_XProfile_Group::get( array( 124 'exclude_groups' => $e2, 125 ) ); 126 $e2 = array( $g2, $g3 ); 127 $groups2 = BP_XProfile_Group::get( 128 array( 129 'exclude_groups' => $e2, 130 ) 131 ); 126 132 127 133 $r_groups2 = array_map( 'absint', wp_list_pluck( $groups2, 'id' ) ); 128 $found2 = array_diff( $all_results, $r_groups2 );134 $found2 = array_diff( $all_results, $r_groups2 ); 129 135 130 136 $this->assertSame( $e2, array_merge( $found2, array() ) ); … … 139 145 $g2 = self::factory()->xprofile_group->create(); 140 146 $g3 = self::factory()->xprofile_group->create(); 141 $e1 = [ $g1, $g2 ];147 $e1 = array( $g1, $g2 ); 142 148 143 149 // Comma-separated list of profile group ids. 144 $groups1 = BP_XProfile_Group::get( [ 'profile_group_id' => join( ',', $e1 ) ]);150 $groups1 = BP_XProfile_Group::get( array( 'profile_group_id' => join( ',', $e1 ) ) ); 145 151 146 152 $this->assertSame( $e1, array_map( 'absint', wp_list_pluck( $groups1, 'id' ) ) ); 147 153 148 154 // Array of profile group ids. 149 $groups2 = BP_XProfile_Group::get( [ 'profile_group_id' => $e1 ]);155 $groups2 = BP_XProfile_Group::get( array( 'profile_group_id' => $e1 ) ); 150 156 151 157 $this->assertSame( $e1, array_map( 'absint', wp_list_pluck( $groups2, 'id' ) ) ); … … 164 170 $field->set_member_types( array( 'foo' ) ); 165 171 166 $found_groups = BP_XProfile_Group::get( array( 167 'user_id' => false, 168 'fetch_fields' => true, 169 ) ); 172 $found_groups = BP_XProfile_Group::get( 173 array( 174 'user_id' => false, 175 'fetch_fields' => true, 176 ) 177 ); 170 178 171 179 // The groups aren't indexed, so we have to go looking for it. … … 191 199 $field->set_member_types( array( 'foo' ) ); 192 200 193 $found_groups = BP_XProfile_Group::get( array( 194 'user_id' => 0, 195 'member_type' => false, 196 'fetch_fields' => true, 197 ) ); 201 $found_groups = BP_XProfile_Group::get( 202 array( 203 'user_id' => 0, 204 'member_type' => false, 205 'fetch_fields' => true, 206 ) 207 ); 198 208 199 209 // The groups aren't indexed, so we have to go looking for it. … … 212 222 */ 213 223 public function test_member_type_restrictions_should_be_obeyed_for_nonzero_user_id() { 214 $g = self::factory()->xprofile_group->create();224 $g = self::factory()->xprofile_group->create(); 215 225 $f1 = self::factory()->xprofile_field->create( array( 'field_group_id' => $g ) ); 216 226 $f2 = self::factory()->xprofile_field->create( array( 'field_group_id' => $g ) ); … … 238 248 bp_set_member_type( $u, 'foo' ); 239 249 240 $found_groups = BP_XProfile_Group::get( array( 241 'user_id' => $u, 242 'fetch_fields' => true, 243 ) ); 250 $found_groups = BP_XProfile_Group::get( 251 array( 252 'user_id' => $u, 253 'fetch_fields' => true, 254 ) 255 ); 244 256 245 257 // The groups aren't indexed, so we have to go looking for it. … … 261 273 */ 262 274 public function test_member_type_restrictions_should_be_obeyed_for_nonzero_user_id_with_no_member_types() { 263 $g = self::factory()->xprofile_group->create();275 $g = self::factory()->xprofile_group->create(); 264 276 $f1 = self::factory()->xprofile_field->create( array( 'field_group_id' => $g ) ); 265 277 $f2 = self::factory()->xprofile_field->create( array( 'field_group_id' => $g ) ); … … 281 293 $u = self::factory()->user->create(); 282 294 283 $found_groups = BP_XProfile_Group::get( array( 284 'user_id' => $u, 285 'fetch_fields' => true, 286 ) ); 295 $found_groups = BP_XProfile_Group::get( 296 array( 297 'user_id' => $u, 298 'fetch_fields' => true, 299 ) 300 ); 287 301 288 302 // The groups aren't indexed, so we have to go looking for it. … … 303 317 */ 304 318 public function test_member_types_of_provided_user_id_should_take_precedence_over_provided_member_type() { 305 $g = self::factory()->xprofile_group->create();319 $g = self::factory()->xprofile_group->create(); 306 320 $f1 = self::factory()->xprofile_field->create( array( 'field_group_id' => $g ) ); 307 321 $f2 = self::factory()->xprofile_field->create( array( 'field_group_id' => $g ) ); … … 317 331 bp_set_member_type( $u, 'foo' ); 318 332 319 $found_groups = BP_XProfile_Group::get( array( 320 'user_id' => $u, 321 'member_type' => 'bar', 322 'fetch_fields' => true, 323 ) ); 333 $found_groups = BP_XProfile_Group::get( 334 array( 335 'user_id' => $u, 336 'member_type' => 'bar', 337 'fetch_fields' => true, 338 ) 339 ); 324 340 325 341 // The groups aren't indexed, so we have to go looking for it. … … 338 354 */ 339 355 public function test_member_type_single_value_should_be_respected() { 340 $g = self::factory()->xprofile_group->create();356 $g = self::factory()->xprofile_group->create(); 341 357 $f1 = self::factory()->xprofile_field->create( array( 'field_group_id' => $g ) ); 342 358 $f2 = self::factory()->xprofile_field->create( array( 'field_group_id' => $g ) ); … … 349 365 $field2->set_member_types( array( 'bar' ) ); 350 366 351 $found_groups = BP_XProfile_Group::get( array( 352 'member_type' => 'bar', 353 'fetch_fields' => true, 354 ) ); 367 $found_groups = BP_XProfile_Group::get( 368 array( 369 'member_type' => 'bar', 370 'fetch_fields' => true, 371 ) 372 ); 355 373 356 374 // The groups aren't indexed, so we have to go looking for it. … … 370 388 */ 371 389 public function test_member_type_array_value_should_be_respected() { 372 $g = self::factory()->xprofile_group->create();390 $g = self::factory()->xprofile_group->create(); 373 391 $f1 = self::factory()->xprofile_field->create( array( 'field_group_id' => $g ) ); 374 392 $f2 = self::factory()->xprofile_field->create( array( 'field_group_id' => $g ) ); … … 381 399 $field2->set_member_types( array( 'bar' ) ); 382 400 383 $found_groups = BP_XProfile_Group::get( array( 384 'member_type' => array( 'bar' ), 385 'fetch_fields' => true, 386 ) ); 401 $found_groups = BP_XProfile_Group::get( 402 array( 403 'member_type' => array( 'bar' ), 404 'fetch_fields' => true, 405 ) 406 ); 387 407 388 408 // The groups aren't indexed, so we have to go looking for it. … … 402 422 */ 403 423 public function test_member_type_null_should_be_respected() { 404 $g = self::factory()->xprofile_group->create();424 $g = self::factory()->xprofile_group->create(); 405 425 $f1 = self::factory()->xprofile_field->create( array( 'field_group_id' => $g ) ); 406 426 $f2 = self::factory()->xprofile_field->create( array( 'field_group_id' => $g ) ); … … 411 431 $field1->set_member_types( array( 'foo' ) ); 412 432 413 $found_groups = BP_XProfile_Group::get( array( 414 'member_type' => array( 'null' ), 415 'fetch_fields' => true, 416 ) ); 433 $found_groups = BP_XProfile_Group::get( 434 array( 435 'member_type' => array( 'null' ), 436 'fetch_fields' => true, 437 ) 438 ); 417 439 418 440 // The groups aren't indexed, so we have to go looking for it. … … 431 453 */ 432 454 public function test_save_xprofile_group_name() { 433 $g1 = self::factory()->xprofile_group->create( array( 434 'name' => "Test ' Name" 435 ) ); 455 $g1 = self::factory()->xprofile_group->create( 456 array( 457 'name' => "Test ' Name", 458 ) 459 ); 436 460 437 461 $e1 = new BP_XProfile_Group( $g1 ); … … 454 478 455 479 // Create a group 456 $g1 = self::factory()->xprofile_group->create( array( 457 'name' => $pristine_name 458 ) ); 480 $g1 = self::factory()->xprofile_group->create( 481 array( 482 'name' => $pristine_name, 483 ) 484 ); 459 485 460 486 // Get the field … … 471 497 $g2 = self::factory()->xprofile_group->create(); 472 498 473 $group_ids = [ 1 ]; // Default group.499 $group_ids = array( 1 ); // Default group. 474 500 $group_ids[] = self::factory()->xprofile_group->create(); 475 501 $group_ids[] = self::factory()->xprofile_group->create(); … … 481 507 $this->assertEqualSets( $group_ids, $found_1 ); 482 508 483 $found_2 = BP_XProfile_Group::get_group_ids( [ 'profile_group_id' => $g1 ]);509 $found_2 = BP_XProfile_Group::get_group_ids( array( 'profile_group_id' => $g1 ) ); 484 510 $this->assertCount( 1, $found_2 ); 485 $this->assertSame( [ $g1 ], $found_2 );486 487 $found_3 = BP_XProfile_Group::get_group_ids( [ 'profile_group_id' => [ $g1 ] ]);511 $this->assertSame( array( $g1 ), $found_2 ); 512 513 $found_3 = BP_XProfile_Group::get_group_ids( array( 'profile_group_id' => array( $g1 ) ) ); 488 514 $this->assertCount( 1, $found_3 ); 489 $this->assertSame( [ $g1 ], $found_3 );490 491 $found_4 = BP_XProfile_Group::get_group_ids( [ 'profile_group_id' => [ $g2 ] ]);515 $this->assertSame( array( $g1 ), $found_3 ); 516 517 $found_4 = BP_XProfile_Group::get_group_ids( array( 'profile_group_id' => array( $g2 ) ) ); 492 518 $this->assertCount( 1, $found_4 ); 493 $this->assertSame( [ $g2 ], $found_4 );494 495 $found_5 = BP_XProfile_Group::get_group_ids( [ 'profile_group_id' => [ $g1, $g2 ] ]);519 $this->assertSame( array( $g2 ), $found_4 ); 520 521 $found_5 = BP_XProfile_Group::get_group_ids( array( 'profile_group_id' => array( $g1, $g2 ) ) ); 496 522 $this->assertCount( 2, $found_5 ); 497 $this->assertSame( [ $g1, $g2 ], $found_5 );498 499 $found_6 = BP_XProfile_Group::get_group_ids( [ 'profile_group_id' => join( ',', [ $g1, $g2 ] ) ]);523 $this->assertSame( array( $g1, $g2 ), $found_5 ); 524 525 $found_6 = BP_XProfile_Group::get_group_ids( array( 'profile_group_id' => join( ',', array( $g1, $g2 ) ) ) ); 500 526 $this->assertCount( 2, $found_6 ); 501 $this->assertSame( [ $g1, $g2 ], $found_6 );502 503 $found_7 = BP_XProfile_Group::get_group_ids( [ 'profile_group_id' => true ]);527 $this->assertSame( array( $g1, $g2 ), $found_6 ); 528 529 $found_7 = BP_XProfile_Group::get_group_ids( array( 'profile_group_id' => true ) ); 504 530 $this->assertEqualSets( $group_ids, $found_7 ); 505 531 } … … 521 547 $group_ids[] = $g2; 522 548 523 $params_1 = [ 'exclude_groups' => false ];549 $params_1 = array( 'exclude_groups' => false ); 524 550 525 551 // Prime cache. … … 534 560 535 561 // Different parameters should trigger a cache miss. 536 $found_3 = BP_XProfile_Group::get_group_ids( [ 'exclude_groups' => [ 0 ] ]);562 $found_3 = BP_XProfile_Group::get_group_ids( array( 'exclude_groups' => array( 0 ) ) ); 537 563 $this->assertEqualSets( $group_ids, $found_3 ); 538 564 $this->assertNotSame( $num_queries, $wpdb->num_queries ); 539 565 540 566 // Again, different parameters should trigger a cache miss. 541 $found_4 = BP_XProfile_Group::get_group_ids( [ 'profile_group_id' => [ $g1, $g2 ] ]);542 $this->assertEqualSets( [ $g1, $g2 ], $found_4 );567 $found_4 = BP_XProfile_Group::get_group_ids( array( 'profile_group_id' => array( $g1, $g2 ) ) ); 568 $this->assertEqualSets( array( $g1, $g2 ), $found_4 ); 543 569 $this->assertNotSame( $num_queries, $wpdb->num_queries ); 544 570 } … … 551 577 $group_ids = array( 1 ); // Default group. 552 578 $group_ids[1] = self::factory()->xprofile_group->create(); 553 $group_ids[2] = self::factory()->xprofile_group->create(); 554 $group_ids[3] = self::factory()->xprofile_group->create(); 579 580 self::factory()->xprofile_group->create(); 581 self::factory()->xprofile_group->create(); 555 582 556 583 // Prime cache. … … 574 601 $group_ids = array( 1 ); // Default group. 575 602 $group_ids[1] = self::factory()->xprofile_group->create(); 576 $group_ids[2] = self::factory()->xprofile_group->create(); 577 $group_ids[3] = self::factory()->xprofile_group->create(); 603 604 self::factory()->xprofile_group->create(); 605 self::factory()->xprofile_group->create(); 578 606 579 607 // Prime cache. … … 586 614 $num_queries = $wpdb->num_queries; 587 615 588 $found = BP_XProfile_Group::get_group_ids(); 616 BP_XProfile_Group::get_group_ids(); 617 589 618 $this->assertNotSame( $num_queries, $wpdb->num_queries ); 590 619 } … … 599 628 $group_ids = array( 1 ); // Default group. 600 629 $group_ids[1] = self::factory()->xprofile_group->create(); 601 $group_ids[2] = self::factory()->xprofile_group->create(); 602 $group_ids[3] = self::factory()->xprofile_group->create(); 603 604 $f = self::factory()->xprofile_field->create( array( 605 'field_group_id' => $group_ids[1], 606 ) ); 630 631 self::factory()->xprofile_group->create(); 632 self::factory()->xprofile_group->create(); 633 634 $f = self::factory()->xprofile_field->create( 635 array( 636 'field_group_id' => $group_ids[1], 637 ) 638 ); 607 639 $field = new BP_XProfile_Field( $f ); 608 640 … … 615 647 $num_queries = $wpdb->num_queries; 616 648 617 $found = BP_XProfile_Group::get_group_ids(); 649 BP_XProfile_Group::get_group_ids(); 650 618 651 $this->assertNotSame( $num_queries, $wpdb->num_queries ); 619 652 } … … 626 659 $group_ids = array( 1 ); // Default group. 627 660 $group_ids[1] = self::factory()->xprofile_group->create(); 628 $group_ids[2] = self::factory()->xprofile_group->create(); 629 $group_ids[3] = self::factory()->xprofile_group->create(); 630 631 $f = self::factory()->xprofile_field->create( array( 632 'field_group_id' => $group_ids[1], 633 ) ); 661 662 self::factory()->xprofile_group->create(); 663 self::factory()->xprofile_group->create(); 664 665 $f = self::factory()->xprofile_field->create( 666 array( 667 'field_group_id' => $group_ids[1], 668 ) 669 ); 634 670 $field = new BP_XProfile_Field( $f ); 635 671 … … 657 693 $group_id = self::factory()->xprofile_group->create(); 658 694 659 $f = self::factory()->xprofile_field->create( array( 660 'field_group_id' => $group_id, 661 ) ); 662 $field = new BP_XProfile_Field( $f ); 695 $f = self::factory()->xprofile_field->create( 696 array( 697 'field_group_id' => $group_id, 698 ) 699 ); 700 701 new BP_XProfile_Field( $f ); 663 702 664 703 // Prime cache. … … 682 721 $group_id = self::factory()->xprofile_group->create(); 683 722 684 $f = self::factory()->xprofile_field->create( array( 685 'field_group_id' => $group_id, 686 ) ); 723 $f = self::factory()->xprofile_field->create( 724 array( 725 'field_group_id' => $group_id, 726 ) 727 ); 687 728 $field = new BP_XProfile_Field( $f ); 688 729 … … 706 747 $group_id = self::factory()->xprofile_group->create(); 707 748 708 $f = self::factory()->xprofile_field->create( array( 709 'field_group_id' => $group_id, 710 ) ); 749 $f = self::factory()->xprofile_field->create( 750 array( 751 'field_group_id' => $group_id, 752 ) 753 ); 711 754 $field = new BP_XProfile_Field( $f ); 712 755 … … 720 763 $this->assertNotContains( $f, $found ); 721 764 } 765 766 /** 767 * @ticket BP8552 768 * @group cache 769 */ 770 public function test_group_query_getter_with_cache_results() { 771 global $wpdb; 772 773 self::factory()->xprofile_group->create_many( 2 ); 774 775 $wpdb->num_queries = 0; 776 777 $first_query = BP_XProfile_Group::get( array( 'cache_results' => true ) ); 778 779 $queries_before = get_num_queries(); 780 781 $second_query = BP_XProfile_Group::get( array( 'cache_results' => false ) ); 782 783 $queries_after = get_num_queries(); 784 785 $this->assertNotSame( $queries_before, $queries_after, 'Assert that queries are run' ); 786 $this->assertSame( 3, $queries_after, 'Assert that the uncached query was run' ); 787 $this->assertEquals( $first_query, $second_query, 'Results of the query are expected to match.' ); 788 } 722 789 }
Note: See TracChangeset
for help on using the changeset viewer.