Skip to:
Content

BuddyPress.org

Changeset 14000


Ignore:
Timestamp:
08/09/2024 01:13:58 PM (2 years ago)
Author:
espellcaste
Message:

XProfile: Add cache_results flag to the BP_XProfile_Group::get getter.

When performing a request with cache_results, it stops the XProfile group information retrieved from being added to the cache.

Props imath.

See #8552
Closes https://github.com/buddypress/buddypress/pull/357

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-xprofile/classes/class-bp-xprofile-group.php

    r13890 r14000  
    9191
    9292                // 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                );
    9698
    9799                // Bail if group not found.
     
    124126
    125127                // 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 );
    127129                $this->description = apply_filters( 'xprofile_group_description_before_save', $this->description, $this->id );
    128130
     
    240242         * @since 8.0.0  Introduced `$hide_field_types` & `$signup_fields_only` arguments.
    241243         * @since 11.0.0 `$profile_group_id` accepts an array of profile group ids.
     244         * @since 15.0.0 Introduced the `cache_results` parameter.
    242245         *
    243246         * @global wpdb $wpdb WordPress database object.
     
    246249         *      Array of optional arguments.
    247250         *
    248          *      @type int|int[]|bool $profile_group_id   Limit results to a single profile group or a comma-separated list or array of
    249          *                                               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 of
    253          *                                               member types. If `$user_id` is provided, the value of `$member_type`
    254          *                                               will be overridden by the member types of the provided user. The
    255          *                                               special value of 'any' will return only those fields that are
    256          *                                               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          *                                              Default: false.
    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 group IDs 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.
    269272         * }
    270273         * @return array
     
    286289                                'fetch_visibility_level' => false,
    287290                                'exclude_groups'         => false,
     291                                'cache_results'          => true,
    288292                                'exclude_fields'         => false,
    289293                                'hide_field_types'       => array(),
     
    305309
    306310                // Get all group data.
    307                 $groups = self::get_group_data( $group_ids );
     311                $groups = self::get_group_data( $group_ids, $r );
    308312
    309313                // Bail if not also getting fields.
     
    337341
    338342                // 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                                }
    346352                        }
    347353                }
     
    386392                                        $maybe_value = maybe_unserialize( $data->value );
    387393
     394                                        $key = array_search( $data->field_id, $field_ids, true );
     395
    388396                                        // 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 ) {
    390398
    391399                                                // Fields that have data get removed from the list.
     
    396404                                // The remaining members of $field_ids are empty. Remove them.
    397405                                foreach ( $fields as $field_key => $field ) {
    398                                         if ( in_array( $field->id, $field_ids ) ) {
     406                                        if ( in_array( $field->id, $field_ids, true ) ) {
    399407                                                unset( $fields[ $field_key ] );
    400408                                        }
     
    415423
    416424                                                // 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();
    419427                                                        $fields[ $field_key ]->data->value = $data->value;
    420428                                                        $fields[ $field_key ]->data->id    = $data->id;
     
    442450                        // Indexes may have been shifted after previous deletions, so we get a
    443451                        // fresh one each time through the loop.
    444                         $index = array_search( $group, $groups );
     452                        $index = array_search( $group, $groups, true );
    445453
    446454                        foreach ( (array) $fields as $field ) {
     
    468476         * @since 5.0.0
    469477         * @since 11.0.0 `$profile_group_id` accepts an array of profile group ids.
     478         * @since 15.0.0 Introduced the `cache_results` parameter.
    470479         *
    471480         * @global wpdb $wpdb WordPress database object.
     
    475484         *
    476485         *    @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.
    478487         *    @type int[]          $exclude_groups    Comma-separated list or array of group IDs to exclude. Default: false.
    479488         *    @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.
    480490         * }
    481491         * @return array
     
    484494                global $wpdb;
    485495
    486                 $r = array_merge(
     496                $r = bp_parse_args(
     497                        $args,
    487498                        array(
    488499                                'profile_group_id'  => false,
    489500                                'exclude_groups'    => false,
    490501                                'hide_empty_groups' => false,
    491                         ),
    492                         $args
     502                                'cache_results'     => true,
     503                        )
    493504                );
    494505
     
    512523                }
    513524
    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 {
    516534                        $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;
    520535                }
    521536
     
    527542         *
    528543         * @since 5.0.0
     544         * @since 15.0.0 Added support for the `cache_results` parameter.
    529545         *
    530546         * @global wpdb $wpdb WordPress database object.
    531547         *
    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[]
    544551         */
    545552        public static function get_group_field_ids( $group_ids, $args = array() ) {
    546553                global $wpdb;
    547554
    548                 $r = array_merge(
     555                $r = bp_parse_args(
     556                        $args,
    549557                        array(
    550558                                'exclude_fields' => false,
    551                                 'user_id' => false,
    552                                 'member_type' => false,
    553                         ),
    554                         $args
     559                                'user_id'        => false,
     560                                'member_type'    => false,
     561                                'cache_results'  => true,
     562                        )
    555563                );
    556564
     
    561569                        $group_ids = array( 0 );
    562570                }
     571
    563572                $group_ids_in = implode( ',', array_map( 'intval', $group_ids ) );
    564573
     
    594603
    595604                                $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 );
    597606                        }
    598607                }
     
    601610                if ( ! empty( $include_field_ids ) ) {
    602611                        $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}) ";
    604613                }
    605614
    606615                $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";
    607616
    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 {
    610626                        $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;
    614627                }
    615628
     
    621634         *
    622635         * @since 2.0.0
     636         * @since 15.0.0 Added additional `$r` parameter.
    623637         *
    624638         * @global wpdb $wpdb WordPress database object.
    625639         *
    626640         * @param array $group_ids Array of IDs.
     641         * @param array $r         Optional. See {@link BP_XProfile_Group::get()}.
    627642         * @return array
    628643         */
    629         protected static function get_group_data( $group_ids ) {
     644        protected static function get_group_data( $group_ids, $r = array() ) {
    630645                global $wpdb;
    631646
     
    634649                        return array();
    635650                }
     651
     652                $r = bp_parse_args(
     653                        $r,
     654                        array( 'cache_results' => true )
     655                );
    636656
    637657                // Setup empty arrays.
     
    647667                                $groups[ $group_id ] = $group_data;
    648668
    649                         // Otherwise leave a placeholder so we don't lose the order.
     669                                // Otherwise leave a placeholder so we don't lose the order.
    650670                        } else {
    651671                                $groups[ $group_id ] = '';
     
    666686
    667687                        // 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} )" );
    669689
    670690                        // Make sure query returned valid data.
     
    679699
    680700                                        // 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                                        }
    682704                                }
    683705                        }
     
    767789                                $field_visibility = $visibility_levels[ $field->id ];
    768790
    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.
    770792                        } else {
    771793                                $fallback_visibility = bp_xprofile_get_meta( $field->id, 'field', 'default_visibility' );
     
    815837                        foreach ( $levels as $level ) {
    816838                                switch ( $level->meta_key ) {
    817                                         case 'default_visibility' :
    818                                                 $default_visibility_levels[ $level->object_id ]['default']      = $level->meta_value;
     839                                        case 'default_visibility':
     840                                                $default_visibility_levels[ $level->object_id ]['default'] = $level->meta_value;
    819841                                                break;
    820                                         case 'allow_custom_visibility' :
     842                                        case 'allow_custom_visibility':
    821843                                                $default_visibility_levels[ $level->object_id ]['allow_custom'] = $level->meta_value;
    822844                                                break;
     
    846868
    847869                // 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                );
    851876
    852877                // New field group.
    853878                if ( empty( $this->id ) ) {
    854                         $title       = __( 'Add New Field Group', 'buddypress' );
    855                         $button      = __( 'Save',                'buddypress' );
    856                         $action      = add_query_arg(
     879                        $title       = __( 'Add New Field Group', 'buddypress' );
     880                        $button      = __( 'Save', 'buddypress' );
     881                        $action      = add_query_arg(
    857882                                array(
    858883                                        'page' => 'bp-profile-setup',
     
    863888                        $description = '';
    864889
    865                 // Existing field group.
     890                        // Existing field group.
    866891                } else {
    867892                        $title       = __( 'Edit Field Group', 'buddypress' );
    868                         $button      = __( 'Update',          'buddypress' );
    869                         $action      = add_query_arg(
     893                        $button      = __( 'Update', 'buddypress' );
     894                        $action      = add_query_arg(
    870895                                array(
    871896                                        'page'     => 'bp-profile-setup',
     
    879904                        if ( $this->can_delete ) {
    880905                                // 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                                );
    886917                        }
    887918                } ?>
     
    906937                                                        <div id="titlediv">
    907938                                                                <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>
    909940                                                                        <input type="text" name="group_name" id="title" value="<?php echo esc_attr( $this->name ); ?>" autocomplete="off" />
    910941                                                                </div>
     
    932963                                                         * @param BP_XProfile_Group $field_group Current instance of the field group.
    933964                                                         */
    934                                                         do_action( 'xprofile_group_admin_after_description', $this ); ?>
     965                                                        do_action( 'xprofile_group_admin_after_description', $this );
     966                                                        ?>
    935967
    936968                                                </div><!-- #post-body-content -->
     
    947979                                                         * @param BP_XProfile_Group $field_group Current instance of the field group.
    948980                                                         */
    949                                                         do_action( 'xprofile_group_before_submitbox', $this ); ?>
     981                                                        do_action( 'xprofile_group_before_submitbox', $this );
     982                                                        ?>
    950983
    951984                                                        <div id="submitdiv" class="postbox">
     
    9671000                                                                                         * @param BP_XProfile_Group $field_group Current instance of the field group.
    9681001                                                                                         */
    969                                                                                         do_action( 'xprofile_group_submitbox_start', $this ); ?>
     1002                                                                                        do_action( 'xprofile_group_submitbox_start', $this );
     1003                                                                                        ?>
    9701004
    9711005                                                                                        <input type="hidden" name="group_order" id="group_order" value="<?php echo esc_attr( $this->group_order ); ?>" />
     
    9951029                                                         * @param BP_XProfile_Group $field_group Current instance of the field group.
    9961030                                                         */
    997                                                         do_action( 'xprofile_group_after_submitbox', $this ); ?>
     1031                                                        do_action( 'xprofile_group_after_submitbox', $this );
     1032                                                        ?>
    9981033
    9991034                                                </div>
     
    10031038                </div>
    10041039
    1005         <?php
     1040                <?php
    10061041        }
    10071042}
  • trunk/tests/phpunit/testcases/xprofile/class-bp-xprofile-group.php

    r13358 r14000  
    1111         */
    1212        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();
    1414                $group = new BP_XProfile_Group( $g );
    1515
     
    2222         * @ticket BP7916
    2323         */
    24         public function test_delete() {
     24        public function test_delete_xprofile_group() {
    2525                $g = self::factory()->xprofile_group->create();
    2626
     
    4343                $u = self::factory()->user->create();
    4444                $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                );
    4850
    4951                $f_obj = new BP_XProfile_Field( $f );
    5052
    5153                $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;
    5759                $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;
    6062                $fields[0]->is_required = $f_obj->is_required;
    61                 $fields[0]->data = new stdClass;
     63                $fields[0]->data        = new stdClass();
    6264                $fields[0]->data->value = 'foo';
    63                 $fields[0]->data->id = 123;
     65                $fields[0]->data->id    = 123;
    6466
    6567                // custom visibility enabled, but no fallback
     
    6971                $found = BP_XProfile_Group::fetch_visibility_level( $u, $fields );
    7072
    71                 $expected = $fields;
     73                $expected                      = $fields;
    7274                $expected[0]->visibility_level = 'adminsonly';
    7375
     
    8183                $found = BP_XProfile_Group::fetch_visibility_level( $u, $fields );
    8284
    83                 $expected = $fields;
     85                $expected                      = $fields;
    8486                $expected[0]->visibility_level = 'public';
    8587
     
    9395                $found = BP_XProfile_Group::fetch_visibility_level( $u, $fields );
    9496
    95                 $expected = $fields;
     97                $expected                      = $fields;
    9698                $expected[0]->visibility_level = 'adminsonly';
    9799
     
    107109                $g3 = self::factory()->xprofile_group->create();
    108110
    109                 $all = BP_XProfile_Group::get();
     111                $all         = BP_XProfile_Group::get();
    110112                $all_results = array_map( 'absint', wp_list_pluck( $all, 'id' ) );
    111113
    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                );
    116120
    117121                $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 );
    119123
    120124                $this->assertSame( $e1, array_merge( $found1, array() ) );
    121125
    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                );
    126132
    127133                $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 );
    129135
    130136                $this->assertSame( $e2, array_merge( $found2, array() ) );
     
    139145                $g2 = self::factory()->xprofile_group->create();
    140146                $g3 = self::factory()->xprofile_group->create();
    141                 $e1 = [ $g1, $g2 ];
     147                $e1 = array( $g1, $g2 );
    142148
    143149                // 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 ) ) );
    145151
    146152                $this->assertSame( $e1, array_map( 'absint', wp_list_pluck( $groups1, 'id' ) ) );
    147153
    148154                // 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 ) );
    150156
    151157                $this->assertSame( $e1, array_map( 'absint', wp_list_pluck( $groups2, 'id' ) ) );
     
    164170                $field->set_member_types( array( 'foo' ) );
    165171
    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                );
    170178
    171179                // The groups aren't indexed, so we have to go looking for it.
     
    191199                $field->set_member_types( array( 'foo' ) );
    192200
    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                );
    198208
    199209                // The groups aren't indexed, so we have to go looking for it.
     
    212222         */
    213223        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();
    215225                $f1 = self::factory()->xprofile_field->create( array( 'field_group_id' => $g ) );
    216226                $f2 = self::factory()->xprofile_field->create( array( 'field_group_id' => $g ) );
     
    238248                bp_set_member_type( $u, 'foo' );
    239249
    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                );
    244256
    245257                // The groups aren't indexed, so we have to go looking for it.
     
    261273         */
    262274        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();
    264276                $f1 = self::factory()->xprofile_field->create( array( 'field_group_id' => $g ) );
    265277                $f2 = self::factory()->xprofile_field->create( array( 'field_group_id' => $g ) );
     
    281293                $u = self::factory()->user->create();
    282294
    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                );
    287301
    288302                // The groups aren't indexed, so we have to go looking for it.
     
    303317         */
    304318        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();
    306320                $f1 = self::factory()->xprofile_field->create( array( 'field_group_id' => $g ) );
    307321                $f2 = self::factory()->xprofile_field->create( array( 'field_group_id' => $g ) );
     
    317331                bp_set_member_type( $u, 'foo' );
    318332
    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                );
    324340
    325341                // The groups aren't indexed, so we have to go looking for it.
     
    338354         */
    339355        public function test_member_type_single_value_should_be_respected() {
    340                 $g = self::factory()->xprofile_group->create();
     356                $g  = self::factory()->xprofile_group->create();
    341357                $f1 = self::factory()->xprofile_field->create( array( 'field_group_id' => $g ) );
    342358                $f2 = self::factory()->xprofile_field->create( array( 'field_group_id' => $g ) );
     
    349365                $field2->set_member_types( array( 'bar' ) );
    350366
    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                );
    355373
    356374                // The groups aren't indexed, so we have to go looking for it.
     
    370388         */
    371389        public function test_member_type_array_value_should_be_respected() {
    372                 $g = self::factory()->xprofile_group->create();
     390                $g  = self::factory()->xprofile_group->create();
    373391                $f1 = self::factory()->xprofile_field->create( array( 'field_group_id' => $g ) );
    374392                $f2 = self::factory()->xprofile_field->create( array( 'field_group_id' => $g ) );
     
    381399                $field2->set_member_types( array( 'bar' ) );
    382400
    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                );
    387407
    388408                // The groups aren't indexed, so we have to go looking for it.
     
    402422         */
    403423        public function test_member_type_null_should_be_respected() {
    404                 $g = self::factory()->xprofile_group->create();
     424                $g  = self::factory()->xprofile_group->create();
    405425                $f1 = self::factory()->xprofile_field->create( array( 'field_group_id' => $g ) );
    406426                $f2 = self::factory()->xprofile_field->create( array( 'field_group_id' => $g ) );
     
    411431                $field1->set_member_types( array( 'foo' ) );
    412432
    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                );
    417439
    418440                // The groups aren't indexed, so we have to go looking for it.
     
    431453         */
    432454        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                );
    436460
    437461                $e1 = new BP_XProfile_Group( $g1 );
     
    454478
    455479                // 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                );
    459485
    460486                // Get the field
     
    471497                $g2 = self::factory()->xprofile_group->create();
    472498
    473                 $group_ids   = [ 1 ]; // Default group.
     499                $group_ids   = array( 1 ); // Default group.
    474500                $group_ids[] = self::factory()->xprofile_group->create();
    475501                $group_ids[] = self::factory()->xprofile_group->create();
     
    481507                $this->assertEqualSets( $group_ids, $found_1 );
    482508
    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 ) );
    484510                $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 ) ) );
    488514                $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 ) ) );
    492518                $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 ) ) );
    496522                $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 ) ) ) );
    500526                $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 ) );
    504530                $this->assertEqualSets( $group_ids, $found_7 );
    505531        }
     
    521547                $group_ids[] = $g2;
    522548
    523                 $params_1 = [ 'exclude_groups' => false ];
     549                $params_1 = array( 'exclude_groups' => false );
    524550
    525551                // Prime cache.
     
    534560
    535561                // 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 ) ) );
    537563                $this->assertEqualSets( $group_ids, $found_3 );
    538564                $this->assertNotSame( $num_queries, $wpdb->num_queries );
    539565
    540566                // 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 );
    543569                $this->assertNotSame( $num_queries, $wpdb->num_queries );
    544570        }
     
    551577                $group_ids    = array( 1 ); // Default group.
    552578                $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();
    555582
    556583                // Prime cache.
     
    574601                $group_ids    = array( 1 ); // Default group.
    575602                $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();
    578606
    579607                // Prime cache.
     
    586614                $num_queries = $wpdb->num_queries;
    587615
    588                 $found = BP_XProfile_Group::get_group_ids();
     616                BP_XProfile_Group::get_group_ids();
     617
    589618                $this->assertNotSame( $num_queries, $wpdb->num_queries );
    590619        }
     
    599628                $group_ids    = array( 1 ); // Default group.
    600629                $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                );
    607639                $field = new BP_XProfile_Field( $f );
    608640
     
    615647                $num_queries = $wpdb->num_queries;
    616648
    617                 $found = BP_XProfile_Group::get_group_ids();
     649                BP_XProfile_Group::get_group_ids();
     650
    618651                $this->assertNotSame( $num_queries, $wpdb->num_queries );
    619652        }
     
    626659                $group_ids    = array( 1 ); // Default group.
    627660                $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                );
    634670                $field = new BP_XProfile_Field( $f );
    635671
     
    657693                $group_id = self::factory()->xprofile_group->create();
    658694
    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 );
    663702
    664703                // Prime cache.
     
    682721                $group_id = self::factory()->xprofile_group->create();
    683722
    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                );
    687728                $field = new BP_XProfile_Field( $f );
    688729
     
    706747                $group_id = self::factory()->xprofile_group->create();
    707748
    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                );
    711754                $field = new BP_XProfile_Field( $f );
    712755
     
    720763                $this->assertNotContains( $f, $found );
    721764        }
     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        }
    722789}
Note: See TracChangeset for help on using the changeset viewer.