3 * Themes List Table class.
6 * @subpackage List_Table
10 class WP_Themes_List_Table extends WP_List_Table {
12 protected $search_terms = array();
13 public $features = array();
21 * @see WP_List_Table::__construct() for more information on default arguments.
23 * @param array $args An associative array of arguments.
25 public function __construct( $args = array() ) {
26 parent::__construct( array(
28 'screen' => isset( $args['screen'] ) ? $args['screen'] : null,
32 public function ajax_user_can() {
33 // Do not check edit_theme_options here. AJAX calls for available themes require switch_themes.
34 return current_user_can( 'switch_themes' );
37 public function prepare_items() {
38 $themes = wp_get_themes( array( 'allowed' => true ) );
40 if ( ! empty( $_REQUEST['s'] ) )
41 $this->search_terms = array_unique( array_filter( array_map( 'trim', explode( ',', strtolower( wp_unslash( $_REQUEST['s'] ) ) ) ) ) );
43 if ( ! empty( $_REQUEST['features'] ) )
44 $this->features = $_REQUEST['features'];
46 if ( $this->search_terms || $this->features ) {
47 foreach ( $themes as $key => $theme ) {
48 if ( ! $this->search_theme( $theme ) )
49 unset( $themes[ $key ] );
53 unset( $themes[ get_option( 'stylesheet' ) ] );
54 WP_Theme::sort_by_name( $themes );
57 $page = $this->get_pagenum();
59 $start = ( $page - 1 ) * $per_page;
61 $this->items = array_slice( $themes, $start, $per_page, true );
63 $this->set_pagination_args( array(
64 'total_items' => count( $themes ),
65 'per_page' => $per_page,
66 'infinite_scroll' => true,
70 public function no_items() {
71 if ( $this->search_terms || $this->features ) {
72 _e( 'No items found.' );
76 if ( is_multisite() ) {
77 if ( current_user_can( 'install_themes' ) && current_user_can( 'manage_network_themes' ) ) {
78 printf( __( 'You only have one theme enabled for this site right now. Visit the Network Admin to <a href="%1$s">enable</a> or <a href="%2$s">install</a> more themes.' ), network_admin_url( 'site-themes.php?id=' . $GLOBALS['blog_id'] ), network_admin_url( 'theme-install.php' ) );
81 } elseif ( current_user_can( 'manage_network_themes' ) ) {
82 printf( __( 'You only have one theme enabled for this site right now. Visit the Network Admin to <a href="%1$s">enable</a> more themes.' ), network_admin_url( 'site-themes.php?id=' . $GLOBALS['blog_id'] ) );
86 // Else, fallthrough. install_themes doesn't help if you can't enable it.
88 if ( current_user_can( 'install_themes' ) ) {
89 printf( __( 'You only have one theme installed right now. Live a little! You can choose from over 1,000 free themes in the WordPress.org Theme Directory at any time: just click on the <a href="%s">Install Themes</a> tab above.' ), admin_url( 'theme-install.php' ) );
95 printf( __( 'Only the current theme is available to you. Contact the %s administrator for information about accessing additional themes.' ), get_site_option( 'site_name' ) );
99 * @param string $which
102 public function tablenav( $which = 'top' ) {
103 if ( $this->get_pagination_arg( 'total_pages' ) <= 1 )
106 <div class="tablenav themes <?php echo $which; ?>">
107 <?php $this->pagination( $which ); ?>
108 <span class="spinner"></span>
114 public function display() {
115 wp_nonce_field( "fetch-list-" . get_class( $this ), '_ajax_fetch_list_nonce' );
117 <?php $this->tablenav( 'top' ); ?>
119 <div id="availablethemes">
120 <?php $this->display_rows_or_placeholder(); ?>
123 <?php $this->tablenav( 'bottom' ); ?>
127 public function get_columns() {
131 public function display_rows_or_placeholder() {
132 if ( $this->has_items() ) {
133 $this->display_rows();
135 echo '<div class="no-items">';
141 public function display_rows() {
142 $themes = $this->items;
144 foreach ( $themes as $theme ):
145 ?><div class="available-theme"><?php
147 $template = $theme->get_template();
148 $stylesheet = $theme->get_stylesheet();
149 $title = $theme->display('Name');
150 $version = $theme->display('Version');
151 $author = $theme->display('Author');
153 $activate_link = wp_nonce_url( "themes.php?action=activate&template=" . urlencode( $template ) . "&stylesheet=" . urlencode( $stylesheet ), 'switch-theme_' . $stylesheet );
155 $preview_link = esc_url( add_query_arg(
156 array( 'preview' => 1, 'template' => urlencode( $template ), 'stylesheet' => urlencode( $stylesheet ), 'preview_iframe' => true, 'TB_iframe' => 'true' ),
160 $actions['activate'] = '<a href="' . $activate_link . '" class="activatelink" title="'
161 . esc_attr( sprintf( __( 'Activate “%s”' ), $title ) ) . '">' . __( 'Activate' ) . '</a>';
163 $actions['preview'] = '<a href="' . $preview_link . '" class="hide-if-customize" title="'
164 . esc_attr( sprintf( __( 'Preview “%s”' ), $title ) ) . '">' . __( 'Preview' ) . '</a>';
166 if ( current_user_can( 'edit_theme_options' ) && current_user_can( 'customize' ) ) {
167 $actions['preview'] .= '<a href="' . wp_customize_url( $stylesheet ) . '" class="load-customize hide-if-no-customize">'
168 . __( 'Live Preview' ) . '</a>';
171 if ( ! is_multisite() && current_user_can( 'delete_themes' ) )
172 $actions['delete'] = '<a class="submitdelete deletion" href="' . wp_nonce_url( 'themes.php?action=delete&stylesheet=' . urlencode( $stylesheet ), 'delete-theme_' . $stylesheet )
173 . '" onclick="' . "return confirm( '" . esc_js( sprintf( __( "You are about to delete this theme '%s'\n 'Cancel' to stop, 'OK' to delete." ), $title ) )
174 . "' );" . '">' . __( 'Delete' ) . '</a>';
176 /** This filter is documented in wp-admin/includes/class-wp-ms-themes-list-table.php */
177 $actions = apply_filters( 'theme_action_links', $actions, $theme );
179 /** This filter is documented in wp-admin/includes/class-wp-ms-themes-list-table.php */
180 $actions = apply_filters( "theme_action_links_$stylesheet", $actions, $theme );
181 $delete_action = isset( $actions['delete'] ) ? '<div class="delete-theme">' . $actions['delete'] . '</div>' : '';
182 unset( $actions['delete'] );
186 <a href="<?php echo $preview_link; ?>" class="screenshot hide-if-customize">
187 <?php if ( $screenshot = $theme->get_screenshot() ) : ?>
188 <img src="<?php echo esc_url( $screenshot ); ?>" alt="" />
191 <a href="<?php echo wp_customize_url( $stylesheet ); ?>" class="screenshot load-customize hide-if-no-customize">
192 <?php if ( $screenshot = $theme->get_screenshot() ) : ?>
193 <img src="<?php echo esc_url( $screenshot ); ?>" alt="" />
197 <h3><?php echo $title; ?></h3>
198 <div class="theme-author"><?php printf( __( 'By %s' ), $author ); ?></div>
199 <div class="action-links">
201 <?php foreach ( $actions as $action ): ?>
202 <li><?php echo $action; ?></li>
204 <li class="hide-if-no-js"><a href="#" class="theme-detail"><?php _e('Details') ?></a></li>
206 <?php echo $delete_action; ?>
208 <?php theme_update_available( $theme ); ?>
211 <div class="themedetaildiv hide-if-js">
212 <p><strong><?php _e('Version:'); ?></strong> <?php echo $version; ?></p>
213 <p><?php echo $theme->display('Description'); ?></p>
214 <?php if ( $theme->parent() ) {
215 printf( ' <p class="howto">' . __( 'This <a href="%1$s">child theme</a> requires its parent theme, %2$s.' ) . '</p>',
216 __( 'http://codex.wordpress.org/Child_Themes' ),
217 $theme->parent()->display( 'Name' ) );
227 * @param WP_Theme $theme
230 public function search_theme( $theme ) {
231 // Search the features
232 foreach ( $this->features as $word ) {
233 if ( ! in_array( $word, $theme->get('Tags') ) )
238 foreach ( $this->search_terms as $word ) {
239 if ( in_array( $word, $theme->get('Tags') ) )
242 foreach ( array( 'Name', 'Description', 'Author', 'AuthorURI' ) as $header ) {
243 // Don't mark up; Do translate.
244 if ( false !== stripos( strip_tags( $theme->display( $header, false, true ) ), $word ) ) {
249 if ( false !== stripos( $theme->get_stylesheet(), $word ) )
252 if ( false !== stripos( $theme->get_template(), $word ) )
262 * Send required variables to JavaScript land
267 * @param array $extra_args
269 public function _js_vars( $extra_args = array() ) {
270 $search_string = isset( $_REQUEST['s'] ) ? esc_attr( wp_unslash( $_REQUEST['s'] ) ) : '';
273 'search' => $search_string,
274 'features' => $this->features,
275 'paged' => $this->get_pagenum(),
276 'total_pages' => ! empty( $this->_pagination_args['total_pages'] ) ? $this->_pagination_args['total_pages'] : 1,
279 if ( is_array( $extra_args ) )
280 $args = array_merge( $args, $extra_args );
282 printf( "<script type='text/javascript'>var theme_list_args = %s;</script>\n", wp_json_encode( $args ) );