BP_Members_Admin::users_table_process_bulk_type_change()

Process bulk member type change submission from the WP admin users list table.


Source Source

File: bp-members/classes/class-bp-members-admin.php

	public function users_table_process_bulk_type_change() {
		// Output the admin notice.
		$this->users_type_change_notice();

		// Bail if no users are specified or if this isn't a BuddyPress action.
		if ( empty( $_REQUEST['users'] )
			|| ( empty( $_REQUEST['bp_change_type'] ) && empty( $_REQUEST['bp_change_type2'] ) )
			|| empty( $_REQUEST['bp_change_member_type'] )
		) {
			return;
		}

		// Bail if nonce check fails.
		check_admin_referer( 'bp-bulk-users-change-type-' . bp_loggedin_user_id(), 'bp-bulk-users-change-type-nonce' );

		// Bail if current user cannot promote users.
		if ( ! bp_current_user_can( 'promote_users' ) ) {
			return;
		}

		$new_type = '';
		if ( ! empty( $_REQUEST['bp_change_type2'] ) ) {
			$new_type = sanitize_text_field( $_REQUEST['bp_change_type2'] );
		} elseif ( ! empty( $_REQUEST['bp_change_type'] ) ) {
			$new_type = sanitize_text_field( $_REQUEST['bp_change_type'] );
		}

		// Check that the selected type actually exists.
		if ( 'remove_member_type' != $new_type && null === bp_get_member_type_object( $new_type ) ) {
			$error = true;
		} else {
			// Run through user ids.
			$error = false;
			foreach ( (array) $_REQUEST['users'] as $user_id ) {
				$user_id = (int) $user_id;

				// Get the old member types to check against.
				$current_types = bp_get_member_type( $user_id, false );

				if ( $current_types && 'remove_member_type' === $new_type ) {
					$member_types = array();
				} elseif ( ! $current_types || 1 !== count( $current_types ) || $new_type !== $current_types[0] ) {
					// Set the new member type.
					$member_types = array( $new_type );
				}

				if ( isset( $member_types ) ) {
					$set = bp_set_member_type( $user_id, $member_types );
					if ( false === $set || is_wp_error( $set ) ) {
						$error = true;
					}
					unset( $member_types );
				}
			}
		}

		// If there were any errors, show the error message.
		if ( $error ) {
			$redirect = add_query_arg( array( 'updated' => 'member-type-change-error' ), wp_get_referer() );
		} else {
			$redirect = add_query_arg( array( 'updated' => 'member-type-change-success' ), wp_get_referer() );
		}

		wp_redirect( $redirect );
		exit();
	}


Top ↑

Changelog Changelog

Changelog
Version Description
2.7.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.