类文档

WP_MS_Users_List_Table

💡 云策文档标注

概述

WP_MS_Users_List_Table 是 WordPress 核心类,用于在网络管理后台实现用户列表表格的显示。它继承自 WP_List_Table,专门处理多站点环境下的用户数据展示和操作。

关键要点

  • 继承自 WP_List_Table,用于网络管理员界面显示用户列表
  • 包含多个列处理方法,如 column_username、column_email、column_blogs 等,用于输出特定列内容
  • 支持批量操作(如删除、标记为垃圾邮件)、分页、排序和视图切换功能
  • 使用 WP_User_Query 进行用户数据查询,并处理大型网络优化
  • 提供过滤器如 wpmu_users_columns 和 ms_user_row_actions,允许开发者自定义列和操作

代码示例

public function prepare_items() {
    global $mode, $usersearch, $role;

    if ( ! empty( $_REQUEST['mode'] ) ) {
        $mode = 'excerpt' === $_REQUEST['mode'] ? 'excerpt' : 'list';
        set_user_setting( 'network_users_list_mode', $mode );
    } else {
        $mode = get_user_setting( 'network_users_list_mode', 'list' );
    }

    $usersearch = isset( $_REQUEST['s'] ) ? wp_unslash( trim( $_REQUEST['s'] ) ) : '';

    $users_per_page = $this->get_items_per_page( 'users_network_per_page' );

    $role = isset( $_REQUEST['role'] ) ? $_REQUEST['role'] : '';

    $paged = $this->get_pagenum();

    $args = array(
        'number'  => $users_per_page,
        'offset'  => ( $paged - 1 ) * $users_per_page,
        'search'  => $usersearch,
        'blog_id' => 0,
        'fields'  => 'all_with_meta',
    );

    if ( wp_is_large_network( 'users' ) ) {
        $args['search'] = ltrim( $args['search'], '*' );
    } elseif ( '' !== $args['search'] ) {
        $args['search'] = trim( $args['search'], '*' );
        $args['search'] = '*' . $args['search'] . '*';
    }

    if ( 'super' === $role ) {
        $args['login__in'] = get_super_admins();
    }

    if ( ! $usersearch && wp_is_large_network( 'users' ) ) {
        if ( ! isset( $_REQUEST['orderby'] ) ) {
            $_GET['orderby']     = 'id';
            $_REQUEST['orderby'] = 'id';
        }
        if ( ! isset( $_REQUEST['order'] ) ) {
            $_GET['order']     = 'DESC';
            $_REQUEST['order'] = 'DESC';
        }
        $args['count_total'] = false;
    }

    if ( isset( $_REQUEST['orderby'] ) ) {
        $args['orderby'] = $_REQUEST['orderby'];
    }

    if ( isset( $_REQUEST['order'] ) ) {
        $args['order'] = $_REQUEST['order'];
    }

    $args = apply_filters( 'users_list_table_query_args', $args );

    $wp_user_search = new WP_User_Query( $args );

    $this->items = $wp_user_search->get_results();

    $this->set_pagination_args(
        array(
            'total_items' => $wp_user_search->get_total(),
            'per_page'    => $users_per_page,
        )
    );
}

注意事项

  • 该类仅适用于多站点(网络)环境,单站点应使用 WP_Users_List_Table
  • 使用前需确保用户具有 manage_network_users 权限,通过 ajax_user_can 方法验证
  • 列输出方法如 column_cb 会跳过超级管理员,避免误操作
  • 大型网络下优化查询性能,避免全量计数

📄 原文内容

Core class used to implement displaying users in a list table for the network admin.

Description

See also

Methods

Name Description
WP_MS_Users_List_Table::_column_blogs
WP_MS_Users_List_Table::ajax_user_can
WP_MS_Users_List_Table::column_blogs Handles the sites column output.
WP_MS_Users_List_Table::column_cb Handles the checkbox column output.
WP_MS_Users_List_Table::column_default Handles the default column output.
WP_MS_Users_List_Table::column_email Handles the email column output.
WP_MS_Users_List_Table::column_id Handles the ID column output.
WP_MS_Users_List_Table::column_name Handles the name column output.
WP_MS_Users_List_Table::column_registered Handles the registered date column output.
WP_MS_Users_List_Table::column_username Handles the username column output.
WP_MS_Users_List_Table::display_rows Generates the list table rows.
WP_MS_Users_List_Table::get_bulk_actions
WP_MS_Users_List_Table::get_columns
WP_MS_Users_List_Table::get_default_primary_column_name Gets the name of the default primary column.
WP_MS_Users_List_Table::get_sortable_columns
WP_MS_Users_List_Table::get_views
WP_MS_Users_List_Table::handle_row_actions Generates and displays row action links.
WP_MS_Users_List_Table::no_items
WP_MS_Users_List_Table::pagination
WP_MS_Users_List_Table::prepare_items

Source

class WP_MS_Users_List_Table extends WP_List_Table {
	/**
	 * @return bool
	 */
	public function ajax_user_can() {
		return current_user_can( 'manage_network_users' );
	}

	/**
	 * @global string $mode       List table view mode.
	 * @global string $usersearch
	 * @global string $role
	 */
	public function prepare_items() {
		global $mode, $usersearch, $role;

		if ( ! empty( $_REQUEST['mode'] ) ) {
			$mode = 'excerpt' === $_REQUEST['mode'] ? 'excerpt' : 'list';
			set_user_setting( 'network_users_list_mode', $mode );
		} else {
			$mode = get_user_setting( 'network_users_list_mode', 'list' );
		}

		$usersearch = isset( $_REQUEST['s'] ) ? wp_unslash( trim( $_REQUEST['s'] ) ) : '';

		$users_per_page = $this->get_items_per_page( 'users_network_per_page' );

		$role = isset( $_REQUEST['role'] ) ? $_REQUEST['role'] : '';

		$paged = $this->get_pagenum();

		$args = array(
			'number'  => $users_per_page,
			'offset'  => ( $paged - 1 ) * $users_per_page,
			'search'  => $usersearch,
			'blog_id' => 0,
			'fields'  => 'all_with_meta',
		);

		if ( wp_is_large_network( 'users' ) ) {
			$args['search'] = ltrim( $args['search'], '*' );
		} elseif ( '' !== $args['search'] ) {
			$args['search'] = trim( $args['search'], '*' );
			$args['search'] = '*' . $args['search'] . '*';
		}

		if ( 'super' === $role ) {
			$args['login__in'] = get_super_admins();
		}

		/*
		 * If the network is large and a search is not being performed,
		 * show only the latest users with no paging in order to avoid
		 * expensive count queries.
		 */
		if ( ! $usersearch && wp_is_large_network( 'users' ) ) {
			if ( ! isset( $_REQUEST['orderby'] ) ) {
				$_GET['orderby']     = 'id';
				$_REQUEST['orderby'] = 'id';
			}
			if ( ! isset( $_REQUEST['order'] ) ) {
				$_GET['order']     = 'DESC';
				$_REQUEST['order'] = 'DESC';
			}
			$args['count_total'] = false;
		}

		if ( isset( $_REQUEST['orderby'] ) ) {
			$args['orderby'] = $_REQUEST['orderby'];
		}

		if ( isset( $_REQUEST['order'] ) ) {
			$args['order'] = $_REQUEST['order'];
		}

		/** This filter is documented in wp-admin/includes/class-wp-users-list-table.php */
		$args = apply_filters( 'users_list_table_query_args', $args );

		// Query the user IDs for this page.
		$wp_user_search = new WP_User_Query( $args );

		$this->items = $wp_user_search->get_results();

		$this->set_pagination_args(
			array(
				'total_items' => $wp_user_search->get_total(),
				'per_page'    => $users_per_page,
			)
		);
	}

	/**
	 * @return array
	 */
	protected function get_bulk_actions() {
		$actions = array();
		if ( current_user_can( 'delete_users' ) ) {
			$actions['delete'] = __( 'Delete' );
		}
		$actions['spam']    = _x( 'Mark as spam', 'user' );
		$actions['notspam'] = _x( 'Not spam', 'user' );

		return $actions;
	}

	/**
	 */
	public function no_items() {
		_e( 'No users found.' );
	}

	/**
	 * @global string $role
	 * @return array
	 */
	protected function get_views() {
		global $role;

		$total_users  = get_user_count();
		$super_admins = get_super_admins();
		$total_admins = count( $super_admins );

		$role_links        = array();
		$role_links['all'] = array(
			'url'     => network_admin_url( 'users.php' ),
			'label'   => sprintf(
				/* translators: Number of users. */
				_nx(
					'All <span class="count">(%s)</span>',
					'All <span class="count">(%s)</span>',
					$total_users,
					'users'
				),
				number_format_i18n( $total_users )
			),
			'current' => 'super' !== $role,
		);

		$role_links['super'] = array(
			'url'     => network_admin_url( 'users.php?role=super' ),
			'label'   => sprintf(
				/* translators: Number of users. */
				_n(
					'Super Admin <span class="count">(%s)</span>',
					'Super Admins <span class="count">(%s)</span>',
					$total_admins
				),
				number_format_i18n( $total_admins )
			),
			'current' => 'super' === $role,
		);

		return $this->get_views_links( $role_links );
	}

	/**
	 * @global string $mode List table view mode.
	 *
	 * @param string $which
	 */
	protected function pagination( $which ) {
		global $mode;

		parent::pagination( $which );

		if ( 'top' === $which ) {
			$this->view_switcher( $mode );
		}
	}

	/**
	 * @return string[] Array of column titles keyed by their column name.
	 */
	public function get_columns() {
		$users_columns = array(
			'cb'         => '<input type="checkbox" />',
			'username'   => __( 'Username' ),
			'name'       => __( 'Name' ),
			'email'      => __( 'Email' ),
			'registered' => _x( 'Registered', 'user' ),
			'blogs'      => __( 'Sites' ),
		);
		/**
		 * Filters the columns displayed in the Network Admin Users list table.
		 *
		 * @since MU (3.0.0)
		 *
		 * @param string[] $users_columns An array of user columns. Default 'cb', 'username',
		 *                                'name', 'email', 'registered', 'blogs'.
		 */
		return apply_filters( 'wpmu_users_columns', $users_columns );
	}

	/**
	 * @return array
	 */
	protected function get_sortable_columns() {
		return array(
			'username'   => array( 'login', false, __( 'Username' ), __( 'Table ordered by Username.' ), 'asc' ),
			'name'       => array( 'name', false, __( 'Name' ), __( 'Table ordered by Name.' ) ),
			'email'      => array( 'email', false, __( 'E-mail' ), __( 'Table ordered by E-mail.' ) ),
			'registered' => array( 'id', false, _x( 'Registered', 'user' ), __( 'Table ordered by User Registered Date.' ) ),
		);
	}

	/**
	 * Handles the checkbox column output.
	 *
	 * @since 4.3.0
	 * @since 5.9.0 Renamed `$user` to `$item` to match parent class for PHP 8 named parameter support.
	 *
	 * @param WP_User $item The current WP_User object.
	 */
	public function column_cb( $item ) {
		// Restores the more descriptive, specific name for use within this method.
		$user = $item;

		if ( is_super_admin( $user->ID ) ) {
			return;
		}
		?>
		<input type="checkbox" id="blog_<?php echo $user->ID; ?>" name="allusers[]" value="<?php echo esc_attr( $user->ID ); ?>" />
		<label for="blog_<?php echo $user->ID; ?>">
			<span class="screen-reader-text">
			user_login );
			?>
			</span>
		</label>
		ID;
	}

	/**
	 * Handles the username column output.
	 *
	 * @since 4.3.0
	 *
	 * @param WP_User $user The current WP_User object.
	 */
	public function column_username( $user ) {
		$super_admins = get_super_admins();
		$avatar       = get_avatar( $user->user_email, 32 );

		echo $avatar;

		if ( current_user_can( 'edit_user', $user->ID ) ) {
			$edit_link = esc_url( add_query_arg( 'wp_http_referer', urlencode( wp_unslash( $_SERVER['REQUEST_URI'] ) ), get_edit_user_link( $user->ID ) ) );
			$edit      = "<a href="{$edit_link}">{$user->user_login}</a>";
		} else {
			$edit = $user->user_login;
		}

		?>
		<strong>
			user_login, $super_admins, true ) ) {
				echo ' — ' . __( 'Super Admin' );
			}
			?>
		</strong>
		first_name && $user->last_name ) {
			printf(
				/* translators: 1: User's first name, 2: Last name. */
				_x( '%1$s %2$s', 'Display name based on first name and last name' ),
				$user->first_name,
				$user->last_name
			);
		} elseif ( $user->first_name ) {
			echo $user->first_name;
		} elseif ( $user->last_name ) {
			echo $user->last_name;
		} else {
			echo '<span aria-hidden="true">—</span><span class="screen-reader-text">' .
				/* translators: Hidden accessibility text. */
				_x( 'Unknown', 'name' ) .
			'</span>';
		}
	}

	/**
	 * Handles the email column output.
	 *
	 * @since 4.3.0
	 *
	 * @param WP_User $user The current WP_User object.
	 */
	public function column_email( $user ) {
		echo "<a href='" . esc_url( "mailto:$user->user_email" ) . "'>$user->user_email</a>";
	}

	/**
	 * Handles the registered date column output.
	 *
	 * @since 4.3.0
	 *
	 * @global string $mode List table view mode.
	 *
	 * @param WP_User $user The current WP_User object.
	 */
	public function column_registered( $user ) {
		global $mode;
		if ( 'list' === $mode ) {
			$date = __( 'Y/m/d' );
		} else {
			$date = __( 'Y/m/d g:i:s a' );
		}
		echo mysql2date( $date, $user->user_registered );
	}

	/**
	 * @since 4.3.0
	 *
	 * @param WP_User $user
	 * @param string  $classes
	 * @param string  $data
	 * @param string  $primary
	 */
	protected function _column_blogs( $user, $classes, $data, $primary ) {
		echo '<td class="', $classes, ' has-row-actions" ', $data, '>';
		echo $this->column_blogs( $user );
		echo $this->handle_row_actions( $user, 'blogs', $primary );
		echo '</td>';
	}

	/**
	 * Handles the sites column output.
	 *
	 * @since 4.3.0
	 *
	 * @param WP_User $user The current WP_User object.
	 */
	public function column_blogs( $user ) {
		$blogs = get_blogs_of_user( $user->ID, true );
		if ( ! is_array( $blogs ) ) {
			return;
		}

		foreach ( $blogs as $site ) {
			if ( ! can_edit_network( $site->site_id ) ) {
				continue;
			}

			$path         = ( '/' === $site->path ) ? '' : $site->path;
			$site_classes = array( 'site-' . $site->site_id );

			/**
			 * Filters the span class for a site listing on the multisite user list table.
			 *
			 * @since 5.2.0
			 *
			 * @param string[] $site_classes Array of class names used within the span tag.
			 *                               Default "site-#" with the site's network ID.
			 * @param int      $site_id      Site ID.
			 * @param int      $network_id   Network ID.
			 * @param WP_User  $user         WP_User object.
			 */
			$site_classes = apply_filters( 'ms_user_list_site_class', $site_classes, $site->userblog_id, $site->site_id, $user );

			if ( is_array( $site_classes ) && ! empty( $site_classes ) ) {
				$site_classes = array_map( 'sanitize_html_class', array_unique( $site_classes ) );
				echo '<span class="' . esc_attr( implode( ' ', $site_classes ) ) . '">';
			} else {
				echo '<span>';
			}

			echo '<a href="' . esc_url( network_admin_url( 'site-info.php?id=' . $site->userblog_id ) ) . '">' . str_replace( '.' . get_network()->domain, '', $site->domain . $path ) . '</a>';
			echo ' <small class="row-actions">';

			$actions         = array();
			$actions['edit'] = '<a href="' . esc_url( network_admin_url( 'site-info.php?id=' . $site->userblog_id ) ) . '">' . __( 'Edit' ) . '</a>';

			$class = '';
			if ( 1 === (int) $site->spam ) {
				$class .= 'site-spammed ';
			}
			if ( 1 === (int) $site->mature ) {
				$class .= 'site-mature ';
			}
			if ( 1 === (int) $site->deleted ) {
				$class .= 'site-deleted ';
			}
			if ( 1 === (int) $site->archived ) {
				$class .= 'site-archived ';
			}

			$actions['view'] = '<a class="' . $class . '" href="' . esc_url( get_home_url( $site->userblog_id ) ) . '">' . __( 'View' ) . '</a>';

			/**
			 * Filters the action links displayed next the sites a user belongs to
			 * in the Network Admin Users list table.
			 *
			 * @since 3.1.0
			 *
			 * @param string[] $actions     An array of action links to be displayed. Default 'Edit', 'View'.
			 * @param int      $userblog_id The site ID.
			 */
			$actions = apply_filters( 'ms_user_list_site_actions', $actions, $site->userblog_id );

			$action_count = count( $actions );

			$i = 0;

			foreach ( $actions as $action => $link ) {
				++$i;

				$separator = ( $i < $action_count ) ? ' | ' : '';

				echo "<span class='$action'>{$link}{$separator}</span>";
			}

			echo '</small></span><br />';
		}
	}

	/**
	 * Handles the default column output.
	 *
	 * @since 4.3.0
	 * @since 5.9.0 Renamed `$user` to `$item` to match parent class for PHP 8 named parameter support.
	 *
	 * @param WP_User $item        The current WP_User object.
	 * @param string  $column_name The current column name.
	 */
	public function column_default( $item, $column_name ) {
		// Restores the more descriptive, specific name for use within this method.
		$user = $item;

		/** This filter is documented in wp-admin/includes/class-wp-users-list-table.php */
		$column_output = apply_filters( 'manage_users_custom_column', '', $column_name, $user->ID );

		/**
		 * Filters the display output of custom columns in the Network Users list table.
		 *
		 * @since 6.8.0
		 *
		 * @param string $output      Custom column output. Default empty.
		 * @param string $column_name Name of the custom column.
		 * @param int    $user_id     ID of the currently-listed user.
		 */
		echo apply_filters( 'manage_users-network_custom_column', $column_output, $column_name, $user->ID );
	}

	/**
	 * Generates the list table rows.
	 *
	 * @since 3.1.0
	 */
	public function display_rows() {
		foreach ( $this->items as $user ) {
			$class = '';

			$status_list = array(
				'spam'    => 'site-spammed',
				'deleted' => 'site-deleted',
			);

			foreach ( $status_list as $status => $col ) {
				if ( $user->$status ) {
					$class .= " $col";
				}
			}

			?>
			<tr class="<?php echo trim( $class ); ?>">
				single_row_columns( $user ); ?>
			</tr>
			ID ) ) {
			$edit_link       = esc_url( add_query_arg( 'wp_http_referer', urlencode( wp_unslash( $_SERVER['REQUEST_URI'] ) ), get_edit_user_link( $user->ID ) ) );
			$actions['edit'] = '<a href="' . $edit_link . '">' . __( 'Edit' ) . '</a>';
		}

		if ( current_user_can( 'delete_user', $user->ID ) && ! in_array( $user->user_login, $super_admins, true ) ) {
			$actions['delete'] = '<a href="' . esc_url( network_admin_url( add_query_arg( '_wp_http_referer', urlencode( wp_unslash( $_SERVER['REQUEST_URI'] ) ), wp_nonce_url( 'users.php', 'deleteuser' ) . '&action=deleteuser&id=' . $user->ID ) ) ) . '" class="delete">' . __( 'Delete' ) . '</a>';
		}

		/**
		 * Filters the action links displayed under each user in the Network Admin Users list table.
		 *
		 * @since 3.2.0
		 *
		 * @param string[] $actions An array of action links to be displayed. Default 'Edit', 'Delete'.
		 * @param WP_User  $user    WP_User object.
		 */
		$actions = apply_filters( 'ms_user_row_actions', $actions, $user );

		return $this->row_actions( $actions );
	}
}

Changelog

Version Description
3.1.0 Introduced.