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