3 * Themes List Table class.
6 * @subpackage List_Table
10 class WP_Themes_List_Table extends WP_List_Table {
12 var $search = array();
13 var $features = array();
15 function ajax_user_can() {
16 // Do not check edit_theme_options here. AJAX calls for available themes require switch_themes.
17 return current_user_can('switch_themes');
20 function prepare_items() {
23 $ct = current_theme_info();
25 $themes = get_allowed_themes();
27 if ( ! empty( $_REQUEST['s'] ) ) {
28 $search = strtolower( stripslashes( $_REQUEST['s'] ) );
29 $this->search = array_merge( $this->search, array_filter( array_map( 'trim', explode( ',', $search ) ) ) );
30 $this->search = array_unique( $this->search );
33 if ( !empty( $_REQUEST['features'] ) ) {
34 $this->features = $_REQUEST['features'];
35 $this->features = array_map( 'trim', $this->features );
36 $this->features = array_map( 'sanitize_title_with_dashes', $this->features );
37 $this->features = array_unique( $this->features );
40 if ( $this->search || $this->features ) {
41 foreach ( $themes as $key => $theme ) {
42 if ( !$this->search_theme( $theme ) )
43 unset( $themes[ $key ] );
47 unset( $themes[$ct->name] );
48 uksort( $themes, "strnatcasecmp" );
51 $page = $this->get_pagenum();
53 $start = ( $page - 1 ) * $per_page;
55 $this->items = array_slice( $themes, $start, $per_page );
57 $this->set_pagination_args( array(
58 'total_items' => count( $themes ),
59 'per_page' => $per_page,
64 if ( $this->search || $this->features ) {
65 _e( 'No items found.' );
69 if ( is_multisite() ) {
70 if ( current_user_can( 'install_themes' ) && current_user_can( 'manage_network_themes' ) ) {
71 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' ) );
74 } elseif ( current_user_can( 'manage_network_themes' ) ) {
75 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'] ) );
79 // else, fallthrough. install_themes doesn't help if you can't enable it.
81 if ( current_user_can( 'install_themes' ) ) {
82 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' ) );
88 printf( __( 'Only the current theme is available to you. Contact the %s administrator for information about accessing additional themes.' ), get_site_option( 'site_name' ) );
91 function tablenav( $which = 'top' ) {
92 if ( $this->get_pagination_arg( 'total_pages' ) <= 1 )
95 <div class="tablenav <?php echo $which; ?>">
96 <?php $this->pagination( $which ); ?>
97 <img src="<?php echo esc_url( admin_url( 'images/wpspin_light.gif' ) ); ?>" class="ajax-loading list-ajax-loading" alt="" />
104 // wp_nonce_field( "fetch-list-" . get_class( $this ), '_ajax_fetch_list_nonce' );
106 <?php $this->tablenav( 'top' ); ?>
108 <table id="availablethemes" cellspacing="0" cellpadding="0">
109 <tbody id="the-list" class="list:themes">
110 <?php $this->display_rows_or_placeholder(); ?>
114 <?php $this->tablenav( 'bottom' ); ?>
118 function get_columns() {
122 function display_rows() {
123 $themes = $this->items;
124 $theme_names = array_keys( $themes );
125 natcasesort( $theme_names );
128 $rows = ceil( count( $theme_names ) / 3 );
129 for ( $row = 1; $row <= $rows; $row++ )
130 for ( $col = 1; $col <= 3; $col++ )
131 $table[$row][$col] = array_shift( $theme_names );
133 foreach ( $table as $row => $cols ) {
137 foreach ( $cols as $col => $theme_name ) {
138 $class = array( 'available-theme' );
139 if ( $row == 1 ) $class[] = 'top';
140 if ( $col == 1 ) $class[] = 'left';
141 if ( $row == $rows ) $class[] = 'bottom';
142 if ( $col == 3 ) $class[] = 'right';
144 <td class="<?php echo join( ' ', $class ); ?>">
145 <?php if ( !empty( $theme_name ) ) :
146 $template = $themes[$theme_name]['Template'];
147 $stylesheet = $themes[$theme_name]['Stylesheet'];
148 $title = $themes[$theme_name]['Title'];
149 $version = $themes[$theme_name]['Version'];
150 $description = $themes[$theme_name]['Description'];
151 $author = $themes[$theme_name]['Author'];
152 $screenshot = $themes[$theme_name]['Screenshot'];
153 $stylesheet_dir = $themes[$theme_name]['Stylesheet Dir'];
154 $template_dir = $themes[$theme_name]['Template Dir'];
155 $parent_theme = $themes[$theme_name]['Parent Theme'];
156 $theme_root = $themes[$theme_name]['Theme Root'];
157 $theme_root_uri = $themes[$theme_name]['Theme Root URI'];
158 $preview_link = esc_url( get_option( 'home' ) . '/' );
160 $preview_link = str_replace( 'http://', 'https://', $preview_link );
161 $preview_link = htmlspecialchars( add_query_arg( array( 'preview' => 1, 'template' => $template, 'stylesheet' => $stylesheet, 'preview_iframe' => true, 'TB_iframe' => 'true' ), $preview_link ) );
162 $preview_text = esc_attr( sprintf( __( 'Preview of “%s”' ), $title ) );
163 $tags = $themes[$theme_name]['Tags'];
164 $thickbox_class = 'thickbox thickbox-preview';
165 $activate_link = wp_nonce_url( "themes.php?action=activate&template=".urlencode( $template )."&stylesheet=".urlencode( $stylesheet ), 'switch-theme_' . $template );
166 $activate_text = esc_attr( sprintf( __( 'Activate “%s”' ), $title ) );
168 $actions[] = '<a href="' . $activate_link . '" class="activatelink" title="' . $activate_text . '">' . __( 'Activate' ) . '</a>';
169 $actions[] = '<a href="' . $preview_link . '" class="thickbox thickbox-preview" title="' . esc_attr( sprintf( __( 'Preview “%s”' ), $theme_name ) ) . '">' . __( 'Preview' ) . '</a>';
170 if ( ! is_multisite() && current_user_can( 'delete_themes' ) )
171 $actions[] = '<a class="submitdelete deletion" href="' . wp_nonce_url( "themes.php?action=delete&template=$stylesheet", 'delete-theme_' . $stylesheet ) . '" onclick="' . "return confirm( '" . esc_js( sprintf( __( "You are about to delete this theme '%s'\n 'Cancel' to stop, 'OK' to delete." ), $theme_name ) ) . "' );" . '">' . __( 'Delete' ) . '</a>';
172 $actions = apply_filters( 'theme_action_links', $actions, $themes[$theme_name] );
174 $actions = implode ( ' | ', $actions );
176 <a href="<?php echo $preview_link; ?>" class="<?php echo $thickbox_class; ?> screenshot">
177 <?php if ( $screenshot ) : ?>
178 <img src="<?php echo $theme_root_uri . '/' . $stylesheet . '/' . $screenshot; ?>" alt="" />
182 /* translators: 1: theme title, 2: theme version, 3: theme author */
183 printf( __( '%1$s %2$s by %3$s' ), $title, $version, $author ) ; ?></h3>
184 <p class="description"><?php echo $description; ?></p>
185 <span class='action-links'><?php echo $actions ?></span>
186 <?php if ( current_user_can( 'edit_themes' ) && $parent_theme ) {
187 /* translators: 1: theme title, 2: template dir, 3: stylesheet_dir, 4: theme title, 5: parent_theme */ ?>
188 <p><?php printf( __( 'The template files are located in <code>%2$s</code>. The stylesheet files are located in <code>%3$s</code>. <strong>%4$s</strong> uses templates from <strong>%5$s</strong>. Changes made to the templates will affect both themes.' ), $title, str_replace( WP_CONTENT_DIR, '', $template_dir ), str_replace( WP_CONTENT_DIR, '', $stylesheet_dir ), $title, $parent_theme ); ?></p>
190 <p><?php printf( __( 'All of this theme’s files are located in <code>%2$s</code>.' ), $title, str_replace( WP_CONTENT_DIR, '', $template_dir ), str_replace( WP_CONTENT_DIR, '', $stylesheet_dir ) ); ?></p>
192 <?php if ( $tags ) : ?>
193 <p><?php _e( 'Tags:' ); ?> <?php echo join( ', ', $tags ); ?></p>
195 <?php theme_update_available( $themes[$theme_name] ); ?>
196 <?php endif; // end if not empty theme_name ?>
198 <?php } // end foreach $cols ?>
200 <?php } // end foreach $table
203 function search_theme( $theme ) {
207 if ( count( $this->search ) > 0 ) {
208 foreach ( $this->search as $word ) {
212 if ( in_array( $word, array_map( 'sanitize_title_with_dashes', $theme['Tags'] ) ) )
215 // In one of the fields?
216 foreach ( array( 'Name', 'Title', 'Description', 'Author', 'Template', 'Stylesheet' ) AS $field ) {
217 if ( stripos( $theme[$field], $word ) !== false )
226 // Now search the features
227 if ( count( $this->features ) > 0 ) {
228 foreach ( $this->features as $word ) {
230 if ( !in_array( $word, array_map( 'sanitize_title_with_dashes', $theme['Tags'] ) ) )
235 // Only get here if each word exists in the tags or one of the fields