Skip to:
Content

BuddyPress.org

Changeset 11532


Ignore:
Timestamp:
04/20/2017 12:19:36 AM (9 years ago)
Author:
dcavins
Message:

Add 'status' parameter to BP_Groups_Group::get().

Add $status parameter to groups_get_groups()` and its underlying
function BP_Groups_Group::get(). Find groups by passing a single
status, a comma-separated list of statuses, or any array of statuses.

Fixes #7501.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-groups/bp-groups-functions.php

    r11530 r11532  
    718718                'meta_query'         => false,          // Filter by groupmeta. See WP_Meta_Query for syntax.
    719719                'show_hidden'        => false,          // Show hidden groups to non-admins.
     720                'status'             => array(),        // Array or comma-separated list of group statuses to limit results to.
    720721                'per_page'           => 20,             // The number of results to return per page.
    721722                'page'               => 1,              // The page to return if limiting per page.
    … …  
    740741                'meta_query'         => $r['meta_query'],
    741742                'show_hidden'        => $r['show_hidden'],
     743                'status'             => $r['status'],
    742744                'per_page'           => $r['per_page'],
    743745                'page'               => $r['page'],
  • trunk/src/bp-groups/classes/class-bp-groups-group.php

    r11530 r11532  
    920920         *                                            groups. Default: false.
    921921         *     @type bool         $show_hidden        Whether to include hidden groups in results. Default: false.
     922         *     @type array|string $status             Optional. Array or comma-separated list of group statuses to limit
     923         *                                            results to. If specified, $show_hidden is ignored.
     924         *                                            Default: empty array.
    922925         * }
    923926         * @return array {
    … …  
    970973                        'exclude'            => false,
    971974                        'show_hidden'        => false,
     975                        'status'             => array()
    972976                );
    973977
    … …  
    990994                $where_conditions = array();
    991995
    992                 if ( empty( $r['show_hidden'] ) ) {
     996
     997                if ( ! empty( $r['status'] ) ) {
     998                        if ( ! is_array( $r['status'] ) ) {
     999                                $r['status'] = preg_split( '/[\s,]+/', $r['status'] );
     1000                        }
     1001                        $r['status'] = array_map( 'sanitize_title', $r['status'] );
     1002                        $status_in = "'" . implode( "','", $r['status'] ) . "'";
     1003                        $where_conditions['status'] = "g.status IN ({$status_in})";
     1004                } elseif ( empty( $r['show_hidden'] ) ) {
    9931005                        $where_conditions['hidden'] = "g.status != 'hidden'";
    9941006                }
  • trunk/tests/phpunit/testcases/groups/class-bp-groups-group.php

    r11526 r11532  
    21172117        }
    21182118
     2119        /**
     2120         * @group get_by_status
     2121         */
     2122        public function test_get_by_status() {
     2123                $g1 = $this->factory->group->create(array(
     2124                        'status'      => 'private'
     2125                ) );
     2126                $g2 = $this->factory->group->create( array(
     2127                        'status'      => 'public'
     2128                ) );
     2129                $g3 = $this->factory->group->create( array(
     2130                        'status'      => 'hidden'
     2131                ) );
     2132
     2133                $groups = BP_Groups_Group::get( array(
     2134                        'status' => array( 'private', 'hidden' ),
     2135                ) );
     2136
     2137                $found = wp_list_pluck( $groups['groups'], 'id' );
     2138                $this->assertEqualSets( array( $g1, $g3 ), $found );
     2139        }
     2140
     2141        /**
     2142         * @group get_by_status
     2143         */
     2144        public function test_get_by_status_accept_string() {
     2145                $g1 = $this->factory->group->create(array(
     2146                        'status'      => 'private'
     2147                ) );
     2148                $g2 = $this->factory->group->create( array(
     2149                        'status'      => 'public'
     2150                ) );
     2151                $g3 = $this->factory->group->create( array(
     2152                        'status'      => 'hidden'
     2153                ) );
     2154
     2155                $groups = BP_Groups_Group::get( array(
     2156                        'status' => 'public',
     2157                ) );
     2158
     2159                $found = wp_list_pluck( $groups['groups'], 'id' );
     2160                $this->assertEqualSets( array( $g2 ), $found );
     2161        }
     2162
     2163        /**
     2164         * @group get_by_status
     2165         */
     2166        public function test_get_by_status_accept_comma_separated_string() {
     2167                $g1 = $this->factory->group->create(array(
     2168                        'status'      => 'private'
     2169                ) );
     2170                $g2 = $this->factory->group->create( array(
     2171                        'status'      => 'public'
     2172                ) );
     2173                $g3 = $this->factory->group->create( array(
     2174                        'status'      => 'hidden'
     2175                ) );
     2176
     2177                $groups = BP_Groups_Group::get( array(
     2178                        'status' => 'private, hidden',
     2179                ) );
     2180
     2181                $found = wp_list_pluck( $groups['groups'], 'id' );
     2182                $this->assertEqualSets( array( $g1, $g3 ), $found );
     2183        }
     2184
    21192185}
    21202186
Note: See TracChangeset for help on using the changeset viewer.