Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
10/24/2020 12:20:39 PM (4 years 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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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.