WordPress 4.7
[autoinstalls/wordpress.git] / wp-admin / includes / class-wp-ms-themes-list-table.php
1 <?php
2 /**
3  * List Table API: WP_MS_Themes_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 themes 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_Themes_List_Table extends WP_List_Table {
19
20         public $site_id;
21         public $is_site_themes;
22
23         private $has_items;
24
25         /**
26          * Constructor.
27          *
28          * @since 3.1.0
29          * @access public
30          *
31          * @see WP_List_Table::__construct() for more information on default arguments.
32          *
33          * @global string $status
34          * @global int    $page
35          *
36          * @param array $args An associative array of arguments.
37          */
38         public function __construct( $args = array() ) {
39                 global $status, $page;
40
41                 parent::__construct( array(
42                         'plural' => 'themes',
43                         'screen' => isset( $args['screen'] ) ? $args['screen'] : null,
44                 ) );
45
46                 $status = isset( $_REQUEST['theme_status'] ) ? $_REQUEST['theme_status'] : 'all';
47                 if ( !in_array( $status, array( 'all', 'enabled', 'disabled', 'upgrade', 'search', 'broken' ) ) )
48                         $status = 'all';
49
50                 $page = $this->get_pagenum();
51
52                 $this->is_site_themes = ( 'site-themes-network' === $this->screen->id ) ? true : false;
53
54                 if ( $this->is_site_themes )
55                         $this->site_id = isset( $_REQUEST['id'] ) ? intval( $_REQUEST['id'] ) : 0;
56         }
57
58         /**
59          *
60          * @return array
61          */
62         protected function get_table_classes() {
63                 // todo: remove and add CSS for .themes
64                 return array( 'widefat', 'plugins' );
65         }
66
67         /**
68          *
69          * @return bool
70          */
71         public function ajax_user_can() {
72                 if ( $this->is_site_themes )
73                         return current_user_can( 'manage_sites' );
74                 else
75                         return current_user_can( 'manage_network_themes' );
76         }
77
78         /**
79          *
80          * @global string $status
81          * @global array $totals
82          * @global int $page
83          * @global string $orderby
84          * @global string $order
85          * @global string $s
86          */
87         public function prepare_items() {
88                 global $status, $totals, $page, $orderby, $order, $s;
89
90                 wp_reset_vars( array( 'orderby', 'order', 's' ) );
91
92                 $themes = array(
93                         /**
94                          * Filters the full array of WP_Theme objects to list in the Multisite
95                          * themes list table.
96                          *
97                          * @since 3.1.0
98                          *
99                          * @param array $all An array of WP_Theme objects to display in the list table.
100                          */
101                         'all' => apply_filters( 'all_themes', wp_get_themes() ),
102                         'search' => array(),
103                         'enabled' => array(),
104                         'disabled' => array(),
105                         'upgrade' => array(),
106                         'broken' => $this->is_site_themes ? array() : wp_get_themes( array( 'errors' => true ) ),
107                 );
108
109                 if ( $this->is_site_themes ) {
110                         $themes_per_page = $this->get_items_per_page( 'site_themes_network_per_page' );
111                         $allowed_where = 'site';
112                 } else {
113                         $themes_per_page = $this->get_items_per_page( 'themes_network_per_page' );
114                         $allowed_where = 'network';
115                 }
116
117                 $maybe_update = current_user_can( 'update_themes' ) && ! $this->is_site_themes && $current = get_site_transient( 'update_themes' );
118
119                 foreach ( (array) $themes['all'] as $key => $theme ) {
120                         if ( $this->is_site_themes && $theme->is_allowed( 'network' ) ) {
121                                 unset( $themes['all'][ $key ] );
122                                 continue;
123                         }
124
125                         if ( $maybe_update && isset( $current->response[ $key ] ) ) {
126                                 $themes['all'][ $key ]->update = true;
127                                 $themes['upgrade'][ $key ] = $themes['all'][ $key ];
128                         }
129
130                         $filter = $theme->is_allowed( $allowed_where, $this->site_id ) ? 'enabled' : 'disabled';
131                         $themes[ $filter ][ $key ] = $themes['all'][ $key ];
132                 }
133
134                 if ( $s ) {
135                         $status = 'search';
136                         $themes['search'] = array_filter( array_merge( $themes['all'], $themes['broken'] ), array( $this, '_search_callback' ) );
137                 }
138
139                 $totals = array();
140                 foreach ( $themes as $type => $list )
141                         $totals[ $type ] = count( $list );
142
143                 if ( empty( $themes[ $status ] ) && !in_array( $status, array( 'all', 'search' ) ) )
144                         $status = 'all';
145
146                 $this->items = $themes[ $status ];
147                 WP_Theme::sort_by_name( $this->items );
148
149                 $this->has_items = ! empty( $themes['all'] );
150                 $total_this_page = $totals[ $status ];
151
152                 wp_localize_script( 'updates', '_wpUpdatesItemCounts', array(
153                         'themes' => $totals,
154                         'totals' => wp_get_update_data(),
155                 ) );
156
157                 if ( $orderby ) {
158                         $orderby = ucfirst( $orderby );
159                         $order = strtoupper( $order );
160
161                         if ( $orderby === 'Name' ) {
162                                 if ( 'ASC' === $order ) {
163                                         $this->items = array_reverse( $this->items );
164                                 }
165                         } else {
166                                 uasort( $this->items, array( $this, '_order_callback' ) );
167                         }
168                 }
169
170                 $start = ( $page - 1 ) * $themes_per_page;
171
172                 if ( $total_this_page > $themes_per_page )
173                         $this->items = array_slice( $this->items, $start, $themes_per_page, true );
174
175                 $this->set_pagination_args( array(
176                         'total_items' => $total_this_page,
177                         'per_page' => $themes_per_page,
178                 ) );
179         }
180
181         /**
182          * @staticvar string $term
183          * @param WP_Theme $theme
184          * @return bool
185          */
186         public function _search_callback( $theme ) {
187                 static $term = null;
188                 if ( is_null( $term ) )
189                         $term = wp_unslash( $_REQUEST['s'] );
190
191                 foreach ( array( 'Name', 'Description', 'Author', 'Author', 'AuthorURI' ) as $field ) {
192                         // Don't mark up; Do translate.
193                         if ( false !== stripos( $theme->display( $field, false, true ), $term ) )
194                                 return true;
195                 }
196
197                 if ( false !== stripos( $theme->get_stylesheet(), $term ) )
198                         return true;
199
200                 if ( false !== stripos( $theme->get_template(), $term ) )
201                         return true;
202
203                 return false;
204         }
205
206         // Not used by any core columns.
207         /**
208          * @global string $orderby
209          * @global string $order
210          * @param array $theme_a
211          * @param array $theme_b
212          * @return int
213          */
214         public function _order_callback( $theme_a, $theme_b ) {
215                 global $orderby, $order;
216
217                 $a = $theme_a[ $orderby ];
218                 $b = $theme_b[ $orderby ];
219
220                 if ( $a == $b )
221                         return 0;
222
223                 if ( 'DESC' === $order )
224                         return ( $a < $b ) ? 1 : -1;
225                 else
226                         return ( $a < $b ) ? -1 : 1;
227         }
228
229         /**
230          * @access public
231          */
232         public function no_items() {
233                 if ( $this->has_items ) {
234                         _e( 'No themes found.' );
235                 } else {
236                         _e( 'You do not appear to have any themes available at this time.' );
237                 }
238         }
239
240         /**
241          *
242          * @return array
243          */
244         public function get_columns() {
245                 return array(
246                         'cb'          => '<input type="checkbox" />',
247                         'name'        => __( 'Theme' ),
248                         'description' => __( 'Description' ),
249                 );
250         }
251
252         /**
253          *
254          * @return array
255          */
256         protected function get_sortable_columns() {
257                 return array(
258                         'name'         => 'name',
259                 );
260         }
261
262         /**
263          * Gets the name of the primary column.
264          *
265          * @since 4.3.0
266          * @access protected
267          *
268          * @return string Unalterable name of the primary column name, in this case, 'name'.
269          */
270         protected function get_primary_column_name() {
271                 return 'name';
272         }
273
274         /**
275          *
276          * @global array $totals
277          * @global string $status
278          * @return array
279          */
280         protected function get_views() {
281                 global $totals, $status;
282
283                 $status_links = array();
284                 foreach ( $totals as $type => $count ) {
285                         if ( !$count )
286                                 continue;
287
288                         switch ( $type ) {
289                                 case 'all':
290                                         $text = _nx( 'All <span class="count">(%s)</span>', 'All <span class="count">(%s)</span>', $count, 'themes' );
291                                         break;
292                                 case 'enabled':
293                                         $text = _n( 'Enabled <span class="count">(%s)</span>', 'Enabled <span class="count">(%s)</span>', $count );
294                                         break;
295                                 case 'disabled':
296                                         $text = _n( 'Disabled <span class="count">(%s)</span>', 'Disabled <span class="count">(%s)</span>', $count );
297                                         break;
298                                 case 'upgrade':
299                                         $text = _n( 'Update Available <span class="count">(%s)</span>', 'Update Available <span class="count">(%s)</span>', $count );
300                                         break;
301                                 case 'broken' :
302                                         $text = _n( 'Broken <span class="count">(%s)</span>', 'Broken <span class="count">(%s)</span>', $count );
303                                         break;
304                         }
305
306                         if ( $this->is_site_themes )
307                                 $url = 'site-themes.php?id=' . $this->site_id;
308                         else
309                                 $url = 'themes.php';
310
311                         if ( 'search' != $type ) {
312                                 $status_links[$type] = sprintf( "<a href='%s' %s>%s</a>",
313                                         esc_url( add_query_arg('theme_status', $type, $url) ),
314                                         ( $type === $status ) ? ' class="current"' : '',
315                                         sprintf( $text, number_format_i18n( $count ) )
316                                 );
317                         }
318                 }
319
320                 return $status_links;
321         }
322
323         /**
324          * @global string $status
325          *
326          * @return array
327          */
328         protected function get_bulk_actions() {
329                 global $status;
330
331                 $actions = array();
332                 if ( 'enabled' != $status )
333                         $actions['enable-selected'] = $this->is_site_themes ? __( 'Enable' ) : __( 'Network Enable' );
334                 if ( 'disabled' != $status )
335                         $actions['disable-selected'] = $this->is_site_themes ? __( 'Disable' ) : __( 'Network Disable' );
336                 if ( ! $this->is_site_themes ) {
337                         if ( current_user_can( 'update_themes' ) )
338                                 $actions['update-selected'] = __( 'Update' );
339                         if ( current_user_can( 'delete_themes' ) )
340                                 $actions['delete-selected'] = __( 'Delete' );
341                 }
342                 return $actions;
343         }
344
345         /**
346          * @access public
347          */
348         public function display_rows() {
349                 foreach ( $this->items as $theme )
350                         $this->single_row( $theme );
351         }
352
353         /**
354          * Handles the checkbox column output.
355          *
356          * @since 4.3.0
357          * @access public
358          *
359          * @param WP_Theme $theme The current WP_Theme object.
360          */
361         public function column_cb( $theme ) {
362                 $checkbox_id = 'checkbox_' . md5( $theme->get('Name') );
363                 ?>
364                 <input type="checkbox" name="checked[]" value="<?php echo esc_attr( $theme->get_stylesheet() ) ?>" id="<?php echo $checkbox_id ?>" />
365                 <label class="screen-reader-text" for="<?php echo $checkbox_id ?>" ><?php _e( 'Select' ) ?>  <?php echo $theme->display( 'Name' ) ?></label>
366                 <?php
367         }
368
369         /**
370          * Handles the name column output.
371          *
372          * @since 4.3.0
373          * @access public
374          *
375          * @global string $status
376          * @global int    $page
377          * @global string $s
378          *
379          * @param WP_Theme $theme The current WP_Theme object.
380          */
381         public function column_name( $theme ) {
382                 global $status, $page, $s;
383
384                 $context = $status;
385
386                 if ( $this->is_site_themes ) {
387                         $url = "site-themes.php?id={$this->site_id}&amp;";
388                         $allowed = $theme->is_allowed( 'site', $this->site_id );
389                 } else {
390                         $url = 'themes.php?';
391                         $allowed = $theme->is_allowed( 'network' );
392                 }
393
394                 // Pre-order.
395                 $actions = array(
396                         'enable' => '',
397                         'disable' => '',
398                         'edit' => '',
399                         'delete' => ''
400                 );
401
402                 $stylesheet = $theme->get_stylesheet();
403                 $theme_key = urlencode( $stylesheet );
404
405                 if ( ! $allowed ) {
406                         if ( ! $theme->errors() ) {
407                                 $url = add_query_arg( array(
408                                         'action' => 'enable',
409                                         'theme'  => $theme_key,
410                                         'paged'  => $page,
411                                         's'      => $s,
412                                 ), $url );
413
414                                 if ( $this->is_site_themes ) {
415                                         /* translators: %s: theme name */
416                                         $aria_label = sprintf( __( 'Enable %s' ), $theme->display( 'Name' ) );
417                                 } else {
418                                         /* translators: %s: theme name */
419                                         $aria_label = sprintf( __( 'Network Enable %s' ), $theme->display( 'Name' ) );
420                                 }
421
422                                 $actions['enable'] = sprintf( '<a href="%s" class="edit" aria-label="%s">%s</a>',
423                                         esc_url( wp_nonce_url( $url, 'enable-theme_' . $stylesheet ) ),
424                                         esc_attr( $aria_label ),
425                                         ( $this->is_site_themes ? __( 'Enable' ) : __( 'Network Enable' ) )
426                                 );
427                         }
428                 } else {
429                         $url = add_query_arg( array(
430                                 'action' => 'disable',
431                                 'theme'  => $theme_key,
432                                 'paged'  => $page,
433                                 's'      => $s,
434                         ), $url );
435
436                         if ( $this->is_site_themes ) {
437                                 /* translators: %s: theme name */
438                                 $aria_label = sprintf( __( 'Disable %s' ), $theme->display( 'Name' ) );
439                         } else {
440                                 /* translators: %s: theme name */
441                                 $aria_label = sprintf( __( 'Network Disable %s' ), $theme->display( 'Name' ) );
442                         }
443
444                         $actions['disable'] = sprintf( '<a href="%s" aria-label="%s">%s</a>',
445                                 esc_url( wp_nonce_url( $url, 'disable-theme_' . $stylesheet ) ),
446                                 esc_attr( $aria_label ),
447                                 ( $this->is_site_themes ? __( 'Disable' ) : __( 'Network Disable' ) )
448                         );
449                 }
450
451                 if ( current_user_can('edit_themes') ) {
452                         $url = add_query_arg( array(
453                                 'theme' => $theme_key,
454                         ), 'theme-editor.php' );
455
456                         /* translators: %s: theme name */
457                         $aria_label = sprintf( __( 'Open %s in the Theme Editor' ), $theme->display( 'Name' ) );
458
459                         $actions['edit'] = sprintf( '<a href="%s" class="edit" aria-label="%s">%s</a>',
460                                 esc_url( $url ),
461                                 esc_attr( $aria_label ),
462                                 __( 'Edit' )
463                         );
464                 }
465
466                 if ( ! $allowed && current_user_can( 'delete_themes' ) && ! $this->is_site_themes && $stylesheet != get_option( 'stylesheet' ) && $stylesheet != get_option( 'template' ) ) {
467                         $url = add_query_arg( array(
468                                 'action'       => 'delete-selected',
469                                 'checked[]'    => $theme_key,
470                                 'theme_status' => $context,
471                                 'paged'        => $page,
472                                 's'            => $s,
473                         ), 'themes.php' );
474
475                         /* translators: %s: theme name */
476                         $aria_label = sprintf( _x( 'Delete %s', 'theme' ), $theme->display( 'Name' ) );
477
478                         $actions['delete'] = sprintf( '<a href="%s" class="delete" aria-label="%s">%s</a>',
479                                 esc_url( wp_nonce_url( $url, 'bulk-themes' ) ),
480                                 esc_attr( $aria_label ),
481                                 __( 'Delete' )
482                         );
483                 }
484                 /**
485                  * Filters the action links displayed for each theme in the Multisite
486                  * themes list table.
487                  *
488                  * The action links displayed are determined by the theme's status, and
489                  * which Multisite themes list table is being displayed - the Network
490                  * themes list table (themes.php), which displays all installed themes,
491                  * or the Site themes list table (site-themes.php), which displays the
492                  * non-network enabled themes when editing a site in the Network admin.
493                  *
494                  * The default action links for the Network themes list table include
495                  * 'Network Enable', 'Network Disable', 'Edit', and 'Delete'.
496                  *
497                  * The default action links for the Site themes list table include
498                  * 'Enable', 'Disable', and 'Edit'.
499                  *
500                  * @since 2.8.0
501                  *
502                  * @param array    $actions An array of action links.
503                  * @param WP_Theme $theme   The current WP_Theme object.
504                  * @param string   $context Status of the theme.
505                  */
506                 $actions = apply_filters( 'theme_action_links', array_filter( $actions ), $theme, $context );
507
508                 /**
509                  * Filters the action links of a specific theme in the Multisite themes
510                  * list table.
511                  *
512                  * The dynamic portion of the hook name, `$stylesheet`, refers to the
513                  * directory name of the theme, which in most cases is synonymous
514                  * with the template name.
515                  *
516                  * @since 3.1.0
517                  *
518                  * @param array    $actions An array of action links.
519                  * @param WP_Theme $theme   The current WP_Theme object.
520                  * @param string   $context Status of the theme.
521                  */
522                 $actions = apply_filters( "theme_action_links_{$stylesheet}", $actions, $theme, $context );
523
524                 echo $this->row_actions( $actions, true );
525         }
526
527         /**
528          * Handles the description column output.
529          *
530          * @since 4.3.0
531          * @access public
532          *
533          * @global string $status
534          * @global array  $totals
535          *
536          * @param WP_Theme $theme The current WP_Theme object.
537          */
538         public function column_description( $theme ) {
539                 global $status, $totals;
540                 if ( $theme->errors() ) {
541                         $pre = $status === 'broken' ? __( 'Broken Theme:' ) . ' ' : '';
542                         echo '<p><strong class="error-message">' . $pre . $theme->errors()->get_error_message() . '</strong></p>';
543                 }
544
545                 if ( $this->is_site_themes ) {
546                         $allowed = $theme->is_allowed( 'site', $this->site_id );
547                 } else {
548                         $allowed = $theme->is_allowed( 'network' );
549                 }
550
551                 $class = ! $allowed ? 'inactive' : 'active';
552                 if ( ! empty( $totals['upgrade'] ) && ! empty( $theme->update ) )
553                         $class .= ' update';
554
555                 echo "<div class='theme-description'><p>" . $theme->display( 'Description' ) . "</p></div>
556                         <div class='$class second theme-version-author-uri'>";
557
558                 $stylesheet = $theme->get_stylesheet();
559                 $theme_meta = array();
560
561                 if ( $theme->get('Version') ) {
562                         $theme_meta[] = sprintf( __( 'Version %s' ), $theme->display('Version') );
563                 }
564                 $theme_meta[] = sprintf( __( 'By %s' ), $theme->display('Author') );
565
566                 if ( $theme->get('ThemeURI') ) {
567                         /* translators: %s: theme name */
568                         $aria_label = sprintf( __( 'Visit %s homepage' ), $theme->display( 'Name' ) );
569
570                         $theme_meta[] = sprintf( '<a href="%s" aria-label="%s">%s</a>',
571                                 $theme->display( 'ThemeURI' ),
572                                 esc_attr( $aria_label ),
573                                 __( 'Visit Theme Site' )
574                         );
575                 }
576                 /**
577                  * Filters the array of row meta for each theme in the Multisite themes
578                  * list table.
579                  *
580                  * @since 3.1.0
581                  *
582                  * @param array    $theme_meta An array of the theme's metadata,
583                  *                             including the version, author, and
584                  *                             theme URI.
585                  * @param string   $stylesheet Directory name of the theme.
586                  * @param WP_Theme $theme      WP_Theme object.
587                  * @param string   $status     Status of the theme.
588                  */
589                 $theme_meta = apply_filters( 'theme_row_meta', $theme_meta, $stylesheet, $theme, $status );
590                 echo implode( ' | ', $theme_meta );
591
592                 echo '</div>';
593         }
594
595         /**
596          * Handles default column output.
597          *
598          * @since 4.3.0
599          * @access public
600          *
601          * @param WP_Theme $theme       The current WP_Theme object.
602          * @param string   $column_name The current column name.
603          */
604         public function column_default( $theme, $column_name ) {
605                 $stylesheet = $theme->get_stylesheet();
606
607                 /**
608                  * Fires inside each custom column of the Multisite themes list table.
609                  *
610                  * @since 3.1.0
611                  *
612                  * @param string   $column_name Name of the column.
613                  * @param string   $stylesheet  Directory name of the theme.
614                  * @param WP_Theme $theme       Current WP_Theme object.
615                  */
616                 do_action( 'manage_themes_custom_column', $column_name, $stylesheet, $theme );
617         }
618
619         /**
620          * Handles the output for a single table row.
621          *
622          * @since 4.3.0
623          * @access public
624          *
625          * @param WP_Theme $item The current WP_Theme object.
626          */
627         public function single_row_columns( $item ) {
628                 list( $columns, $hidden, $sortable, $primary ) = $this->get_column_info();
629
630                 foreach ( $columns as $column_name => $column_display_name ) {
631                         $extra_classes = '';
632                         if ( in_array( $column_name, $hidden ) ) {
633                                 $extra_classes .= ' hidden';
634                         }
635
636                         switch ( $column_name ) {
637                                 case 'cb':
638                                         echo '<th scope="row" class="check-column">';
639
640                                         $this->column_cb( $item );
641
642                                         echo '</th>';
643                                         break;
644
645                                 case 'name':
646                                         echo "<td class='theme-title column-primary{$extra_classes}'><strong>" . $item->display('Name') . "</strong>";
647
648                                         $this->column_name( $item );
649
650                                         echo "</td>";
651                                         break;
652
653                                 case 'description':
654                                         echo "<td class='column-description desc{$extra_classes}'>";
655
656                                         $this->column_description( $item );
657
658                                         echo '</td>';
659                                         break;
660
661                                 default:
662                                         echo "<td class='$column_name column-$column_name{$extra_classes}'>";
663
664                                         $this->column_default( $item, $column_name );
665
666                                         echo "</td>";
667                                         break;
668                         }
669                 }
670         }
671
672         /**
673          * @global string $status
674          * @global array  $totals
675          *
676          * @param WP_Theme $theme
677          */
678         public function single_row( $theme ) {
679                 global $status, $totals;
680
681                 if ( $this->is_site_themes ) {
682                         $allowed = $theme->is_allowed( 'site', $this->site_id );
683                 } else {
684                         $allowed = $theme->is_allowed( 'network' );
685                 }
686
687                 $stylesheet = $theme->get_stylesheet();
688
689                 $class = ! $allowed ? 'inactive' : 'active';
690                 if ( ! empty( $totals['upgrade'] ) && ! empty( $theme->update ) ) {
691                         $class .= ' update';
692                 }
693
694                 printf( '<tr class="%s" data-slug="%s">',
695                         esc_attr( $class ),
696                         esc_attr( $stylesheet )
697                 );
698
699                 $this->single_row_columns( $theme );
700
701                 echo "</tr>";
702
703                 if ( $this->is_site_themes )
704                         remove_action( "after_theme_row_$stylesheet", 'wp_theme_update_row' );
705
706                 /**
707                  * Fires after each row in the Multisite themes list table.
708                  *
709                  * @since 3.1.0
710                  *
711                  * @param string   $stylesheet Directory name of the theme.
712                  * @param WP_Theme $theme      Current WP_Theme object.
713                  * @param string   $status     Status of the theme.
714                  */
715                 do_action( 'after_theme_row', $stylesheet, $theme, $status );
716
717                 /**
718                  * Fires after each specific row in the Multisite themes list table.
719                  *
720                  * The dynamic portion of the hook name, `$stylesheet`, refers to the
721                  * directory name of the theme, most often synonymous with the template
722                  * name of the theme.
723                  *
724                  * @since 3.5.0
725                  *
726                  * @param string   $stylesheet Directory name of the theme.
727                  * @param WP_Theme $theme      Current WP_Theme object.
728                  * @param string   $status     Status of the theme.
729                  */
730                 do_action( "after_theme_row_{$stylesheet}", $stylesheet, $theme, $status );
731         }
732 }