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' );
64 * @global string $mode
67 public function prepare_items() {
68 global $s, $mode, $wpdb;
70 $current_site = get_current_site();
72 if ( ! empty( $_REQUEST['mode'] ) ) {
73 $mode = $_REQUEST['mode'] === 'excerpt' ? 'excerpt' : 'list';
74 set_user_setting( 'sites_list_mode', $mode );
76 $mode = get_user_setting( 'sites_list_mode', 'list' );
79 $per_page = $this->get_items_per_page( 'sites_network_per_page' );
81 $pagenum = $this->get_pagenum();
83 $s = isset( $_REQUEST['s'] ) ? wp_unslash( trim( $_REQUEST[ 's' ] ) ) : '';
85 if ( false !== strpos($s, '*') ) {
91 * If the network is large and a search is not being performed, show only
92 * the latest sites with no paging in order to avoid expensive count queries.
94 if ( !$s && wp_is_large_network() ) {
95 if ( !isset($_REQUEST['orderby']) )
96 $_GET['orderby'] = $_REQUEST['orderby'] = '';
97 if ( !isset($_REQUEST['order']) )
98 $_GET['order'] = $_REQUEST['order'] = 'DESC';
101 $query = "SELECT * FROM {$wpdb->blogs} WHERE site_id = '{$wpdb->siteid}' ";
105 } elseif ( preg_match( '/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/', $s ) ||
106 preg_match( '/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.?$/', $s ) ||
107 preg_match( '/^[0-9]{1,3}\.[0-9]{1,3}\.?$/', $s ) ||
108 preg_match( '/^[0-9]{1,3}\.$/', $s ) ) {
110 $sql = $wpdb->prepare( "SELECT blog_id FROM {$wpdb->registration_log} WHERE {$wpdb->registration_log}.IP LIKE %s", $wpdb->esc_like( $s ) . $wild );
111 $reg_blog_ids = $wpdb->get_col( $sql );
113 if ( !$reg_blog_ids )
114 $reg_blog_ids = array( 0 );
118 WHERE site_id = '{$wpdb->siteid}'
119 AND {$wpdb->blogs}.blog_id IN (" . implode( ', ', $reg_blog_ids ) . ")";
121 if ( is_numeric($s) && empty( $wild ) ) {
122 $query .= $wpdb->prepare( " AND ( {$wpdb->blogs}.blog_id = %s )", $s );
123 } elseif ( is_subdomain_install() ) {
124 $blog_s = str_replace( '.' . $current_site->domain, '', $s );
125 $blog_s = $wpdb->esc_like( $blog_s ) . $wild . $wpdb->esc_like( '.' . $current_site->domain );
126 $query .= $wpdb->prepare( " AND ( {$wpdb->blogs}.domain LIKE %s ) ", $blog_s );
128 if ( $s != trim('/', $current_site->path) ) {
129 $blog_s = $wpdb->esc_like( $current_site->path . $s ) . $wild . $wpdb->esc_like( '/' );
131 $blog_s = $wpdb->esc_like( $s );
133 $query .= $wpdb->prepare( " AND ( {$wpdb->blogs}.path LIKE %s )", $blog_s );
137 $order_by = isset( $_REQUEST['orderby'] ) ? $_REQUEST['orderby'] : '';
138 if ( $order_by === 'registered' ) {
139 $query .= ' ORDER BY registered ';
140 } elseif ( $order_by === 'lastupdated' ) {
141 $query .= ' ORDER BY last_updated ';
142 } elseif ( $order_by === 'blogname' ) {
143 if ( is_subdomain_install() ) {
144 $query .= ' ORDER BY domain ';
146 $query .= ' ORDER BY path ';
148 } elseif ( $order_by === 'blog_id' ) {
149 $query .= ' ORDER BY blog_id ';
154 if ( isset( $order_by ) ) {
155 $order = ( isset( $_REQUEST['order'] ) && 'DESC' === strtoupper( $_REQUEST['order'] ) ) ? "DESC" : "ASC";
159 // Don't do an unbounded count on large networks
160 if ( ! wp_is_large_network() )
161 $total = $wpdb->get_var( str_replace( 'SELECT *', 'SELECT COUNT( blog_id )', $query ) );
163 $query .= " LIMIT " . intval( ( $pagenum - 1 ) * $per_page ) . ", " . intval( $per_page );
164 $this->items = $wpdb->get_results( $query, ARRAY_A );
166 if ( wp_is_large_network() )
167 $total = count($this->items);
169 $this->set_pagination_args( array(
170 'total_items' => $total,
171 'per_page' => $per_page,
178 public function no_items() {
179 _e( 'No sites found.' );
186 protected function get_bulk_actions() {
188 if ( current_user_can( 'delete_sites' ) )
189 $actions['delete'] = __( 'Delete' );
190 $actions['spam'] = _x( 'Mark as Spam', 'site' );
191 $actions['notspam'] = _x( 'Not Spam', 'site' );
197 * @global string $mode
199 * @param string $which
201 protected function pagination( $which ) {
204 parent::pagination( $which );
206 if ( 'top' === $which )
207 $this->view_switcher( $mode );
213 public function get_columns() {
214 $sites_columns = array(
215 'cb' => '<input type="checkbox" />',
216 'blogname' => __( 'URL' ),
217 'lastupdated' => __( 'Last Updated' ),
218 'registered' => _x( 'Registered', 'site' ),
219 'users' => __( 'Users' ),
222 if ( has_filter( 'wpmublogsaction' ) ) {
223 $sites_columns['plugins'] = __( 'Actions' );
227 * Filter the displayed site columns in Sites list table.
231 * @param array $sites_columns An array of displayed site columns. Default 'cb',
232 * 'blogname', 'lastupdated', 'registered', 'users'.
234 return apply_filters( 'wpmu_blogs_columns', $sites_columns );
240 protected function get_sortable_columns() {
242 'blogname' => 'blogname',
243 'lastupdated' => 'lastupdated',
244 'registered' => 'blog_id',
249 * Handles the checkbox column output.
254 * @param array $blog Current site.
256 public function column_cb( $blog ) {
257 if ( ! is_main_site( $blog['blog_id'] ) ) :
258 $blogname = untrailingslashit( $blog['domain'] . $blog['path'] );
260 <label class="screen-reader-text" for="blog_<?php echo $blog['blog_id']; ?>"><?php
261 printf( __( 'Select %s' ), $blogname );
263 <input type="checkbox" id="blog_<?php echo $blog['blog_id'] ?>" name="allblogs[]" value="<?php echo esc_attr( $blog['blog_id'] ) ?>" />
268 * Handles the ID column output.
273 * @param array $blog Current site.
275 public function column_id( $blog ) {
276 echo $blog['blog_id'];
280 * Handles the site name column output.
285 * @global string $mode
287 * @param array $blog Current site.
289 public function column_blogname( $blog ) {
292 $blogname = untrailingslashit( $blog['domain'] . $blog['path'] );
293 $blog_states = array();
294 reset( $this->status_list );
296 foreach ( $this->status_list as $status => $col ) {
297 if ( $blog[ $status ] == 1 ) {
298 $blog_states[] = $col[1];
302 if ( ! empty( $blog_states ) ) {
303 $state_count = count( $blog_states );
305 $blog_state .= ' - ';
306 foreach ( $blog_states as $state ) {
308 $sep = ( $i == $state_count ) ? '' : ', ';
309 $blog_state .= "<span class='post-state'>$state$sep</span>";
314 <a href="<?php echo esc_url( network_admin_url( 'site-info.php?id=' . $blog['blog_id'] ) ); ?>" class="edit"><?php echo $blogname . $blog_state; ?></a>
316 if ( 'list' !== $mode ) {
317 switch_to_blog( $blog['blog_id'] );
320 /* translators: 1: site name, 2: site tagline. */
321 __( '%1$s – %2$s' ),
322 get_option( 'blogname' ),
323 '<em>' . get_option( 'blogdescription ' ) . '</em>'
326 restore_current_blog();
331 * Handles the lastupdated column output.
336 * @param array $blog Current site.
338 public function column_lastupdated( $blog ) {
341 if ( 'list' === $mode ) {
342 $date = __( 'Y/m/d' );
344 $date = __( 'Y/m/d g:i:s a' );
347 echo ( $blog['last_updated'] === '0000-00-00 00:00:00' ) ? __( 'Never' ) : mysql2date( $date, $blog['last_updated'] );
351 * Handles the registered column output.
356 * @param array $blog Current site.
358 public function column_registered( $blog ) {
361 if ( 'list' === $mode ) {
362 $date = __( 'Y/m/d' );
364 $date = __( 'Y/m/d g:i:s a' );
367 if ( $blog['registered'] === '0000-00-00 00:00:00' ) {
370 echo mysql2date( $date, $blog['registered'] );
375 * Handles the users column output.
380 * @param array $blog Current site.
382 public function column_users( $blog ) {
383 $user_count = wp_cache_get( $blog['blog_id'] . '_user_count', 'blog-details' );
384 if ( ! $user_count ) {
385 $blog_users = get_users( array( 'blog_id' => $blog['blog_id'], 'fields' => 'ID' ) );
386 $user_count = count( $blog_users );
387 unset( $blog_users );
388 wp_cache_set( $blog['blog_id'] . '_user_count', $user_count, 'blog-details', 12 * HOUR_IN_SECONDS );
392 '<a href="%s">%s</a>',
393 esc_url( network_admin_url( 'site-users.php?id=' . $blog['blog_id'] ) ),
394 number_format_i18n( $user_count )
399 * Handles the plugins column output.
404 * @param array $blog Current site.
406 public function column_plugins( $blog ) {
407 if ( has_filter( 'wpmublogsaction' ) ) {
409 * Fires inside the auxiliary 'Actions' column of the Sites list table.
411 * By default this column is hidden unless something is hooked to the action.
415 * @param int $blog_id The site ID.
417 do_action( 'wpmublogsaction', $blog['blog_id'] );
422 * Handles output for the default column.
427 * @param array $blog Current site.
428 * @param string $column_name Current column name.
430 public function column_default( $blog, $column_name ) {
432 * Fires for each registered custom column in the Sites list table.
436 * @param string $column_name The name of the column to display.
437 * @param int $blog_id The site ID.
439 do_action( 'manage_sites_custom_column', $column_name, $blog['blog_id'] );
444 * @global string $mode
446 public function display_rows() {
447 foreach ( $this->items as $blog ) {
449 reset( $this->status_list );
451 foreach ( $this->status_list as $status => $col ) {
452 if ( $blog[ $status ] == 1 ) {
453 $class = " class='{$col[0]}'";
459 $this->single_row_columns( $blog );
466 * Gets the name of the default primary column.
471 * @return string Name of the default primary column, in this case, 'blogname'.
473 protected function get_default_primary_column_name() {
478 * Generates and displays row action links.
483 * @param object $blog Site being acted upon.
484 * @param string $column_name Current column name.
485 * @param string $primary Primary column name.
486 * @return string Row actions output.
488 protected function handle_row_actions( $blog, $column_name, $primary ) {
489 if ( $primary !== $column_name ) {
493 $blogname = untrailingslashit( $blog['domain'] . $blog['path'] );
497 'edit' => '', 'backend' => '',
498 'activate' => '', 'deactivate' => '',
499 'archive' => '', 'unarchive' => '',
500 'spam' => '', 'unspam' => '',
505 $actions['edit'] = '<a href="' . esc_url( network_admin_url( 'site-info.php?id=' . $blog['blog_id'] ) ) . '">' . __( 'Edit' ) . '</a>';
506 $actions['backend'] = "<a href='" . esc_url( get_admin_url( $blog['blog_id'] ) ) . "' class='edit'>" . __( 'Dashboard' ) . '</a>';
507 if ( get_current_site()->blog_id != $blog['blog_id'] ) {
508 if ( $blog['deleted'] == '1' ) {
509 $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>';
511 $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>';
514 if ( $blog['archived'] == '1' ) {
515 $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>';
517 $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>';
520 if ( $blog['spam'] == '1' ) {
521 $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>';
523 $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>';
526 if ( current_user_can( 'delete_site', $blog['blog_id'] ) ) {
527 $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>';
531 $actions['visit'] = "<a href='" . esc_url( get_home_url( $blog['blog_id'], '/' ) ) . "' rel='permalink'>" . __( 'Visit' ) . '</a>';
534 * Filter the action links displayed for each site in the Sites list table.
536 * The 'Edit', 'Dashboard', 'Delete', and 'Visit' links are displayed by
537 * default for each site. The site's status determines whether to show the
538 * 'Activate' or 'Deactivate' link, 'Unarchive' or 'Archive' links, and
539 * 'Not Spam' or 'Spam' link for each site.
543 * @param array $actions An array of action links to be displayed.
544 * @param int $blog_id The site ID.
545 * @param string $blogname Site path, formatted depending on whether it is a sub-domain
546 * or subdirectory multisite install.
548 $actions = apply_filters( 'manage_sites_action_links', array_filter( $actions ), $blog['blog_id'], $blogname );
549 return $this->row_actions( $actions );