3 * List Table API: WP_MS_Sites_List_Table class
6 * @subpackage Administration
11 * Core class used to implement displaying sites in a list table for the network admin.
18 class WP_MS_Sites_List_Table extends WP_List_Table {
35 * @see WP_List_Table::__construct() for more information on default arguments.
37 * @param array $args An associative array of arguments.
39 public function __construct( $args = array() ) {
40 $this->status_list = array(
41 'archived' => array( 'site-archived', __( 'Archived' ) ),
42 'spam' => array( 'site-spammed', _x( 'Spam', 'site' ) ),
43 'deleted' => array( 'site-deleted', __( 'Deleted' ) ),
44 'mature' => array( 'site-mature', __( 'Mature' ) )
47 parent::__construct( array(
49 'screen' => isset( $args['screen'] ) ? $args['screen'] : null,
57 public function ajax_user_can() {
58 return current_user_can( 'manage_sites' );
62 * Prepares the list of sites for display.
67 * @global string $mode
70 public function prepare_items() {
71 global $s, $mode, $wpdb;
73 if ( ! empty( $_REQUEST['mode'] ) ) {
74 $mode = $_REQUEST['mode'] === 'excerpt' ? 'excerpt' : 'list';
75 set_user_setting( 'sites_list_mode', $mode );
77 $mode = get_user_setting( 'sites_list_mode', 'list' );
80 $per_page = $this->get_items_per_page( 'sites_network_per_page' );
82 $pagenum = $this->get_pagenum();
84 $s = isset( $_REQUEST['s'] ) ? wp_unslash( trim( $_REQUEST[ 's' ] ) ) : '';
86 if ( false !== strpos($s, '*') ) {
92 * If the network is large and a search is not being performed, show only
93 * the latest sites with no paging in order to avoid expensive count queries.
95 if ( !$s && wp_is_large_network() ) {
96 if ( !isset($_REQUEST['orderby']) )
97 $_GET['orderby'] = $_REQUEST['orderby'] = '';
98 if ( !isset($_REQUEST['order']) )
99 $_GET['order'] = $_REQUEST['order'] = 'DESC';
103 'number' => intval( $per_page ),
104 'offset' => intval( ( $pagenum - 1 ) * $per_page ),
105 'network_id' => get_current_network_id(),
110 } elseif ( preg_match( '/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/', $s ) ||
111 preg_match( '/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.?$/', $s ) ||
112 preg_match( '/^[0-9]{1,3}\.[0-9]{1,3}\.?$/', $s ) ||
113 preg_match( '/^[0-9]{1,3}\.$/', $s ) ) {
115 $sql = $wpdb->prepare( "SELECT blog_id FROM {$wpdb->registration_log} WHERE {$wpdb->registration_log}.IP LIKE %s", $wpdb->esc_like( $s ) . ( ! empty( $wild ) ? '%' : '' ) );
116 $reg_blog_ids = $wpdb->get_col( $sql );
118 if ( $reg_blog_ids ) {
119 $args['site__in'] = $reg_blog_ids;
121 } elseif ( is_numeric( $s ) && empty( $wild ) ) {
124 $args['search'] = $s;
126 if ( ! is_subdomain_install() ) {
127 $args['search_columns'] = array( 'path' );
131 $order_by = isset( $_REQUEST['orderby'] ) ? $_REQUEST['orderby'] : '';
132 if ( 'registered' === $order_by ) {
133 // registered is a valid field name.
134 } elseif ( 'lastupdated' === $order_by ) {
135 $order_by = 'last_updated';
136 } elseif ( 'blogname' === $order_by ) {
137 if ( is_subdomain_install() ) {
138 $order_by = 'domain';
142 } elseif ( 'blog_id' === $order_by ) {
144 } elseif ( ! $order_by ) {
148 $args['orderby'] = $order_by;
151 $args['order'] = ( isset( $_REQUEST['order'] ) && 'DESC' === strtoupper( $_REQUEST['order'] ) ) ? "DESC" : "ASC";
154 if ( wp_is_large_network() ) {
155 $args['no_found_rows'] = true;
157 $args['no_found_rows'] = false;
161 * Filters the arguments for the site query in the sites list table.
165 * @param array $args An array of get_sites() arguments.
167 $args = apply_filters( 'ms_sites_list_table_query_args', $args );
169 $_sites = get_sites( $args );
170 if ( is_array( $_sites ) ) {
171 update_site_cache( $_sites );
173 $this->items = array_slice( $_sites, 0, $per_page );
176 $total_sites = get_sites( array_merge( $args, array(
182 $this->set_pagination_args( array(
183 'total_items' => $total_sites,
184 'per_page' => $per_page,
191 public function no_items() {
192 _e( 'No sites found.' );
199 protected function get_bulk_actions() {
201 if ( current_user_can( 'delete_sites' ) )
202 $actions['delete'] = __( 'Delete' );
203 $actions['spam'] = _x( 'Mark as Spam', 'site' );
204 $actions['notspam'] = _x( 'Not Spam', 'site' );
210 * @global string $mode
212 * @param string $which
214 protected function pagination( $which ) {
217 parent::pagination( $which );
219 if ( 'top' === $which )
220 $this->view_switcher( $mode );
226 public function get_columns() {
227 $sites_columns = array(
228 'cb' => '<input type="checkbox" />',
229 'blogname' => __( 'URL' ),
230 'lastupdated' => __( 'Last Updated' ),
231 'registered' => _x( 'Registered', 'site' ),
232 'users' => __( 'Users' ),
235 if ( has_filter( 'wpmublogsaction' ) ) {
236 $sites_columns['plugins'] = __( 'Actions' );
240 * Filters the displayed site columns in Sites list table.
244 * @param array $sites_columns An array of displayed site columns. Default 'cb',
245 * 'blogname', 'lastupdated', 'registered', 'users'.
247 return apply_filters( 'wpmu_blogs_columns', $sites_columns );
253 protected function get_sortable_columns() {
255 'blogname' => 'blogname',
256 'lastupdated' => 'lastupdated',
257 'registered' => 'blog_id',
262 * Handles the checkbox column output.
267 * @param array $blog Current site.
269 public function column_cb( $blog ) {
270 if ( ! is_main_site( $blog['blog_id'] ) ) :
271 $blogname = untrailingslashit( $blog['domain'] . $blog['path'] );
273 <label class="screen-reader-text" for="blog_<?php echo $blog['blog_id']; ?>"><?php
274 printf( __( 'Select %s' ), $blogname );
276 <input type="checkbox" id="blog_<?php echo $blog['blog_id'] ?>" name="allblogs[]" value="<?php echo esc_attr( $blog['blog_id'] ) ?>" />
281 * Handles the ID column output.
286 * @param array $blog Current site.
288 public function column_id( $blog ) {
289 echo $blog['blog_id'];
293 * Handles the site name column output.
298 * @global string $mode
300 * @param array $blog Current site.
302 public function column_blogname( $blog ) {
305 $blogname = untrailingslashit( $blog['domain'] . $blog['path'] );
306 $blog_states = array();
307 reset( $this->status_list );
309 foreach ( $this->status_list as $status => $col ) {
310 if ( $blog[ $status ] == 1 ) {
311 $blog_states[] = $col[1];
315 if ( ! empty( $blog_states ) ) {
316 $state_count = count( $blog_states );
318 $blog_state .= ' - ';
319 foreach ( $blog_states as $state ) {
321 $sep = ( $i == $state_count ) ? '' : ', ';
322 $blog_state .= "<span class='post-state'>$state$sep</span>";
327 <a href="<?php echo esc_url( network_admin_url( 'site-info.php?id=' . $blog['blog_id'] ) ); ?>" class="edit"><?php echo $blogname . $blog_state; ?></a>
329 if ( 'list' !== $mode ) {
330 switch_to_blog( $blog['blog_id'] );
333 /* translators: 1: site name, 2: site tagline. */
334 __( '%1$s – %2$s' ),
335 get_option( 'blogname' ),
336 '<em>' . get_option( 'blogdescription ' ) . '</em>'
339 restore_current_blog();
344 * Handles the lastupdated column output.
349 * @param array $blog Current site.
351 public function column_lastupdated( $blog ) {
354 if ( 'list' === $mode ) {
355 $date = __( 'Y/m/d' );
357 $date = __( 'Y/m/d g:i:s a' );
360 echo ( $blog['last_updated'] === '0000-00-00 00:00:00' ) ? __( 'Never' ) : mysql2date( $date, $blog['last_updated'] );
364 * Handles the registered column output.
369 * @param array $blog Current site.
371 public function column_registered( $blog ) {
374 if ( 'list' === $mode ) {
375 $date = __( 'Y/m/d' );
377 $date = __( 'Y/m/d g:i:s a' );
380 if ( $blog['registered'] === '0000-00-00 00:00:00' ) {
383 echo mysql2date( $date, $blog['registered'] );
388 * Handles the users column output.
393 * @param array $blog Current site.
395 public function column_users( $blog ) {
396 $user_count = wp_cache_get( $blog['blog_id'] . '_user_count', 'blog-details' );
397 if ( ! $user_count ) {
398 $blog_users = get_users( array( 'blog_id' => $blog['blog_id'], 'fields' => 'ID' ) );
399 $user_count = count( $blog_users );
400 unset( $blog_users );
401 wp_cache_set( $blog['blog_id'] . '_user_count', $user_count, 'blog-details', 12 * HOUR_IN_SECONDS );
405 '<a href="%s">%s</a>',
406 esc_url( network_admin_url( 'site-users.php?id=' . $blog['blog_id'] ) ),
407 number_format_i18n( $user_count )
412 * Handles the plugins column output.
417 * @param array $blog Current site.
419 public function column_plugins( $blog ) {
420 if ( has_filter( 'wpmublogsaction' ) ) {
422 * Fires inside the auxiliary 'Actions' column of the Sites list table.
424 * By default this column is hidden unless something is hooked to the action.
428 * @param int $blog_id The site ID.
430 do_action( 'wpmublogsaction', $blog['blog_id'] );
435 * Handles output for the default column.
440 * @param array $blog Current site.
441 * @param string $column_name Current column name.
443 public function column_default( $blog, $column_name ) {
445 * Fires for each registered custom column in the Sites list table.
449 * @param string $column_name The name of the column to display.
450 * @param int $blog_id The site ID.
452 do_action( 'manage_sites_custom_column', $column_name, $blog['blog_id'] );
457 * @global string $mode
459 public function display_rows() {
460 foreach ( $this->items as $blog ) {
461 $blog = $blog->to_array();
463 reset( $this->status_list );
465 foreach ( $this->status_list as $status => $col ) {
466 if ( $blog[ $status ] == 1 ) {
467 $class = " class='{$col[0]}'";
473 $this->single_row_columns( $blog );
480 * Gets the name of the default primary column.
485 * @return string Name of the default primary column, in this case, 'blogname'.
487 protected function get_default_primary_column_name() {
492 * Generates and displays row action links.
497 * @param object $blog Site being acted upon.
498 * @param string $column_name Current column name.
499 * @param string $primary Primary column name.
500 * @return string Row actions output.
502 protected function handle_row_actions( $blog, $column_name, $primary ) {
503 if ( $primary !== $column_name ) {
507 $blogname = untrailingslashit( $blog['domain'] . $blog['path'] );
511 'edit' => '', 'backend' => '',
512 'activate' => '', 'deactivate' => '',
513 'archive' => '', 'unarchive' => '',
514 'spam' => '', 'unspam' => '',
519 $actions['edit'] = '<a href="' . esc_url( network_admin_url( 'site-info.php?id=' . $blog['blog_id'] ) ) . '">' . __( 'Edit' ) . '</a>';
520 $actions['backend'] = "<a href='" . esc_url( get_admin_url( $blog['blog_id'] ) ) . "' class='edit'>" . __( 'Dashboard' ) . '</a>';
521 if ( get_current_site()->blog_id != $blog['blog_id'] ) {
522 if ( $blog['deleted'] == '1' ) {
523 $actions['activate'] = '<a href="' . esc_url( wp_nonce_url( network_admin_url( 'sites.php?action=confirm&action2=activateblog&id=' . $blog['blog_id'] ), 'activateblog_' . $blog['blog_id'] ) ) . '">' . __( 'Activate' ) . '</a>';
525 $actions['deactivate'] = '<a href="' . esc_url( wp_nonce_url( network_admin_url( 'sites.php?action=confirm&action2=deactivateblog&id=' . $blog['blog_id'] ), 'deactivateblog_' . $blog['blog_id'] ) ) . '">' . __( 'Deactivate' ) . '</a>';
528 if ( $blog['archived'] == '1' ) {
529 $actions['unarchive'] = '<a href="' . esc_url( wp_nonce_url( network_admin_url( 'sites.php?action=confirm&action2=unarchiveblog&id=' . $blog['blog_id'] ), 'unarchiveblog_' . $blog['blog_id'] ) ) . '">' . __( 'Unarchive' ) . '</a>';
531 $actions['archive'] = '<a href="' . esc_url( wp_nonce_url( network_admin_url( 'sites.php?action=confirm&action2=archiveblog&id=' . $blog['blog_id'] ), 'archiveblog_' . $blog['blog_id'] ) ) . '">' . _x( 'Archive', 'verb; site' ) . '</a>';
534 if ( $blog['spam'] == '1' ) {
535 $actions['unspam'] = '<a href="' . esc_url( wp_nonce_url( network_admin_url( 'sites.php?action=confirm&action2=unspamblog&id=' . $blog['blog_id'] ), 'unspamblog_' . $blog['blog_id'] ) ) . '">' . _x( 'Not Spam', 'site' ) . '</a>';
537 $actions['spam'] = '<a href="' . esc_url( wp_nonce_url( network_admin_url( 'sites.php?action=confirm&action2=spamblog&id=' . $blog['blog_id'] ), 'spamblog_' . $blog['blog_id'] ) ) . '">' . _x( 'Spam', 'site' ) . '</a>';
540 if ( current_user_can( 'delete_site', $blog['blog_id'] ) ) {
541 $actions['delete'] = '<a href="' . esc_url( wp_nonce_url( network_admin_url( 'sites.php?action=confirm&action2=deleteblog&id=' . $blog['blog_id'] ), 'deleteblog_' . $blog['blog_id'] ) ) . '">' . __( 'Delete' ) . '</a>';
545 $actions['visit'] = "<a href='" . esc_url( get_home_url( $blog['blog_id'], '/' ) ) . "' rel='permalink'>" . __( 'Visit' ) . '</a>';
548 * Filters the action links displayed for each site in the Sites list table.
550 * The 'Edit', 'Dashboard', 'Delete', and 'Visit' links are displayed by
551 * default for each site. The site's status determines whether to show the
552 * 'Activate' or 'Deactivate' link, 'Unarchive' or 'Archive' links, and
553 * 'Not Spam' or 'Spam' link for each site.
557 * @param array $actions An array of action links to be displayed.
558 * @param int $blog_id The site ID.
559 * @param string $blogname Site path, formatted depending on whether it is a sub-domain
560 * or subdirectory multisite install.
562 $actions = apply_filters( 'manage_sites_action_links', array_filter( $actions ), $blog['blog_id'], $blogname );
563 return $this->row_actions( $actions );