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,
36 public function ajax_user_can() {
37 // Do not check edit_theme_options here. AJAX calls for available themes require switch_themes.
38 return current_user_can( 'switch_themes' );
44 public function prepare_items() {
45 $themes = wp_get_themes( array( 'allowed' => true ) );
47 if ( ! empty( $_REQUEST['s'] ) )
48 $this->search_terms = array_unique( array_filter( array_map( 'trim', explode( ',', strtolower( wp_unslash( $_REQUEST['s'] ) ) ) ) ) );
50 if ( ! empty( $_REQUEST['features'] ) )
51 $this->features = $_REQUEST['features'];
53 if ( $this->search_terms || $this->features ) {
54 foreach ( $themes as $key => $theme ) {
55 if ( ! $this->search_theme( $theme ) )
56 unset( $themes[ $key ] );
60 unset( $themes[ get_option( 'stylesheet' ) ] );
61 WP_Theme::sort_by_name( $themes );
64 $page = $this->get_pagenum();
66 $start = ( $page - 1 ) * $per_page;
68 $this->items = array_slice( $themes, $start, $per_page, true );
70 $this->set_pagination_args( array(
71 'total_items' => count( $themes ),
72 'per_page' => $per_page,
73 'infinite_scroll' => true,
80 public function no_items() {
81 if ( $this->search_terms || $this->features ) {
82 _e( 'No items found.' );
86 if ( is_multisite() ) {
87 if ( current_user_can( 'install_themes' ) && current_user_can( 'manage_network_themes' ) ) {
88 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' ) );
91 } elseif ( current_user_can( 'manage_network_themes' ) ) {
92 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'] ) );
96 // Else, fallthrough. install_themes doesn't help if you can't enable it.
98 if ( current_user_can( 'install_themes' ) ) {
99 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' ) );
105 printf( __( 'Only the current theme is available to you. Contact the %s administrator for information about accessing additional themes.' ), get_site_option( 'site_name' ) );
109 * @param string $which
111 public function tablenav( $which = 'top' ) {
112 if ( $this->get_pagination_arg( 'total_pages' ) <= 1 )
115 <div class="tablenav themes <?php echo $which; ?>">
116 <?php $this->pagination( $which ); ?>
117 <span class="spinner"></span>
126 public function display() {
127 wp_nonce_field( "fetch-list-" . get_class( $this ), '_ajax_fetch_list_nonce' );
129 <?php $this->tablenav( 'top' ); ?>
131 <div id="availablethemes">
132 <?php $this->display_rows_or_placeholder(); ?>
135 <?php $this->tablenav( 'bottom' ); ?>
143 public function get_columns() {
150 public function display_rows_or_placeholder() {
151 if ( $this->has_items() ) {
152 $this->display_rows();
154 echo '<div class="no-items">';
163 public function display_rows() {
164 $themes = $this->items;
166 foreach ( $themes as $theme ):
167 ?><div class="available-theme"><?php
169 $template = $theme->get_template();
170 $stylesheet = $theme->get_stylesheet();
171 $title = $theme->display('Name');
172 $version = $theme->display('Version');
173 $author = $theme->display('Author');
175 $activate_link = wp_nonce_url( "themes.php?action=activate&template=" . urlencode( $template ) . "&stylesheet=" . urlencode( $stylesheet ), 'switch-theme_' . $stylesheet );
178 $actions['activate'] = '<a href="' . $activate_link . '" class="activatelink" title="'
179 . esc_attr( sprintf( __( 'Activate “%s”' ), $title ) ) . '">' . __( 'Activate' ) . '</a>';
181 if ( current_user_can( 'edit_theme_options' ) && current_user_can( 'customize' ) ) {
182 $actions['preview'] .= '<a href="' . wp_customize_url( $stylesheet ) . '" class="load-customize hide-if-no-customize">'
183 . __( 'Live Preview' ) . '</a>';
186 if ( ! is_multisite() && current_user_can( 'delete_themes' ) )
187 $actions['delete'] = '<a class="submitdelete deletion" href="' . wp_nonce_url( 'themes.php?action=delete&stylesheet=' . urlencode( $stylesheet ), 'delete-theme_' . $stylesheet )
188 . '" onclick="' . "return confirm( '" . esc_js( sprintf( __( "You are about to delete this theme '%s'\n 'Cancel' to stop, 'OK' to delete." ), $title ) )
189 . "' );" . '">' . __( 'Delete' ) . '</a>';
191 /** This filter is documented in wp-admin/includes/class-wp-ms-themes-list-table.php */
192 $actions = apply_filters( 'theme_action_links', $actions, $theme );
194 /** This filter is documented in wp-admin/includes/class-wp-ms-themes-list-table.php */
195 $actions = apply_filters( "theme_action_links_$stylesheet", $actions, $theme );
196 $delete_action = isset( $actions['delete'] ) ? '<div class="delete-theme">' . $actions['delete'] . '</div>' : '';
197 unset( $actions['delete'] );
201 <span class="screenshot hide-if-customize">
202 <?php if ( $screenshot = $theme->get_screenshot() ) : ?>
203 <img src="<?php echo esc_url( $screenshot ); ?>" alt="" />
206 <a href="<?php echo wp_customize_url( $stylesheet ); ?>" class="screenshot load-customize hide-if-no-customize">
207 <?php if ( $screenshot = $theme->get_screenshot() ) : ?>
208 <img src="<?php echo esc_url( $screenshot ); ?>" alt="" />
212 <h3><?php echo $title; ?></h3>
213 <div class="theme-author"><?php printf( __( 'By %s' ), $author ); ?></div>
214 <div class="action-links">
216 <?php foreach ( $actions as $action ): ?>
217 <li><?php echo $action; ?></li>
219 <li class="hide-if-no-js"><a href="#" class="theme-detail"><?php _e('Details') ?></a></li>
221 <?php echo $delete_action; ?>
223 <?php theme_update_available( $theme ); ?>
226 <div class="themedetaildiv hide-if-js">
227 <p><strong><?php _e('Version:'); ?></strong> <?php echo $version; ?></p>
228 <p><?php echo $theme->display('Description'); ?></p>
229 <?php if ( $theme->parent() ) {
230 printf( ' <p class="howto">' . __( 'This <a href="%1$s">child theme</a> requires its parent theme, %2$s.' ) . '</p>',
231 __( 'https://codex.wordpress.org/Child_Themes' ),
232 $theme->parent()->display( 'Name' ) );
242 * @param WP_Theme $theme
245 public function search_theme( $theme ) {
246 // Search the features
247 foreach ( $this->features as $word ) {
248 if ( ! in_array( $word, $theme->get('Tags') ) )
253 foreach ( $this->search_terms as $word ) {
254 if ( in_array( $word, $theme->get('Tags') ) )
257 foreach ( array( 'Name', 'Description', 'Author', 'AuthorURI' ) as $header ) {
258 // Don't mark up; Do translate.
259 if ( false !== stripos( strip_tags( $theme->display( $header, false, true ) ), $word ) ) {
264 if ( false !== stripos( $theme->get_stylesheet(), $word ) )
267 if ( false !== stripos( $theme->get_template(), $word ) )
277 * Send required variables to JavaScript land
282 * @param array $extra_args
284 public function _js_vars( $extra_args = array() ) {
285 $search_string = isset( $_REQUEST['s'] ) ? esc_attr( wp_unslash( $_REQUEST['s'] ) ) : '';
288 'search' => $search_string,
289 'features' => $this->features,
290 'paged' => $this->get_pagenum(),
291 'total_pages' => ! empty( $this->_pagination_args['total_pages'] ) ? $this->_pagination_args['total_pages'] : 1,
294 if ( is_array( $extra_args ) )
295 $args = array_merge( $args, $extra_args );
297 printf( "<script type='text/javascript'>var theme_list_args = %s;</script>\n", wp_json_encode( $args ) );