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 * Filter 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 ];
153 $orderby = ucfirst( $orderby );
154 $order = strtoupper( $order );
156 if ( $orderby === 'Name' ) {
157 if ( 'ASC' === $order ) {
158 $this->items = array_reverse( $this->items );
161 uasort( $this->items, array( $this, '_order_callback' ) );
165 $start = ( $page - 1 ) * $themes_per_page;
167 if ( $total_this_page > $themes_per_page )
168 $this->items = array_slice( $this->items, $start, $themes_per_page, true );
170 $this->set_pagination_args( array(
171 'total_items' => $total_this_page,
172 'per_page' => $themes_per_page,
177 * @staticvar string $term
178 * @param WP_Theme $theme
181 public function _search_callback( $theme ) {
183 if ( is_null( $term ) )
184 $term = wp_unslash( $_REQUEST['s'] );
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 ) )
192 if ( false !== stripos( $theme->get_stylesheet(), $term ) )
195 if ( false !== stripos( $theme->get_template(), $term ) )
201 // Not used by any core columns.
203 * @global string $orderby
204 * @global string $order
205 * @param array $theme_a
206 * @param array $theme_b
209 public function _order_callback( $theme_a, $theme_b ) {
210 global $orderby, $order;
212 $a = $theme_a[ $orderby ];
213 $b = $theme_b[ $orderby ];
218 if ( 'DESC' === $order )
219 return ( $a < $b ) ? 1 : -1;
221 return ( $a < $b ) ? -1 : 1;
227 public function no_items() {
228 if ( $this->has_items ) {
229 _e( 'No themes found.' );
231 _e( 'You do not appear to have any themes available at this time.' );
239 public function get_columns() {
241 'cb' => '<input type="checkbox" />',
242 'name' => __( 'Theme' ),
243 'description' => __( 'Description' ),
251 protected function get_sortable_columns() {
258 * Gets the name of the primary column.
263 * @return string Unalterable name of the primary column name, in this case, 'name'.
265 protected function get_primary_column_name() {
271 * @global array $totals
272 * @global string $status
275 protected function get_views() {
276 global $totals, $status;
278 $status_links = array();
279 foreach ( $totals as $type => $count ) {
285 $text = _nx( 'All <span class="count">(%s)</span>', 'All <span class="count">(%s)</span>', $count, 'themes' );
288 $text = _n( 'Enabled <span class="count">(%s)</span>', 'Enabled <span class="count">(%s)</span>', $count );
291 $text = _n( 'Disabled <span class="count">(%s)</span>', 'Disabled <span class="count">(%s)</span>', $count );
294 $text = _n( 'Update Available <span class="count">(%s)</span>', 'Update Available <span class="count">(%s)</span>', $count );
297 $text = _n( 'Broken <span class="count">(%s)</span>', 'Broken <span class="count">(%s)</span>', $count );
301 if ( $this->is_site_themes )
302 $url = 'site-themes.php?id=' . $this->site_id;
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 ) )
315 return $status_links;
319 * @global string $status
323 protected function get_bulk_actions() {
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' );
343 public function display_rows() {
344 foreach ( $this->items as $theme )
345 $this->single_row( $theme );
349 * Handles the checkbox column output.
354 * @param WP_Theme $theme The current WP_Theme object.
356 public function column_cb( $theme ) {
357 $checkbox_id = 'checkbox_' . md5( $theme->get('Name') );
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>
365 * Handles the name column output.
370 * @global string $status
374 * @param WP_Theme $theme The current WP_Theme object.
376 public function column_name( $theme ) {
377 global $status, $page, $s;
381 if ( $this->is_site_themes ) {
382 $url = "site-themes.php?id={$this->site_id}&";
383 $allowed = $theme->is_allowed( 'site', $this->site_id );
385 $url = 'themes.php?';
386 $allowed = $theme->is_allowed( 'network' );
397 $stylesheet = $theme->get_stylesheet();
398 $theme_key = urlencode( $stylesheet );
401 if ( ! $theme->errors() ) {
402 $actions['enable'] = '<a href="' . esc_url( wp_nonce_url($url . 'action=enable&theme=' . $theme_key . '&paged=' . $page . '&s=' . $s, 'enable-theme_' . $stylesheet ) ) . '" title="' . esc_attr__('Enable this theme') . '" class="edit">' . ( $this->is_site_themes ? __( 'Enable' ) : __( 'Network Enable' ) ) . '</a>';
405 $actions['disable'] = '<a href="' . esc_url( wp_nonce_url($url . 'action=disable&theme=' . $theme_key . '&paged=' . $page . '&s=' . $s, 'disable-theme_' . $stylesheet ) ) . '" title="' . esc_attr__('Disable this theme') . '">' . ( $this->is_site_themes ? __( 'Disable' ) : __( 'Network Disable' ) ) . '</a>';
408 if ( current_user_can('edit_themes') ) {
409 $actions['edit'] = '<a href="' . esc_url('theme-editor.php?theme=' . $theme_key ) . '" title="' . esc_attr__('Open this theme in the Theme Editor') . '" class="edit">' . __('Edit') . '</a>';
412 if ( ! $allowed && current_user_can( 'delete_themes' ) && ! $this->is_site_themes && $stylesheet != get_option( 'stylesheet' ) && $stylesheet != get_option( 'template' ) ) {
413 $actions['delete'] = '<a href="' . esc_url( wp_nonce_url( 'themes.php?action=delete-selected&checked[]=' . $theme_key . '&theme_status=' . $context . '&paged=' . $page . '&s=' . $s, 'bulk-themes' ) ) . '" title="' . esc_attr__( 'Delete this theme' ) . '" class="delete">' . __( 'Delete' ) . '</a>';
416 * Filter the action links displayed for each theme in the Multisite
419 * The action links displayed are determined by the theme's status, and
420 * which Multisite themes list table is being displayed - the Network
421 * themes list table (themes.php), which displays all installed themes,
422 * or the Site themes list table (site-themes.php), which displays the
423 * non-network enabled themes when editing a site in the Network admin.
425 * The default action links for the Network themes list table include
426 * 'Network Enable', 'Network Disable', 'Edit', and 'Delete'.
428 * The default action links for the Site themes list table include
429 * 'Enable', 'Disable', and 'Edit'.
433 * @param array $actions An array of action links.
434 * @param WP_Theme $theme The current WP_Theme object.
435 * @param string $context Status of the theme.
437 $actions = apply_filters( 'theme_action_links', array_filter( $actions ), $theme, $context );
440 * Filter the action links of a specific theme in the Multisite themes
443 * The dynamic portion of the hook name, `$stylesheet`, refers to the
444 * directory name of the theme, which in most cases is synonymous
445 * with the template name.
449 * @param array $actions An array of action links.
450 * @param WP_Theme $theme The current WP_Theme object.
451 * @param string $context Status of the theme.
453 $actions = apply_filters( "theme_action_links_$stylesheet", $actions, $theme, $context );
455 echo $this->row_actions( $actions, true );
459 * Handles the description column output.
464 * @global string $status
465 * @global array $totals
467 * @param WP_Theme $theme The current WP_Theme object.
469 public function column_description( $theme ) {
470 global $status, $totals;
471 if ( $theme->errors() ) {
472 $pre = $status === 'broken' ? __( 'Broken Theme:' ) . ' ' : '';
473 echo '<p><strong class="error-message">' . $pre . $theme->errors()->get_error_message() . '</strong></p>';
476 if ( $this->is_site_themes ) {
477 $allowed = $theme->is_allowed( 'site', $this->site_id );
479 $allowed = $theme->is_allowed( 'network' );
482 $class = ! $allowed ? 'inactive' : 'active';
483 if ( ! empty( $totals['upgrade'] ) && ! empty( $theme->update ) )
486 echo "<div class='theme-description'><p>" . $theme->display( 'Description' ) . "</p></div>
487 <div class='$class second theme-version-author-uri'>";
489 $stylesheet = $theme->get_stylesheet();
490 $theme_meta = array();
492 if ( $theme->get('Version') ) {
493 $theme_meta[] = sprintf( __( 'Version %s' ), $theme->display('Version') );
495 $theme_meta[] = sprintf( __( 'By %s' ), $theme->display('Author') );
497 if ( $theme->get('ThemeURI') ) {
498 $theme_meta[] = '<a href="' . $theme->display('ThemeURI') . '" title="' . esc_attr__( 'Visit theme homepage' ) . '">' . __( 'Visit Theme Site' ) . '</a>';
501 * Filter the array of row meta for each theme in the Multisite themes
506 * @param array $theme_meta An array of the theme's metadata,
507 * including the version, author, and
509 * @param string $stylesheet Directory name of the theme.
510 * @param WP_Theme $theme WP_Theme object.
511 * @param string $status Status of the theme.
513 $theme_meta = apply_filters( 'theme_row_meta', $theme_meta, $stylesheet, $theme, $status );
514 echo implode( ' | ', $theme_meta );
520 * Handles default column output.
525 * @param WP_Theme $theme The current WP_Theme object.
526 * @param string $column_name The current column name.
528 public function column_default( $theme, $column_name ) {
529 $stylesheet = $theme->get_stylesheet();
532 * Fires inside each custom column of the Multisite themes list table.
536 * @param string $column_name Name of the column.
537 * @param string $stylesheet Directory name of the theme.
538 * @param WP_Theme $theme Current WP_Theme object.
540 do_action( 'manage_themes_custom_column', $column_name, $stylesheet, $theme );
544 * Handles the output for a single table row.
549 * @param WP_Theme $item The current WP_Theme object.
551 public function single_row_columns( $item ) {
552 list( $columns, $hidden, $sortable, $primary ) = $this->get_column_info();
554 foreach ( $columns as $column_name => $column_display_name ) {
556 if ( in_array( $column_name, $hidden ) ) {
557 $extra_classes .= ' hidden';
560 switch ( $column_name ) {
562 echo '<th scope="row" class="check-column">';
564 $this->column_cb( $item );
570 echo "<td class='theme-title column-primary{$extra_classes}'><strong>" . $item->display('Name') . "</strong>";
572 $this->column_name( $item );
578 echo "<td class='column-description desc{$extra_classes}'>";
580 $this->column_description( $item );
586 echo "<td class='$column_name column-$column_name{$extra_classes}'>";
588 $this->column_default( $item, $column_name );
597 * @global string $status
598 * @global array $totals
600 * @param WP_Theme $theme
602 public function single_row( $theme ) {
603 global $status, $totals;
605 if ( $this->is_site_themes ) {
606 $allowed = $theme->is_allowed( 'site', $this->site_id );
608 $allowed = $theme->is_allowed( 'network' );
611 $stylesheet = $theme->get_stylesheet();
613 $class = ! $allowed ? 'inactive' : 'active';
615 $id = sanitize_html_class( $theme->get_stylesheet() );
617 if ( ! empty( $totals['upgrade'] ) && ! empty( $theme->update ) ) {
621 echo "<tr id='$id' class='$class'>";
623 $this->single_row_columns( $theme );
627 if ( $this->is_site_themes )
628 remove_action( "after_theme_row_$stylesheet", 'wp_theme_update_row' );
631 * Fires after each row in the Multisite themes list table.
635 * @param string $stylesheet Directory name of the theme.
636 * @param WP_Theme $theme Current WP_Theme object.
637 * @param string $status Status of the theme.
639 do_action( 'after_theme_row', $stylesheet, $theme, $status );
642 * Fires after each specific row in the Multisite themes list table.
644 * The dynamic portion of the hook name, `$stylesheet`, refers to the
645 * directory name of the theme, most often synonymous with the template
650 * @param string $stylesheet Directory name of the theme.
651 * @param WP_Theme $theme Current WP_Theme object.
652 * @param string $status Status of the theme.
654 do_action( "after_theme_row_$stylesheet", $stylesheet, $theme, $status );