BP_Members_Admin::users_table_process_bulk_type_change()

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

Description

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();

		// Flag for custom error message.
		$bp_error_message = false;

		// 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 profile type to check against.
				$member_type = bp_get_member_type( $user_id );

				if ( 'remove_member_type' === $new_type ) {
					// Remove the current profile type, if there's one to remove.
					if ( $member_type ) {
						$removed = bp_remove_member_type( $user_id, $member_type );
						if ( false === $removed || is_wp_error( $removed ) ) {
							$error = true;
						}
					}
				} else {

					if ( $user_id === get_current_user_id() ) {

						// Set the new profile type.
						if ( $new_type !== $member_type ) {

							// Get post id of selected profile type.
							$post_id = bp_member_type_post_by_type( $new_type );

							// Get selected profile type role.
							$selected_member_type_wp_roles = get_post_meta( $post_id, '_bp_member_type_wp_roles', true );
							if ( empty( $selected_member_type_wp_roles ) ) {
								$set = bp_set_member_type( $user_id, $new_type );
								if ( false === $set || is_wp_error( $set ) ) {
									$error = true;
								}
							} else {
								if ( 'administrator' !== $selected_member_type_wp_roles[0] ) {
									$bp_error_message = true;
									$error = true;
								} else {
									$set = bp_set_member_type( $user_id, $new_type );
									if ( false === $set || is_wp_error( $set ) ) {
										$error = true;
									}
								}
							}
						}
					} else {

						// Set the new profile type.
						if ( $new_type !== $member_type ) {
							$set = bp_set_member_type( $user_id, $new_type );
							if ( false === $set || is_wp_error( $set ) ) {
								$error = true;
							}
						}
					}
				}
			}
		}

		// If there were any errors, show the error message.
		if ( $error ) {

			if ( true === $bp_error_message ) {
				$redirect = add_query_arg( array( 'updated' => 'member-type-change-owner-error' ), wp_get_referer() );
			} else {
				$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();
	}

Changelog

Changelog
Version Description
BuddyPress 2.7.0 Introduced.

Questions?

We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.