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' ) );
98 public function tablenav( $which = 'top' ) {
99 if ( $this->get_pagination_arg( 'total_pages' ) <= 1 )
102 <div class="tablenav themes <?php echo $which; ?>">
103 <?php $this->pagination( $which ); ?>
104 <span class="spinner"></span>
110 public function display() {
111 wp_nonce_field( "fetch-list-" . get_class( $this ), '_ajax_fetch_list_nonce' );
113 <?php $this->tablenav( 'top' ); ?>
115 <div id="availablethemes">
116 <?php $this->display_rows_or_placeholder(); ?>
119 <?php $this->tablenav( 'bottom' ); ?>
123 public function get_columns() {
127 public function display_rows_or_placeholder() {
128 if ( $this->has_items() ) {
129 $this->display_rows();
131 echo '<div class="no-items">';
137 public function display_rows() {
138 $themes = $this->items;
140 foreach ( $themes as $theme ):
141 ?><div class="available-theme"><?php
143 $template = $theme->get_template();
144 $stylesheet = $theme->get_stylesheet();
145 $title = $theme->display('Name');
146 $version = $theme->display('Version');
147 $author = $theme->display('Author');
149 $activate_link = wp_nonce_url( "themes.php?action=activate&template=" . urlencode( $template ) . "&stylesheet=" . urlencode( $stylesheet ), 'switch-theme_' . $stylesheet );
151 $preview_link = esc_url( add_query_arg(
152 array( 'preview' => 1, 'template' => urlencode( $template ), 'stylesheet' => urlencode( $stylesheet ), 'preview_iframe' => true, 'TB_iframe' => 'true' ),
156 $actions['activate'] = '<a href="' . $activate_link . '" class="activatelink" title="'
157 . esc_attr( sprintf( __( 'Activate “%s”' ), $title ) ) . '">' . __( 'Activate' ) . '</a>';
159 $actions['preview'] = '<a href="' . $preview_link . '" class="hide-if-customize" title="'
160 . esc_attr( sprintf( __( 'Preview “%s”' ), $title ) ) . '">' . __( 'Preview' ) . '</a>';
162 if ( current_user_can( 'edit_theme_options' ) && current_user_can( 'customize' ) ) {
163 $actions['preview'] .= '<a href="' . wp_customize_url( $stylesheet ) . '" class="load-customize hide-if-no-customize">'
164 . __( 'Live Preview' ) . '</a>';
167 if ( ! is_multisite() && current_user_can( 'delete_themes' ) )
168 $actions['delete'] = '<a class="submitdelete deletion" href="' . wp_nonce_url( 'themes.php?action=delete&stylesheet=' . urlencode( $stylesheet ), 'delete-theme_' . $stylesheet )
169 . '" onclick="' . "return confirm( '" . esc_js( sprintf( __( "You are about to delete this theme '%s'\n 'Cancel' to stop, 'OK' to delete." ), $title ) )
170 . "' );" . '">' . __( 'Delete' ) . '</a>';
172 /** This filter is documented in wp-admin/includes/class-wp-ms-themes-list-table.php */
173 $actions = apply_filters( 'theme_action_links', $actions, $theme );
175 /** This filter is documented in wp-admin/includes/class-wp-ms-themes-list-table.php */
176 $actions = apply_filters( "theme_action_links_$stylesheet", $actions, $theme );
177 $delete_action = isset( $actions['delete'] ) ? '<div class="delete-theme">' . $actions['delete'] . '</div>' : '';
178 unset( $actions['delete'] );
182 <a href="<?php echo $preview_link; ?>" class="screenshot hide-if-customize">
183 <?php if ( $screenshot = $theme->get_screenshot() ) : ?>
184 <img src="<?php echo esc_url( $screenshot ); ?>" alt="" />
187 <a href="<?php echo wp_customize_url( $stylesheet ); ?>" class="screenshot load-customize hide-if-no-customize">
188 <?php if ( $screenshot = $theme->get_screenshot() ) : ?>
189 <img src="<?php echo esc_url( $screenshot ); ?>" alt="" />
193 <h3><?php echo $title; ?></h3>
194 <div class="theme-author"><?php printf( __( 'By %s' ), $author ); ?></div>
195 <div class="action-links">
197 <?php foreach ( $actions as $action ): ?>
198 <li><?php echo $action; ?></li>
200 <li class="hide-if-no-js"><a href="#" class="theme-detail"><?php _e('Details') ?></a></li>
202 <?php echo $delete_action; ?>
204 <?php theme_update_available( $theme ); ?>
207 <div class="themedetaildiv hide-if-js">
208 <p><strong><?php _e('Version: '); ?></strong><?php echo $version; ?></p>
209 <p><?php echo $theme->display('Description'); ?></p>
210 <?php if ( $theme->parent() ) {
211 printf( ' <p class="howto">' . __( 'This <a href="%1$s">child theme</a> requires its parent theme, %2$s.' ) . '</p>',
212 __( 'http://codex.wordpress.org/Child_Themes' ),
213 $theme->parent()->display( 'Name' ) );
222 public function search_theme( $theme ) {
223 // Search the features
224 foreach ( $this->features as $word ) {
225 if ( ! in_array( $word, $theme->get('Tags') ) )
230 foreach ( $this->search_terms as $word ) {
231 if ( in_array( $word, $theme->get('Tags') ) )
234 foreach ( array( 'Name', 'Description', 'Author', 'AuthorURI' ) as $header ) {
235 // Don't mark up; Do translate.
236 if ( false !== stripos( strip_tags( $theme->display( $header, false, true ) ), $word ) ) {
241 if ( false !== stripos( $theme->get_stylesheet(), $word ) )
244 if ( false !== stripos( $theme->get_template(), $word ) )
254 * Send required variables to JavaScript land
259 * @uses $this->features Array of all feature search terms.
260 * @uses get_pagenum()
261 * @uses _pagination_args['total_pages']
263 public function _js_vars( $extra_args = array() ) {
264 $search_string = isset( $_REQUEST['s'] ) ? esc_attr( wp_unslash( $_REQUEST['s'] ) ) : '';
267 'search' => $search_string,
268 'features' => $this->features,
269 'paged' => $this->get_pagenum(),
270 'total_pages' => ! empty( $this->_pagination_args['total_pages'] ) ? $this->_pagination_args['total_pages'] : 1,
273 if ( is_array( $extra_args ) )
274 $args = array_merge( $args, $extra_args );
276 printf( "<script type='text/javascript'>var theme_list_args = %s;</script>\n", json_encode( $args ) );