Skip to:
Content

BuddyPress.org

Changeset 12765


Ignore:
Timestamp:
10/24/2020 12:20:39 PM (6 weeks ago)
Author:
imath
Message:

Administration: add support for multiple member types assignment

Community Administrators can now use the multiple select box of the User's Extended Profile Member Type metabox to assign one or more Member Types to the user.

Data for the Member Type column of the WP Users List Table are now displaying a comma separated list of Member Types for Users being assigned to more than one Member Type (which was the original ticket's need).

The bulk removing or assigning member types functionalities have been improved to behave as follows :

  • Bulk removing member types is removing all assigned member types.
  • Bulk assigning a member type is replacing all assigned member types with the selected member type.

Props etatus, vapvarun

Fixes #8292

Location:
trunk/src/bp-members
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-members/admin/css/admin-rtl.css

    r12582 r12765  
    2020div#community-profile-page #profile-nav {
    2121    margin-bottom: 1em;
     22}
     23
     24div#community-profile-page #bp-members-profile-member-type {
     25    width: 100%;
    2226}
    2327
  • trunk/src/bp-members/admin/css/admin.css

    r12582 r12765  
    2020div#community-profile-page #profile-nav {
    2121    margin-bottom: 1em;
     22}
     23
     24div#community-profile-page #bp-members-profile-member-type {
     25    width: 100%;
    2226}
    2327
  • trunk/src/bp-members/bp-members-functions.php

    r12754 r12765  
    29942994 *
    29952995 * @since 2.2.0
    2996  *
    2997  * @param int    $user_id     ID of the user.
    2998  * @param string $member_type Member type.
    2999  * @param bool   $append      Optional. True to append this to existing types for user,
    3000  *                            false to replace. Default: false.
     2996 * @since 7.0.0 $member_type parameter also accepts an array of member type names.
     2997 *
     2998 * @param int          $user_id     ID of the user.
     2999 * @param string|array $member_type The member type name or an array of member type names.
     3000 * @param bool         $append      Optional. True to append this to existing types for user,
     3001 *                                  false to replace. Default: false.
    30013002 * @return false|array $retval See {@see bp_set_object_terms()}.
    30023003 */
    30033004function bp_set_member_type( $user_id, $member_type, $append = false ) {
    30043005    // Pass an empty $member_type to remove a user's type.
    3005     if ( ! empty( $member_type ) && ! bp_get_member_type_object( $member_type ) ) {
    3006         return false;
     3006    if ( ! empty( $member_type ) ) {
     3007        $member_types = (array) $member_type;
     3008        $valid_types  = array_filter( array_map( 'bp_get_member_type_object', $member_types ) );
     3009
     3010        if ( $valid_types ) {
     3011            $member_type = wp_list_pluck( $valid_types, 'name' );
     3012        } else {
     3013            return false;
     3014        }
    30073015    }
    30083016
     
    30183026         * @since 2.2.0
    30193027         *
    3020          * @param int    $user_id     ID of the user whose member type has been updated.
    3021          * @param string $member_type Member type.
    3022          * @param bool   $append      Whether the type is being appended to existing types.
     3028         * @param int          $user_id     ID of the user whose member type has been updated.
     3029         * @param string|array $member_type The member type name or an array of member type names.
     3030         * @param bool         $append      Whether the type is being appended to existing types.
    30233031         */
    30243032        do_action( 'bp_set_member_type', $user_id, $member_type, $append );
     
    31233131     * @since 2.2.0
    31243132     *
    3125      * @param string $type    Member type.
    3126      * @param int    $user_id ID of the user.
    3127      * @param bool   $single  Whether to return a single type string, or an array.
     3133     * @param string|array|bool $type    a single member type (if $single is true) or an array of member types
     3134     *                                   (if $single is false) or false on failure.
     3135     * @param int               $user_id ID of the user.
     3136     * @param bool              $single  Whether to return a single type string, or an array.
    31283137     */
    31293138    return apply_filters( 'bp_get_member_type', $type, $user_id, $single );
  • trunk/src/bp-members/classes/class-bp-members-admin.php

    r12725 r12765  
    12871287        }
    12881288
    1289         $types = bp_get_member_types( array(), 'objects' );
    1290         $current_type = bp_get_member_type( $user->ID );
     1289        $types        = bp_get_member_types( array(), 'objects' );
     1290        $current_type = bp_get_member_type( $user->ID, false );
    12911291        ?>
    12921292
    1293         <label for="bp-members-profile-member-type" class="screen-reader-text"><?php
     1293        <label for="bp-members-profile-member-type" class="screen-reader-text">
     1294            <?php
    12941295            /* translators: accessibility text */
    12951296            esc_html_e( 'Select member type', 'buddypress' );
    1296         ?></label>
    1297         <select name="bp-members-profile-member-type" id="bp-members-profile-member-type">
    1298             <option value="" <?php selected( '', $current_type ); ?>><?php
    1299                 /* translators: no option picked in select box */
    1300                 esc_attr_e( '----', 'buddypress' );
    1301             ?></option>
     1297            ?>
     1298        </label>
     1299        <select name="bp-members-profile-member-type[]" id="bp-members-profile-member-type" multiple="multiple">
     1300            <option value="" <?php selected( ! $current_type ); ?>>
     1301                <?php
     1302                    /* translators: no option picked in select box */
     1303                    esc_attr_e( '----', 'buddypress' );
     1304                ?>
     1305            </option>
    13021306            <?php foreach ( $types as $type ) : ?>
    1303                 <option value="<?php echo esc_attr( $type->name ) ?>" <?php selected( $type->name, $current_type ) ?>><?php echo esc_html( $type->labels['singular_name'] ) ?></option>
     1307                <option value="<?php echo esc_attr( $type->name ) ?>" <?php selected( in_array( $type->name, (array) $current_type, true ) ) ?>><?php echo esc_html( $type->labels['singular_name'] ) ?></option>
    13041308            <?php endforeach; ?>
    13051309        </select>
    13061310
    13071311        <?php
    1308 
    13091312        wp_nonce_field( 'bp-member-type-change-' . $user->ID, 'bp-member-type-nonce' );
    13101313    }
     
    13291332        }
    13301333
    1331         // Member type string must either reference a valid member type, or be empty.
    1332         $member_type = stripslashes( $_POST['bp-members-profile-member-type'] );
    1333         if ( ! empty( $member_type ) && ! bp_get_member_type_object( $member_type ) ) {
    1334             return;
    1335         }
     1334        // Member type [string] must either reference a valid member type, or be empty.
     1335        $member_type = wp_parse_slug_list( wp_unslash( $_POST['bp-members-profile-member-type'] ) );
     1336        $member_type = array_filter( $member_type );
    13361337
    13371338        /*
     
    23882389                $user_id = (int) $user_id;
    23892390
    2390                 // Get the old member type to check against.
    2391                 $member_type = bp_get_member_type( $user_id );
    2392 
    2393                 if ( 'remove_member_type' === $new_type ) {
    2394                     // Remove the current member type, if there's one to remove.
    2395                     if ( $member_type ) {
    2396                         $removed = bp_remove_member_type( $user_id, $member_type );
    2397                         if ( false === $removed || is_wp_error( $removed ) ) {
    2398                             $error = true;
    2399                         }
     2391                // Get the old member types to check against.
     2392                $current_types = bp_get_member_type( $user_id, false );
     2393
     2394                if ( $current_types && 'remove_member_type' === $new_type ) {
     2395                    $member_types = array();
     2396                } elseif ( ! $current_types || 1 !== count( $current_types ) || $new_type !== $current_types[0] ) {
     2397                    // Set the new member type.
     2398                    $member_types = array( $new_type );
     2399                }
     2400
     2401                if ( isset( $member_types ) ) {
     2402                    $set = bp_set_member_type( $user_id, $member_types );
     2403                    if ( false === $set || is_wp_error( $set ) ) {
     2404                        $error = true;
    24002405                    }
    2401                 } else {
    2402                     // Set the new member type.
    2403                     if ( $new_type !== $member_type ) {
    2404                         $set = bp_set_member_type( $user_id, $new_type );
    2405                         if ( false === $set || is_wp_error( $set ) ) {
    2406                             $error = true;
    2407                         }
    2408                     }
     2406                    unset( $member_types );
    24092407                }
    24102408            }
     
    24782476
    24792477        // Get the member type.
    2480         $type = bp_get_member_type( $user_id );
    2481 
    2482         // Output the
    2483         if ( $type_obj = bp_get_member_type_object( $type ) ) {
    2484             $url = add_query_arg( array( 'bp-member-type' => urlencode( $type ) ) );
    2485             $retval = '<a href="' . esc_url( $url ) . '">' . esc_html( $type_obj->labels['singular_name'] ) . '</a>';
     2478        $member_type = bp_get_member_type( $user_id, false );
     2479
     2480        // Build the Output.
     2481        if ( $member_type ) {
     2482            $member_types = array_filter( array_map( 'bp_get_member_type_object', $member_type ) );
     2483            if ( ! $member_types ) {
     2484                return $retval;
     2485            }
     2486
     2487            $type_links = array();
     2488            foreach ( $member_types as $type ) {
     2489                $url          = add_query_arg( array( 'bp-member-type' => urlencode( $type->name ) ) );
     2490                $type_links[] = sprintf(
     2491                    '<a href="%1$s">%2$s</a>',
     2492                    esc_url( $url ),
     2493                    esc_html( $type->labels['singular_name'] )
     2494                );
     2495            }
     2496
     2497            $retval = implode( ', ', $type_links );
    24862498        }
    24872499
Note: See TracChangeset for help on using the changeset viewer.