Skip to:
Content

BuddyPress.org

Changeset 9210


Ignore:
Timestamp:
12/04/2014 01:47:49 AM (6 years ago)
Author:
boonebgorges
Message:

Introduce Member Types API.

BuddyPress member types are akin to WordPress's custom post types. Developers
can use bp_register_member_type() to register their types with BuddyPress,
and BP will automatically provide a number of pieces of functionality:

  • Mechanisms for storing and fetching member types (as a WP taxonomy term), with full cache support.
  • A 'member_type' argument for the bp_has_members()/BP_User_Query stack, which allows filtering member loops by member type.
  • Admin UI for changing member types on Dashboard > Users > Community Profile (appears when member types have been registered).

We'll continue to build out more core member type functionality in future
versions of BuddyPress. In the meantime, this is a good starting point for BP
site implementations to have a shared infrastructure for storing and retrieving
this data.

See #6006.

Location:
trunk
Files:
3 added
10 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-core/bp-core-actions.php

    r8610 r9210  
    6666 */
    6767add_action( 'bp_init', 'bp_core_set_uri_globals',    2  );
     68add_action( 'bp_init', 'bp_register_taxonomies',     3  );
    6869add_action( 'bp_init', 'bp_setup_globals',           4  );
    6970add_action( 'bp_init', 'bp_setup_canonical_stack',   5  );
  • trunk/src/bp-core/bp-core-classes.php

    r9178 r9210  
    3838 *                                              override all others; BP User objects will be constructed using these
    3939 *                                              IDs only. Default: false.
     40 *     @type array|string      $member_type     Array or comma-separated list of member types to limit results to.
    4041 *     @type string|bool       $meta_key        Limit results to users that have usermeta associated with this meta_key.
    4142 *                                              Usually used with $meta_value. Default: false.
     
    165166                'exclude'         => false,
    166167                'user_ids'        => false,
     168                'member_type'     => '',
    167169                'meta_key'        => false,
    168170                'meta_value'      => false,
     
    400402                $search_terms_space
    401403            );
     404        }
     405
     406        // Member type.
     407        if ( ! empty( $member_type ) ) {
     408            $member_types = array();
     409
     410            if ( ! is_array( $member_type ) ) {
     411                $member_type = preg_split( '/[,\s+]/', $member_type );
     412            }
     413
     414            foreach ( $member_type as $mt ) {
     415                if ( ! bp_get_member_type_object( $mt ) ) {
     416                    continue;
     417                }
     418
     419                $member_types[] = $mt;
     420            }
     421
     422            if ( ! empty( $member_types ) ) {
     423                $member_type_tq = new WP_Tax_Query( array(
     424                    array(
     425                        'taxonomy' => 'bp_member_type',
     426                        'field'    => 'name',
     427                        'operator' => 'IN',
     428                        'terms'    => $member_types,
     429                    ),
     430                ) );
     431
     432                // Switch to the root blog, where member type taxonomies live.
     433                switch_to_blog( bp_get_root_blog_id() );
     434
     435                $member_type_sql_clauses = $member_type_tq->get_sql( 'u', $this->uid_name );
     436                restore_current_blog();
     437
     438
     439                // Grab the first term_relationships clause and convert to a subquery.
     440                if ( preg_match( '/' . $wpdb->term_relationships . '\.term_taxonomy_id.*/', $member_type_sql_clauses['where'], $matches ) ) {
     441                    $sql['where']['member_type'] = "u.{$this->uid_name} IN ( SELECT object_id FROM $wpdb->term_relationships WHERE {$matches[0]} )";
     442                }
     443            }
    402444        }
    403445
  • trunk/src/bp-core/bp-core-dependency.php

    r8610 r9210  
    3737function bp_setup_canonical_stack() {
    3838    do_action( 'bp_setup_canonical_stack' );
     39}
     40
     41/**
     42 * Fire the 'bp_register_taxonomies' action, where plugins should register taxonomies.
     43 *
     44 * @since BuddyPress (2.2.0)
     45 */
     46function bp_register_taxonomies() {
     47    do_action( 'bp_register_taxonomies' );
    3948}
    4049
  • trunk/src/bp-loader.php

    r9182 r9210  
    433433        require( $this->plugin_dir . 'bp-core/bp-core-options.php'    );
    434434        require( $this->plugin_dir . 'bp-core/bp-core-classes.php'    );
     435        require( $this->plugin_dir . 'bp-core/bp-core-taxonomy.php'   );
    435436        require( $this->plugin_dir . 'bp-core/bp-core-filters.php'    );
    436437        require( $this->plugin_dir . 'bp-core/bp-core-avatars.php'    );
  • trunk/src/bp-members/bp-members-admin.php

    r9190 r9210  
    186186        // Add user row actions for single site
    187187        add_filter( 'user_row_actions', array( $this, 'row_actions' ), 10, 2 );
     188
     189        // Process changes to member type.
     190        add_action( 'bp_members_admin_load', array( $this, 'process_member_type_update' ) );
    188191
    189192        /** Signups ***********************************************************/
     
    720723            );
    721724
     725            // Member Type metabox. Only added if member types have been registered.
     726            $member_types = bp_get_member_types();
     727            if ( ! empty( $member_types ) ) {
     728                add_meta_box(
     729                    'bp_members_admin_member_type',
     730                    _x( 'Member Type', 'members user-admin edit screen', 'buddypress' ),
     731                    array( $this, 'user_admin_member_type_metabox' ),
     732                    get_current_screen()->id,
     733                    'side',
     734                    'core'
     735                );
     736            }
     737
    722738            /**
    723739             * Custom metabox ?
     
    9961012
    9971013        <?php
     1014    }
     1015
     1016    /**
     1017     * Render the Member Type metabox.
     1018     *
     1019     * @since BuddyPress (2.2.0)
     1020     * @access public
     1021     *
     1022     * @param WP_User $user The WP_User object to be edited.
     1023     */
     1024    public function user_admin_member_type_metabox( $user = null ) {
     1025
     1026        // Bail if no user ID.
     1027        if ( empty( $user->ID ) ) {
     1028            return;
     1029        }
     1030
     1031        $types = bp_get_member_types( array(), 'objects' );
     1032        $current_type = bp_get_member_type( $user->ID );
     1033
     1034        /* translators: no option picked in select box */
     1035        $null_option = __( '----', 'buddypress' );
     1036
     1037        ?>
     1038        <select name="bp-members-profile-member-type">
     1039            <option value="" <?php selected( '', $current_type ); ?>><?php esc_attr_e( $null_option ) ?></option>
     1040            <?php foreach ( $types as $type ) : ?>
     1041                <option value="<?php echo esc_attr( $type->name ) ?>" <?php selected( $type->name, $current_type ) ?>><?php echo esc_html( $type->labels['singular_name'] ) ?></option>
     1042            <?php endforeach; ?>
     1043        </select>
     1044
     1045        <?php
     1046
     1047        wp_nonce_field( 'bp-member-type-change-' . $user->ID, 'bp-member-type-nonce' );
     1048    }
     1049
     1050    /**
     1051     * Process changes from the Member Type metabox.
     1052     *
     1053     * @since BuddyPress (2.2.0)
     1054     * @access public
     1055     */
     1056    public function process_member_type_update() {
     1057        if ( ! isset( $_POST['bp-member-type-nonce'] ) || ! isset( $_POST['bp-members-profile-member-type'] ) ) {
     1058            return;
     1059        }
     1060
     1061        $user_id = $this->get_user_id();
     1062
     1063        check_admin_referer( 'bp-member-type-change-' . $user_id, 'bp-member-type-nonce' );
     1064
     1065        // Permission check.
     1066        if ( ! current_user_can( 'bp_moderate' ) && $user_id != bp_loggedin_user_id() ) {
     1067            return;
     1068        }
     1069
     1070        // Member type string must either reference a valid member type, or be empty.
     1071        $member_type = stripslashes( $_POST['bp-members-profile-member-type'] );
     1072        if ( ! empty( $member_type ) && ! bp_get_member_type_object( $member_type ) ) {
     1073            return;
     1074        }
     1075
     1076        /*
     1077         * If an invalid member type is passed, someone's doing something
     1078         * fishy with the POST request, so we can fail silently.
     1079         */
     1080        if ( bp_set_member_type( $user_id, $member_type ) ) {
     1081            // @todo Success messages can't be posted because other stuff happens on the pageload.
     1082        }
    9981083    }
    9991084
  • trunk/src/bp-members/bp-members-functions.php

    r9197 r9210  
    7979 *     Array of arguments. All are optional. See {@link BP_User_Query} for
    8080 *     a more complete description of arguments.
    81  *     @type string $type Sort order. Default: 'active'.
    82  *     @type int $user_id Limit results to friends of a user. Default: false.
    83  *     @type mixed $exclude IDs to exclude from results. Default: false.
    84  *     @type string $search_terms Limit to users matching search terms. Default: false.
    85  *     @type string $meta_key Limit to users with a meta_key. Default: false.
    86  *     @type string $meta_value Limit to users with a meta_value (with
    87  *           meta_key). Default: false.
     81 *     @type string       $type            Sort order. Default: 'active'.
     82 *     @type int          $user_id        Limit results to friends of a user. Default: false.
     83 *     @type mixed        $exclude        IDs to exclude from results. Default: false.
     84 *     @type string       $search_terms    Limit to users matching search terms. Default: false.
     85 *     @type string       $meta_key        Limit to users with a meta_key. Default: false.
     86 *     @type string       $meta_value      Limit to users with a meta_value (with meta_key). Default: false.
     87 *     @type array|string $member_type     Array or comma-separated string of member types.
    8888 *     @type mixed $include Limit results by user IDs. Default: false.
    89  *     @type int $per_page Results per page. Default: 20.
    90  *     @type int $page Page of results. Default: 1.
    91  *     @type bool $populate_extras Fetch optional extras. Default: true.
    92  *     @type string|bool $count_total How to do total user count.
    93  *           Default: 'count_query'.
     89 *     @type int          $per_page        Results per page. Default: 20.
     90 *     @type int          $page            Page of results. Default: 1.
     91 *     @type bool         $populate_extras Fetch optional extras. Default: true.
     92 *     @type string|bool  $count_total     How to do total user count. Default: 'count_query'.
    9493 * }
    9594 * @return array
     
    105104        'meta_key'        => false,        // Limit to users who have this piece of usermeta
    106105        'meta_value'      => false,        // With meta_key, limit to users where usermeta matches this value
     106        'member_type'     => '',
    107107        'include'         => false,        // Pass comma separated list of user_ids to limit to only these users
    108108        'per_page'        => 20,           // The number of results to return per page
     
    21492149}
    21502150add_action( 'login_form_bp-spam', 'bp_live_spammer_login_error' );
     2151
     2152/** Member Types *************************************************************/
     2153
     2154/**
     2155 * Register a member type.
     2156 *
     2157 * @since BuddyPress (2.2.0)
     2158 *
     2159 * @param string $member_type Unique string identifier for the member type.
     2160 * @param array  $args {
     2161 *     Array of arguments describing the member type.
     2162 *
     2163 *     @type array $labels {
     2164 *         Array of labels to use in various parts of the interface.
     2165 *
     2166 *         @type string $name          Default name. Should typically be plural.
     2167 *         @type string $singular_name Singular name.
     2168 *     }
     2169 * }
     2170 * @return object|WP_Error Member type object on success, WP_Error object on failure.
     2171 */
     2172function bp_register_member_type( $member_type, $args = array() ) {
     2173    $bp = buddypress();
     2174
     2175    if ( isset( $bp->members->types[ $member_type ] ) ) {
     2176        return new WP_Error( 'bp_member_type_exists', __( 'Member type already exists.', 'buddypress' ), $member_type );
     2177    }
     2178
     2179    $r = bp_parse_args( $args, array(
     2180        'labels' => array(),
     2181    ), 'register_member_type' );
     2182
     2183    $type = (object) $r;
     2184
     2185    // Store the post type name as data in the object (not just as the array key).
     2186    $type->name = $member_type;
     2187
     2188    // Make sure the relevant labels have been filled in.
     2189    $default_name = isset( $r['labels']['name'] ) ? $r['labels']['name'] : ucfirst( $type->name );
     2190    $r['labels'] = array_merge( array(
     2191        'name'          => $default_name,
     2192        'singular_name' => $default_name,
     2193    ), $r['labels'] );
     2194
     2195    $bp->members->types[ $member_type ] = $type;
     2196
     2197    /**
     2198     * Fires after a member type is registered.
     2199     *
     2200     * @since BuddyPress (2.2.0)
     2201     *
     2202     * @param string $member_type Member type identifier.
     2203     * @param object $type        Member type object.
     2204     */
     2205    do_action( 'bp_registered_member_type', $member_type, $type );
     2206
     2207    return $type;
     2208}
     2209
     2210/**
     2211 * Retrieve a member type object by name.
     2212 *
     2213 * @since BuddyPress (2.2.0)
     2214 *
     2215 * @param  string $post_type The name of the member type.
     2216 * @return object A member type object.
     2217 */
     2218function bp_get_member_type_object( $member_type ) {
     2219    $types = bp_get_member_types( array(), 'objects' );
     2220
     2221    if ( empty( $types[ $member_type ] ) ) {
     2222        return null;
     2223    }
     2224
     2225    return $types[ $member_type ];
     2226}
     2227
     2228/**
     2229 * Get a list of all registered member type objects.
     2230 *
     2231 * @since BuddyPress (2.2.0)
     2232 *
     2233 * @see bp_register_member_type() for accepted arguments.
     2234 *
     2235 * @param array|string $args     Optional. An array of key => value arguments to match against
     2236 *                               the member type objects. Default empty array.
     2237 * @param string       $output   Optional. The type of output to return. Accepts 'names'
     2238 *                               or 'objects'. Default 'names'.
     2239 * @param string       $operator Optional. The logical operation to perform. 'or' means only one
     2240 *                               element from the array needs to match; 'and' means all elements
     2241 *                               must match. Accepts 'or' or 'and'. Default 'and'.
     2242 * @return array A list of member type names or objects.
     2243 */
     2244function bp_get_member_types( $args = array(), $output = 'names', $operator = 'and' ) {
     2245    $types = buddypress()->members->types;
     2246
     2247    $field = 'names' == $output ? 'name' : false;
     2248
     2249    $types = wp_filter_object_list( $types, $args, $operator );
     2250
     2251    /**
     2252     * Filters the array of member type objects.
     2253     *
     2254     * This filter is run before the $output filter has been applied, so that
     2255     * filtering functions have access to the entire member type objects.
     2256     *
     2257     * @since BuddyPress (2.2.0)
     2258     *
     2259     * @param array  $types     Member type objects, keyed by name.
     2260     * @param array  $args      Array of key=>value arguments for filtering.
     2261     * @param string $operator 'or' to match any of $args, 'and' to require all.
     2262     */
     2263    $types = apply_filters( 'bp_get_member_types', $types, $args, $operator );
     2264
     2265    if ( 'names' === $output ) {
     2266        $types = wp_list_pluck( $types, 'name' );
     2267    }
     2268
     2269    return $types;
     2270}
     2271
     2272/**
     2273 * Set type for a member.
     2274 *
     2275 * @since BuddyPress (2.2.0)
     2276 *
     2277 * @param int    $user_id     ID of the user.
     2278 * @param string $member_type Member type.
     2279 * @param bool   $append      Optional. True to append this to existing types for user,
     2280 *                            false to replace. Default: false.
     2281 * @return See {@see bp_set_object_terms()}.
     2282 */
     2283function bp_set_member_type( $user_id, $member_type, $append = false ) {
     2284    // Pass an empty $member_type to remove a user's type.
     2285    if ( ! empty( $member_type ) && ! bp_get_member_type_object( $member_type ) ) {
     2286        return false;
     2287    }
     2288
     2289    $retval = bp_set_object_terms( $user_id, $member_type, 'bp_member_type', $append );
     2290
     2291    // Bust the cache if the type has been updated.
     2292    if ( ! is_wp_error( $retval ) ) {
     2293        wp_cache_delete( $user_id, 'bp_member_type' );
     2294
     2295        /**
     2296         * Fires just after a user's member type has been changed.
     2297         *
     2298         * @since BuddyPress (2.2.0)
     2299         *
     2300         * @param int    $user_id     ID of the user whose member type has been updated.
     2301         * @param string $member_type Member type.
     2302         * @param bool   $append      Whether the type is being appended to existing types.
     2303         */
     2304        do_action( 'bp_set_member_type', $user_id, $member_type, $append );
     2305    }
     2306
     2307    return $retval;
     2308}
     2309
     2310/**
     2311 * Get type for a member.
     2312 *
     2313 * @since BuddyPress (2.2.0)
     2314 *
     2315 * @param  int               $user_id ID of the user.
     2316 * @param  bool              $single  Optional. Whether to return a single type string. If multiple types are found
     2317 *                                    for the user, the oldest one will be returned. Default: true.
     2318 * @return string|array|bool On success, returns a single member type (if $single is true) or an array of member
     2319 *                           types (if $single is false). Returns false on failure.
     2320 */
     2321function bp_get_member_type( $user_id, $single = true ) {
     2322    $types = wp_cache_get( $user_id, 'bp_member_type' );
     2323
     2324    if ( false === $types ) {
     2325        $types = bp_get_object_terms( $user_id, 'bp_member_type'  );
     2326
     2327        if ( ! is_wp_error( $types ) ) {
     2328            $types = wp_list_pluck( $types, 'name' );
     2329            wp_cache_set( $user_id, $types, 'bp_member_type' );
     2330        }
     2331    }
     2332
     2333    $type = false;
     2334    if ( ! empty( $types ) ) {
     2335        if ( $single ) {
     2336            $type = array_pop( $types );
     2337        } else {
     2338            $type = $types;
     2339        }
     2340    }
     2341
     2342    /**
     2343     * Filters a user's member type(s).
     2344     *
     2345     * @since BuddyPress (2.2.0)
     2346     *
     2347     * @param string $type    Member type.
     2348     * @param int    $user_id ID of the user.
     2349     * @param bool   $single  Whether to return a single type string, or an array.
     2350     */
     2351    return apply_filters( 'bp_get_member_type', $type, $user_id, $single );
     2352}
     2353
     2354/**
     2355 * Delete a user's member type when the user when the user is deleted.
     2356 *
     2357 * @since BuddyPress (2.2.0)
     2358 *
     2359 * @param  int $user_id ID of the user.
     2360 * @return See {@see bp_set_member_type()}.
     2361 */
     2362function bp_remove_member_type_on_user_delete( $user_id ) {
     2363    return bp_set_member_type( $user_id, '' );
     2364}
     2365add_action( 'wpmu_delete_user', 'bp_remove_member_type_on_user_delete' );
     2366add_action( 'delete_user', 'bp_remove_member_type_on_user_delete' );
  • trunk/src/bp-members/bp-members-loader.php

    r8705 r9210  
    1212
    1313class BP_Members_Component extends BP_Component {
     14    /**
     15     * Member types.
     16     *
     17     * @see bp_register_member_type()
     18     *
     19     * @access public
     20     * @since  BuddyPress (2.2.0)
     21     * @var    array
     22     */
     23    public $types = array();
    1424
    1525    /**
     
    4656            'template',
    4757            'adminbar',
    48             'functions'
     58            'functions',
     59            'cache',
    4960        );
    5061
  • trunk/src/bp-members/bp-members-template.php

    r9189 r9210  
    222222     * @see BP_User_Query for an in-depth description of parameters.
    223223     *
    224      * @param string $type Sort order.
    225      * @param int $page_number Page of results.
    226      * @param int $per_page Number of results per page.
    227      * @param int $max Max number of results to return.
    228      * @param int $user_id Limit to friends of a user.
    229      * @param string $search_terms Limit to users matching search terms.
    230      * @param array $include Limit results by these user IDs.
    231      * @param bool $populate_extras Fetch optional extras.
    232      * @param array $exclude Exclude these IDs from results.
    233      * @param array $meta_key Limit to users with a meta_key.
    234      * @param array $meta_value Limit to users with a meta_value (with meta_key).
    235      * @param array $page_arg Optional. The string used as a query
    236      *        parameter in pagination links. Default: 'upage'.
    237      */
    238     function __construct( $type, $page_number, $per_page, $max, $user_id, $search_terms, $include, $populate_extras, $exclude, $meta_key, $meta_value, $page_arg = 'upage' ) {
     224     * @param string       $type            Sort order.
     225     * @param int          $page_number     Page of results.
     226     * @param int          $per_page        Number of results per page.
     227     * @param int          $max             Max number of results to return.
     228     * @param int          $user_id         Limit to friends of a user.
     229     * @param string       $search_terms    Limit to users matching search terms.
     230     * @param array        $include         Limit results by these user IDs.
     231     * @param bool         $populate_extras Fetch optional extras.
     232     * @param array        $exclude         Exclude these IDs from results.
     233     * @param array        $meta_key        Limit to users with a meta_key.
     234     * @param array        $meta_value      Limit to users with a meta_value (with meta_key).
     235     * @param array        $page_arg        Optional. The string used as a query parameter in pagination links.
     236     *                                      Default: 'upage'.
     237     * @param array|string $member_type     Array or comma-separated string of member types to limit results to.
     238     */
     239    function __construct( $type, $page_number, $per_page, $max, $user_id, $search_terms, $include, $populate_extras, $exclude, $meta_key, $meta_value, $page_arg = 'upage', $member_type = '' ) {
    239240
    240241        $this->pag_page = !empty( $_REQUEST[$page_arg] ) ? intval( $_REQUEST[$page_arg] ) : (int) $page_number;
     
    245246            $this->members = BP_Core_User::get_users_by_letter( $_REQUEST['letter'], $this->pag_num, $this->pag_page, $populate_extras, $exclude );
    246247        else
    247             $this->members = bp_core_get_users( array( 'type' => $this->type, 'per_page' => $this->pag_num, 'page' => $this->pag_page, 'user_id' => $user_id, 'include' => $include, 'search_terms' => $search_terms, 'populate_extras' => $populate_extras, 'exclude' => $exclude, 'meta_key' => $meta_key, 'meta_value' => $meta_value ) );
     248            $this->members = bp_core_get_users( array( 'type' => $this->type, 'per_page' => $this->pag_num, 'page' => $this->pag_page, 'user_id' => $user_id, 'include' => $include, 'search_terms' => $search_terms, 'populate_extras' => $populate_extras, 'exclude' => $exclude, 'meta_key' => $meta_key, 'meta_value' => $meta_value, 'member_type' => $member_type ) );
    248249
    249250        if ( !$max || $max >= (int) $this->members['total'] )
     
    399400 *     string (eg, 'user_id=4&per_page=3').
    400401 *
    401  *     @type int $type Sort order. 'active', 'random', 'newest', 'popular',
    402  *           'online', 'alphabetical'. Default: 'active'.
    403  *     @type int|bool $page Page of results to display. Default: 1.
    404  *     @type int|bool $per_page Number of results per page. Default: 20.
    405  *     @type int|bool $max Maximum number of results to return.
    406  *           Default: false (unlimited).
    407  *     @type string $page_arg The string used as a query parameter in
    408  *           pagination links. Default: 'bpage'.
    409  *     @type array|int|string|bool $include Limit results by a list of user
    410  *           IDs. Accepts an array, a single integer, a comma-separated list of
    411  *           IDs, or false (to disable this limiting). Default: false.
    412  *       'active', 'alphabetical', 'newest', or 'random'.
    413  *     @type array|int|string|bool $exclude Exclude users from results by ID.
    414  *           Accepts an array, a single integer, a comma-separated list of
    415  *           IDs, or false (to disable this limiting). Default: false.
    416  *     @type int $user_id If provided, results are limited to the friends of
    417  *           the specified user. When on a user's Friends page, defaults to
    418  *           the ID of the displayed user. Otherwise defaults to 0.
    419  *     @type string $search_terms Limit results by a search term. Default: null.
    420  *     @type string $meta_key Limit results by the presence of a usermeta key.
     402 *     @type int                   $type            Sort order. Accepts 'active', 'random', 'newest', 'popular',
     403 *                                                  'online', 'alphabetical'. Default: 'active'.
     404 *     @type int|bool              $page            Page of results to display. Default: 1.
     405 *     @type int|bool              $per_page        Number of results per page. Default: 20.
     406 *     @type int|bool              $max             Maximum number of results to return. Default: false (unlimited).
     407 *     @type string                $page_arg        The string used as a query parameter in pagination links.
     408 *                                                  Default: 'bpage'.
     409 *     @type array|int|string|bool $include         Limit results by a list of user IDs. Accepts an array, a
     410 *                                                  single integer, a comma-separated list of IDs, or false (to
     411 *                                                  disable this limiting). Accepts 'active', 'alphabetical',
     412 *                                                  'newest', or 'random'. Default: false.
     413 *     @type array|int|string|bool $exclude         Exclude users from results by ID. Accepts an array, a single
     414 *                                                  integer, a comma-separated list of IDs, or false (to disable
     415 *                                                  this limiting). Default: false.
     416 *     @type int                   $user_id         If provided, results are limited to the friends of the specified
     417 *                                                  user. When on a user's Friends page, defaults to the ID of the
     418 *                                                  displayed user. Otherwise defaults to 0.
     419 *     @type string|array          $member_type     Array or comma-separated list of member types to limit results to.
     420 *     @type string                $search_terms    Limit results by a search term. Default: null.
     421 *     @type string                $meta_key        Limit results by the presence of a usermeta key.
    421422 *           Default: false.
    422  *     @type mixed $meta_value When used with meta_key, limits results by the
     423 *     @type mixed                 $meta_value      When used with meta_key, limits results by the
    423424 *           a matching usermeta value. Default: false.
    424  *     @type bool $populate_extras Whether to fetch optional data, such as
     425 *     @type bool                  $populate_extras Whether to fetch optional data, such as
    425426 *           friend counts. Default: true.
    426427 * }
     
    451452
    452453        'user_id'         => $user_id, // Pass a user_id to only show friends of this user
     454        'member_type'     => '',
    453455        'search_terms'    => null,     // Pass search_terms to filter users by their profile data
    454456
     
    486488        $r['meta_key'],
    487489        $r['meta_value'],
    488         $r['page_arg']
     490        $r['page_arg'],
     491        $r['member_type']
    489492    );
    490493
  • trunk/tests/phpunit/testcases/core/class-bp-user-query.php

    r9139 r9210  
    408408    }
    409409
     410    /**
     411     * @group member_types
     412     */
     413    public function test_member_type_single_value() {
     414        bp_register_member_type( 'foo' );
     415        bp_register_member_type( 'bar' );
     416        $users = $this->factory->user->create_many( 3 );
     417        bp_set_member_type( $users[0], 'foo' );
     418        bp_set_member_type( $users[1], 'bar' );
     419
     420        $q = new BP_User_Query( array(
     421            'member_type' => 'bar',
     422        ) );
     423
     424        $found = array_values( wp_list_pluck( $q->results, 'ID' ) );
     425        $this->assertEquals( array( $users[1] ), $found );
     426    }
     427
     428    /**
     429     * @group member_types
     430     */
     431    public function test_member_type_array_with_single_value() {
     432        bp_register_member_type( 'foo' );
     433        bp_register_member_type( 'bar' );
     434        $users = $this->factory->user->create_many( 3 );
     435        bp_set_member_type( $users[0], 'foo' );
     436        bp_set_member_type( $users[1], 'bar' );
     437
     438        $q = new BP_User_Query( array(
     439            'member_type' => array( 'bar' ),
     440        ) );
     441
     442        $found = array_values( wp_list_pluck( $q->results, 'ID' ) );
     443        $this->assertEquals( array( $users[1] ), $found );
     444    }
     445
     446    /**
     447     * @group member_types
     448     */
     449    public function test_member_type_comma_separated_values() {
     450        bp_register_member_type( 'foo' );
     451        bp_register_member_type( 'bar' );
     452        $users = $this->factory->user->create_many( 3 );
     453        bp_set_member_type( $users[0], 'foo' );
     454        bp_set_member_type( $users[1], 'bar' );
     455
     456        $q = new BP_User_Query( array(
     457            'member_type' => 'foo, bar',
     458        ) );
     459
     460        $found = array_values( wp_list_pluck( $q->results, 'ID' ) );
     461        $this->assertEqualSets( array( $users[0], $users[1] ), $found );
     462    }
     463
     464    /**
     465     * @group member_types
     466     */
     467    public function test_member_type_array_with_multiple_values() {
     468        bp_register_member_type( 'foo' );
     469        bp_register_member_type( 'bar' );
     470        $users = $this->factory->user->create_many( 3 );
     471        bp_set_member_type( $users[0], 'foo' );
     472        bp_set_member_type( $users[1], 'bar' );
     473
     474        $q = new BP_User_Query( array(
     475            'member_type' => array( 'foo', 'bar' ),
     476        ) );
     477
     478        $found = array_values( wp_list_pluck( $q->results, 'ID' ) );
     479        $this->assertEqualSets( array( $users[0], $users[1] ), $found );
     480    }
     481
     482    /**
     483     * @group member_types
     484     */
     485    public function test_member_type_comma_separated_values_should_discard_non_existent_taxonomies() {
     486        bp_register_member_type( 'foo' );
     487        bp_register_member_type( 'bar' );
     488        $users = $this->factory->user->create_many( 3 );
     489        bp_set_member_type( $users[0], 'foo' );
     490        bp_set_member_type( $users[1], 'bar' );
     491
     492        $q = new BP_User_Query( array(
     493            'member_type' => 'foo, baz',
     494        ) );
     495
     496        $found = array_values( wp_list_pluck( $q->results, 'ID' ) );
     497        $this->assertEqualSets( array( $users[0] ), $found );
     498    }
     499
     500
     501    /**
     502     * @group cache
     503     * @group member_types
     504     */
     505    public function test_member_type_should_be_prefetched_into_cache_during_user_query() {
     506        bp_register_member_type( 'foo' );
     507        bp_register_member_type( 'bar' );
     508        $users = $this->factory->user->create_many( 4 );
     509        bp_set_member_type( $users[0], 'foo' );
     510        bp_set_member_type( $users[1], 'bar' );
     511        bp_set_member_type( $users[2], 'foo' );
     512
     513        $q = new BP_User_Query( array(
     514            'include' => $users,
     515        ) );
     516
     517        $this->assertSame( 'foo', wp_cache_get( $users[0], 'bp_member_type' ) );
     518        $this->assertSame( 'bar', wp_cache_get( $users[1], 'bp_member_type' ) );
     519        $this->assertSame( 'foo', wp_cache_get( $users[2], 'bp_member_type' ) );
     520        $this->assertSame( '', wp_cache_get( $users[3], 'bp_member_type' ) );
     521    }
    410522}
  • trunk/tests/phpunit/testcases/members/template.php

    r9139 r9210  
    125125
    126126    /**
     127     * @group bp_has_members
     128     */
     129    public function test_bp_has_members_should_pass_member_type_param_to_query() {
     130        bp_register_member_type( 'foo' );
     131        bp_register_member_type( 'bar' );
     132        $users = $this->factory->user->create_many( 3 );
     133        bp_set_member_type( $users[0], 'foo' );
     134        bp_set_member_type( $users[1], 'bar' );
     135
     136        global $members_template;
     137        $old_members_template = $members_template;
     138
     139        bp_has_members( array(
     140            'member_type' => 'bar',
     141        ) );
     142
     143        $members = is_array( $members_template->members ) ? array_values( $members_template->members ) : array();
     144        $member_ids = wp_list_pluck( $members, 'ID' );
     145        $this->assertEquals( array( $users[1]), $member_ids );
     146
     147        $GLOBALS['members_template'] = $old_members_template;
     148    }
     149
     150    /**
    127151     * @group bp_get_member_last_active
    128152     */
Note: See TracChangeset for help on using the changeset viewer.