Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
12/04/2014 01:47:49 AM (11 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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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' );
Note: See TracChangeset for help on using the changeset viewer.