]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/includes/class-wp-ms-sites-list-table.php
WordPress 4.1.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          * 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 \<\b\r \/\> g:i:s a';
209                 }
210
211                 $class = '';
212                 foreach ( $this->items as $blog ) {
213                         $class = ( 'alternate' == $class ) ? '' : 'alternate';
214                         reset( $status_list );
215
216                         $blog_states = array();
217                         foreach ( $status_list as $status => $col ) {
218                                 if ( get_blog_status( $blog['blog_id'], $status ) == 1 ) {
219                                         $class = $col[0];
220                                         $blog_states[] = $col[1];
221                                 }
222                         }
223                         $blog_state = '';
224                         if ( ! empty( $blog_states ) ) {
225                                 $state_count = count( $blog_states );
226                                 $i = 0;
227                                 $blog_state .= ' - ';
228                                 foreach ( $blog_states as $state ) {
229                                         ++$i;
230                                         ( $i == $state_count ) ? $sep = '' : $sep = ', ';
231                                         $blog_state .= "<span class='post-state'>$state$sep</span>";
232                                 }
233                         }
234                         echo "<tr class='$class'>";
235
236                         $blogname = ( is_subdomain_install() ) ? str_replace( '.' . get_current_site()->domain, '', $blog['domain'] ) : $blog['path'];
237
238                         list( $columns, $hidden ) = $this->get_column_info();
239
240                         foreach ( $columns as $column_name => $column_display_name ) {
241                                 $style = '';
242                                 if ( in_array( $column_name, $hidden ) )
243                                         $style = ' style="display:none;"';
244
245                                 switch ( $column_name ) {
246                                         case 'cb': ?>
247                                                 <th scope="row" class="check-column">
248                                                         <?php if ( ! is_main_site( $blog['blog_id'] ) ) : ?>
249                                                         <label class="screen-reader-text" for="blog_<?php echo $blog['blog_id']; ?>"><?php printf( __( 'Select %s' ), $blogname ); ?></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                                                 </th>
253                                         <?php
254                                         break;
255
256                                         case 'id':?>
257                                                 <th scope="row">
258                                                         <?php echo $blog['blog_id'] ?>
259                                                 </th>
260                                         <?php
261                                         break;
262
263                                         case 'blogname':
264                                                 echo "<td class='column-$column_name $column_name'$style>"; ?>
265                                                         <a href="<?php echo esc_url( network_admin_url( 'site-info.php?id=' . $blog['blog_id'] ) ); ?>" class="edit"><?php echo $blogname . $blog_state; ?></a>
266                                                         <?php
267                                                         if ( 'list' != $mode ) {
268                                                                 switch_to_blog( $blog['blog_id'] );
269                                                                 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>';
270                                                                 restore_current_blog();
271                                                         }
272
273                                                         // Preordered.
274                                                         $actions = array(
275                                                                 'edit' => '', 'backend' => '',
276                                                                 'activate' => '', 'deactivate' => '',
277                                                                 'archive' => '', 'unarchive' => '',
278                                                                 'spam' => '', 'unspam' => '',
279                                                                 'delete' => '',
280                                                                 'visit' => '',
281                                                         );
282
283                                                         $actions['edit']        = '<span class="edit"><a href="' . esc_url( network_admin_url( 'site-info.php?id=' . $blog['blog_id'] ) ) . '">' . __( 'Edit' ) . '</a></span>';
284                                                         $actions['backend']     = "<span class='backend'><a href='" . esc_url( get_admin_url( $blog['blog_id'] ) ) . "' class='edit'>" . __( 'Dashboard' ) . '</a></span>';
285                                                         if ( get_current_site()->blog_id != $blog['blog_id'] ) {
286                                                                 if ( get_blog_status( $blog['blog_id'], 'deleted' ) == '1' )
287                                                                         $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>';
288                                                                 else
289                                                                         $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>';
290
291                                                                 if ( get_blog_status( $blog['blog_id'], 'archived' ) == '1' )
292                                                                         $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>';
293                                                                 else
294                                                                         $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>';
295
296                                                                 if ( get_blog_status( $blog['blog_id'], 'spam' ) == '1' )
297                                                                         $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>';
298                                                                 else
299                                                                         $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>';
300
301                                                                 if ( current_user_can( 'delete_site', $blog['blog_id'] ) )
302                                                                         $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>';
303                                                         }
304
305                                                         $actions['visit']       = "<span class='view'><a href='" . esc_url( get_home_url( $blog['blog_id'], '/' ) ) . "' rel='permalink'>" . __( 'Visit' ) . '</a></span>';
306
307                                                         /**
308                                                          * Filter the action links displayed for each site in the Sites list table.
309                                                          *
310                                                          * The 'Edit', 'Dashboard', 'Delete', and 'Visit' links are displayed by
311                                                          * default for each site. The site's status determines whether to show the
312                                                          * 'Activate' or 'Deactivate' link, 'Unarchive' or 'Archive' links, and
313                                                          * 'Not Spam' or 'Spam' link for each site.
314                                                          *
315                                                          * @since 3.1.0
316                                                          *
317                                                          * @param array  $actions  An array of action links to be displayed.
318                                                          * @param int    $blog_id  The site ID.
319                                                          * @param string $blogname Site path, formatted depending on whether it is a sub-domain
320                                                          *                         or subdirectory multisite install.
321                                                          */
322                                                         $actions = apply_filters( 'manage_sites_action_links', array_filter( $actions ), $blog['blog_id'], $blogname );
323                                                         echo $this->row_actions( $actions );
324                                         ?>
325                                                 </td>
326                                         <?php
327                                         break;
328
329                                         case 'lastupdated':
330                                                 echo "<td class='$column_name column-$column_name'$style>";
331                                                         echo ( $blog['last_updated'] == '0000-00-00 00:00:00' ) ? __( 'Never' ) : mysql2date( $date, $blog['last_updated'] ); ?>
332                                                 </td>
333                                         <?php
334                                         break;
335                                 case 'registered':
336                                                 echo "<td class='$column_name column-$column_name'$style>";
337                                                 if ( $blog['registered'] == '0000-00-00 00:00:00' )
338                                                         echo '&#x2014;';
339                                                 else
340                                                         echo mysql2date( $date, $blog['registered'] );
341                                                 ?>
342                                                 </td>
343                                         <?php
344                                         break;
345                                 case 'users':
346                                                 echo "<td class='$column_name column-$column_name'$style>";
347                                                         $blogusers = get_users( array( 'blog_id' => $blog['blog_id'], 'number' => 6) );
348                                                         if ( is_array( $blogusers ) ) {
349                                                                 $blogusers_warning = '';
350                                                                 if ( count( $blogusers ) > 5 ) {
351                                                                         $blogusers = array_slice( $blogusers, 0, 5 );
352                                                                         $blogusers_warning = __( 'Only showing first 5 users.' ) . ' <a href="' . esc_url( network_admin_url( 'site-users.php?id=' . $blog['blog_id'] ) ) . '">' . __( 'More' ) . '</a>';
353                                                                 }
354                                                                 foreach ( $blogusers as $user_object ) {
355                                                                         echo '<a href="' . esc_url( network_admin_url( 'user-edit.php?user_id=' . $user_object->ID ) ) . '">' . esc_html( $user_object->user_login ) . '</a> ';
356                                                                         if ( 'list' != $mode )
357                                                                                 echo '( ' . $user_object->user_email . ' )';
358                                                                         echo '<br />';
359                                                                 }
360                                                                 if ( $blogusers_warning != '' )
361                                                                         echo '<strong>' . $blogusers_warning . '</strong><br />';
362                                                         }
363                                                         ?>
364                                                 </td>
365                                         <?php
366                                         break;
367
368                                 case 'plugins': ?>
369                                         <?php if ( has_filter( 'wpmublogsaction' ) ) {
370                                         echo "<td class='$column_name column-$column_name'$style>";
371                                                 /**
372                                                  * Fires inside the auxiliary 'Actions' column of the Sites list table.
373                                                  *
374                                                  * By default this column is hidden unless something is hooked to the action.
375                                                  *
376                                                  * @since MU
377                                                  *
378                                                  * @param int $blog_id The site ID.
379                                                  */
380                                                 do_action( 'wpmublogsaction', $blog['blog_id'] ); ?>
381                                         </td>
382                                         <?php }
383                                         break;
384
385                                 default:
386                                         echo "<td class='$column_name column-$column_name'$style>";
387                                         /**
388                                          * Fires for each registered custom column in the Sites list table.
389                                          *
390                                          * @since 3.1.0
391                                          *
392                                          * @param string $column_name The name of the column to display.
393                                          * @param int    $blog_id     The site ID.
394                                          */
395                                         do_action( 'manage_sites_custom_column', $column_name, $blog['blog_id'] );
396                                         echo "</td>";
397                                         break;
398                                 }
399                         }
400                         ?>
401                         </tr>
402                         <?php
403                 }
404         }
405 }