diff --git src/bp-members/bp-members-functions.php src/bp-members/bp-members-functions.php
index c55e942c6..88a81b2da 100644
|
|
add_action( bp_get_member_type_tax_name() . '_add_form', 'bp_insert_member_types |
2993 | 2993 | * Set type for a member. |
2994 | 2994 | * |
2995 | 2995 | * @since 2.2.0 |
| 2996 | * @since 7.0.0 $member_type parameter also accepts an array of member type names. |
2996 | 2997 | * |
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. |
| 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. |
3001 | 3002 | * @return false|array $retval See {@see bp_set_object_terms()}. |
3002 | 3003 | */ |
3003 | 3004 | function bp_set_member_type( $user_id, $member_type, $append = false ) { |
3004 | 3005 | // 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 | } |
3007 | 3015 | } |
3008 | 3016 | |
3009 | 3017 | $retval = bp_set_object_terms( $user_id, $member_type, bp_get_member_type_tax_name(), $append ); |
… |
… |
function bp_set_member_type( $user_id, $member_type, $append = false ) { |
3017 | 3025 | * |
3018 | 3026 | * @since 2.2.0 |
3019 | 3027 | * |
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. |
3023 | 3031 | */ |
3024 | 3032 | do_action( 'bp_set_member_type', $user_id, $member_type, $append ); |
3025 | 3033 | } |
… |
… |
function bp_get_member_type( $user_id, $single = true, $use_db = true ) { |
3122 | 3130 | * |
3123 | 3131 | * @since 2.2.0 |
3124 | 3132 | * |
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 Member type. |
| 3134 | * @param int $user_id ID of the user. |
| 3135 | * @param bool $single Whether to return a single type string, or an array. |
3128 | 3136 | */ |
3129 | 3137 | return apply_filters( 'bp_get_member_type', $type, $user_id, $single ); |
3130 | 3138 | } |
diff --git src/bp-members/classes/class-bp-members-admin.php src/bp-members/classes/class-bp-members-admin.php
index d344b5d7c..ff0a60d3b 100644
|
|
class BP_Members_Admin { |
1286 | 1286 | return; |
1287 | 1287 | } |
1288 | 1288 | |
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 ); |
1291 | 1291 | ?> |
1292 | 1292 | |
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 |
1294 | 1295 | /* translators: accessibility text */ |
1295 | 1296 | 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> |
1302 | 1306 | <?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> |
1304 | 1308 | <?php endforeach; ?> |
1305 | 1309 | </select> |
1306 | 1310 | |
1307 | 1311 | <?php |
1308 | | |
1309 | 1312 | wp_nonce_field( 'bp-member-type-change-' . $user->ID, 'bp-member-type-nonce' ); |
1310 | 1313 | } |
1311 | 1314 | |
… |
… |
class BP_Members_Admin { |
1328 | 1331 | return; |
1329 | 1332 | } |
1330 | 1333 | |
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 ); |
1336 | 1337 | |
1337 | 1338 | /* |
1338 | 1339 | * If an invalid member type is passed, someone's doing something |
… |
… |
class BP_Members_Admin { |
2387 | 2388 | foreach ( (array) $_REQUEST['users'] as $user_id ) { |
2388 | 2389 | $user_id = (int) $user_id; |
2389 | 2390 | |
2390 | | // Get the old member type to check against. |
2391 | | $member_type = bp_get_member_type( $user_id ); |
| 2391 | // Get the old member types to check against. |
| 2392 | $current_types = bp_get_member_type( $user_id, false ); |
2392 | 2393 | |
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 | | } |
2400 | | } |
2401 | | } else { |
| 2394 | if ( $current_types && 'remove_member_type' === $new_type ) { |
| 2395 | $member_types = array(); |
| 2396 | } elseif ( 1 !== count( $current_types ) || $new_type !== $current_types[0] ) { |
2402 | 2397 | // 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 | | } |
| 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; |
2408 | 2405 | } |
| 2406 | unset( $member_types ); |
2409 | 2407 | } |
2410 | 2408 | } |
2411 | 2409 | } |
… |
… |
class BP_Members_Admin { |
2477 | 2475 | } |
2478 | 2476 | |
2479 | 2477 | // Get the member type. |
2480 | | $type = bp_get_member_type( $user_id ); |
| 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 | } |
2481 | 2496 | |
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>'; |
| 2497 | $retval = implode( ', ', $type_links ); |
2486 | 2498 | } |
2487 | 2499 | |
2488 | 2500 | return $retval; |