WordPress 4.2
[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          * Constructor.
14          *
15          * @since 3.1.0
16          * @access public
17          *
18          * @see WP_List_Table::__construct() for more information on default arguments.
19          *
20          * @param array $args An associative array of arguments.
21          */
22         public function __construct( $args = array() ) {
23                 parent::__construct( array(
24                         'plural' => 'sites',
25                         'screen' => isset( $args['screen'] ) ? $args['screen'] : null,
26                 ) );
27         }
28
29         public function ajax_user_can() {
30                 return current_user_can( 'manage_sites' );
31         }
32
33         public function prepare_items() {
34                 global $s, $mode, $wpdb;
35
36                 $current_site = get_current_site();
37
38                 $mode = ( empty( $_REQUEST['mode'] ) ) ? 'list' : $_REQUEST['mode'];
39
40                 $per_page = $this->get_items_per_page( 'sites_network_per_page' );
41
42                 $pagenum = $this->get_pagenum();
43
44                 $s = isset( $_REQUEST['s'] ) ? wp_unslash( trim( $_REQUEST[ 's' ] ) ) : '';
45                 $wild = '';
46                 if ( false !== strpos($s, '*') ) {
47                         $wild = '%';
48                         $s = trim($s, '*');
49                 }
50
51                 /*
52                  * If the network is large and a search is not being performed, show only
53                  * the latest blogs with no paging in order to avoid expensive count queries.
54                  */
55                 if ( !$s && wp_is_large_network() ) {
56                         if ( !isset($_REQUEST['orderby']) )
57                                 $_GET['orderby'] = $_REQUEST['orderby'] = '';
58                         if ( !isset($_REQUEST['order']) )
59                                 $_GET['order'] = $_REQUEST['order'] = 'DESC';
60                 }
61
62                 $query = "SELECT * FROM {$wpdb->blogs} WHERE site_id = '{$wpdb->siteid}' ";
63
64                 if ( empty($s) ) {
65                         // Nothing to do.
66                 } elseif ( preg_match( '/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/', $s ) ||
67                                         preg_match( '/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.?$/', $s ) ||
68                                         preg_match( '/^[0-9]{1,3}\.[0-9]{1,3}\.?$/', $s ) ||
69                                         preg_match( '/^[0-9]{1,3}\.$/', $s ) ) {
70                         // IPv4 address
71                         $sql = $wpdb->prepare( "SELECT blog_id FROM {$wpdb->registration_log} WHERE {$wpdb->registration_log}.IP LIKE %s", $wpdb->esc_like( $s ) . $wild );
72                         $reg_blog_ids = $wpdb->get_col( $sql );
73
74                         if ( !$reg_blog_ids )
75                                 $reg_blog_ids = array( 0 );
76
77                         $query = "SELECT *
78                                 FROM {$wpdb->blogs}
79                                 WHERE site_id = '{$wpdb->siteid}'
80                                 AND {$wpdb->blogs}.blog_id IN (" . implode( ', ', $reg_blog_ids ) . ")";
81                 } else {
82                         if ( is_numeric($s) && empty( $wild ) ) {
83                                 $query .= $wpdb->prepare( " AND ( {$wpdb->blogs}.blog_id = %s )", $s );
84                         } elseif ( is_subdomain_install() ) {
85                                 $blog_s = str_replace( '.' . $current_site->domain, '', $s );
86                                 $blog_s = $wpdb->esc_like( $blog_s ) . $wild . $wpdb->esc_like( '.' . $current_site->domain );
87                                 $query .= $wpdb->prepare( " AND ( {$wpdb->blogs}.domain LIKE %s ) ", $blog_s );
88                         } else {
89                                 if ( $s != trim('/', $current_site->path) ) {
90                                         $blog_s = $wpdb->esc_like( $current_site->path . $s ) . $wild . $wpdb->esc_like( '/' );
91                                 } else {
92                                         $blog_s = $wpdb->esc_like( $s );
93                                 }
94                                 $query .= $wpdb->prepare( " AND  ( {$wpdb->blogs}.path LIKE %s )", $blog_s );
95                         }
96                 }
97
98                 $order_by = isset( $_REQUEST['orderby'] ) ? $_REQUEST['orderby'] : '';
99                 if ( $order_by == 'registered' ) {
100                         $query .= ' ORDER BY registered ';
101                 } elseif ( $order_by == 'lastupdated' ) {
102                         $query .= ' ORDER BY last_updated ';
103                 } elseif ( $order_by == 'blogname' ) {
104                         if ( is_subdomain_install() )
105                                 $query .= ' ORDER BY domain ';
106                         else
107                                 $query .= ' ORDER BY path ';
108                 } elseif ( $order_by == 'blog_id' ) {
109                         $query .= ' ORDER BY blog_id ';
110                 } else {
111                         $order_by = null;
112                 }
113
114                 if ( isset( $order_by ) ) {
115                         $order = ( isset( $_REQUEST['order'] ) && 'DESC' == strtoupper( $_REQUEST['order'] ) ) ? "DESC" : "ASC";
116                         $query .= $order;
117                 }
118
119                 // Don't do an unbounded count on large networks
120                 if ( ! wp_is_large_network() )
121                         $total = $wpdb->get_var( str_replace( 'SELECT *', 'SELECT COUNT( blog_id )', $query ) );
122
123                 $query .= " LIMIT " . intval( ( $pagenum - 1 ) * $per_page ) . ", " . intval( $per_page );
124                 $this->items = $wpdb->get_results( $query, ARRAY_A );
125
126                 if ( wp_is_large_network() )
127                         $total = count($this->items);
128
129                 $this->set_pagination_args( array(
130                         'total_items' => $total,
131                         'per_page' => $per_page,
132                 ) );
133         }
134
135         public function no_items() {
136                 _e( 'No sites found.' );
137         }
138
139         protected function get_bulk_actions() {
140                 $actions = array();
141                 if ( current_user_can( 'delete_sites' ) )
142                         $actions['delete'] = __( 'Delete' );
143                 $actions['spam'] = _x( 'Mark as Spam', 'site' );
144                 $actions['notspam'] = _x( 'Not Spam', 'site' );
145
146                 return $actions;
147         }
148
149         /**
150          * @param string $which
151          */
152         protected function pagination( $which ) {
153                 global $mode;
154
155                 parent::pagination( $which );
156
157                 if ( 'top' == $which )
158                         $this->view_switcher( $mode );
159         }
160
161         public function get_columns() {
162                 $blogname_columns = ( is_subdomain_install() ) ? __( 'Domain' ) : __( 'Path' );
163                 $sites_columns = array(
164                         'cb'          => '<input type="checkbox" />',
165                         'blogname'    => $blogname_columns,
166                         'lastupdated' => __( 'Last Updated' ),
167                         'registered'  => _x( 'Registered', 'site' ),
168                         'users'       => __( 'Users' )
169                 );
170
171                 if ( has_filter( 'wpmublogsaction' ) )
172                         $sites_columns['plugins'] = __( 'Actions' );
173
174                 /**
175                  * Filter the displayed site columns in Sites list table.
176                  *
177                  * @since MU
178                  *
179                  * @param array $sites_columns An array of displayed site columns. Default 'cb',
180                  *                             'blogname', 'lastupdated', 'registered', 'users'.
181                  */
182                 $sites_columns = apply_filters( 'wpmu_blogs_columns', $sites_columns );
183
184                 return $sites_columns;
185         }
186
187         protected function get_sortable_columns() {
188                 return array(
189                         'blogname'    => 'blogname',
190                         'lastupdated' => 'lastupdated',
191                         'registered'  => 'blog_id',
192                 );
193         }
194
195         public function display_rows() {
196                 global $mode;
197
198                 $status_list = array(
199                         'archived' => array( 'site-archived', __( 'Archived' ) ),
200                         'spam'     => array( 'site-spammed', _x( 'Spam', 'site' ) ),
201                         'deleted'  => array( 'site-deleted', __( 'Deleted' ) ),
202                         'mature'   => array( 'site-mature', __( 'Mature' ) )
203                 );
204
205                 if ( 'list' == $mode ) {
206                         $date = __( 'Y/m/d' );
207                 } else {
208                         $date = __( 'Y/m/d g:i:s a' );
209                 }
210
211                 foreach ( $this->items as $blog ) {
212                         $class = '';
213                         reset( $status_list );
214
215                         $blog_states = array();
216                         foreach ( $status_list as $status => $col ) {
217                                 if ( get_blog_status( $blog['blog_id'], $status ) == 1 ) {
218                                         $class = " class='{$col[0]}'";
219                                         $blog_states[] = $col[1];
220                                 }
221                         }
222                         $blog_state = '';
223                         if ( ! empty( $blog_states ) ) {
224                                 $state_count = count( $blog_states );
225                                 $i = 0;
226                                 $blog_state .= ' - ';
227                                 foreach ( $blog_states as $state ) {
228                                         ++$i;
229                                         ( $i == $state_count ) ? $sep = '' : $sep = ', ';
230                                         $blog_state .= "<span class='post-state'>$state$sep</span>";
231                                 }
232                         }
233                         echo "<tr{$class}>";
234
235                         $blogname = ( is_subdomain_install() ) ? str_replace( '.' . get_current_site()->domain, '', $blog['domain'] ) : $blog['path'];
236
237                         list( $columns, $hidden ) = $this->get_column_info();
238
239                         foreach ( $columns as $column_name => $column_display_name ) {
240                                 $style = '';
241                                 if ( in_array( $column_name, $hidden ) )
242                                         $style = ' style="display:none;"';
243
244                                 switch ( $column_name ) {
245                                         case 'cb': ?>
246                                                 <th scope="row" class="check-column">
247                                                         <?php if ( ! is_main_site( $blog['blog_id'] ) ) : ?>
248                                                         <label class="screen-reader-text" for="blog_<?php echo $blog['blog_id']; ?>"><?php printf( __( 'Select %s' ), $blogname ); ?></label>
249                                                         <input type="checkbox" id="blog_<?php echo $blog['blog_id'] ?>" name="allblogs[]" value="<?php echo esc_attr( $blog['blog_id'] ) ?>" />
250                                                         <?php endif; ?>
251                                                 </th>
252                                         <?php
253                                         break;
254
255                                         case 'id':?>
256                                                 <th scope="row">
257                                                         <?php echo $blog['blog_id'] ?>
258                                                 </th>
259                                         <?php
260                                         break;
261
262                                         case 'blogname':
263                                                 echo "<td class='column-$column_name $column_name'$style>"; ?>
264                                                         <a href="<?php echo esc_url( network_admin_url( 'site-info.php?id=' . $blog['blog_id'] ) ); ?>" class="edit"><?php echo $blogname . $blog_state; ?></a>
265                                                         <?php
266                                                         if ( 'list' != $mode ) {
267                                                                 switch_to_blog( $blog['blog_id'] );
268                                                                 echo '<p>' . sprintf( _x( '%1$s &#8211; <em>%2$s</em>', '%1$s: site name. %2$s: site tagline.' ), get_option( 'blogname' ), get_option( 'blogdescription ' ) ) . '</p>';
269                                                                 restore_current_blog();
270                                                         }
271
272                                                         // Preordered.
273                                                         $actions = array(
274                                                                 'edit' => '', 'backend' => '',
275                                                                 'activate' => '', 'deactivate' => '',
276                                                                 'archive' => '', 'unarchive' => '',
277                                                                 'spam' => '', 'unspam' => '',
278                                                                 'delete' => '',
279                                                                 'visit' => '',
280                                                         );
281
282                                                         $actions['edit']        = '<span class="edit"><a href="' . esc_url( network_admin_url( 'site-info.php?id=' . $blog['blog_id'] ) ) . '">' . __( 'Edit' ) . '</a></span>';
283                                                         $actions['backend']     = "<span class='backend'><a href='" . esc_url( get_admin_url( $blog['blog_id'] ) ) . "' class='edit'>" . __( 'Dashboard' ) . '</a></span>';
284                                                         if ( get_current_site()->blog_id != $blog['blog_id'] ) {
285                                                                 if ( get_blog_status( $blog['blog_id'], 'deleted' ) == '1' )
286                                                                         $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'] . '&amp;msg=' . urlencode( sprintf( __( 'You are about to activate the site %s' ), $blogname ) ) ), 'confirm' ) ) . '">' . __( 'Activate' ) . '</a></span>';
287                                                                 else
288                                                                         $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'] . '&amp;msg=' . urlencode( sprintf( __( 'You are about to deactivate the site %s' ), $blogname ) ) ), 'confirm') ) . '">' . __( 'Deactivate' ) . '</a></span>';
289
290                                                                 if ( get_blog_status( $blog['blog_id'], 'archived' ) == '1' )
291                                                                         $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'] . '&amp;msg=' . urlencode( sprintf( __( 'You are about to unarchive the site %s.' ), $blogname ) ) ), 'confirm') ) . '">' . __( 'Unarchive' ) . '</a></span>';
292                                                                 else
293                                                                         $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'] . '&amp;msg=' . urlencode( sprintf( __( 'You are about to archive the site %s.' ), $blogname ) ) ), 'confirm') ) . '">' . _x( 'Archive', 'verb; site' ) . '</a></span>';
294
295                                                                 if ( get_blog_status( $blog['blog_id'], 'spam' ) == '1' )
296                                                                         $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'] . '&amp;msg=' . urlencode( sprintf( __( 'You are about to unspam the site %s.' ), $blogname ) ) ), 'confirm') ) . '">' . _x( 'Not Spam', 'site' ) . '</a></span>';
297                                                                 else
298                                                                         $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'] . '&amp;msg=' . urlencode( sprintf( __( 'You are about to mark the site %s as spam.' ), $blogname ) ) ), 'confirm') ) . '">' . _x( 'Spam', 'site' ) . '</a></span>';
299
300                                                                 if ( current_user_can( 'delete_site', $blog['blog_id'] ) )
301                                                                         $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'] . '&amp;msg=' . urlencode( sprintf( __( 'You are about to delete the site %s.' ), $blogname ) ) ), 'confirm') ) . '">' . __( 'Delete' ) . '</a></span>';
302                                                         }
303
304                                                         $actions['visit']       = "<span class='view'><a href='" . esc_url( get_home_url( $blog['blog_id'], '/' ) ) . "' rel='permalink'>" . __( 'Visit' ) . '</a></span>';
305
306                                                         /**
307                                                          * Filter the action links displayed for each site in the Sites list table.
308                                                          *
309                                                          * The 'Edit', 'Dashboard', 'Delete', and 'Visit' links are displayed by
310                                                          * default for each site. The site's status determines whether to show the
311                                                          * 'Activate' or 'Deactivate' link, 'Unarchive' or 'Archive' links, and
312                                                          * 'Not Spam' or 'Spam' link for each site.
313                                                          *
314                                                          * @since 3.1.0
315                                                          *
316                                                          * @param array  $actions  An array of action links to be displayed.
317                                                          * @param int    $blog_id  The site ID.
318                                                          * @param string $blogname Site path, formatted depending on whether it is a sub-domain
319                                                          *                         or subdirectory multisite install.
320                                                          */
321                                                         $actions = apply_filters( 'manage_sites_action_links', array_filter( $actions ), $blog['blog_id'], $blogname );
322                                                         echo $this->row_actions( $actions );
323                                         ?>
324                                                 </td>
325                                         <?php
326                                         break;
327
328                                         case 'lastupdated':
329                                                 echo "<td class='$column_name column-$column_name'$style>";
330                                                         echo ( $blog['last_updated'] == '0000-00-00 00:00:00' ) ? __( 'Never' ) : mysql2date( $date, $blog['last_updated'] ); ?>
331                                                 </td>
332                                         <?php
333                                         break;
334                                 case 'registered':
335                                                 echo "<td class='$column_name column-$column_name'$style>";
336                                                 if ( $blog['registered'] == '0000-00-00 00:00:00' )
337                                                         echo '&#x2014;';
338                                                 else
339                                                         echo mysql2date( $date, $blog['registered'] );
340                                                 ?>
341                                                 </td>
342                                         <?php
343                                         break;
344                                 case 'users':
345                                                 echo "<td class='$column_name column-$column_name'$style>";
346                                                         $blogusers = get_users( array( 'blog_id' => $blog['blog_id'], 'number' => 6) );
347                                                         if ( is_array( $blogusers ) ) {
348                                                                 $blogusers_warning = '';
349                                                                 if ( count( $blogusers ) > 5 ) {
350                                                                         $blogusers = array_slice( $blogusers, 0, 5 );
351                                                                         $blogusers_warning = __( 'Only showing first 5 users.' ) . ' <a href="' . esc_url( network_admin_url( 'site-users.php?id=' . $blog['blog_id'] ) ) . '">' . __( 'More' ) . '</a>';
352                                                                 }
353                                                                 foreach ( $blogusers as $user_object ) {
354                                                                         echo '<a href="' . esc_url( network_admin_url( 'user-edit.php?user_id=' . $user_object->ID ) ) . '">' . esc_html( $user_object->user_login ) . '</a> ';
355                                                                         if ( 'list' != $mode )
356                                                                                 echo '( ' . $user_object->user_email . ' )';
357                                                                         echo '<br />';
358                                                                 }
359                                                                 if ( $blogusers_warning != '' )
360                                                                         echo '<strong>' . $blogusers_warning . '</strong><br />';
361                                                         }
362                                                         ?>
363                                                 </td>
364                                         <?php
365                                         break;
366
367                                 case 'plugins': ?>
368                                         <?php if ( has_filter( 'wpmublogsaction' ) ) {
369                                         echo "<td class='$column_name column-$column_name'$style>";
370                                                 /**
371                                                  * Fires inside the auxiliary 'Actions' column of the Sites list table.
372                                                  *
373                                                  * By default this column is hidden unless something is hooked to the action.
374                                                  *
375                                                  * @since MU
376                                                  *
377                                                  * @param int $blog_id The site ID.
378                                                  */
379                                                 do_action( 'wpmublogsaction', $blog['blog_id'] ); ?>
380                                         </td>
381                                         <?php }
382                                         break;
383
384                                 default:
385                                         echo "<td class='$column_name column-$column_name'$style>";
386                                         /**
387                                          * Fires for each registered custom column in the Sites list table.
388                                          *
389                                          * @since 3.1.0
390                                          *
391                                          * @param string $column_name The name of the column to display.
392                                          * @param int    $blog_id     The site ID.
393                                          */
394                                         do_action( 'manage_sites_custom_column', $column_name, $blog['blog_id'] );
395                                         echo "</td>";
396                                         break;
397                                 }
398                         }
399                         ?>
400                         </tr>
401                         <?php
402                 }
403         }
404 }