<?php
/**
 * BuddyPress Groups component admin screen.
 *
 * Props to WordPress core for the Comments admin screen, and its contextual
 * help text, on which this implementation is heavily based.
 *
 * @package BuddyPress
 * @subpackage Groups
 * @since 1.7.0
 */

// Exit if accessed directly.
defined( 'ABSPATH' ) || exit;

// Include WP's list table class.
if ( ! class_exists( 'WP_List_Table' ) ) {
	require ABSPATH . 'wp-admin/includes/class-wp-list-table.php';
}

/**
 * Register the Groups component admin screen.
 *
 * @since 1.7.0
 */
function bp_groups_add_admin_menu() {

	// Add our screen.
	$hook = add_menu_page(
		_x( 'Groups', 'Admin Groups page title', 'buddypress' ),
		_x( 'Groups', 'Admin Groups menu', 'buddypress' ),
		'bp_moderate',
		'bp-groups',
		'bp_groups_admin',
		'div'
	);

	// Hook into early actions to load custom CSS and our init handler.
	add_action( "load-$hook", 'bp_groups_admin_load' );
}
add_action( bp_core_admin_hook(), 'bp_groups_add_admin_menu' );

/**
 * Redirects the user on the Goups network admin screen when BuddyPress is network activated.
 *
 * @since 7.0.0
 */
function bp_group_site_admin_network_admin_redirect() {
	wp_safe_redirect( add_query_arg( 'page', 'bp-groups', network_admin_url( 'admin.php' ) ) );
	exit;
}

/**
 * Create Groups submenu to manage BuddyPress types.
 *
 * @since 7.0.0
 */
function bp_groups_admin_types_menu() {
	if ( ! bp_is_root_blog() ) {
		return;
	}

	if ( bp_is_network_activated() && ! bp_is_multiblog_mode() && is_network_admin() ) {
		// Adds a 'bp-groups' submenu to go to the root blog Group types screen.
		$group_type_admin_url = add_query_arg( 'taxonomy', 'bp_group_type', get_admin_url( bp_get_root_blog_id(), 'edit-tags.php' ) );
		add_submenu_page(
			'bp-groups',
			__( 'Group Types', 'buddypress' ),
			__( 'Group Types', 'buddypress' ),
			'bp_moderate',
			esc_url( $group_type_admin_url )
		);
	} elseif ( ! is_network_admin() ) {
		if ( bp_is_network_activated() && ! bp_is_multiblog_mode() ) {
			// Adds a 'bp-groups' menu to the root blog menu.
			$redirect_hook = add_menu_page(
				_x( 'Groups', 'Admin Groups page title', 'buddypress' ),
				_x( 'Groups', 'Admin Groups menu', 'buddypress' ),
				'bp_moderate',
				'bp-groups',
				'__return_empty_string',
				'div'
			);

			add_action( "load-{$redirect_hook}", 'bp_group_site_admin_network_admin_redirect' );
		}

		// Add the submenu to manage Group Types.
		add_submenu_page(
			'bp-groups',
			__( 'Group Types', 'buddypress' ),
			__( 'Group Types', 'buddypress' ),
			'bp_moderate',
			basename( add_query_arg( 'taxonomy', 'bp_group_type', bp_get_admin_url( 'edit-tags.php' ) ) )
		);
	}
}
add_action( 'bp_admin_menu', 'bp_groups_admin_types_menu' );

/**
 * Add groups component to custom menus array.
 *
 * This ensures that the Groups menu item appears in the proper order on the
 * main Dashboard menu.
 *
 * @since 1.7.0
 *
 * @param array $custom_menus Array of BP top-level menu items.
 * @return array Menu item array, with Groups added.
 */
function bp_groups_admin_menu_order( $custom_menus = array() ) {
	array_push( $custom_menus, 'bp-groups' );
	return $custom_menus;
}
add_filter( 'bp_admin_menu_order', 'bp_groups_admin_menu_order' );

/**
 * Set up the Groups admin page.
 *
 * Loaded before the page is rendered, this function does all initial setup,
 * including: processing form requests, registering contextual help, and
 * setting up screen options.
 *
 * @since 1.7.0
 *
 * @global BP_Groups_List_Table $bp_groups_list_table Groups screen list table.
 */
function bp_groups_admin_load() {
	global $bp_groups_list_table;
	$bp = buddypress();

	// Traces the current BP Admin screen.
	$bp->admin->trace_current_screen();

	// Build redirection URL.
	$redirect_to = remove_query_arg( array( 'action', 'action2', 'gid', 'deleted', 'error', 'updated' ), $_SERVER['REQUEST_URI'] );

	$doaction   = bp_admin_list_table_current_bulk_action();
	$min        = bp_core_get_minified_asset_suffix();

	/**
	 * Fires at top of groups admin page.
	 *
	 * @since 1.7.0
	 *
	 * @param string $doaction Current $_GET action being performed in admin screen.
	 */
	do_action( 'bp_groups_admin_load', $doaction );

	// Edit screen.
	if ( 'do_delete' == $doaction && ! empty( $_GET['gid'] ) ) {

		check_admin_referer( 'bp-groups-delete' );

		$group_ids = wp_parse_id_list( $_GET['gid'] );

		$count = 0;
		foreach ( $group_ids as $group_id ) {
			if ( groups_delete_group( $group_id ) ) {
				$count++;
			}
		}

		$redirect_to = add_query_arg( 'deleted', $count, $redirect_to );

		bp_core_redirect( $redirect_to );

	} elseif ( 'edit' == $doaction && ! empty( $_GET['gid'] ) ) {
		// Columns screen option.
		add_screen_option( 'layout_columns', array( 'default' => 2, 'max' => 2, ) );

		get_current_screen()->add_help_tab( array(
			'id'      => 'bp-group-edit-overview',
			'title'   => __( 'Overview', 'buddypress' ),
			'content' =>
				'<p>' . __( 'This page is a convenient way to edit the details associated with one of your groups.', 'buddypress' ) . '</p>' .
				'<p>' . __( 'The Name and Description box is fixed in place, but you can reposition all the other boxes using drag and drop, and can minimize or expand them by clicking the title bar of each box. Use the Screen Options tab to hide or unhide, or to choose a 1- or 2-column layout for this screen.', 'buddypress' ) . '</p>'
		) );

		// Help panel - sidebar links.
		get_current_screen()->set_help_sidebar(
			'<p><strong>' . __( 'For more information:', 'buddypress' ) . '</strong></p>' .
			'<p><a href="https://buddypress.org/support">' . __( 'Support Forums', 'buddypress' ) . '</a></p>'
		);

		// Register metaboxes for the edit screen.
		add_meta_box( 'submitdiv', _x( 'Save', 'group admin edit screen', 'buddypress' ), 'bp_groups_admin_edit_metabox_status', get_current_screen()->id, 'side', 'high' );
		add_meta_box( 'bp_group_settings', _x( 'Settings', 'group admin edit screen', 'buddypress' ), 'bp_groups_admin_edit_metabox_settings', get_current_screen()->id, 'side', 'core' );
		add_meta_box( 'bp_group_add_members', _x( 'Add New Members', 'group admin edit screen', 'buddypress' ), 'bp_groups_admin_edit_metabox_add_new_members', get_current_screen()->id, 'normal', 'core' );
		add_meta_box( 'bp_group_members', _x( 'Manage Members', 'group admin edit screen', 'buddypress' ), 'bp_groups_admin_edit_metabox_members', get_current_screen()->id, 'normal', 'core' );

		// Group Type metabox. Only added if group types have been registered.
		$group_types = bp_groups_get_group_types();
		if ( ! empty( $group_types ) ) {
			add_meta_box(
				'bp_groups_admin_group_type',
				_x( 'Group Type', 'groups admin edit screen', 'buddypress' ),
				'bp_groups_admin_edit_metabox_group_type',
				get_current_screen()->id,
				'side',
				'core'
			);
		}

		/**
		 * Fires after the registration of all of the default group meta boxes.
		 *
		 * @since 1.7.0
		 */
		do_action( 'bp_groups_admin_meta_boxes' );

		// Enqueue JavaScript files.
		wp_enqueue_script( 'postbox' );
		wp_enqueue_script( 'dashboard' );

	// Index screen.
	} else {
		// Create the Groups screen list table.
		$bp_groups_list_table = new BP_Groups_List_Table();

		// The per_page screen option.
		add_screen_option( 'per_page', array( 'label' => _x( 'Groups', 'Groups per page (screen options)', 'buddypress' )) );

		// Help panel - overview text.
		get_current_screen()->add_help_tab( array(
			'id'      => 'bp-groups-overview',
			'title'   => __( 'Overview', 'buddypress' ),
			'content' =>
				'<p>' . __( 'You can manage groups much like you can manage comments and other content. This screen is customizable in the same ways as other management screens, and you can act on groups by using the on-hover action links or the Bulk Actions.', 'buddypress' ) . '</p>',
		) );

		get_current_screen()->add_help_tab( array(
			'id'      => 'bp-groups-overview-actions',
			'title'   => __( 'Group Actions', 'buddypress' ),
			'content' =>
				'<p>' . __( 'Clicking "View" will take you to the group&#8217;s public page. Use this link to see what the group looks like on the front end of your site.', 'buddypress' ) . '</p>' .
				'<p>' . __( 'Clicking "Edit" will take you to a Dashboard panel where you can manage various details about the group, such as its name and description, its members, and other settings.', 'buddypress' ) . '</p>' .
				'<p>' . __( 'If you click "Delete" under a specific group, or select a number of groups and then choose Delete from the Bulk Actions menu, you will be led to a page where you&#8217;ll be asked to confirm the permanent deletion of the group(s).', 'buddypress' ) . '</p>',
		) );

		// Help panel - sidebar links.
		get_current_screen()->set_help_sidebar(
			'<p><strong>' . __( 'For more information:', 'buddypress' ) . '</strong></p>' .
			'<p>' . __( '<a href="https://buddypress.org/support/">Support Forums</a>', 'buddypress' ) . '</p>'
		);

		// Add accessible hidden heading and text for Groups screen pagination.
		get_current_screen()->set_screen_reader_content( array(
			/* translators: accessibility text */
			'heading_pagination' => __( 'Groups list navigation', 'buddypress' ),
		) );
	}

	// Enqueue CSS and JavaScript.
	wp_enqueue_script( 'bp_groups_admin_js', $bp->plugin_url . "bp-groups/admin/js/admin{$min}.js", array( 'jquery', 'wp-ajax-response', 'jquery-ui-autocomplete' ), bp_get_version(), true );
	wp_localize_script( 'bp_groups_admin_js', 'BP_Group_Admin', array(
		'add_member_placeholder' => __( 'Start typing a username to add a new member.', 'buddypress' ),
		'warn_on_leave'          => __( 'If you leave this page, you will lose any unsaved changes you have made to the group.', 'buddypress' ),
	) );
	wp_enqueue_style( 'bp_groups_admin_css', $bp->plugin_url . "bp-groups/admin/css/admin{$min}.css", array(), bp_get_version() );

	wp_style_add_data( 'bp_groups_admin_css', 'rtl', 'replace' );
	if ( $min ) {
		wp_style_add_data( 'bp_groups_admin_css', 'suffix', $min );
	}

	// Saving group edits.
	if ( $doaction && 'save' == $doaction ) {
		// Get group ID.
		$group_id = 0;
		if ( isset( $_REQUEST['gid'] ) ) {
			$group_id = (int) wp_unslash( $_REQUEST['gid'] );
		}

		$redirect_to = add_query_arg(
			array(
				'gid'    => $group_id,
				'action' => 'edit'
			),
			$redirect_to
		);

		// Check this is a valid form submission.
		check_admin_referer( 'edit-group_' . $group_id );

		// Get the group from the database.
		$group = groups_get_group( $group_id );

		// If the group doesn't exist, just redirect back to the index.
		if ( empty( $group->slug ) ) {
			wp_safe_redirect( $redirect_to );
			exit;
		}

		/*
		 * Check the form for the updated properties.
		 * Store errors.
		 */
		$error   = array();
		$updated = array();

		// Name must not be empty.
		$group_name = '';
		if ( empty( $_POST['bp-groups-name'] ) ) {
			$error['missing'][] = _x( 'name', 'group admin field', 'buddypress' );
		} else {
			$group_name = sanitize_text_field( wp_unslash( $_POST['bp-groups-name'] ) );
		}

		// Description must not be empty.
		$group_description = '';
		if ( empty( $_POST['bp-groups-description'] ) ) {
			$error['missing'][] = _x( 'description', 'group admin field', 'buddypress' );
		} else {
			$group_description = sanitize_textarea_field( wp_unslash( $_POST['bp-groups-description'] ) );
		}

		//Slug must not be empty.
		$group_slug = '';
		if ( empty( $_POST['bp-groups-slug'] ) ) {
			$error['missing'][] = _x( 'slug', 'group admin field', 'buddypress' );
		} else {
			$group_slug = sanitize_title( wp_unslash( $_POST['bp-groups-slug'] ) );
		}

		/*
		 * Group name, slug, and description are handled with
		 * groups_edit_base_group_details().
		 */
		if ( ! $error ) {
			$updated_group = groups_edit_base_group_details(
				array(
					'group_id'       => $group_id,
					'name'           => $group_name,
					'slug'           => $group_slug,
					'description'    => $group_description,
					'notify_members' => false,
				)
			);

			if ( ! $updated_group ) {
				$error['group_details'] = _x( 'group details', 'group admin group details error', 'buddypress' );
			} else {
				if ( $group_name !== $group->name ) {
					$updated['group_details'][] = _x( 'name', 'group admin field', 'buddypress' );
				}

				if ( $group_description !== $group->description ) {
					$updated['group_details'][] = _x( 'description', 'group admin field', 'buddypress' );
				}

				if ( $group_slug !== $group->slug ) {
					$updated['group_details'][] = _x( 'slug', 'group admin field', 'buddypress' );
				}
			}
		}

		// Enable discussion forum.
		$enable_forum = 0;
		if ( isset( $_POST['group-show-forum'] ) ) {
			$enable_forum = 1;
		};

		/**
		 * Filters the allowed status values for the group.
		 *
		 * @since 1.0.2
		 *
		 * @param array $value Array of allowed group statuses.
		 */
		$allowed_status = apply_filters( 'groups_allowed_status', array( 'public', 'private', 'hidden' ) );
		$status         = 'public';
		if ( isset( $_POST['group-status'] ) ) {
			$sent_status = sanitize_text_field( wp_unslash( $_POST['group-status'] ) );

			if ( in_array( $sent_status, (array) $allowed_status, true ) ) {
				$status = $sent_status;
			}
		}

		/**
		 * Filters the allowed invite status values for the group.
		 *
		 * @since 1.5.0
		 *
		 * @param array $value Array of allowed invite statuses.
		 */
		$allowed_invite_status     = apply_filters( 'groups_allowed_invite_status', array( 'members', 'mods', 'admins' ) );
		$invite_status             = 'members';
		$is_updating_invite_status = false;
		if ( isset( $_POST['group-invite-status'] ) ) {
			$sent_invite_status = sanitize_text_field( wp_unslash( $_POST['group-invite-status'] ) );

			if ( in_array( $sent_invite_status, (array) $allowed_invite_status, true ) ) {
				$invite_status             = $sent_invite_status;
				$is_updating_invite_status = $invite_status !== groups_get_groupmeta( $group->id, 'invite_status' );
			}
		}

		if ( ! groups_edit_group_settings( $group_id, $enable_forum, $status, $invite_status ) ) {
			$error['group_settings'] = _x( 'group settings', 'group admin group details error', 'buddypress' );
		} else {
			if ( $is_updating_invite_status ) {
				$updated['group_settings'][] = _x( 'group invite status', 'group admin group details updated', 'buddypress' );
			}

			if ( $status !== $group->status ) {
				$updated['group_settings'][] = _x( 'group status', 'group admin group details updated', 'buddypress' );
			}
		}

		// Process new members.
		$user_names = array();
		if ( ! empty( $_POST['bp-groups-new-members'] ) ) {
			$sent_usernames = array_map( 'sanitize_user', explode( ',', wp_unslash( $_POST['bp-groups-new-members'] ) ) );
			$user_names     = array_merge( $user_names, $sent_usernames );
		}

		if ( ! empty( $user_names ) ) {
			$new_members = count( $user_names );

			if ( 1 < $new_members ) {
				bp_groups_defer_group_members_count( true );
			}

			foreach ( array_values( $user_names ) as $user_name ) {
				$un = trim( $user_name );

				// Make sure the user exists before attempting
				// to add to the group.
				$user = get_user_by( 'slug', $un );

				if ( empty( $user ) ) {
					$error['new_user'][] = $un;
				} else {
					if ( ! groups_join_group( $group_id, $user->ID ) ) {
						$error['new_user'][] = $un;
					} else {
						$updated['new_user'][] = $un;
					}
				}
			}

			if ( 1 < $new_members ) {
				bp_groups_defer_group_members_count( false, $group_id );
			}
		}

		// Process member role changes.
		if ( ! empty( $_POST['bp-groups-role'] ) && ! empty( $_POST['bp-groups-existing-role'] ) ) {
			$new_group_role     = array_map( 'sanitize_text_field', (array) wp_unslash( $_POST['bp-groups-role'] ) );
			$current_group_role = array_map( 'sanitize_text_field', (array) wp_unslash( $_POST['bp-groups-existing-role'] ) );

			// Before processing anything, make sure you're not
			// attempting to remove the all user admins.
			$admin_count = 0;
			foreach ( $new_group_role as $new_role ) {
				if ( 'admin' == $new_role ) {
					$admin_count++;
					break;
				}
			}

			if ( ! $admin_count ) {
				$error['no_admins'] = 1;

			} else {

				// Process only those users who have had their roles changed.
				foreach ( $new_group_role as $user_id => $new_role ) {
					$user_id = (int) $user_id;

					$existing_role = isset( $current_group_role[ $user_id ] ) ? $current_group_role[ $user_id ] : '';

					if ( $existing_role !== $new_role ) {
						$result = false;

						switch ( $new_role ) {
							case 'mod' :
								// Admin to mod is a demotion. Demote to
								// member, then fall through.
								if ( 'admin' === $existing_role ) {
									$result = groups_demote_member( $user_id, $group_id );
								}

							case 'admin' :
								// If the user was banned, we must
								// unban first.
								if ( 'banned' === $existing_role ) {
									groups_unban_member( $user_id, $group_id );
								}

								// At this point, each existing_role
								// is a member, so promote.
								$result = groups_promote_member( $user_id, $group_id, $new_role );

								break;

							case 'member' :

								if ( 'admin' === $existing_role || 'mod' === $existing_role ) {
									$result = groups_demote_member( $user_id, $group_id );
								} elseif ( 'banned' === $existing_role ) {
									$result = groups_unban_member( $user_id, $group_id );
								}

								break;

							case 'banned' :

								$result = groups_ban_member( $user_id, $group_id );

								break;

							case 'remove' :

								$result = groups_remove_member( $user_id, $group_id );

								break;
						}

						// Store the success or failure.
						if ( $result ) {
							$updated['roles'][ $new_role ][] = $user_id;
						} else {
							$error['roles'][ $new_role ][] = $user_id;
						}
					}
				}
			}
		}

		/**
		 * Fires before redirect so plugins can do something first on save action.
		 *
		 * @since 1.6.0
		 *
		 * @param int $group_id ID of the group being edited.
		 */
		do_action( 'bp_group_admin_edit_after', $group_id );

		$redirect_args = array();

		// Create the redirect URL.
		if ( $error ) {
			// This means there was an error updating group details.
			$redirect_args['error'] = $error;
		}

		if ( $updated ) {
			// Group details were update successfully.
			$redirect_args['updated'] = $updated;
		}

		$redirect_to = add_query_arg( $redirect_args, $redirect_to );

		/**
		 * Filters the URL to redirect to after successfully editing a group.
		 *
		 * @since 1.7.0
		 *
		 * @param string $redirect_to URL to redirect user to.
		 */
		wp_safe_redirect( apply_filters( 'bp_group_admin_edit_redirect', $redirect_to ) );
		exit;


	// If a referrer and a nonce is supplied, but no action, redirect back.
	} elseif ( ! empty( $_GET['_wp_http_referer'] ) ) {
		wp_safe_redirect( remove_query_arg( array( '_wp_http_referer', '_wpnonce' ), stripslashes( $_SERVER['REQUEST_URI'] ) ) );
		exit;
	}
}

/**
 * Select the appropriate Groups admin screen, and output it.
 *
 * @since 1.7.0
 */
function bp_groups_admin() {
	// Decide whether to load the index or edit screen.
	$doaction = bp_admin_list_table_current_bulk_action();

	// Display the single group edit screen.
	if ( 'edit' == $doaction && ! empty( $_GET['gid'] ) ) {
		bp_groups_admin_edit();

	// Display the group deletion confirmation screen.
	} elseif ( 'delete' == $doaction && ! empty( $_GET['gid'] ) ) {
		bp_groups_admin_delete();

	// Otherwise, display the groups index screen.
	} else {
		bp_groups_admin_index();
	}
}

/**
 * Display the single groups edit screen.
 *
 * @since 1.7.0
 */
function bp_groups_admin_edit() {

	if ( ! bp_current_user_can( 'bp_moderate' ) ) {
		die( '-1' );
	}

	$messages = array();

	// If the user has just made a change to a group, build status messages.
	if ( ! empty( $_REQUEST['error'] ) || ! empty( $_REQUEST['updated'] ) ) {
		$group_roles = bp_groups_get_group_roles();

		// Fake a group role for removed members.
		$group_roles[] = (object) array(
			'id' => 'remove',
		);

		$error = array();
		if ( ! empty( $_REQUEST['error'] ) ) {
			$error = (array) wp_unslash( $_REQUEST['error'] );

			foreach ( $error as $error_key => $error_data ) {
				if ( 'missing' === $error_key ) {
					$error_data = implode( ', ', $error_data );

					/* translators: %s is a comma separated list of required fields. */
					$messages['error'][] = sprintf( __( '%s are required fields, please make sure to define their values.', 'buddypress' ), esc_html( $error_data ) );

				} elseif ( 'group_details' === $error_key ) {
					$messages['error'][] = __( 'An error occurred when trying to update your group details.', 'buddypress' );

				} elseif ( 'group_settings' === $error_key ) {
					$messages['error'][] = __( 'An error occurred when trying to update your group settings.', 'buddypress' );

				} elseif ( 'new_user' === $error_key ) {
					$error_data = implode( ', ', $error_data );

					/* translators: %s: comma separated list of usernames */
					$messages['error'][] = sprintf( __( 'The following users could not be added to the group: %s.', 'buddypress' ), esc_html( $error_data ) );

				} elseif ( 'no_admins' === $error_key ) {
					$messages['error'][] = __( 'You cannot remove all administrators from a group.', 'buddypress' );

					// Deal with promoted/demoted members.
				} elseif ( 'roles' === $error_key ) {
					foreach ( $group_roles as $group_role ) {
						if ( isset( $error_data[ $group_role->id ] ) ) {
							$group_members_un = bp_groups_admin_get_usernames_from_ids( $error_data[ $group_role->id ] );

							if ( isset( $group_role->plural_name ) ) {
								$messages['error'][] = sprintf(
									/* translators: 1: comma separated list of usernames. 2: group role plural name. */
									__( 'An error occured when trying to update the following members: %1$s as %2$s.', 'buddypress' ),
									'<em>' . esc_html( implode( ', ', $group_members_un ) ) . '</em>',
									esc_html( $group_role->plural_name )
								);

								// Deal with "banned" role.
							} elseif ( 'banned' === $group_role->id ) {
								/* translators: %s: comma separated list of usernames. */
								$messages['error'][] = sprintf( __( 'An error occured when trying to ban the following members: %s.', 'buddypress' ), '<em>' . esc_html( implode( ', ', $group_members_un ) ) . '</em>' );

								// Deal with fake "remove" role.
							} elseif ( 'remove' === $group_role->id ) {
								/* translators: %s: comma separated list of usernames. */
								$messages['error'][] = sprintf( __( 'An error occured when trying to remove the following members: %s.', 'buddypress' ), '<em>' . esc_html( implode( ', ', $group_members_un ) ) . '</em>' );
							}
						}
					}
				}
			}
		}

		$updated = array();
		if ( ! empty( $_REQUEST['updated'] ) ) {
			$updated = (array) wp_unslash( $_REQUEST['updated'] );

			foreach ( $updated as $updated_key => $updated_data ) {
				if ( 'roles' === $updated_key ) {
					foreach ( $group_roles as $group_role ) {
						if ( isset( $updated_data[ $group_role->id ] ) ) {
							$group_members_un = bp_groups_admin_get_usernames_from_ids( $updated_data[ $group_role->id ] );

							if ( isset( $group_role->plural_name ) ) {
								$messages['updated'][] = sprintf(
									/* translators: 1: comma separated list of usernames. 2: group role plural name. */
									__( 'The following members: %1$s are now %2$s.', 'buddypress' ),
									'<em>' . esc_html( implode( ', ', $group_members_un ) ) . '</em>',
									esc_html( $group_role->plural_name )
								);

								// Deal with "banned" role.
							} elseif ( 'banned' === $group_role->id ) {
								/* translators: %s: comma separated list of usernames */
								$messages['updated'][] = sprintf( __( 'The following members were banned: %s.', 'buddypress' ), '<em>' . esc_html( implode( ', ', $group_members_un ) ) . '</em>' );

								// Deal with fake "remove" role.
							} elseif ( 'remove' === $group_role->id ) {
								/* translators: %s: comma separated list of usernames */
								$messages['updated'][] = sprintf( __( 'The following members were removed: %s.', 'buddypress' ), '<em>' . esc_html( implode( ', ', $group_members_un ) ) . '</em>' );
							}
						}
					}
				} else {
					$edited_data = implode( ', ', $updated_data );

					if ( 'group_details' === $updated_key ) {
						/* translators: %s: comma separated list of group details */
						$messages['updated'][] = sprintf( __( 'These details about the group were successfully updated: %s.', 'buddypress' ), esc_html( $edited_data ) );

					} elseif ( 'group_settings' === $updated_key ) {
						/* translators: %s: comma separated list of group settings */
						$messages['updated'][] = sprintf( __( 'These settings about the group were successfully updated: %s.', 'buddypress' ), esc_html( $edited_data ) );

					} elseif ( 'new_user' === $updated_key ) {
						/* translators: %s: comma separated list of usernames */
						$messages['updated'][] = sprintf( __( 'The following users were successfully added to the group: %s.', 'buddypress' ), esc_html( $edited_data ) );
					}
				}
			}
		}
	}

	// Get the group from the database.
	$group = groups_get_group( (int) $_GET['gid'] );

	$group_name = isset( $group->name ) ? bp_get_group_name( $group ) : '';

	// Construct URL for form.
	$form_url   = remove_query_arg( array( 'action', 'deleted', 'error', 'updated' ), $_SERVER['REQUEST_URI'] );
	$form_url   = add_query_arg( 'action', 'save', $form_url );
	$create_url = bp_groups_get_create_url();

	/**
	 * Fires before the display of the edit form.
	 *
	 * Useful for plugins to modify the group before display.
	 *
	 * @since 1.7.0
	 *
	 * @param BP_Groups_Group $group Instance of the current group being edited. Passed by reference.
	 */
	do_action_ref_array( 'bp_groups_admin_edit', array( &$group ) ); ?>

	<div class="wrap">
		<h1 class="wp-heading-inline"><?php esc_html_e( 'Edit Group', 'buddypress' ); ?></h1>

		<?php if ( is_user_logged_in() && bp_user_can_create_groups() ) : ?>
			<a class="page-title-action" href="<?php echo esc_url( $create_url ); ?>"><?php esc_html_e( 'Add New', 'buddypress' ); ?></a>
		<?php endif; ?>

		<hr class="wp-header-end">

		<?php if ( ! empty( $messages ) ) : ?>
			<?php foreach ( $messages as $type => $messages_list ) : ?>
				<div id="moderated" class="<?php echo esc_attr( $type ); ?> notice is-dismissible"><p><?php echo implode( "</p><p>", array_map( 'wp_kses_post', $messages_list ) ); ?></p></div>
			<?php endforeach; ?>
		<?php endif; ?>

		<?php if ( $group->id ) : ?>

			<form action="<?php echo esc_url( $form_url ); ?>" id="bp-groups-edit-form" method="post">
				<div id="poststuff">

					<div id="post-body" class="metabox-holder columns-<?php echo 1 == get_current_screen()->get_columns() ? '1' : '2'; ?>">
						<div id="post-body-content">
							<div id="postdiv">
								<div id="bp_groups_name" class="groupbox">
									<h2><?php esc_html_e( 'Name and Description', 'buddypress' ); ?></h2>
									<div class="inside">
										<label for="bp-groups-name" class="screen-reader-text">
											<?php
												/* translators: accessibility text */
												esc_html_e( 'Group Name', 'buddypress' );
											?>
										</label>
										<input type="text" name="bp-groups-name" id="bp-groups-name" value="<?php echo esc_attr( stripslashes( $group_name ) ) ?>" />
										<div id="bp-groups-permalink-box">
											<strong><?php esc_html_e( 'Permalink:', 'buddypress' ) ?></strong>
											<span id="bp-groups-permalink">
												<?php bp_groups_directory_url(); ?> <input type="text" id="bp-groups-slug" name="bp-groups-slug" value="<?php bp_group_slug( $group ); ?>" autocomplete="off"> /
											</span>
											<a href="<?php bp_group_url( $group ) ?>" class="button button-small" id="bp-groups-visit-group"><?php esc_html_e( 'View Group', 'buddypress' ) ?></a>
										</div>

										<label for="bp-groups-description" class="screen-reader-text">
											<?php
												/* translators: accessibility text */
												esc_html_e( 'Group Description', 'buddypress' );
											?>
										</label>
										<?php wp_editor( stripslashes( $group->description ), 'bp-groups-description', array( 'media_buttons' => false, 'teeny' => true, 'textarea_rows' => 5, 'quicktags' => array( 'buttons' => 'strong,em,link,block,del,ins,img,code,spell,close' ) ) ); ?>
									</div>
								</div>
							</div>
						</div><!-- #post-body-content -->

						<div id="postbox-container-1" class="postbox-container">
							<?php do_meta_boxes( get_current_screen()->id, 'side', $group ); ?>
						</div>

						<div id="postbox-container-2" class="postbox-container">
							<?php do_meta_boxes( get_current_screen()->id, 'normal', $group ); ?>
							<?php do_meta_boxes( get_current_screen()->id, 'advanced', $group ); ?>
						</div>
					</div><!-- #post-body -->

				</div><!-- #poststuff -->
				<?php wp_nonce_field( 'closedpostboxes', 'closedpostboxesnonce', false ); ?>
				<?php wp_nonce_field( 'meta-box-order', 'meta-box-order-nonce', false ); ?>
				<?php wp_nonce_field( 'edit-group_' . $group->id ); ?>
			</form>

		<?php else : ?>

			<p>
				<?php
					printf(
						'%1$s <a href="%2$s">%3$s</a>',
						esc_html__( 'No group found with this ID.', 'buddypress' ),
						esc_url( bp_get_admin_url( 'admin.php?page=bp-groups' ) ),
						esc_html__( 'Go back and try again.', 'buddypress' )
					);
				?>
			</p>

		<?php endif; ?>

	</div><!-- .wrap -->

<?php
}

/**
 * Display the Group delete confirmation screen.
 *
 * We include a separate confirmation because group deletion is truly
 * irreversible.
 *
 * @since 1.7.0
 */
function bp_groups_admin_delete() {

	if ( ! bp_current_user_can( 'bp_moderate' ) ) {
		die( '-1' );
	}

	$group_ids = isset( $_REQUEST['gid'] ) ? $_REQUEST['gid'] : 0;
	if ( ! is_array( $group_ids ) ) {
		$group_ids = explode( ',', $group_ids );
	}
	$group_ids = wp_parse_id_list( $group_ids );
	$groups    = groups_get_groups( array(
		'include'     => $group_ids,
		'show_hidden' => true,
		'per_page'    => null, // Return all results.
	) );

	// Create a new list of group ids, based on those that actually exist.
	$gids = array();
	foreach ( $groups['groups'] as $group ) {
		$gids[] = $group->id;
	}

	$base_url = remove_query_arg( array( 'action', 'action2', 'paged', 's', '_wpnonce', 'gid' ), $_SERVER['REQUEST_URI'] ); ?>

	<div class="wrap">
		<h1 class="wp-heading-inline"><?php esc_html_e( 'Delete Groups', 'buddypress' ) ?></h1>
		<hr class="wp-header-end">

		<p><?php esc_html_e( 'You are about to delete the following groups:', 'buddypress' ) ?></p>

		<ul class="bp-group-delete-list">
		<?php foreach ( $groups['groups'] as $group ) : ?>
			<li><?php echo esc_html( bp_get_group_name( $group ) ); ?></li>
		<?php endforeach; ?>
		</ul>

		<p><strong><?php esc_html_e( 'This action cannot be undone.', 'buddypress' ) ?></strong></p>

		<a class="button-primary" href="<?php echo esc_url( wp_nonce_url( add_query_arg( array( 'action' => 'do_delete', 'gid' => implode( ',', $gids ) ), $base_url ), 'bp-groups-delete' ) ); ?>"><?php esc_html_e( 'Delete Permanently', 'buddypress' ) ?></a>
		<a class="button" href="<?php echo esc_attr( $base_url ); ?>"><?php esc_html_e( 'Cancel', 'buddypress' ) ?></a>
	</div>

	<?php
}

/**
 * Display the Groups admin index screen.
 *
 * This screen contains a list of all BuddyPress groups.
 *
 * @since 1.7.0
 *
 * @global BP_Groups_List_Table $bp_groups_list_table Group screen list table.
 * @global string $plugin_page Currently viewed plugin page.
 */
function bp_groups_admin_index() {
	global $bp_groups_list_table, $plugin_page;

	$messages = array();

	// If the user has just made a change to a group, build status messages.
	if ( ! empty( $_REQUEST['deleted'] ) ) {
		$deleted  = ! empty( $_REQUEST['deleted'] ) ? (int) $_REQUEST['deleted'] : 0;

		if ( $deleted > 0 ) {
			/* translators: %s: number of deleted groups */
			$messages[] = sprintf( _n( '%s group has been permanently deleted.', '%s groups have been permanently deleted.', $deleted, 'buddypress' ), number_format_i18n( $deleted ) );
		}
	}

	// Prepare the group items for display.
	$bp_groups_list_table->prepare_items();
	$create_url = bp_groups_get_create_url();

	/**
	 * Fires before the display of messages for the edit form.
	 *
	 * Useful for plugins to modify the messages before display.
	 *
	 * @since 1.7.0
	 *
	 * @param array $messages Array of messages to be displayed.
	 */
	do_action( 'bp_groups_admin_index', $messages ); ?>

	<div class="wrap">

		<h1 class="wp-heading-inline"><?php esc_html_e( 'Groups', 'buddypress' ); ?></h1>

		<?php if ( is_user_logged_in() && bp_user_can_create_groups() ) : ?>
			<a class="page-title-action" href="<?php echo esc_url( $create_url ); ?>"><?php esc_html_e( 'Add New', 'buddypress' ); ?></a>
		<?php endif; ?>

		<?php if ( ! empty( $_REQUEST['s'] ) ) : ?>
			<span class="subtitle"><?php printf( esc_html__( 'Search results for &#8220;%s&#8221;', 'buddypress' ), esc_html( wp_html_excerpt( stripslashes( $_REQUEST['s'] ), 50 ) ) ); ?></span>
		<?php endif; ?>

		<hr class="wp-header-end">

		<?php // If the user has just made a change to an group, display the status messages. ?>
		<?php if ( ! empty( $messages ) ) : ?>
			<div id="moderated" class="<?php echo esc_attr( ( ! empty( $_REQUEST['error'] ) ) ? 'error' : 'updated' ); ?> notice is-dismissible"><p><?php echo implode( "<br/>\n", array_map( 'esc_html', $messages ) ); ?></p></div>
		<?php endif; ?>

		<?php // Display each group on its own row. ?>
		<?php $bp_groups_list_table->views(); ?>

		<form id="bp-groups-form" action="" method="get">
			<?php $bp_groups_list_table->search_box( esc_html__( 'Search all Groups', 'buddypress' ), 'bp-groups' ); ?>
			<input type="hidden" name="page" value="<?php echo esc_attr( $plugin_page ); ?>" />
			<?php $bp_groups_list_table->display(); ?>
		</form>

	</div>

<?php
}

/**
 * Markup for the single group's Settings metabox.
 *
 * @since 1.7.0
 *
 * @param object $item Information about the current group.
 */
function bp_groups_admin_edit_metabox_settings( $item ) {

	$invite_status = bp_group_get_invite_status( $item->id ); ?>

	<?php if ( bp_is_active( 'forums' ) ) : ?>
		<div class="bp-groups-settings-section" id="bp-groups-settings-section-forum">
			<label for="group-show-forum"><input type="checkbox" name="group-show-forum" id="group-show-forum" <?php checked( $item->enable_forum ) ?> /> <?php esc_html_e( 'Enable discussion forum', 'buddypress' ) ?></label>
		</div>
	<?php endif; ?>

	<div class="bp-groups-settings-section" id="bp-groups-settings-section-status">
		<fieldset>
			<legend><?php esc_html_e( 'Privacy', 'buddypress' ); ?></legend>

			<label for="bp-group-status-public"><input type="radio" name="group-status" id="bp-group-status-public" value="public" <?php checked( $item->status, 'public' ) ?> /><?php esc_html_e( 'Public', 'buddypress' ) ?></label>
			<label for="bp-group-status-private"><input type="radio" name="group-status" id="bp-group-status-private" value="private" <?php checked( $item->status, 'private' ) ?> /><?php esc_html_e( 'Private', 'buddypress' ) ?></label>
			<label for="bp-group-status-hidden"><input type="radio" name="group-status" id="bp-group-status-hidden" value="hidden" <?php checked( $item->status, 'hidden' ) ?> /><?php esc_html_e( 'Hidden', 'buddypress' ) ?></label>
		</fieldset>
	</div>

	<div class="bp-groups-settings-section" id="bp-groups-settings-section-invite-status">
		<fieldset>
			<legend><?php esc_html_e( 'Who can invite others to this group?', 'buddypress' ); ?></legend>

			<label for="bp-group-invite-status-members"><input type="radio" name="group-invite-status" id="bp-group-invite-status-members" value="members" <?php checked( $invite_status, 'members' ) ?> /><?php esc_html_e( 'All group members', 'buddypress' ) ?></label>
			<label for="bp-group-invite-status-mods"><input type="radio" name="group-invite-status" id="bp-group-invite-status-mods" value="mods" <?php checked( $invite_status, 'mods' ) ?> /><?php esc_html_e( 'Group admins and mods only', 'buddypress' ) ?></label>
			<label for="bp-group-invite-status-admins"><input type="radio" name="group-invite-status" id="bp-group-invite-status-admins" value="admins" <?php checked( $invite_status, 'admins' ) ?> /><?php esc_html_e( 'Group admins only', 'buddypress' ) ?></label>
		</fieldset>
	</div>

<?php
}

/**
 * Output the markup for a single group's Add New Members metabox.
 *
 * @since 1.7.0
 *
 * @param BP_Groups_Group $item The BP_Groups_Group object for the current group.
 */
function bp_groups_admin_edit_metabox_add_new_members( $item ) {
	if ( bp_is_large_install() ) {
		$class  = '';
		$notice = __( 'Enter a comma-separated list of user logins.', 'buddypress' );
	} else {
		$class  = 'bp-suggest-user';
		$notice = '';
	}

	?>

	<label for="bp-groups-new-members" class="screen-reader-text">
		<?php
			/* translators: accessibility text */
			esc_html_e( 'Add new members', 'buddypress' );
		?>
	</label>
	<input name="bp-groups-new-members" type="text" id="bp-groups-new-members" class="<?php echo esc_attr( $class ); ?>" placeholder="" />
	<?php if ( $notice ) : ?>
		<p class="description"><?php echo esc_html( $notice ); ?></p>
	<?php endif; ?>
	<ul id="bp-groups-new-members-list"></ul>
	<?php
}

/**
 * Renders the Members metabox on single group pages.
 *
 * @since 1.7.0
 *
 * @param BP_Groups_Group $item The BP_Groups_Group object for the current group.
 */
function bp_groups_admin_edit_metabox_members( $item ) {
	// Use the BP REST API if it supported.
	if ( bp_rest_api_is_available() && bp_groups_has_manage_group_members_templates() ) {
		wp_enqueue_script( 'bp-group-manage-members' );
		wp_localize_script(
			'bp-group-manage-members',
			'bpGroupManageMembersSettings',
			bp_groups_get_group_manage_members_script_data( $item->id )
		);

		// Inject the Group Manage Members interface.
		bp_groups_manage_group_members_interface( 'admin_footer' );

		/**
		 * Echo out the JavaScript variable.
		 * This seems to be required by the autocompleter, leaving this here for now...
		 */
		echo '<script type="text/javascript">var group_id = "' . esc_js( $item->id ) . '";</script>';
		return;
	}

	// Pull up a list of group members, so we can separate out the types
	// We'll also keep track of group members here to place them into a
	// JavaScript variable, which will help with group member autocomplete.
	$members = array(
		'admin'  => array(),
		'mod'    => array(),
		'member' => array(),
		'banned' => array(),
	);

	$pagination = array(
		'admin'  => array(),
		'mod'    => array(),
		'member' => array(),
		'banned' => array(),
	);

	foreach ( $members as $type => &$member_type_users ) {
		$page_qs_key       = $type . '_page';
		$current_type_page = isset( $_GET[ $page_qs_key ] ) ? absint( $_GET[ $page_qs_key ] ) : 1;
		$member_type_query = new BP_Group_Member_Query( array(
			'group_id'   => $item->id,
			'group_role' => array( $type ),
			'type'       => 'alphabetical',
			/**
			 * Filters the admin members type per page value.
			 *
			 * @since 2.8.0
			 *
			 * @param int    $value Member types per page. Default 10.
			 * @param string $type  Member type.
			 */
			'per_page'   => apply_filters( 'bp_groups_admin_members_type_per_page', 10, $type ),
			'page'       => $current_type_page,
		) );

		$member_type_users   = $member_type_query->results;
		$pagination[ $type ] = bp_groups_admin_create_pagination_links( $member_type_query, $type );
	}

	// Echo out the JavaScript variable.
	echo '<script type="text/javascript">var group_id = "' . esc_js( $item->id ) . '";</script>';

	// Loop through each member type.
	foreach ( $members as $member_type => $type_users ) : ?>

		<div class="bp-groups-member-type" id="bp-groups-member-type-<?php echo esc_attr( $member_type ) ?>">

			<h3>
				<?php
				switch ( $member_type ) :
					case 'admin'  : esc_html_e( 'Administrators', 'buddypress' ); break;
					case 'mod'    : esc_html_e( 'Moderators',     'buddypress' ); break;
					case 'member' : esc_html_e( 'Members',        'buddypress' ); break;
					case 'banned' : esc_html_e( 'Banned Members', 'buddypress' ); break;
				endswitch;
				?>
			</h3>

			<div class="bp-group-admin-pagination table-top">
				<?php
					// phpcs:ignore WordPress.Security.EscapeOutput
					echo $pagination[ $member_type ];
				?>
			</div>

		<?php if ( ! empty( $type_users ) ) : ?>

			<table class="widefat bp-group-members">
				<thead>
					<tr>
						<th scope="col" class="uid-column"><?php echo esc_html_x( 'ID', 'Group member user_id in group admin', 'buddypress' ); ?></th>
						<th scope="col" class="uname-column"><?php echo esc_html_x( 'Name', 'Group member name in group admin', 'buddypress' ); ?></th>
						<th scope="col" class="urole-column"><?php echo esc_html_x( 'Group Role', 'Group member role in group admin', 'buddypress' ); ?></th>
					</tr>
				</thead>

				<tbody>

				<?php foreach ( $type_users as $type_user ) : ?>
					<tr>
						<th scope="row" class="uid-column"><?php echo esc_html( $type_user->ID ); ?></th>

						<td class="uname-column">
							<a style="float: left;" href="<?php echo esc_url( bp_members_get_user_url( $type_user->ID ) ); ?>">
								<?php
									// phpcs:ignore WordPress.Security.EscapeOutput
									echo bp_core_fetch_avatar(
										array(
											'item_id' => $type_user->ID,
											'width'   => '32',
											'height'  => '32'
										)
									);
								?>
							</a>

							<span style="margin: 8px; float: left;">
								<?php
									// phpcs:ignore WordPress.Security.EscapeOutput
									echo bp_core_get_userlink( $type_user->ID );
								?>
							</span>
						</td>

						<td class="urole-column">
							<label for="bp-groups-role-<?php echo esc_attr( $type_user->ID ); ?>" class="screen-reader-text">
								<?php
									/* translators: accessibility text */
									esc_html_e( 'Select group role for member', 'buddypress' );
								?>
							</label>
							<select class="bp-groups-role" id="bp-groups-role-<?php echo esc_attr( $type_user->ID ); ?>" name="bp-groups-role[<?php echo esc_attr( $type_user->ID ); ?>]">
								<optgroup label="<?php esc_attr_e( 'Roles', 'buddypress' ); ?>">
									<option class="admin"  value="admin"  <?php selected( 'admin',  $member_type ); ?>><?php esc_html_e( 'Administrator', 'buddypress' ); ?></option>
									<option class="mod"    value="mod"    <?php selected( 'mod',    $member_type ); ?>><?php esc_html_e( 'Moderator', 'buddypress' ); ?></option>
									<option class="member" value="member" <?php selected( 'member', $member_type ); ?>><?php esc_html_e( 'Member', 'buddypress' ); ?></option>

									<?php if ( 'banned' === $member_type ) : ?>
										<option class="banned" value="banned" <?php selected( 'banned', $member_type ); ?>><?php esc_html_e( 'Banned', 'buddypress' ); ?></option>
									<?php endif; ?>

								</optgroup>
								<optgroup label="<?php esc_attr_e( 'Actions', 'buddypress' ); ?>">
									<option class="remove" value="remove"><?php esc_html_e( 'Remove', 'buddypress' ); ?></option>

									<?php if ( 'banned' !== $member_type ) : ?>
										<option class="banned" value="banned"><?php esc_html_e( 'Ban', 'buddypress' ); ?></option>
									<?php endif; ?>

								</optgroup>
							</select>

							<?php
							/**
							 * Store the current role for this user,
							 * so we can easily detect changes.
							 *
							 * @todo remove this, and do database detection on save
							 */
							?>
							<input type="hidden" name="bp-groups-existing-role[<?php echo esc_attr( $type_user->ID ); ?>]" value="<?php echo esc_attr( $member_type ); ?>" />
						</td>
					</tr>

					<?php if ( has_filter( 'bp_groups_admin_manage_member_row' ) ) : ?>
						<tr>
							<td colspan="3">
								<?php

								/**
								 * Fires after the listing of a single row for members in a group on the group edit screen.
								 *
								 * @since 1.8.0
								 *
								 * @param int             $ID   ID of the user being rendered.
								 * @param BP_Groups_Group $item Object for the current group.
								 */
								do_action( 'bp_groups_admin_manage_member_row', $type_user->ID, $item ); ?>
							</td>
						</tr>
					<?php endif; ?>

				<?php endforeach; ?>

				</tbody>
			</table>

		<?php else : ?>

			<p class="bp-groups-no-members description"><?php esc_html_e( 'No members of this type', 'buddypress' ); ?></p>

		<?php endif; ?>

		</div><!-- .bp-groups-member-type -->

	<?php endforeach;
}

/**
 * Renders the Status metabox for the Groups admin edit screen.
 *
 * @since 1.7.0
 *
 * @param object $item Information about the currently displayed group.
 */
function bp_groups_admin_edit_metabox_status( $item ) {
	$base_url = add_query_arg(
		array(
			'page' => 'bp-groups',
			'gid'  => $item->id
		),
		bp_get_admin_url( 'admin.php' )
	);
	?>

	<div id="submitcomment" class="submitbox">
		<div id="major-publishing-actions">
			<div id="delete-action">
				<a class="submitdelete deletion" href="<?php echo esc_url( wp_nonce_url( add_query_arg( 'action', 'delete', $base_url ), 'bp-groups-delete' ) ); ?>"><?php esc_html_e( 'Delete Group', 'buddypress' ) ?></a>
			</div>

			<div id="publishing-action">
				<?php submit_button( __( 'Save Changes', 'buddypress' ), 'primary', 'save', false ); ?>
			</div>
			<div class="clear"></div>
		</div><!-- #major-publishing-actions -->
	</div><!-- #submitcomment -->

<?php
}

/**
 * Render the Group Type metabox.
 *
 * @since 2.6.0
 *
 * @param BP_Groups_Group|null $group The BP_Groups_Group object corresponding to the group being edited.
 */
function bp_groups_admin_edit_metabox_group_type( BP_Groups_Group $group = null ) {

	// Bail if no group ID.
	if ( empty( $group->id ) ) {
		return;
	}

	$types         = bp_groups_get_group_types( array(), 'objects' );
	$current_types = (array) bp_groups_get_group_type( $group->id, false );
	$backend_only  = bp_groups_get_group_types( array( 'show_in_create_screen' => false ) );
	?>

	<label for="bp-groups-group-type" class="screen-reader-text">
		<?php
			/* translators: accessibility text */
			esc_html_e( 'Select group type', 'buddypress' );
		?>
	</label>

	<ul class="categorychecklist form-no-clear">
		<?php foreach ( $types as $type ) : ?>
			<li>
				<label class="selectit"><input value="<?php echo esc_attr( $type->name ) ?>" name="bp-groups-group-type[]" type="checkbox" <?php checked( true, in_array( $type->name, $current_types ) ); ?>>
					<?php
						echo esc_html( $type->labels['singular_name'] );
						if ( in_array( $type->name, $backend_only ) ) {
							printf( ' <span class="description">%s</span>', esc_html__( '(Not available on the front end)', 'buddypress' ) );
						}
					?>

				</label>
			</li>

		<?php endforeach; ?>

	</ul>

	<?php

	wp_nonce_field( 'bp-group-type-change-' . $group->id, 'bp-group-type-nonce' );
}

/**
 * Process changes from the Group Type metabox.
 *
 * @since 2.6.0
 *
 * @param int $group_id Group ID.
 */
function bp_groups_process_group_type_update( $group_id ) {
	if ( ! isset( $_POST['bp-group-type-nonce'] ) ) {
		return;
	}

	check_admin_referer( 'bp-group-type-change-' . $group_id, 'bp-group-type-nonce' );

	// Permission check.
	if ( ! bp_current_user_can( 'bp_moderate' ) ) {
		return;
	}

	$group_types = ! empty( $_POST['bp-groups-group-type'] ) ? wp_unslash( $_POST['bp-groups-group-type'] ) : array();

	/*
	 * If an invalid group type is passed, someone's doing something
	 * fishy with the POST request, so we can fail silently.
	 */
	if ( bp_groups_set_group_type( $group_id, $group_types ) ) {
		// @todo Success messages can't be posted because other stuff happens on the page load.
	}
}
add_action( 'bp_group_admin_edit_after', 'bp_groups_process_group_type_update' );

/**
 * Create pagination links out of a BP_Group_Member_Query.
 *
 * This function is intended to create pagination links for use under the
 * Manage Members section of the Groups Admin Dashboard pages. It is a stopgap
 * measure until a more general pagination solution is in place for BuddyPress.
 * Plugin authors should not use this function, as it is likely to be
 * deprecated soon.
 *
 * @since 1.8.0
 *
 * @param BP_Group_Member_Query $query       A BP_Group_Member_Query object.
 * @param string                $member_type member|mod|admin|banned.
 * @return string Pagination links HTML.
 */
function bp_groups_admin_create_pagination_links( BP_Group_Member_Query $query, $member_type ) {
	$pagination = '';

	if ( ! in_array( $member_type, array( 'admin', 'mod', 'member', 'banned' ) ) ) {
		return $pagination;
	}

	// The key used to paginate this member type in the $_GET global.
	$qs_key   = $member_type . '_page';
	$url_base = remove_query_arg( array( $qs_key, 'updated', 'success_modified' ), $_SERVER['REQUEST_URI'] );

	$page = isset( $_GET[ $qs_key ] ) ? absint( $_GET[ $qs_key ] ) : 1;

	/**
	 * Filters the number of members per member type that is displayed in group editing admin area.
	 *
	 * @since 2.8.0
	 *
	 * @param string $member_type Member type, which is a group role (admin, mod etc).
	 */
	$per_page = apply_filters( 'bp_groups_admin_members_type_per_page', 10, $member_type );

	// Don't show anything if there's no pagination.
	if ( 1 === $page && $query->total_users <= $per_page ) {
		return $pagination;
	}

	$current_page_start = ( ( $page - 1 ) * $per_page ) + 1;
	$current_page_end   = $page * $per_page > intval( $query->total_users ) ? $query->total_users : $page * $per_page;

	$pag_links = paginate_links(
		array(
			'base'      => add_query_arg( $qs_key, '%#%', $url_base ),
			'format'    => '',
			'prev_text' => __( '&laquo;', 'buddypress' ),
			'next_text' => __( '&raquo;', 'buddypress' ),
			'total'     => ceil( $query->total_users / $per_page ),
			'current'   => $page,
		)
	);

	if ( 1 == $query->total_users ) {
		$viewing_text = __( 'Viewing 1 member', 'buddypress' );
	} else {
		$viewing_text = sprintf(
			/* translators: 1: group member from number. 2: group member to number. 3: total group members. */
			_nx( 'Viewing %1$s - %2$s of %3$s member', 'Viewing %1$s - %2$s of %3$s members', $query->total_users, 'Group members pagination in group admin', 'buddypress' ),
			bp_core_number_format( $current_page_start ),
			bp_core_number_format( $current_page_end ),
			bp_core_number_format( $query->total_users )
		);
	}

	$pagination .= '<span class="bp-group-admin-pagination-viewing">' . esc_html( $viewing_text ) . '</span>';

	// phpcs:ignore WordPress.Security.EscapeOutput
	$pagination .= '<span class="bp-group-admin-pagination-links">' . $pag_links . '</span>';

	return $pagination;
}

/**
 * Get a set of usernames corresponding to a set of user IDs.
 *
 * @since 1.7.0
 *
 * @param array $user_ids Array of user IDs.
 * @return array Array of user_logins corresponding to $user_ids.
 */
function bp_groups_admin_get_usernames_from_ids( $user_ids = array() ) {

	$usernames = array();
	$users     = new WP_User_Query( array( 'blog_id' => 0, 'include' => $user_ids ) );

	foreach ( (array) $users->results as $user ) {
		$usernames[] = $user->user_login;
	}

	return $usernames;
}

/**
 * AJAX handler for group member autocomplete requests.
 *
 * @since 1.7.0
 */
function bp_groups_admin_autocomplete_handler() {

	// Bail if user user shouldn't be here, or is a large network.
	if ( ! bp_current_user_can( 'bp_moderate' ) || bp_is_large_install() ) {
		wp_die( -1 );
	}

	$term     = isset( $_GET['term'] )     ? sanitize_text_field( $_GET['term'] ) : '';
	$group_id = isset( $_GET['group_id'] ) ? absint( $_GET['group_id'] )          : 0;

	if ( ! $term || ! $group_id ) {
		wp_die( -1 );
	}

	$suggestions = bp_core_get_suggestions(
		array(
			'group_id' => -$group_id,  // A negative value will exclude this group's members from the suggestions.
			'limit'    => 10,
			'term'     => $term,
			'type'     => 'members',
		)
	);

	$matches = array();

	if ( $suggestions && ! is_wp_error( $suggestions ) ) {
		foreach ( $suggestions as $user ) {

			$matches[] = array(
				// Translators: 1: user_login, 2: user_email.
				'label' => sprintf( __( '%1$s (%2$s)', 'buddypress' ), $user->name, $user->ID ),
				'value' => $user->ID,
			);
		}
	}

	wp_die( json_encode( $matches ) );
}
add_action( 'wp_ajax_bp_group_admin_member_autocomplete', 'bp_groups_admin_autocomplete_handler' );

/**
 * Process input from the Group Type bulk change select.
 *
 * @since 2.7.0
 *
 * @param string $doaction Current $_GET action being performed in admin screen.
 */
function bp_groups_admin_process_group_type_bulk_changes( $doaction ) {
	// Bail if no groups are specified or if this isn't a relevant action.
	if ( empty( $_REQUEST['gid'] )
		|| ( empty( $_REQUEST['bp_change_type'] ) && empty( $_REQUEST['bp_change_type2'] ) )
		|| empty( $_REQUEST['bp_change_group_type'] )
	) {
		return;
	}

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

	if ( ! bp_current_user_can( 'bp_moderate' )  ) {
		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_group_type' !== $new_type && null === bp_groups_get_group_type_object( $new_type ) ) {
		$error = true;
	} else {
		// Run through group ids.
		$error = false;
		foreach ( (array) $_REQUEST['gid'] as $group_id ) {
			$group_id = (int) $group_id;

			// Get the old group type to check against.
			$current_types = bp_groups_get_group_type( $group_id, false );

			if ( $current_types && 'remove_group_type' === $new_type ) {
				$group_types = array();
			} elseif ( ! $current_types || 1 !== count( $current_types ) || $new_type !== $current_types[0] ) {
				$group_types = array( $new_type );
			}

			// Set the new group type.
			if ( isset( $group_types ) ) {
				$set = bp_groups_set_group_type( $group_id, $group_types );
				if ( false === $set || is_wp_error( $set ) ) {
					$error = true;
				}
				unset( $group_types );
			}
		}
	}

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

	wp_safe_redirect( $redirect );
	exit;
}
add_action( 'bp_groups_admin_load', 'bp_groups_admin_process_group_type_bulk_changes' );

/**
 * Display an admin notice upon group type bulk update.
 *
 * @since 2.7.0
 */
function bp_groups_admin_groups_type_change_notice() {
	$updated = isset( $_REQUEST['updated'] ) ? $_REQUEST['updated'] : false;

	// Display feedback.
	if ( $updated && in_array( $updated, array( 'group-type-change-error', 'group-type-change-success' ), true ) ) {

		if ( 'group-type-change-error' === $updated ) {
			$notice = __( 'There was an error while changing group type. Please try again.', 'buddypress' );
			$type   = 'error';
		} else {
			$notice = __( 'Group type was changed successfully.', 'buddypress' );
			$type   = 'updated';
		}

		bp_core_add_admin_notice( $notice, $type );
	}
}
add_action( bp_core_admin_hook(), 'bp_groups_admin_groups_type_change_notice' );

/**
 * Checks whether a group type already exists.
 *
 * @since 7.0.0
 *
 * @param  boolean $exists  True if the group type already exists. False otherwise.
 * @param  string  $type_id The group type identifier.
 * @return bool          True if the group type already exists. False otherwise.
 */
function bp_groups_type_admin_type_exists( $exists = false, $type_id = '' ) {
	if ( ! $type_id ) {
		return $exists;
	}

	return ! is_null( bp_groups_get_group_type_object( $type_id ) );
}
add_filter( bp_get_group_type_tax_name() . '_check_existing_type', 'bp_groups_type_admin_type_exists', 1, 2 );

/**
 * Set the feedback messages for the Group Types Admin actions.
 *
 * @since 7.0.0
 *
 * @param array  $messages The feedback messages.
 * @return array           The feedback messages including the ones for the Group Types Admin actions.
 */
function bp_groups_type_admin_updated_messages( $messages = array() ) {
	$type_taxonomy = bp_get_group_type_tax_name();

	$messages[ $type_taxonomy ] = array(
		0  => '',
		1  => __( 'Please define the Group Type ID field.', 'buddypress' ),
		2  => __( 'Group type successfully added.', 'buddypress' ),
		3  => __( 'Sorry, there was an error and the Group type wasn’t added.', 'buddypress' ),
		// The following one needs to be != 5.
		4  => __( 'Group type successfully updated.', 'buddypress' ),
		5  => __( 'Sorry, this Group type already exists.', 'buddypress' ),
		6  => __( 'Sorry, the Group type was not deleted: it does not exist.', 'buddypress' ),
		7  => __( 'Sorry, This Group type is registered using code, deactivate the plugin or remove the custom code before trying to delete it again.', 'buddypress' ),
		8  => __( 'Sorry, there was an error while trying to delete this Group type.', 'buddypress' ),
		9  => __( 'Group type successfully deleted.', 'buddypress' ),
		10 => __( 'Group type could not be updated due to missing required information.', 'buddypress' ),
		11 => __( 'Please define the Group Type Singular Name field.', 'buddypress' ),
		12 => __( 'Please define the Group Type Plural Name field.', 'buddypress' ),
	);

	return $messages;
}
add_filter( 'term_updated_messages', 'bp_groups_type_admin_updated_messages' );
