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 $mode = ( empty( $_REQUEST['mode'] ) ) ? 'list' : $_REQUEST['mode'];
74 $per_page = $this->get_items_per_page( 'sites_network_per_page' );
76 $pagenum = $this->get_pagenum();
78 $s = isset( $_REQUEST['s'] ) ? wp_unslash( trim( $_REQUEST[ 's' ] ) ) : '';
80 if ( false !== strpos($s, '*') ) {
86 * If the network is large and a search is not being performed, show only
87 * the latest blogs with no paging in order to avoid expensive count queries.
89 if ( !$s && wp_is_large_network() ) {
90 if ( !isset($_REQUEST['orderby']) )
91 $_GET['orderby'] = $_REQUEST['orderby'] = '';
92 if ( !isset($_REQUEST['order']) )
93 $_GET['order'] = $_REQUEST['order'] = 'DESC';
96 $query = "SELECT * FROM {$wpdb->blogs} WHERE site_id = '{$wpdb->siteid}' ";
100 } elseif ( preg_match( '/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/', $s ) ||
101 preg_match( '/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.?$/', $s ) ||
102 preg_match( '/^[0-9]{1,3}\.[0-9]{1,3}\.?$/', $s ) ||
103 preg_match( '/^[0-9]{1,3}\.$/', $s ) ) {
105 $sql = $wpdb->prepare( "SELECT blog_id FROM {$wpdb->registration_log} WHERE {$wpdb->registration_log}.IP LIKE %s", $wpdb->esc_like( $s ) . $wild );
106 $reg_blog_ids = $wpdb->get_col( $sql );
108 if ( !$reg_blog_ids )
109 $reg_blog_ids = array( 0 );
113 WHERE site_id = '{$wpdb->siteid}'
114 AND {$wpdb->blogs}.blog_id IN (" . implode( ', ', $reg_blog_ids ) . ")";
116 if ( is_numeric($s) && empty( $wild ) ) {
117 $query .= $wpdb->prepare( " AND ( {$wpdb->blogs}.blog_id = %s )", $s );
118 } elseif ( is_subdomain_install() ) {
119 $blog_s = str_replace( '.' . $current_site->domain, '', $s );
120 $blog_s = $wpdb->esc_like( $blog_s ) . $wild . $wpdb->esc_like( '.' . $current_site->domain );
121 $query .= $wpdb->prepare( " AND ( {$wpdb->blogs}.domain LIKE %s ) ", $blog_s );
123 if ( $s != trim('/', $current_site->path) ) {
124 $blog_s = $wpdb->esc_like( $current_site->path . $s ) . $wild . $wpdb->esc_like( '/' );
126 $blog_s = $wpdb->esc_like( $s );
128 $query .= $wpdb->prepare( " AND ( {$wpdb->blogs}.path LIKE %s )", $blog_s );
132 $order_by = isset( $_REQUEST['orderby'] ) ? $_REQUEST['orderby'] : '';
133 if ( $order_by === 'registered' ) {
134 $query .= ' ORDER BY registered ';
135 } elseif ( $order_by === 'lastupdated' ) {
136 $query .= ' ORDER BY last_updated ';
137 } elseif ( $order_by === 'blogname' ) {
138 if ( is_subdomain_install() ) {
139 $query .= ' ORDER BY domain ';
141 $query .= ' ORDER BY path ';
143 } elseif ( $order_by === 'blog_id' ) {
144 $query .= ' ORDER BY blog_id ';
149 if ( isset( $order_by ) ) {
150 $order = ( isset( $_REQUEST['order'] ) && 'DESC' === strtoupper( $_REQUEST['order'] ) ) ? "DESC" : "ASC";
154 // Don't do an unbounded count on large networks
155 if ( ! wp_is_large_network() )
156 $total = $wpdb->get_var( str_replace( 'SELECT *', 'SELECT COUNT( blog_id )', $query ) );
158 $query .= " LIMIT " . intval( ( $pagenum - 1 ) * $per_page ) . ", " . intval( $per_page );
159 $this->items = $wpdb->get_results( $query, ARRAY_A );
161 if ( wp_is_large_network() )
162 $total = count($this->items);
164 $this->set_pagination_args( array(
165 'total_items' => $total,
166 'per_page' => $per_page,
173 public function no_items() {
174 _e( 'No sites found.' );
181 protected function get_bulk_actions() {
183 if ( current_user_can( 'delete_sites' ) )
184 $actions['delete'] = __( 'Delete' );
185 $actions['spam'] = _x( 'Mark as Spam', 'site' );
186 $actions['notspam'] = _x( 'Not Spam', 'site' );
192 * @global string $mode
194 * @param string $which
196 protected function pagination( $which ) {
199 parent::pagination( $which );
201 if ( 'top' === $which )
202 $this->view_switcher( $mode );
208 public function get_columns() {
209 $sites_columns = array(
210 'cb' => '<input type="checkbox" />',
211 'blogname' => __( 'URL' ),
212 'lastupdated' => __( 'Last Updated' ),
213 'registered' => _x( 'Registered', 'site' ),
214 'users' => __( 'Users' ),
217 if ( has_filter( 'wpmublogsaction' ) ) {
218 $sites_columns['plugins'] = __( 'Actions' );
222 * Filter the displayed site columns in Sites list table.
226 * @param array $sites_columns An array of displayed site columns. Default 'cb',
227 * 'blogname', 'lastupdated', 'registered', 'users'.
229 return apply_filters( 'wpmu_blogs_columns', $sites_columns );
235 protected function get_sortable_columns() {
237 'blogname' => 'blogname',
238 'lastupdated' => 'lastupdated',
239 'registered' => 'blog_id',
244 * Handles the checkbox column output.
249 * @param array $blog Current site.
251 public function column_cb( $blog ) {
252 if ( ! is_main_site( $blog['blog_id'] ) ) :
253 $blogname = untrailingslashit( $blog['domain'] . $blog['path'] );
255 <label class="screen-reader-text" for="blog_<?php echo $blog['blog_id']; ?>"><?php
256 printf( __( 'Select %s' ), $blogname );
258 <input type="checkbox" id="blog_<?php echo $blog['blog_id'] ?>" name="allblogs[]" value="<?php echo esc_attr( $blog['blog_id'] ) ?>" />
263 * Handles the ID column output.
268 * @param array $blog Current site.
270 public function column_id( $blog ) {
271 echo $blog['blog_id'];
275 * Handles the blogname column output.
280 * @global string $mode
282 * @param array $blog Current blog.
284 public function column_blogname( $blog ) {
287 $blogname = untrailingslashit( $blog['domain'] . $blog['path'] );
288 $blog_states = array();
289 reset( $this->status_list );
291 foreach ( $this->status_list as $status => $col ) {
292 if ( $blog[ $status ] == 1 ) {
293 $blog_states[] = $col[1];
297 if ( ! empty( $blog_states ) ) {
298 $state_count = count( $blog_states );
300 $blog_state .= ' - ';
301 foreach ( $blog_states as $state ) {
303 $sep = ( $i == $state_count ) ? '' : ', ';
304 $blog_state .= "<span class='post-state'>$state$sep</span>";
309 <a href="<?php echo esc_url( network_admin_url( 'site-info.php?id=' . $blog['blog_id'] ) ); ?>" class="edit"><?php echo $blogname . $blog_state; ?></a>
311 if ( 'list' !== $mode ) {
312 switch_to_blog( $blog['blog_id'] );
313 /* translators: 1: site name, 2: site tagline. */
314 echo '<p>' . sprintf( __( '%1$s – <em>%2$s</em>' ), get_option( 'blogname' ), get_option( 'blogdescription ' ) ) . '</p>';
315 restore_current_blog();
320 * Handles the lastupdated column output.
325 * @param array $blog Current site.
327 public function column_lastupdated( $blog ) {
330 if ( 'list' === $mode ) {
331 $date = __( 'Y/m/d' );
333 $date = __( 'Y/m/d g:i:s a' );
336 echo ( $blog['last_updated'] === '0000-00-00 00:00:00' ) ? __( 'Never' ) : mysql2date( $date, $blog['last_updated'] );
340 * Handles the registered column output.
345 * @param array $blog Current site.
347 public function column_registered( $blog ) {
350 if ( 'list' === $mode ) {
351 $date = __( 'Y/m/d' );
353 $date = __( 'Y/m/d g:i:s a' );
356 if ( $blog['registered'] === '0000-00-00 00:00:00' ) {
359 echo mysql2date( $date, $blog['registered'] );
364 * Handles the users column output.
369 * @param array $blog Current site.
371 public function column_users( $blog ) {
372 $user_count = wp_cache_get( $blog['blog_id'] . '_user_count', 'blog-details' );
373 if ( ! $user_count ) {
374 $blog_users = get_users( array( 'blog_id' => $blog['blog_id'], 'fields' => 'ID' ) );
375 $user_count = count( $blog_users );
376 unset( $blog_users );
377 wp_cache_set( $blog['blog_id'] . '_user_count', $user_count, 'blog-details', 12 * HOUR_IN_SECONDS );
381 '<a href="%s">%s</a>',
382 esc_url( network_admin_url( 'site-users.php?id=' . $blog['blog_id'] ) ),
383 number_format_i18n( $user_count )
388 * Handles the plugins column output.
393 * @param array $blog Current site.
395 public function column_plugins( $blog ) {
396 if ( has_filter( 'wpmublogsaction' ) ) {
398 * Fires inside the auxiliary 'Actions' column of the Sites list table.
400 * By default this column is hidden unless something is hooked to the action.
404 * @param int $blog_id The site ID.
406 do_action( 'wpmublogsaction', $blog['blog_id'] );
411 * Handles output for the default column.
416 * @param array $blog Current site.
417 * @param string $column_name Current column name.
419 public function column_default( $blog, $column_name ) {
421 * Fires for each registered custom column in the Sites list table.
425 * @param string $column_name The name of the column to display.
426 * @param int $blog_id The site ID.
428 do_action( 'manage_sites_custom_column', $column_name, $blog['blog_id'] );
433 * @global string $mode
435 public function display_rows() {
436 foreach ( $this->items as $blog ) {
438 reset( $this->status_list );
440 foreach ( $this->status_list as $status => $col ) {
441 if ( $blog[ $status ] == 1 ) {
442 $class = " class='{$col[0]}'";
448 $this->single_row_columns( $blog );
455 * Gets the name of the default primary column.
460 * @return string Name of the default primary column, in this case, 'blogname'.
462 protected function get_default_primary_column_name() {
467 * Generates and displays row action links.
472 * @param object $blog Blog being acted upon.
473 * @param string $column_name Current column name.
474 * @param string $primary Primary column name.
475 * @return string Row actions output.
477 protected function handle_row_actions( $blog, $column_name, $primary ) {
478 if ( $primary !== $column_name ) {
482 $blogname = untrailingslashit( $blog['domain'] . $blog['path'] );
486 'edit' => '', 'backend' => '',
487 'activate' => '', 'deactivate' => '',
488 'archive' => '', 'unarchive' => '',
489 'spam' => '', 'unspam' => '',
494 $actions['edit'] = '<a href="' . esc_url( network_admin_url( 'site-info.php?id=' . $blog['blog_id'] ) ) . '">' . __( 'Edit' ) . '</a>';
495 $actions['backend'] = "<a href='" . esc_url( get_admin_url( $blog['blog_id'] ) ) . "' class='edit'>" . __( 'Dashboard' ) . '</a>';
496 if ( get_current_site()->blog_id != $blog['blog_id'] ) {
497 if ( $blog['deleted'] == '1' ) {
498 $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>';
500 $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>';
503 if ( $blog['archived'] == '1' ) {
504 $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>';
506 $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>';
509 if ( $blog['spam'] == '1' ) {
510 $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>';
512 $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>';
515 if ( current_user_can( 'delete_site', $blog['blog_id'] ) ) {
516 $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>';
520 $actions['visit'] = "<a href='" . esc_url( get_home_url( $blog['blog_id'], '/' ) ) . "' rel='permalink'>" . __( 'Visit' ) . '</a>';
523 * Filter the action links displayed for each site in the Sites list table.
525 * The 'Edit', 'Dashboard', 'Delete', and 'Visit' links are displayed by
526 * default for each site. The site's status determines whether to show the
527 * 'Activate' or 'Deactivate' link, 'Unarchive' or 'Archive' links, and
528 * 'Not Spam' or 'Spam' link for each site.
532 * @param array $actions An array of action links to be displayed.
533 * @param int $blog_id The site ID.
534 * @param string $blogname Site path, formatted depending on whether it is a sub-domain
535 * or subdirectory multisite install.
537 $actions = apply_filters( 'manage_sites_action_links', array_filter( $actions ), $blog['blog_id'], $blogname );
538 return $this->row_actions( $actions );