3 * List Table API: WP_MS_Themes_List_Table class
6 * @subpackage Administration
11 * Core class used to implement displaying themes in a list table for the network admin.
18 class WP_MS_Themes_List_Table extends WP_List_Table {
21 public $is_site_themes;
31 * @see WP_List_Table::__construct() for more information on default arguments.
33 * @global string $status
36 * @param array $args An associative array of arguments.
38 public function __construct( $args = array() ) {
39 global $status, $page;
41 parent::__construct( array(
43 'screen' => isset( $args['screen'] ) ? $args['screen'] : null,
46 $status = isset( $_REQUEST['theme_status'] ) ? $_REQUEST['theme_status'] : 'all';
47 if ( !in_array( $status, array( 'all', 'enabled', 'disabled', 'upgrade', 'search', 'broken' ) ) )
50 $page = $this->get_pagenum();
52 $this->is_site_themes = ( 'site-themes-network' === $this->screen->id ) ? true : false;
54 if ( $this->is_site_themes )
55 $this->site_id = isset( $_REQUEST['id'] ) ? intval( $_REQUEST['id'] ) : 0;
62 protected function get_table_classes() {
63 // todo: remove and add CSS for .themes
64 return array( 'widefat', 'plugins' );
71 public function ajax_user_can() {
72 if ( $this->is_site_themes )
73 return current_user_can( 'manage_sites' );
75 return current_user_can( 'manage_network_themes' );
80 * @global string $status
81 * @global array $totals
83 * @global string $orderby
84 * @global string $order
87 public function prepare_items() {
88 global $status, $totals, $page, $orderby, $order, $s;
90 wp_reset_vars( array( 'orderby', 'order', 's' ) );
94 * Filters the full array of WP_Theme objects to list in the Multisite
99 * @param array $all An array of WP_Theme objects to display in the list table.
101 'all' => apply_filters( 'all_themes', wp_get_themes() ),
103 'enabled' => array(),
104 'disabled' => array(),
105 'upgrade' => array(),
106 'broken' => $this->is_site_themes ? array() : wp_get_themes( array( 'errors' => true ) ),
109 if ( $this->is_site_themes ) {
110 $themes_per_page = $this->get_items_per_page( 'site_themes_network_per_page' );
111 $allowed_where = 'site';
113 $themes_per_page = $this->get_items_per_page( 'themes_network_per_page' );
114 $allowed_where = 'network';
117 $maybe_update = current_user_can( 'update_themes' ) && ! $this->is_site_themes && $current = get_site_transient( 'update_themes' );
119 foreach ( (array) $themes['all'] as $key => $theme ) {
120 if ( $this->is_site_themes && $theme->is_allowed( 'network' ) ) {
121 unset( $themes['all'][ $key ] );
125 if ( $maybe_update && isset( $current->response[ $key ] ) ) {
126 $themes['all'][ $key ]->update = true;
127 $themes['upgrade'][ $key ] = $themes['all'][ $key ];
130 $filter = $theme->is_allowed( $allowed_where, $this->site_id ) ? 'enabled' : 'disabled';
131 $themes[ $filter ][ $key ] = $themes['all'][ $key ];
136 $themes['search'] = array_filter( array_merge( $themes['all'], $themes['broken'] ), array( $this, '_search_callback' ) );
140 foreach ( $themes as $type => $list )
141 $totals[ $type ] = count( $list );
143 if ( empty( $themes[ $status ] ) && !in_array( $status, array( 'all', 'search' ) ) )
146 $this->items = $themes[ $status ];
147 WP_Theme::sort_by_name( $this->items );
149 $this->has_items = ! empty( $themes['all'] );
150 $total_this_page = $totals[ $status ];
152 wp_localize_script( 'updates', '_wpUpdatesItemCounts', array(
154 'totals' => wp_get_update_data(),
158 $orderby = ucfirst( $orderby );
159 $order = strtoupper( $order );
161 if ( $orderby === 'Name' ) {
162 if ( 'ASC' === $order ) {
163 $this->items = array_reverse( $this->items );
166 uasort( $this->items, array( $this, '_order_callback' ) );
170 $start = ( $page - 1 ) * $themes_per_page;
172 if ( $total_this_page > $themes_per_page )
173 $this->items = array_slice( $this->items, $start, $themes_per_page, true );
175 $this->set_pagination_args( array(
176 'total_items' => $total_this_page,
177 'per_page' => $themes_per_page,
182 * @staticvar string $term
183 * @param WP_Theme $theme
186 public function _search_callback( $theme ) {
188 if ( is_null( $term ) )
189 $term = wp_unslash( $_REQUEST['s'] );
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 ) )
197 if ( false !== stripos( $theme->get_stylesheet(), $term ) )
200 if ( false !== stripos( $theme->get_template(), $term ) )
206 // Not used by any core columns.
208 * @global string $orderby
209 * @global string $order
210 * @param array $theme_a
211 * @param array $theme_b
214 public function _order_callback( $theme_a, $theme_b ) {
215 global $orderby, $order;
217 $a = $theme_a[ $orderby ];
218 $b = $theme_b[ $orderby ];
223 if ( 'DESC' === $order )
224 return ( $a < $b ) ? 1 : -1;
226 return ( $a < $b ) ? -1 : 1;
232 public function no_items() {
233 if ( $this->has_items ) {
234 _e( 'No themes found.' );
236 _e( 'You do not appear to have any themes available at this time.' );
244 public function get_columns() {
246 'cb' => '<input type="checkbox" />',
247 'name' => __( 'Theme' ),
248 'description' => __( 'Description' ),
256 protected function get_sortable_columns() {
263 * Gets the name of the primary column.
268 * @return string Unalterable name of the primary column name, in this case, 'name'.
270 protected function get_primary_column_name() {
276 * @global array $totals
277 * @global string $status
280 protected function get_views() {
281 global $totals, $status;
283 $status_links = array();
284 foreach ( $totals as $type => $count ) {
290 $text = _nx( 'All <span class="count">(%s)</span>', 'All <span class="count">(%s)</span>', $count, 'themes' );
293 $text = _n( 'Enabled <span class="count">(%s)</span>', 'Enabled <span class="count">(%s)</span>', $count );
296 $text = _n( 'Disabled <span class="count">(%s)</span>', 'Disabled <span class="count">(%s)</span>', $count );
299 $text = _n( 'Update Available <span class="count">(%s)</span>', 'Update Available <span class="count">(%s)</span>', $count );
302 $text = _n( 'Broken <span class="count">(%s)</span>', 'Broken <span class="count">(%s)</span>', $count );
306 if ( $this->is_site_themes )
307 $url = 'site-themes.php?id=' . $this->site_id;
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 ) )
320 return $status_links;
324 * @global string $status
328 protected function get_bulk_actions() {
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' );
348 public function display_rows() {
349 foreach ( $this->items as $theme )
350 $this->single_row( $theme );
354 * Handles the checkbox column output.
359 * @param WP_Theme $theme The current WP_Theme object.
361 public function column_cb( $theme ) {
362 $checkbox_id = 'checkbox_' . md5( $theme->get('Name') );
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>
370 * Handles the name column output.
375 * @global string $status
379 * @param WP_Theme $theme The current WP_Theme object.
381 public function column_name( $theme ) {
382 global $status, $page, $s;
386 if ( $this->is_site_themes ) {
387 $url = "site-themes.php?id={$this->site_id}&";
388 $allowed = $theme->is_allowed( 'site', $this->site_id );
390 $url = 'themes.php?';
391 $allowed = $theme->is_allowed( 'network' );
402 $stylesheet = $theme->get_stylesheet();
403 $theme_key = urlencode( $stylesheet );
406 if ( ! $theme->errors() ) {
407 $url = add_query_arg( array(
408 'action' => 'enable',
409 'theme' => $theme_key,
414 if ( $this->is_site_themes ) {
415 /* translators: %s: theme name */
416 $aria_label = sprintf( __( 'Enable %s' ), $theme->display( 'Name' ) );
418 /* translators: %s: theme name */
419 $aria_label = sprintf( __( 'Network Enable %s' ), $theme->display( 'Name' ) );
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' ) )
429 $url = add_query_arg( array(
430 'action' => 'disable',
431 'theme' => $theme_key,
436 if ( $this->is_site_themes ) {
437 /* translators: %s: theme name */
438 $aria_label = sprintf( __( 'Disable %s' ), $theme->display( 'Name' ) );
440 /* translators: %s: theme name */
441 $aria_label = sprintf( __( 'Network Disable %s' ), $theme->display( 'Name' ) );
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' ) )
451 if ( current_user_can('edit_themes') ) {
452 $url = add_query_arg( array(
453 'theme' => $theme_key,
454 ), 'theme-editor.php' );
456 /* translators: %s: theme name */
457 $aria_label = sprintf( __( 'Open %s in the Theme Editor' ), $theme->display( 'Name' ) );
459 $actions['edit'] = sprintf( '<a href="%s" class="edit" aria-label="%s">%s</a>',
461 esc_attr( $aria_label ),
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,
475 /* translators: %s: theme name */
476 $aria_label = sprintf( _x( 'Delete %s', 'theme' ), $theme->display( 'Name' ) );
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 ),
485 * Filters the action links displayed for each theme in the Multisite
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.
494 * The default action links for the Network themes list table include
495 * 'Network Enable', 'Network Disable', 'Edit', and 'Delete'.
497 * The default action links for the Site themes list table include
498 * 'Enable', 'Disable', and 'Edit'.
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.
506 $actions = apply_filters( 'theme_action_links', array_filter( $actions ), $theme, $context );
509 * Filters the action links of a specific theme in the Multisite themes
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.
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.
522 $actions = apply_filters( "theme_action_links_{$stylesheet}", $actions, $theme, $context );
524 echo $this->row_actions( $actions, true );
528 * Handles the description column output.
533 * @global string $status
534 * @global array $totals
536 * @param WP_Theme $theme The current WP_Theme object.
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>';
545 if ( $this->is_site_themes ) {
546 $allowed = $theme->is_allowed( 'site', $this->site_id );
548 $allowed = $theme->is_allowed( 'network' );
551 $class = ! $allowed ? 'inactive' : 'active';
552 if ( ! empty( $totals['upgrade'] ) && ! empty( $theme->update ) )
555 echo "<div class='theme-description'><p>" . $theme->display( 'Description' ) . "</p></div>
556 <div class='$class second theme-version-author-uri'>";
558 $stylesheet = $theme->get_stylesheet();
559 $theme_meta = array();
561 if ( $theme->get('Version') ) {
562 $theme_meta[] = sprintf( __( 'Version %s' ), $theme->display('Version') );
564 $theme_meta[] = sprintf( __( 'By %s' ), $theme->display('Author') );
566 if ( $theme->get('ThemeURI') ) {
567 /* translators: %s: theme name */
568 $aria_label = sprintf( __( 'Visit %s homepage' ), $theme->display( 'Name' ) );
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' )
577 * Filters the array of row meta for each theme in the Multisite themes
582 * @param array $theme_meta An array of the theme's metadata,
583 * including the version, author, and
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.
589 $theme_meta = apply_filters( 'theme_row_meta', $theme_meta, $stylesheet, $theme, $status );
590 echo implode( ' | ', $theme_meta );
596 * Handles default column output.
601 * @param WP_Theme $theme The current WP_Theme object.
602 * @param string $column_name The current column name.
604 public function column_default( $theme, $column_name ) {
605 $stylesheet = $theme->get_stylesheet();
608 * Fires inside each custom column of the Multisite themes list table.
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.
616 do_action( 'manage_themes_custom_column', $column_name, $stylesheet, $theme );
620 * Handles the output for a single table row.
625 * @param WP_Theme $item The current WP_Theme object.
627 public function single_row_columns( $item ) {
628 list( $columns, $hidden, $sortable, $primary ) = $this->get_column_info();
630 foreach ( $columns as $column_name => $column_display_name ) {
632 if ( in_array( $column_name, $hidden ) ) {
633 $extra_classes .= ' hidden';
636 switch ( $column_name ) {
638 echo '<th scope="row" class="check-column">';
640 $this->column_cb( $item );
646 echo "<td class='theme-title column-primary{$extra_classes}'><strong>" . $item->display('Name') . "</strong>";
648 $this->column_name( $item );
654 echo "<td class='column-description desc{$extra_classes}'>";
656 $this->column_description( $item );
662 echo "<td class='$column_name column-$column_name{$extra_classes}'>";
664 $this->column_default( $item, $column_name );
673 * @global string $status
674 * @global array $totals
676 * @param WP_Theme $theme
678 public function single_row( $theme ) {
679 global $status, $totals;
681 if ( $this->is_site_themes ) {
682 $allowed = $theme->is_allowed( 'site', $this->site_id );
684 $allowed = $theme->is_allowed( 'network' );
687 $stylesheet = $theme->get_stylesheet();
689 $class = ! $allowed ? 'inactive' : 'active';
690 if ( ! empty( $totals['upgrade'] ) && ! empty( $theme->update ) ) {
694 printf( '<tr class="%s" data-slug="%s">',
696 esc_attr( $stylesheet )
699 $this->single_row_columns( $theme );
703 if ( $this->is_site_themes )
704 remove_action( "after_theme_row_$stylesheet", 'wp_theme_update_row' );
707 * Fires after each row in the Multisite themes list table.
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.
715 do_action( 'after_theme_row', $stylesheet, $theme, $status );
718 * Fires after each specific row in the Multisite themes list table.
720 * The dynamic portion of the hook name, `$stylesheet`, refers to the
721 * directory name of the theme, most often synonymous with the template
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.
730 do_action( "after_theme_row_{$stylesheet}", $stylesheet, $theme, $status );