]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/includes/class-wp-ms-sites-list-table.php
WordPress 4.7-scripts
[autoinstalls/wordpress.git] / wp-admin / includes / class-wp-ms-sites-list-table.php
1 <?php
2 /**
3  * List Table API: WP_MS_Sites_List_Table class
4  *
5  * @package WordPress
6  * @subpackage Administration
7  * @since 3.1.0
8  */
9
10 /**
11  * Core class used to implement displaying sites in a list table for the network admin.
12  *
13  * @since 3.1.0
14  * @access private
15  *
16  * @see WP_List_Table
17  */
18 class WP_MS_Sites_List_Table extends WP_List_Table {
19
20         /**
21          * Site status list.
22          *
23          * @since 4.3.0
24          * @access public
25          * @var array
26          */
27         public $status_list;
28
29         /**
30          * Constructor.
31          *
32          * @since 3.1.0
33          * @access public
34          *
35          * @see WP_List_Table::__construct() for more information on default arguments.
36          *
37          * @param array $args An associative array of arguments.
38          */
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' ) )
45                 );
46
47                 parent::__construct( array(
48                         'plural' => 'sites',
49                         'screen' => isset( $args['screen'] ) ? $args['screen'] : null,
50                 ) );
51         }
52
53         /**
54          *
55          * @return bool
56          */
57         public function ajax_user_can() {
58                 return current_user_can( 'manage_sites' );
59         }
60
61         /**
62          * Prepares the list of sites for display.
63          *
64          * @since 3.1.0
65          *
66          * @global string $s
67          * @global string $mode
68          * @global wpdb   $wpdb
69          */
70         public function prepare_items() {
71                 global $s, $mode, $wpdb;
72
73                 if ( ! empty( $_REQUEST['mode'] ) ) {
74                         $mode = $_REQUEST['mode'] === 'excerpt' ? 'excerpt' : 'list';
75                         set_user_setting( 'sites_list_mode', $mode );
76                 } else {
77                         $mode = get_user_setting( 'sites_list_mode', 'list' );
78                 }
79
80                 $per_page = $this->get_items_per_page( 'sites_network_per_page' );
81
82                 $pagenum = $this->get_pagenum();
83
84                 $s = isset( $_REQUEST['s'] ) ? wp_unslash( trim( $_REQUEST[ 's' ] ) ) : '';
85                 $wild = '';
86                 if ( false !== strpos($s, '*') ) {
87                         $wild = '*';
88                         $s = trim($s, '*');
89                 }
90
91                 /*
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.
94                  */
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';
100                 }
101
102                 $args = array(
103                         'number'     => intval( $per_page ),
104                         'offset'     => intval( ( $pagenum - 1 ) * $per_page ),
105                         'network_id' => get_current_network_id(),
106                 );
107
108                 if ( empty($s) ) {
109                         // Nothing to do.
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 ) ) {
114                         // IPv4 address
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 );
117
118                         if ( $reg_blog_ids ) {
119                                 $args['site__in'] = $reg_blog_ids;
120                         }
121                 } elseif ( is_numeric( $s ) && empty( $wild ) ) {
122                         $args['ID'] = $s;
123                 } else {
124                         $args['search'] = $s;
125
126                         if ( ! is_subdomain_install() ) {
127                                 $args['search_columns'] = array( 'path' );
128                         }
129                 }
130
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';
139                         } else {
140                                 $order_by = 'path';
141                         }
142                 } elseif ( 'blog_id' === $order_by ) {
143                         $order_by = 'id';
144                 } elseif ( ! $order_by ) {
145                         $order_by = false;
146                 }
147
148                 $args['orderby'] = $order_by;
149
150                 if ( $order_by ) {
151                         $args['order'] = ( isset( $_REQUEST['order'] ) && 'DESC' === strtoupper( $_REQUEST['order'] ) ) ? "DESC" : "ASC";
152                 }
153
154                 if ( wp_is_large_network() ) {
155                         $args['no_found_rows'] = true;
156                 } else {
157                         $args['no_found_rows'] = false;
158                 }
159
160                 /**
161                  * Filters the arguments for the site query in the sites list table.
162                  *
163                  * @since 4.6.0
164                  *
165                  * @param array $args An array of get_sites() arguments.
166                  */
167                 $args = apply_filters( 'ms_sites_list_table_query_args', $args );
168
169                 $_sites = get_sites( $args );
170                 if ( is_array( $_sites ) ) {
171                         update_site_cache( $_sites );
172
173                         $this->items = array_slice( $_sites, 0, $per_page );
174                 }
175
176                 $total_sites = get_sites( array_merge( $args, array(
177                         'count' => true,
178                         'offset' => 0,
179                         'number' => 0,
180                 ) ) );
181
182                 $this->set_pagination_args( array(
183                         'total_items' => $total_sites,
184                         'per_page' => $per_page,
185                 ) );
186         }
187
188         /**
189          * @access public
190          */
191         public function no_items() {
192                 _e( 'No sites found.' );
193         }
194
195         /**
196          *
197          * @return array
198          */
199         protected function get_bulk_actions() {
200                 $actions = array();
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' );
205
206                 return $actions;
207         }
208
209         /**
210          * @global string $mode
211          *
212          * @param string $which
213          */
214         protected function pagination( $which ) {
215                 global $mode;
216
217                 parent::pagination( $which );
218
219                 if ( 'top' === $which )
220                         $this->view_switcher( $mode );
221         }
222
223         /**
224          * @return array
225          */
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' ),
233                 );
234
235                 if ( has_filter( 'wpmublogsaction' ) ) {
236                         $sites_columns['plugins'] = __( 'Actions' );
237                 }
238
239                 /**
240                  * Filters the displayed site columns in Sites list table.
241                  *
242                  * @since MU
243                  *
244                  * @param array $sites_columns An array of displayed site columns. Default 'cb',
245                  *                             'blogname', 'lastupdated', 'registered', 'users'.
246                  */
247                 return apply_filters( 'wpmu_blogs_columns', $sites_columns );
248         }
249
250         /**
251          * @return array
252          */
253         protected function get_sortable_columns() {
254                 return array(
255                         'blogname'    => 'blogname',
256                         'lastupdated' => 'lastupdated',
257                         'registered'  => 'blog_id',
258                 );
259         }
260
261         /**
262          * Handles the checkbox column output.
263          *
264          * @since 4.3.0
265          * @access public
266          *
267          * @param array $blog Current site.
268          */
269         public function column_cb( $blog ) {
270                 if ( ! is_main_site( $blog['blog_id'] ) ) :
271                         $blogname = untrailingslashit( $blog['domain'] . $blog['path'] );
272                 ?>
273                         <label class="screen-reader-text" for="blog_<?php echo $blog['blog_id']; ?>"><?php
274                                 printf( __( 'Select %s' ), $blogname );
275                         ?></label>
276                         <input type="checkbox" id="blog_<?php echo $blog['blog_id'] ?>" name="allblogs[]" value="<?php echo esc_attr( $blog['blog_id'] ) ?>" />
277                 <?php endif;
278         }
279
280         /**
281          * Handles the ID column output.
282          *
283          * @since 4.4.0
284          * @access public
285          *
286          * @param array $blog Current site.
287          */
288         public function column_id( $blog ) {
289                 echo $blog['blog_id'];
290         }
291
292         /**
293          * Handles the site name column output.
294          *
295          * @since 4.3.0
296          * @access public
297          *
298          * @global string $mode
299          *
300          * @param array $blog Current site.
301          */
302         public function column_blogname( $blog ) {
303                 global $mode;
304
305                 $blogname = untrailingslashit( $blog['domain'] . $blog['path'] );
306                 $blog_states = array();
307                 reset( $this->status_list );
308
309                 foreach ( $this->status_list as $status => $col ) {
310                         if ( $blog[ $status ] == 1 ) {
311                                 $blog_states[] = $col[1];
312                         }
313                 }
314                 $blog_state = '';
315                 if ( ! empty( $blog_states ) ) {
316                         $state_count = count( $blog_states );
317                         $i = 0;
318                         $blog_state .= ' - ';
319                         foreach ( $blog_states as $state ) {
320                                 ++$i;
321                                 $sep = ( $i == $state_count ) ? '' : ', ';
322                                 $blog_state .= "<span class='post-state'>$state$sep</span>";
323                         }
324                 }
325
326                 ?>
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>
328                 <?php
329                 if ( 'list' !== $mode ) {
330                         switch_to_blog( $blog['blog_id'] );
331                         echo '<p>';
332                         printf(
333                                 /* translators: 1: site name, 2: site tagline. */
334                                 __( '%1$s &#8211; %2$s' ),
335                                 get_option( 'blogname' ),
336                                 '<em>' . get_option( 'blogdescription ' ) . '</em>'
337                         );
338                         echo '</p>';
339                         restore_current_blog();
340                 }
341         }
342
343         /**
344          * Handles the lastupdated column output.
345          *
346          * @since 4.3.0
347          * @access public
348          *
349          * @param array $blog Current site.
350          */
351         public function column_lastupdated( $blog ) {
352                 global $mode;
353
354                 if ( 'list' === $mode ) {
355                         $date = __( 'Y/m/d' );
356                 } else {
357                         $date = __( 'Y/m/d g:i:s a' );
358                 }
359
360                 echo ( $blog['last_updated'] === '0000-00-00 00:00:00' ) ? __( 'Never' ) : mysql2date( $date, $blog['last_updated'] );
361         }
362
363         /**
364          * Handles the registered column output.
365          *
366          * @since 4.3.0
367          * @access public
368          *
369          * @param array $blog Current site.
370          */
371         public function column_registered( $blog ) {
372                 global $mode;
373
374                 if ( 'list' === $mode ) {
375                         $date = __( 'Y/m/d' );
376                 } else {
377                         $date = __( 'Y/m/d g:i:s a' );
378                 }
379
380                 if ( $blog['registered'] === '0000-00-00 00:00:00' ) {
381                         echo '&#x2014;';
382                 } else {
383                         echo mysql2date( $date, $blog['registered'] );
384                 }
385         }
386
387         /**
388          * Handles the users column output.
389          *
390          * @since 4.3.0
391          * @access public
392          *
393          * @param array $blog Current site.
394          */
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 );
402                 }
403
404                 printf(
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 )
408                 );
409         }
410
411         /**
412          * Handles the plugins column output.
413          *
414          * @since 4.3.0
415          * @access public
416          *
417          * @param array $blog Current site.
418          */
419         public function column_plugins( $blog ) {
420                 if ( has_filter( 'wpmublogsaction' ) ) {
421                         /**
422                          * Fires inside the auxiliary 'Actions' column of the Sites list table.
423                          *
424                          * By default this column is hidden unless something is hooked to the action.
425                          *
426                          * @since MU
427                          *
428                          * @param int $blog_id The site ID.
429                          */
430                         do_action( 'wpmublogsaction', $blog['blog_id'] );
431                 }
432         }
433
434         /**
435          * Handles output for the default column.
436          *
437          * @since 4.3.0
438          * @access public
439          *
440          * @param array  $blog        Current site.
441          * @param string $column_name Current column name.
442          */
443         public function column_default( $blog, $column_name ) {
444                 /**
445                  * Fires for each registered custom column in the Sites list table.
446                  *
447                  * @since 3.1.0
448                  *
449                  * @param string $column_name The name of the column to display.
450                  * @param int    $blog_id     The site ID.
451                  */
452                 do_action( 'manage_sites_custom_column', $column_name, $blog['blog_id'] );
453         }
454
455         /**
456          *
457          * @global string $mode
458          */
459         public function display_rows() {
460                 foreach ( $this->items as $blog ) {
461                         $blog = $blog->to_array();
462                         $class = '';
463                         reset( $this->status_list );
464
465                         foreach ( $this->status_list as $status => $col ) {
466                                 if ( $blog[ $status ] == 1 ) {
467                                         $class = " class='{$col[0]}'";
468                                 }
469                         }
470
471                         echo "<tr{$class}>";
472
473                         $this->single_row_columns( $blog );
474
475                         echo '</tr>';
476                 }
477         }
478
479         /**
480          * Gets the name of the default primary column.
481          *
482          * @since 4.3.0
483          * @access protected
484          *
485          * @return string Name of the default primary column, in this case, 'blogname'.
486          */
487         protected function get_default_primary_column_name() {
488                 return 'blogname';
489         }
490
491         /**
492          * Generates and displays row action links.
493          *
494          * @since 4.3.0
495          * @access protected
496          *
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.
501          */
502         protected function handle_row_actions( $blog, $column_name, $primary ) {
503                 if ( $primary !== $column_name ) {
504                         return;
505                 }
506
507                 $blogname = untrailingslashit( $blog['domain'] . $blog['path'] );
508
509                 // Preordered.
510                 $actions = array(
511                         'edit' => '', 'backend' => '',
512                         'activate' => '', 'deactivate' => '',
513                         'archive' => '', 'unarchive' => '',
514                         'spam' => '', 'unspam' => '',
515                         'delete' => '',
516                         'visit' => '',
517                 );
518
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_network()->site_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&amp;action2=activateblog&amp;id=' . $blog['blog_id'] ), 'activateblog_' . $blog['blog_id'] ) ) . '">' . __( 'Activate' ) . '</a>';
524                         } else {
525                                 $actions['deactivate'] = '<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>';
526                         }
527
528                         if ( $blog['archived'] == '1' ) {
529                                 $actions['unarchive'] = '<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>';
530                         } else {
531                                 $actions['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>';
532                         }
533
534                         if ( $blog['spam'] == '1' ) {
535                                 $actions['unspam'] = '<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>';
536                         } else {
537                                 $actions['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>';
538                         }
539
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&amp;action2=deleteblog&amp;id=' . $blog['blog_id'] ), 'deleteblog_' . $blog['blog_id'] ) ) . '">' . __( 'Delete' ) . '</a>';
542                         }
543                 }
544
545                 $actions['visit']       = "<a href='" . esc_url( get_home_url( $blog['blog_id'], '/' ) ) . "' rel='permalink'>" . __( 'Visit' ) . '</a>';
546
547                 /**
548                  * Filters the action links displayed for each site in the Sites list table.
549                  *
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.
554                  *
555                  * @since 3.1.0
556                  *
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.
561                  */
562                 $actions = apply_filters( 'manage_sites_action_links', array_filter( $actions ), $blog['blog_id'], $blogname );
563                 return $this->row_actions( $actions );
564         }
565 }