Skip to:
Content

BuddyPress.org

Changeset 13358


Ignore:
Timestamp:
11/08/2022 06:27:01 AM (4 years ago)
Author:
imath
Message:

xProfile: allow the loop to fetch fields from multiple fields group

You can now pass an array of fields group ID into the $profile_group_id
parameter.

Props boonebgorges, espellcaste

Closes https://github.com/buddypress/buddypress/pull/30
Fixes #4075

Location:
trunk
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-xprofile/bp-xprofile-template.php

    r13108 r13358  
    1515 *
    1616 * @since 1.0.0
    17  * @since 2.4.0 Introduced `$member_type` argument.
    18  * @since 8.0.0 Introduced `$hide_field_types` & `$signup_fields_only` arguments.
     17 * @since 2.4.0  Introduced `$member_type` argument.
     18 * @since 8.0.0  Introduced `$hide_field_types` & `$signup_fields_only` arguments.
     19 * @since 11.0.0 `$profile_group_id` accepts an array of profile group ids.
    1920 *
    2021 * @global object $profile_template
     
    2425 *     Array of arguments. See BP_XProfile_Group::get() for full description. Those arguments whose defaults differ
    2526 *     from that method are described here:
    26  *     @type int          $user_id                Default: ID of the displayed user.
    27  *     @type string|array $member_type            Default: 'any'.
    28  *     @type int|bool    $profile_group_id       Default: false.
    29  *     @type bool         $hide_empty_groups      Default: true.
    30  *     @type bool         $hide_empty_fields      Defaults to true on the Dashboard, on a user's Edit Profile page,
    31  *                                                or during registration. Otherwise false.
    32  *     @type bool         $fetch_fields           Default: true.
    33  *     @type bool         $fetch_field_data       Default: true.
    34  *     @type bool         $fetch_visibility_level Defaults to true when an admin is viewing a profile, or when a user is
    35  *                                                viewing her own profile, or during registration. Otherwise false.
    36  *     @type int[]|bool   $exclude_groups         Default: false.
    37  *     @type int[]|bool   $exclude_fields         Default: false.
    38  *     @type string[]     $hide_field_types       Default: empty array.
    39  *     @type bool         $signup_fields_only     Default: false.
    40  *     @type bool         $update_meta_cache      Default: true.
     27 *     @type int            $user_id                Default: ID of the displayed user.
     28 *     @type string|array   $member_type            Default: 'any'.
     29 *     @type int|int[]|bool $profile_group_id       Default: false.
     30 *     @type bool           $hide_empty_groups      Default: true.
     31 *     @type bool           $hide_empty_fields      Defaults to true on the Dashboard, on a user's Edit Profile page,
     32 *                                                  or during registration. Otherwise false.
     33 *     @type bool           $fetch_fields           Default: true.
     34 *     @type bool           $fetch_field_data       Default: true.
     35 *     @type bool           $fetch_visibility_level Defaults to true when an admin is viewing a profile, or when a user is
     36 *                                                  viewing her own profile, or during registration. Otherwise false.
     37 *     @type int[]|bool     $exclude_groups         Default: false.
     38 *     @type int[]|bool     $exclude_fields         Default: false.
     39 *     @type string[]       $hide_field_types       Default: empty array.
     40 *     @type bool           $signup_fields_only     Default: false.
     41 *     @type bool           $update_meta_cache      Default: true.
    4142 * }
    42  *
    4343 * @return bool
    4444 */
  • trunk/src/bp-xprofile/classes/class-bp-xprofile-data-template.php

    r13108 r13358  
    105105         *
    106106         * @since 1.5.0
    107          * @since 2.4.0 Introduced `$member_type` argument.
    108          * @since 8.0.0 Introduced `$hide_field_types` & `$signup_fields_only` arguments.
     107         * @since 2.4.0  Introduced `$member_type` argument.
     108         * @since 8.0.0  Introduced `$hide_field_types` & `$signup_fields_only` arguments.
     109         * @since 11.0.0 `$profile_group_id` accepts an array of profile group ids.
    109110         *
    110111         * @param array|string $args {
    111112         *     An array of arguments. All items are optional.
    112113         *
    113          *     @type int          $user_id                 Fetch field data for this user ID.
    114          *     @type string|array $member_type             Limit results to those matching member type(s).
    115          *     @type int          $profile_group_id        Field group to fetch fields & data for.
    116          *     @type int|bool     $hide_empty_groups       Should empty field groups be skipped.
    117          *     @type int|bool     $fetch_fields            Fetch fields for field group.
    118          *     @type int|bool     $fetch_field_data        Fetch field data for fields in group.
    119          *     @type array        $exclude_groups          Exclude these field groups.
    120          *     @type array        $exclude_fields          Exclude these fields.
    121          *     @type int|bool     $hide_empty_fields       Should empty fields be skipped.
    122          *     @type int|bool     $fetch_visibility_level  Fetch visibility levels.
    123          *     @type string[]     $hide_field_types        List of field types to hide form loop. Default: empty array.
    124          *     @type bool         $signup_fields_only      Whether to only return signup fields. Default: false.
    125          *     @type int|bool     $update_meta_cache       Should metadata cache be updated.
     114         *     @type int            $user_id                 Fetch field data for this user ID.
     115         *     @type string|array   $member_type             Limit results to those matching member type(s).
     116         *     @type int|int[]|bool $profile_group_id        Field group(s) to fetch fields & data for. Default: false.
     117         *     @type int|bool       $hide_empty_groups       Should empty field groups be skipped.
     118         *     @type int|bool       $fetch_fields            Fetch fields for field group.
     119         *     @type int|bool       $fetch_field_data        Fetch field data for fields in group.
     120         *     @type array          $exclude_groups          Exclude these field groups.
     121         *     @type array          $exclude_fields          Exclude these fields.
     122         *     @type int|bool       $hide_empty_fields       Should empty fields be skipped.
     123         *     @type int|bool       $fetch_visibility_level  Fetch visibility levels.
     124         *     @type string[]       $hide_field_types        List of field types to hide form loop. Default: empty array.
     125         *     @type bool           $signup_fields_only      Whether to only return signup fields. Default: false.
     126         *     @type int|bool       $update_meta_cache       Should metadata cache be updated.
    126127         * }
    127128         */
  • trunk/src/bp-xprofile/classes/class-bp-xprofile-group.php

    r13108 r13358  
    238238         *
    239239         * @since 1.2.0
    240          * @since 2.4.0 Introduced `$member_type` argument.
    241          * @since 8.0.0 Introduced `$hide_field_types` & `$signup_fields_only` arguments.
     240         * @since 2.4.0  Introduced `$member_type` argument.
     241         * @since 8.0.0  Introduced `$hide_field_types` & `$signup_fields_only` arguments.
     242         * @since 11.0.0 `$profile_group_id` accepts an array of profile group ids.
    242243         *
    243244         * @global object $wpdb WordPress DB access object.
     
    245246         * @param array $args {
    246247         *      Array of optional arguments.
    247          *      @type int          $profile_group_id   Limit results to a single profile group.
    248          *      @type int          $user_id            Required if you want to load a specific user's data.
    249          *                                             Default: displayed user's ID.
    250          *      @type array|string $member_type        Limit fields by those restricted to a given member type, or array of
    251          *                                             member types. If `$user_id` is provided, the value of `$member_type`
    252          *                                             will be overridden by the member types of the provided user. The
    253          *                                             special value of 'any' will return only those fields that are
    254          *                                             unrestricted by member type - i.e., those applicable to any type.
    255          *      @type bool         $hide_empty_groups  True to hide groups that don't have any fields. Default: false.
    256          *      @type bool         $hide_empty_fields  True to hide fields where the user has not provided data.
    257          *                                             Default: false.
    258          *      @type bool         $fetch_fields       Whether to fetch each group's fields. Default: false.
    259          *      @type bool         $fetch_field_data   Whether to fetch data for each field. Requires a $user_id.
    260          *                                             Default: false.
    261          *      @type int[]|bool   $exclude_groups     Comma-separated list or array of group IDs to exclude.
    262          *      @type int[]|bool   $exclude_fields     Comma-separated list or array of field IDs to exclude.
    263          *      @type string[]     $hide_field_types   List of field types to hide form loop. Default: empty array.
    264          *      @type bool         $signup_fields_only Whether to only return signup fields. Default: false.
    265          *      @type bool         $update_meta_cache  Whether to pre-fetch xprofilemeta for all retrieved groups, fields,
    266          *                                             and data. Default: true.
     248         *
     249         *      @type int|int[]|bool $profile_group_id   Limit results to a single profile group or a comma-separated list or array of
     250         *                                               profile group ids. Default: false.
     251         *      @type int            $user_id            Required if you want to load a specific user's data.
     252         *                                               Default: displayed user's ID.
     253         *      @type array|string   $member_type        Limit fields by those restricted to a given member type, or array of
     254         *                                               member types. If `$user_id` is provided, the value of `$member_type`
     255         *                                               will be overridden by the member types of the provided user. The
     256         *                                               special value of 'any' will return only those fields that are
     257         *                                               unrestricted by member type - i.e., those applicable to any type.
     258         *      @type bool           $hide_empty_groups  True to hide groups that don't have any fields. Default: false.
     259         *      @type bool           $hide_empty_fields  True to hide fields where the user has not provided data.
     260         *                                               Default: false.
     261         *      @type bool           $fetch_fields       Whether to fetch each group's fields. Default: false.
     262         *      @type bool           $fetch_field_data   Whether to fetch data for each field. Requires a $user_id.
     263         *                                               Default: false.
     264         *      @type int[]|bool     $exclude_groups     Comma-separated list or array of group IDs to exclude.
     265         *      @type int[]|bool     $exclude_fields     Comma-separated list or array of field IDs to exclude.
     266         *      @type string[]       $hide_field_types   List of field types to hide form loop. Default: empty array.
     267         *      @type bool           $signup_fields_only Whether to only return signup fields. Default: false.
     268         *      @type bool           $update_meta_cache  Whether to pre-fetch xprofilemeta for all retrieved groups, fields,
     269         *                                               and data. Default: true.
    267270         * }
    268          * @return array $groups
     271         * @return array
    269272         */
    270273        public static function get( $args = array() ) {
     
    463466         *
    464467         * @since 5.0.0
     468         * @since 11.0.0 `$profile_group_id` accepts an array of profile group ids.
    465469         *
    466470         * @param array $args {
    467          *    Array of optional arguments:
    468          *    @type int   $profile_group_id  Limit results to a single profile group. Default false.
    469          *    @type array $exclude_groups    Comma-separated list or array of group IDs to exclude. Default false.
    470          *    @type bool  $hide_empty_groups True to hide groups that don't have any fields. Default: false.
     471         *    Array of optional arguments.
     472         *
     473         *    @type int|int[]|bool $profile_group_id  Limit results to a single profile group or a comma-separated list or array of
     474         *                                       profile group ids. Default: false.
     475         *    @type int[]          $exclude_groups    Comma-separated list or array of group IDs to exclude. Default: false.
     476         *    @type bool           $hide_empty_groups True to hide groups that don't have any fields. Default: false.
    471477         * }
    472478         * @return array
     
    486492                $bp = buddypress();
    487493
    488                 if ( ! empty( $r['profile_group_id'] ) ) {
    489                         $where_sql = $wpdb->prepare( 'WHERE g.id = %d', $r['profile_group_id'] );
     494                if ( ! empty( $r['profile_group_id'] ) && ! is_bool( $r['profile_group_id'] ) ) {
     495                        $profile_group_ids = join( ',', wp_parse_id_list( $r['profile_group_id'] ) );
     496                        $where_sql         = "WHERE g.id IN ({$profile_group_ids})";
    490497                } elseif ( $r['exclude_groups'] ) {
    491498                        $exclude   = join( ',', wp_parse_id_list( $r['exclude_groups'] ) );
  • trunk/tests/phpunit/testcases/xprofile/class-bp-xprofile-group.php

    r12317 r13358  
    132132
    133133        /**
     134         * @group get_xprofile_groups
     135         * @group BP4075
     136         */
     137        public function test_get_specific_xprofile_groups() {
     138                $g1 = self::factory()->xprofile_group->create();
     139                $g2 = self::factory()->xprofile_group->create();
     140                $g3 = self::factory()->xprofile_group->create();
     141                $e1 = [ $g1, $g2 ];
     142
     143                // Comma-separated list of profile group ids.
     144                $groups1 = BP_XProfile_Group::get( [ 'profile_group_id' => join( ',', $e1 ) ] );
     145
     146                $this->assertSame( $e1, array_map( 'absint', wp_list_pluck( $groups1, 'id' ) ) );
     147
     148                // Array of profile group ids.
     149                $groups2 = BP_XProfile_Group::get( [ 'profile_group_id' => $e1 ] );
     150
     151                $this->assertSame( $e1, array_map( 'absint', wp_list_pluck( $groups2, 'id' ) ) );
     152        }
     153
     154        /**
    134155         * @group member_types
    135156         * @ticket BP5192
     
    444465
    445466        /**
    446          * @group BP7435
    447          * @group cache
    448          */
    449         public function test_group_ids_query_should_be_cached() {
    450                 global $wpdb;
    451 
    452                 $group_ids   = array( 1 ); // Default group.
     467         * @group BP4075
     468         */
     469        public function test_group_ids_query() {
     470                $g1 = self::factory()->xprofile_group->create();
     471                $g2 = self::factory()->xprofile_group->create();
     472
     473                $group_ids   = [ 1 ]; // Default group.
    453474                $group_ids[] = self::factory()->xprofile_group->create();
    454475                $group_ids[] = self::factory()->xprofile_group->create();
    455476                $group_ids[] = self::factory()->xprofile_group->create();
    456 
    457                 $params_1 = array(
    458                         'exclude_groups' => false,
    459                 );
    460 
    461                 $params_2 = array(
    462                         'exclude_groups' => array( 0 ),
    463                 );
     477                $group_ids[] = $g1;
     478                $group_ids[] = $g2;
     479
     480                $found_1 = BP_XProfile_Group::get_group_ids();
     481                $this->assertEqualSets( $group_ids, $found_1 );
     482
     483                $found_2 = BP_XProfile_Group::get_group_ids( [ 'profile_group_id' => $g1 ] );
     484                $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 ] ] );
     488                $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 ] ] );
     492                $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 ] ] );
     496                $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 ] ) ] );
     500                $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 ] );
     504                $this->assertEqualSets( $group_ids, $found_7 );
     505        }
     506
     507        /**
     508         * @group BP7435
     509         * @group cache
     510         */
     511        public function test_group_ids_query_should_be_cached() {
     512                global $wpdb;
     513
     514                $group_ids   = array( 1 ); // Default group.
     515                $g1          = self::factory()->xprofile_group->create();
     516                $g2          = self::factory()->xprofile_group->create();
     517                $group_ids[] = self::factory()->xprofile_group->create();
     518                $group_ids[] = self::factory()->xprofile_group->create();
     519                $group_ids[] = self::factory()->xprofile_group->create();
     520                $group_ids[] = $g1;
     521                $group_ids[] = $g2;
     522
     523                $params_1 = [ 'exclude_groups' => false ];
    464524
    465525                // Prime cache.
     
    469529                $num_queries = $wpdb->num_queries;
    470530
    471                 $found_1 = BP_XProfile_Group::get_group_ids( $params_1 );
    472                 $this->assertEqualSets( $group_ids, $found_1 );
     531                $found_2 = BP_XProfile_Group::get_group_ids( $params_1 );
     532                $this->assertEqualSets( $group_ids, $found_2 );
    473533                $this->assertSame( $num_queries, $wpdb->num_queries );
    474534
    475535                // Different parameters should trigger a cache miss.
    476                 $found_2 = BP_XProfile_Group::get_group_ids( $params_2 );
    477                 $this->assertEqualSets( $group_ids, $found_2 );
     536                $found_3 = BP_XProfile_Group::get_group_ids( [ 'exclude_groups' => [ 0 ] ] );
     537                $this->assertEqualSets( $group_ids, $found_3 );
     538                $this->assertNotSame( $num_queries, $wpdb->num_queries );
     539
     540                // 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 );
    478543                $this->assertNotSame( $num_queries, $wpdb->num_queries );
    479544        }
  • trunk/tests/phpunit/testcases/xprofile/template.php

    r12887 r13358  
    127127                $profile_template = $reset_profile_template;
    128128        }
     129
     130        /**
     131         * @group bp_has_profile
     132         */
     133        public function test_bp_has_profile_get_multiple_groups() {
     134                global $profile_template;
     135                $reset_profile_template = $profile_template;
     136
     137                $g1 = self::factory()->xprofile_group->create();
     138                $g2 = self::factory()->xprofile_group->create();
     139                $g3 = self::factory()->xprofile_group->create();
     140
     141                $field_in_1 = self::factory()->xprofile_field->create(
     142                        [
     143                                'field_group_id' => $g1,
     144                                'type'           => 'textbox',
     145                                'name'           => 'field1'
     146                        ]
     147                );
     148
     149                $field_in_2 = self::factory()->xprofile_field->create(
     150                        [
     151                                'field_group_id' => $g2,
     152                                'type'           => 'textbox',
     153                                'name'           => 'field2'
     154                        ]
     155                );
     156
     157                self::factory()->xprofile_field->create(
     158                        [
     159                                'field_group_id' => $g3,
     160                                'type'           => 'textbox',
     161                                'name'           => 'field3'
     162                        ]
     163                );
     164
     165                bp_has_profile( [ 'profile_group_id' => join( ',', [ $g1, $g2 ] ) ] );
     166
     167                $group_ids = wp_list_pluck( $profile_template->groups, 'id' );
     168
     169                $this->assertSame( 2, $profile_template->group_count );
     170                $this->assertContains( $g1, $group_ids );
     171                $this->assertContains( $g2, $group_ids );
     172                $this->assertNotContains( $g3, $group_ids );
     173
     174                xprofile_delete_field_group( $g1 );
     175                xprofile_delete_field_group( $g2 );
     176                xprofile_delete_field_group( $g3 );
     177
     178                $profile_template = $reset_profile_template;
     179        }
    129180}
Note: See TracChangeset for help on using the changeset viewer.