]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/includes/class-wp-ms-sites-list-table.php
WordPress 4.3
[autoinstalls/wordpress.git] / wp-admin / includes / class-wp-ms-sites-list-table.php
1 <?php
2 /**
3  * Sites List Table class.
4  *
5  * @package WordPress
6  * @subpackage List_Table
7  * @since 3.1.0
8  * @access private
9  */
10 class WP_MS_Sites_List_Table extends WP_List_Table {
11
12         /**
13          * Site status list.
14          *
15          * @since 4.3.0
16          * @access public
17          * @var array
18          */
19         public $status_list;
20
21         /**
22          * Constructor.
23          *
24          * @since 3.1.0
25          * @access public
26          *
27          * @see WP_List_Table::__construct() for more information on default arguments.
28          *
29          * @param array $args An associative array of arguments.
30          */
31         public function __construct( $args = array() ) {
32                 $this->status_list = array(
33                         'archived' => array( 'site-archived', __( 'Archived' ) ),
34                         'spam'     => array( 'site-spammed', _x( 'Spam', 'site' ) ),
35                         'deleted'  => array( 'site-deleted', __( 'Deleted' ) ),
36                         'mature'   => array( 'site-mature', __( 'Mature' ) )
37                 );
38
39                 parent::__construct( array(
40                         'plural' => 'sites',
41                         'screen' => isset( $args['screen'] ) ? $args['screen'] : null,
42                 ) );
43         }
44
45         /**
46          *
47          * @return bool
48          */
49         public function ajax_user_can() {
50                 return current_user_can( 'manage_sites' );
51         }
52
53         /**
54          *
55          * @global string $s
56          * @global string $mode
57          * @global wpdb   $wpdb
58          */
59         public function prepare_items() {
60                 global $s, $mode, $wpdb;
61
62                 $current_site = get_current_site();
63
64                 $mode = ( empty( $_REQUEST['mode'] ) ) ? 'list' : $_REQUEST['mode'];
65
66                 $per_page = $this->get_items_per_page( 'sites_network_per_page' );
67
68                 $pagenum = $this->get_pagenum();
69
70                 $s = isset( $_REQUEST['s'] ) ? wp_unslash( trim( $_REQUEST[ 's' ] ) ) : '';
71                 $wild = '';
72                 if ( false !== strpos($s, '*') ) {
73                         $wild = '%';
74                         $s = trim($s, '*');
75                 }
76
77                 /*
78                  * If the network is large and a search is not being performed, show only
79                  * the latest blogs with no paging in order to avoid expensive count queries.
80                  */
81                 if ( !$s && wp_is_large_network() ) {
82                         if ( !isset($_REQUEST['orderby']) )
83                                 $_GET['orderby'] = $_REQUEST['orderby'] = '';
84                         if ( !isset($_REQUEST['order']) )
85                                 $_GET['order'] = $_REQUEST['order'] = 'DESC';
86                 }
87
88                 $query = "SELECT * FROM {$wpdb->blogs} WHERE site_id = '{$wpdb->siteid}' ";
89
90                 if ( empty($s) ) {
91                         // Nothing to do.
92                 } elseif ( preg_match( '/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/', $s ) ||
93                                         preg_match( '/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.?$/', $s ) ||
94                                         preg_match( '/^[0-9]{1,3}\.[0-9]{1,3}\.?$/', $s ) ||
95                                         preg_match( '/^[0-9]{1,3}\.$/', $s ) ) {
96                         // IPv4 address
97                         $sql = $wpdb->prepare( "SELECT blog_id FROM {$wpdb->registration_log} WHERE {$wpdb->registration_log}.IP LIKE %s", $wpdb->esc_like( $s ) . $wild );
98                         $reg_blog_ids = $wpdb->get_col( $sql );
99
100                         if ( !$reg_blog_ids )
101                                 $reg_blog_ids = array( 0 );
102
103                         $query = "SELECT *
104                                 FROM {$wpdb->blogs}
105                                 WHERE site_id = '{$wpdb->siteid}'
106                                 AND {$wpdb->blogs}.blog_id IN (" . implode( ', ', $reg_blog_ids ) . ")";
107                 } else {
108                         if ( is_numeric($s) && empty( $wild ) ) {
109                                 $query .= $wpdb->prepare( " AND ( {$wpdb->blogs}.blog_id = %s )", $s );
110                         } elseif ( is_subdomain_install() ) {
111                                 $blog_s = str_replace( '.' . $current_site->domain, '', $s );
112                                 $blog_s = $wpdb->esc_like( $blog_s ) . $wild . $wpdb->esc_like( '.' . $current_site->domain );
113                                 $query .= $wpdb->prepare( " AND ( {$wpdb->blogs}.domain LIKE %s ) ", $blog_s );
114                         } else {
115                                 if ( $s != trim('/', $current_site->path) ) {
116                                         $blog_s = $wpdb->esc_like( $current_site->path . $s ) . $wild . $wpdb->esc_like( '/' );
117                                 } else {
118                                         $blog_s = $wpdb->esc_like( $s );
119                                 }
120                                 $query .= $wpdb->prepare( " AND  ( {$wpdb->blogs}.path LIKE %s )", $blog_s );
121                         }
122                 }
123
124                 $order_by = isset( $_REQUEST['orderby'] ) ? $_REQUEST['orderby'] : '';
125                 if ( $order_by == 'registered' ) {
126                         $query .= ' ORDER BY registered ';
127                 } elseif ( $order_by == 'lastupdated' ) {
128                         $query .= ' ORDER BY last_updated ';
129                 } elseif ( $order_by == 'blogname' ) {
130                         if ( is_subdomain_install() ) {
131                                 $query .= ' ORDER BY domain ';
132                         } else {
133                                 $query .= ' ORDER BY path ';
134                         }
135                 } elseif ( $order_by == 'blog_id' ) {
136                         $query .= ' ORDER BY blog_id ';
137                 } else {
138                         $order_by = null;
139                 }
140
141                 if ( isset( $order_by ) ) {
142                         $order = ( isset( $_REQUEST['order'] ) && 'DESC' == strtoupper( $_REQUEST['order'] ) ) ? "DESC" : "ASC";
143                         $query .= $order;
144                 }
145
146                 // Don't do an unbounded count on large networks
147                 if ( ! wp_is_large_network() )
148                         $total = $wpdb->get_var( str_replace( 'SELECT *', 'SELECT COUNT( blog_id )', $query ) );
149
150                 $query .= " LIMIT " . intval( ( $pagenum - 1 ) * $per_page ) . ", " . intval( $per_page );
151                 $this->items = $wpdb->get_results( $query, ARRAY_A );
152
153                 if ( wp_is_large_network() )
154                         $total = count($this->items);
155
156                 $this->set_pagination_args( array(
157                         'total_items' => $total,
158                         'per_page' => $per_page,
159                 ) );
160         }
161
162         /**
163          * @access public
164          */
165         public function no_items() {
166                 _e( 'No sites found.' );
167         }
168
169         /**
170          *
171          * @return array
172          */
173         protected function get_bulk_actions() {
174                 $actions = array();
175                 if ( current_user_can( 'delete_sites' ) )
176                         $actions['delete'] = __( 'Delete' );
177                 $actions['spam'] = _x( 'Mark as Spam', 'site' );
178                 $actions['notspam'] = _x( 'Not Spam', 'site' );
179
180                 return $actions;
181         }
182
183         /**
184          * @global string $mode
185          *
186          * @param string $which
187          */
188         protected function pagination( $which ) {
189                 global $mode;
190
191                 parent::pagination( $which );
192
193                 if ( 'top' == $which )
194                         $this->view_switcher( $mode );
195         }
196
197         /**
198          * @return array
199          */
200         public function get_columns() {
201                 $sites_columns = array(
202                         'cb'          => '<input type="checkbox" />',
203                         'blogname'    => __( 'URL' ),
204                         'lastupdated' => __( 'Last Updated' ),
205                         'registered'  => _x( 'Registered', 'site' ),
206                         'users'       => __( 'Users' ),
207                 );
208
209                 if ( has_filter( 'wpmublogsaction' ) ) {
210                         $sites_columns['plugins'] = __( 'Actions' );
211                 }
212
213                 /**
214                  * Filter the displayed site columns in Sites list table.
215                  *
216                  * @since MU
217                  *
218                  * @param array $sites_columns An array of displayed site columns. Default 'cb',
219                  *                             'blogname', 'lastupdated', 'registered', 'users'.
220                  */
221                 return apply_filters( 'wpmu_blogs_columns', $sites_columns );
222         }
223
224         /**
225          * @return array
226          */
227         protected function get_sortable_columns() {
228                 return array(
229                         'blogname'    => 'blogname',
230                         'lastupdated' => 'lastupdated',
231                         'registered'  => 'blog_id',
232                 );
233         }
234
235         /**
236          * Handles the checkbox column output.
237          *
238          * @since 4.3.0
239          * @access public
240          *
241          * @param array $blog Current site.
242          */
243         public function column_cb( $blog ) {
244                 if ( ! is_main_site( $blog['blog_id'] ) ) :
245                         $blogname = untrailingslashit( $blog['domain'] . $blog['path'] );
246                 ?>
247                         <label class="screen-reader-text" for="blog_<?php echo $blog['blog_id']; ?>"><?php
248                                 printf( __( 'Select %s' ), $blogname );
249                         ?></label>
250                         <input type="checkbox" id="blog_<?php echo $blog['blog_id'] ?>" name="allblogs[]" value="<?php echo esc_attr( $blog['blog_id'] ) ?>" />
251                 <?php endif;
252         }
253
254         /**
255          * Handles the blogname column output.
256          *
257          * @since 4.3.0
258          * @access public
259          *
260          * @global string $mode
261          *
262          * @param array $blog Current blog.
263          */
264         public function column_blogname( $blog ) {
265                 global $mode;
266
267                 $blogname = untrailingslashit( $blog['domain'] . $blog['path'] );
268                 $blog_states = array();
269                 reset( $this->status_list );
270
271                 foreach ( $this->status_list as $status => $col ) {
272                         if ( $blog[ $status ] == 1 ) {
273                                 $blog_states[] = $col[1];
274                         }
275                 }
276                 $blog_state = '';
277                 if ( ! empty( $blog_states ) ) {
278                         $state_count = count( $blog_states );
279                         $i = 0;
280                         $blog_state .= ' - ';
281                         foreach ( $blog_states as $state ) {
282                                 ++$i;
283                                 $sep = ( $i == $state_count ) ? '' : ', ';
284                                 $blog_state .= "<span class='post-state'>$state$sep</span>";
285                         }
286                 }
287
288                 ?>
289                 <a href="<?php echo esc_url( network_admin_url( 'site-info.php?id=' . $blog['blog_id'] ) ); ?>" class="edit"><?php echo $blogname . $blog_state; ?></a>
290                 <?php
291                 if ( 'list' !== $mode ) {
292                         switch_to_blog( $blog['blog_id'] );
293                         /* translators: 1: site name, 2: site tagline. */
294                         echo '<p>' . sprintf( __( '%1$s &#8211; <em>%2$s</em>' ), get_option( 'blogname' ), get_option( 'blogdescription ' ) ) . '</p>';
295                         restore_current_blog();
296                 }
297         }
298
299         /**
300          * Handles the lastupdated column output.
301          *
302          * @since 4.3.0
303          * @access public
304          *
305          * @param array $blog Current site.
306          */
307         public function column_lastupdated( $blog ) {
308                 global $mode;
309
310                 if ( 'list' == $mode ) {
311                         $date = __( 'Y/m/d' );
312                 } else {
313                         $date = __( 'Y/m/d g:i:s a' );
314                 }
315
316                 echo ( $blog['last_updated'] == '0000-00-00 00:00:00' ) ? __( 'Never' ) : mysql2date( $date, $blog['last_updated'] );
317         }
318
319         /**
320          * Handles the registered column output.
321          *
322          * @since 4.3.0
323          * @access public
324          *
325          * @param array $blog Current site.
326          */
327         public function column_registered( $blog ) {
328                 global $mode;
329
330                 if ( 'list' == $mode ) {
331                         $date = __( 'Y/m/d' );
332                 } else {
333                         $date = __( 'Y/m/d g:i:s a' );
334                 }
335
336                 if ( $blog['registered'] == '0000-00-00 00:00:00' ) {
337                         echo '&#x2014;';
338                 } else {
339                         echo mysql2date( $date, $blog['registered'] );
340                 }
341         }
342
343         /**
344          * Handles the users column output.
345          *
346          * @since 4.3.0
347          * @access public
348          *
349          * @param array $blog Current site.
350          */
351         public function column_users( $blog ) {
352                 $user_count = wp_cache_get( $blog['blog_id'] . '_user_count', 'blog-details' );
353                 if ( ! $user_count ) {
354                         $blog_users = get_users( array( 'blog_id' => $blog['blog_id'], 'fields' => 'ID' ) );
355                         $user_count = count( $blog_users );
356                         unset( $blog_users );
357                         wp_cache_set( $blog['blog_id'] . '_user_count', $user_count, 'blog-details', 12 * HOUR_IN_SECONDS );
358                 }
359
360                 printf(
361                         '<a href="%s">%s</a>',
362                         esc_url( network_admin_url( 'site-users.php?id=' . $blog['blog_id'] ) ),
363                         number_format_i18n( $user_count )
364                 );
365         }
366
367         /**
368          * Handles the plugins column output.
369          *
370          * @since 4.3.0
371          * @access public
372          *
373          * @param array $blog Current site.
374          */
375         public function column_plugins( $blog ) {
376                 if ( has_filter( 'wpmublogsaction' ) ) {
377                         /**
378                          * Fires inside the auxiliary 'Actions' column of the Sites list table.
379                          *
380                          * By default this column is hidden unless something is hooked to the action.
381                          *
382                          * @since MU
383                          *
384                          * @param int $blog_id The site ID.
385                          */
386                         do_action( 'wpmublogsaction', $blog['blog_id'] );
387                 }
388         }
389
390         /**
391          * Handles output for the default column.
392          *
393          * @since 4.3.0
394          * @access public
395          *
396          * @param array  $blog        Current site.
397          * @param string $column_name Current column name.
398          */
399         public function column_default( $blog, $column_name ) {
400                 /**
401                  * Fires for each registered custom column in the Sites list table.
402                  *
403                  * @since 3.1.0
404                  *
405                  * @param string $column_name The name of the column to display.
406                  * @param int    $blog_id     The site ID.
407                  */
408                 do_action( 'manage_sites_custom_column', $column_name, $blog['blog_id'] );
409         }
410
411         /**
412          *
413          * @global string $mode
414          */
415         public function display_rows() {
416                 foreach ( $this->items as $blog ) {
417                         $class = '';
418                         reset( $this->status_list );
419
420                         foreach ( $this->status_list as $status => $col ) {
421                                 if ( $blog[ $status ] == 1 ) {
422                                         $class = " class='{$col[0]}'";
423                                 }
424                         }
425
426                         echo "<tr{$class}>";
427
428                         $this->single_row_columns( $blog );
429
430                         echo '</tr>';
431                 }
432         }
433
434         /**
435          * Gets the name of the default primary column.
436          *
437          * @since 4.3.0
438          * @access protected
439          *
440          * @return string Name of the default primary column, in this case, 'blogname'.
441          */
442         protected function get_default_primary_column_name() {
443                 return 'blogname';
444         }
445
446         /**
447          * Generates and displays row action links.
448          *
449          * @since 4.3.0
450          * @access protected
451          *
452          * @param object $blog        Blog being acted upon.
453          * @param string $column_name Current column name.
454          * @param string $primary     Primary column name.
455          * @return string Row actions output.
456          */
457         protected function handle_row_actions( $blog, $column_name, $primary ) {
458                 if ( $primary !== $column_name ) {
459                         return;
460                 }
461
462                 $blogname = untrailingslashit( $blog['domain'] . $blog['path'] );
463
464                 // Preordered.
465                 $actions = array(
466                         'edit' => '', 'backend' => '',
467                         'activate' => '', 'deactivate' => '',
468                         'archive' => '', 'unarchive' => '',
469                         'spam' => '', 'unspam' => '',
470                         'delete' => '',
471                         'visit' => '',
472                 );
473
474                 $actions['edit']        = '<span class="edit"><a href="' . esc_url( network_admin_url( 'site-info.php?id=' . $blog['blog_id'] ) ) . '">' . __( 'Edit' ) . '</a></span>';
475                 $actions['backend']     = "<span class='backend'><a href='" . esc_url( get_admin_url( $blog['blog_id'] ) ) . "' class='edit'>" . __( 'Dashboard' ) . '</a></span>';
476                 if ( get_current_site()->blog_id != $blog['blog_id'] ) {
477                         if ( $blog['deleted'] == '1' ) {
478                                 $actions['activate']   = '<span class="activate"><a href="' . esc_url( wp_nonce_url( network_admin_url( 'sites.php?action=confirm&amp;action2=activateblog&amp;id=' . $blog['blog_id'] ), 'activateblog_' . $blog['blog_id'] ) ) . '">' . __( 'Activate' ) . '</a></span>';
479                         } else {
480                                 $actions['deactivate'] = '<span class="activate"><a href="' . esc_url( wp_nonce_url( network_admin_url( 'sites.php?action=confirm&amp;action2=deactivateblog&amp;id=' . $blog['blog_id'] ), 'deactivateblog_' . $blog['blog_id'] ) ) . '">' . __( 'Deactivate' ) . '</a></span>';
481                         }
482
483                         if ( $blog['archived'] == '1' ) {
484                                 $actions['unarchive'] = '<span class="archive"><a href="' . esc_url( wp_nonce_url( network_admin_url( 'sites.php?action=confirm&amp;action2=unarchiveblog&amp;id=' . $blog['blog_id'] ), 'unarchiveblog_' . $blog['blog_id'] ) ) . '">' . __( 'Unarchive' ) . '</a></span>';
485                         } else {
486                                 $actions['archive']   = '<span class="archive"><a href="' . esc_url( wp_nonce_url( network_admin_url( 'sites.php?action=confirm&amp;action2=archiveblog&amp;id=' . $blog['blog_id'] ), 'archiveblog_' . $blog['blog_id'] ) ) . '">' . _x( 'Archive', 'verb; site' ) . '</a></span>';
487                         }
488
489                         if ( $blog['spam'] == '1' ) {
490                                 $actions['unspam'] = '<span class="spam"><a href="' . esc_url( wp_nonce_url( network_admin_url( 'sites.php?action=confirm&amp;action2=unspamblog&amp;id=' . $blog['blog_id'] ), 'unspamblog_' . $blog['blog_id'] ) ) . '">' . _x( 'Not Spam', 'site' ) . '</a></span>';
491                         } else {
492                                 $actions['spam']   = '<span class="spam"><a href="' . esc_url( wp_nonce_url( network_admin_url( 'sites.php?action=confirm&amp;action2=spamblog&amp;id=' . $blog['blog_id'] ), 'spamblog_' . $blog['blog_id'] ) ) . '">' . _x( 'Spam', 'site' ) . '</a></span>';
493                         }
494
495                         if ( current_user_can( 'delete_site', $blog['blog_id'] ) ) {
496                                 $actions['delete'] = '<span class="delete"><a href="' . esc_url( wp_nonce_url( network_admin_url( 'sites.php?action=confirm&amp;action2=deleteblog&amp;id=' . $blog['blog_id'] ), 'deleteblog_' . $blog['blog_id'] ) ) . '">' . __( 'Delete' ) . '</a></span>';
497                         }
498                 }
499
500                 $actions['visit']       = "<span class='view'><a href='" . esc_url( get_home_url( $blog['blog_id'], '/' ) ) . "' rel='permalink'>" . __( 'Visit' ) . '</a></span>';
501
502                 /**
503                  * Filter the action links displayed for each site in the Sites list table.
504                  *
505                  * The 'Edit', 'Dashboard', 'Delete', and 'Visit' links are displayed by
506                  * default for each site. The site's status determines whether to show the
507                  * 'Activate' or 'Deactivate' link, 'Unarchive' or 'Archive' links, and
508                  * 'Not Spam' or 'Spam' link for each site.
509                  *
510                  * @since 3.1.0
511                  *
512                  * @param array  $actions  An array of action links to be displayed.
513                  * @param int    $blog_id  The site ID.
514                  * @param string $blogname Site path, formatted depending on whether it is a sub-domain
515                  *                         or subdirectory multisite install.
516                  */
517                 $actions = apply_filters( 'manage_sites_action_links', array_filter( $actions ), $blog['blog_id'], $blogname );
518                 return $this->row_actions( $actions );
519         }
520 }