3 * Theme Installer List Table class.
6 * @subpackage List_Table
10 class WP_Theme_Install_List_Table extends WP_Themes_List_Table {
12 var $features = array();
14 function ajax_user_can() {
15 return current_user_can( 'install_themes' );
18 function prepare_items() {
19 include( ABSPATH . 'wp-admin/includes/theme-install.php' );
21 global $tabs, $tab, $paged, $type, $theme_field_defaults;
22 wp_reset_vars( array( 'tab' ) );
24 $search_terms = array();
26 if ( ! empty( $_REQUEST['s'] ) ){
27 $search_string = strtolower( wp_unslash( $_REQUEST['s'] ) );
28 $search_terms = array_unique( array_filter( array_map( 'trim', explode( ',', $search_string ) ) ) );
31 if ( ! empty( $_REQUEST['features'] ) )
32 $this->features = $_REQUEST['features'];
34 $paged = $this->get_pagenum();
38 // These are the tabs which are shown on the page,
40 $tabs['dashboard'] = __( 'Search' );
41 if ( 'search' == $tab )
42 $tabs['search'] = __( 'Search Results' );
43 $tabs['upload'] = __( 'Upload' );
44 $tabs['featured'] = _x( 'Featured','Theme Installer' );
45 //$tabs['popular'] = _x( 'Popular','Theme Installer' );
46 $tabs['new'] = _x( 'Newest','Theme Installer' );
47 $tabs['updated'] = _x( 'Recently Updated','Theme Installer' );
49 $nonmenu_tabs = array( 'theme-information' ); // Valid actions to perform which do not have a Menu item.
51 $tabs = apply_filters( 'install_themes_tabs', $tabs );
52 $nonmenu_tabs = apply_filters( 'install_themes_nonmenu_tabs', $nonmenu_tabs );
54 // If a non-valid menu tab has been selected, And it's not a non-menu action.
55 if ( empty( $tab ) || ( ! isset( $tabs[ $tab ] ) && ! in_array( $tab, (array) $nonmenu_tabs ) ) )
58 $args = array( 'page' => $paged, 'per_page' => $per_page, 'fields' => $theme_field_defaults );
62 $type = isset( $_REQUEST['type'] ) ? wp_unslash( $_REQUEST['type'] ) : 'term';
65 $args['tag'] = array_map( 'sanitize_key', $search_terms );
68 $args['search'] = $search_string;
71 $args['author'] = $search_string;
75 if ( ! empty( $this->features ) ) {
76 $args['tag'] = $this->features;
77 $_REQUEST['s'] = implode( ',', $this->features );
78 $_REQUEST['type'] = 'tag';
81 add_action( 'install_themes_table_header', 'install_theme_search_form', 10, 0 );
88 $args['browse'] = $tab;
98 $api = themes_api( 'query_themes', $args );
100 if ( is_wp_error( $api ) )
101 wp_die( $api->get_error_message() . '</p> <p><a href="#" onclick="document.location.reload(); return false;">' . __( 'Try again' ) . '</a>' );
103 $this->items = $api->themes;
105 $this->set_pagination_args( array(
106 'total_items' => $api->info['results'],
107 'per_page' => $per_page,
108 'infinite_scroll' => true,
112 function no_items() {
113 _e( 'No themes match your request.' );
116 function get_views() {
119 $display_tabs = array();
120 foreach ( (array) $tabs as $action => $text ) {
121 $class = ( $action == $tab ) ? ' class="current"' : '';
122 $href = self_admin_url('theme-install.php?tab=' . $action);
123 $display_tabs['theme-install-'.$action] = "<a href='$href'$class>$text</a>";
126 return $display_tabs;
130 wp_nonce_field( "fetch-list-" . get_class( $this ), '_ajax_fetch_list_nonce' );
132 <div class="tablenav top themes">
133 <div class="alignleft actions">
134 <?php do_action( 'install_themes_table_header' ); ?>
136 <?php $this->pagination( 'top' ); ?>
140 <div id="availablethemes">
141 <?php $this->display_rows_or_placeholder(); ?>
145 parent::tablenav( 'bottom' );
148 function display_rows() {
149 $themes = $this->items;
150 foreach ( $themes as $theme ) {
152 <div class="available-theme installable-theme"><?php
153 $this->single_row( $theme );
155 <?php } // end foreach $theme_names
157 $this->theme_installer();
161 * Prints a theme from the WordPress.org API.
163 * @param object $theme An object that contains theme data returned by the WordPress.org API.
165 * Example theme data:
166 * object(stdClass)[59]
167 * public 'name' => string 'Magazine Basic'
168 * public 'slug' => string 'magazine-basic'
169 * public 'version' => string '1.1'
170 * public 'author' => string 'tinkerpriest'
171 * public 'preview_url' => string 'http://wp-themes.com/?magazine-basic'
172 * public 'screenshot_url' => string 'http://wp-themes.com/wp-content/themes/magazine-basic/screenshot.png'
173 * public 'rating' => float 80
174 * public 'num_ratings' => int 1
175 * public 'homepage' => string 'http://wordpress.org/themes/magazine-basic'
176 * public 'description' => string 'A basic magazine style layout with a fully customizable layout through a backend interface. Designed by <a href="http://bavotasan.com">c.bavota</a> of <a href="http://tinkerpriestmedia.com">Tinker Priest Media</a>.'
177 * public 'download_link' => string 'http://wordpress.org/themes/download/magazine-basic.1.1.zip'
179 function single_row( $theme ) {
180 global $themes_allowedtags;
182 if ( empty( $theme ) )
185 $name = wp_kses( $theme->name, $themes_allowedtags );
186 $author = wp_kses( $theme->author, $themes_allowedtags );
188 $preview_title = sprintf( __('Preview “%s”'), $name );
189 $preview_url = add_query_arg( array(
190 'tab' => 'theme-information',
191 'theme' => $theme->slug,
196 $install_url = add_query_arg( array(
197 'action' => 'install-theme',
198 'theme' => $theme->slug,
199 ), self_admin_url( 'update.php' ) );
201 $update_url = add_query_arg( array(
202 'action' => 'upgrade-theme',
203 'theme' => $theme->slug,
204 ), self_admin_url( 'update.php' ) );
206 $status = $this->_get_theme_status( $theme );
211 $actions[] = '<a class="install-now" href="' . esc_url( wp_nonce_url( $install_url, 'install-theme_' . $theme->slug ) ) . '" title="' . esc_attr( sprintf( __( 'Install %s' ), $name ) ) . '">' . __( 'Install Now' ) . '</a>';
213 case 'update_available':
214 $actions[] = '<a class="install-now" href="' . esc_url( wp_nonce_url( $update_url, 'upgrade-theme_' . $theme->slug ) ) . '" title="' . esc_attr( sprintf( __( 'Update to version %s' ), $theme->version ) ) . '">' . __( 'Update' ) . '</a>';
216 case 'newer_installed':
217 case 'latest_installed':
218 $actions[] = '<span class="install-now" title="' . esc_attr__( 'This theme is already installed and is up to date' ) . '">' . _x( 'Installed', 'theme' ) . '</span>';
222 $actions[] = '<a class="install-theme-preview" href="' . esc_url( $preview_url ) . '" title="' . esc_attr( sprintf( __( 'Preview %s' ), $name ) ) . '">' . __( 'Preview' ) . '</a>';
224 $actions = apply_filters( 'theme_install_actions', $actions, $theme );
227 <a class="screenshot install-theme-preview" href="<?php echo esc_url( $preview_url ); ?>" title="<?php echo esc_attr( $preview_title ); ?>">
228 <img src='<?php echo esc_url( $theme->screenshot_url ); ?>' width='150' />
231 <h3><?php echo $name; ?></h3>
232 <div class="theme-author"><?php printf( __( 'By %s' ), $author ); ?></div>
234 <div class="action-links">
236 <?php foreach ( $actions as $action ): ?>
237 <li><?php echo $action; ?></li>
239 <li class="hide-if-no-js"><a href="#" class="theme-detail"><?php _e('Details') ?></a></li>
244 $this->install_theme_info( $theme );
248 * Prints the wrapper for the theme installer.
250 function theme_installer() {
252 <div id="theme-installer" class="wp-full-overlay expanded">
253 <div class="wp-full-overlay-sidebar">
254 <div class="wp-full-overlay-header">
255 <a href="#" class="close-full-overlay"><?php _e( '← Close' ); ?></a>
257 <div class="wp-full-overlay-sidebar-content">
258 <div class="install-theme-info"></div>
260 <div class="wp-full-overlay-footer">
261 <a href="#" class="collapse-sidebar button-secondary" title="<?php esc_attr_e('Collapse Sidebar'); ?>">
262 <span class="collapse-sidebar-label"><?php _e('Collapse'); ?></span>
263 <span class="collapse-sidebar-arrow"></span>
267 <div class="wp-full-overlay-main"></div>
273 * Prints the wrapper for the theme installer with a provided theme's data.
274 * Used to make the theme installer work for no-js.
276 * @param object $theme - A WordPress.org Theme API object.
278 function theme_installer_single( $theme ) {
280 <div id="theme-installer" class="wp-full-overlay single-theme">
281 <div class="wp-full-overlay-sidebar">
282 <?php $this->install_theme_info( $theme ); ?>
284 <div class="wp-full-overlay-main">
285 <iframe src="<?php echo esc_url( $theme->preview_url ); ?>"></iframe>
292 * Prints the info for a theme (to be used in the theme installer modal).
294 * @param object $theme - A WordPress.org Theme API object.
296 function install_theme_info( $theme ) {
297 global $themes_allowedtags;
299 if ( empty( $theme ) )
302 $name = wp_kses( $theme->name, $themes_allowedtags );
303 $author = wp_kses( $theme->author, $themes_allowedtags );
305 $num_ratings = sprintf( _n( '(based on %s rating)', '(based on %s ratings)', $theme->num_ratings ), number_format_i18n( $theme->num_ratings ) );
307 $install_url = add_query_arg( array(
308 'action' => 'install-theme',
309 'theme' => $theme->slug,
310 ), self_admin_url( 'update.php' ) );
312 $update_url = add_query_arg( array(
313 'action' => 'upgrade-theme',
314 'theme' => $theme->slug,
315 ), self_admin_url( 'update.php' ) );
317 $status = $this->_get_theme_status( $theme );
320 <div class="install-theme-info"><?php
324 echo '<a class="theme-install button-primary" href="' . esc_url( wp_nonce_url( $install_url, 'install-theme_' . $theme->slug ) ) . '">' . __( 'Install' ) . '</a>';
326 case 'update_available':
327 echo '<a class="theme-install button-primary" href="' . esc_url( wp_nonce_url( $update_url, 'upgrade-theme_' . $theme->slug ) ) . '" title="' . esc_attr( sprintf( __( 'Update to version %s' ), $theme->version ) ) . '">' . __( 'Update' ) . '</a>';
329 case 'newer_installed':
330 case 'latest_installed':
331 echo '<span class="theme-install" title="' . esc_attr__( 'This theme is already installed and is up to date' ) . '">' . _x( 'Installed', 'theme' ) . '</span>';
334 <h3 class="theme-name"><?php echo $name; ?></h3>
335 <span class="theme-by"><?php printf( __( 'By %s' ), $author ); ?></span>
336 <?php if ( isset( $theme->screenshot_url ) ): ?>
337 <img class="theme-screenshot" src="<?php echo esc_url( $theme->screenshot_url ); ?>" />
339 <div class="theme-details">
340 <div class="star-holder" title="<?php echo esc_attr( $num_ratings ); ?>">
341 <div class="star-rating" style="width:<?php echo esc_attr( intval( $theme->rating ) . 'px' ); ?>;"></div>
343 <div class="theme-version">
344 <strong><?php _e('Version:') ?> </strong>
345 <?php echo wp_kses( $theme->version, $themes_allowedtags ); ?>
347 <div class="theme-description">
348 <?php echo wp_kses( $theme->description, $themes_allowedtags ); ?>
351 <input class="theme-preview-url" type="hidden" value="<?php echo esc_url( $theme->preview_url ); ?>" />
357 * Send required variables to JavaScript land
362 * @uses $tab Global; current tab within Themes->Install screen
363 * @uses $type Global; type of search.
365 function _js_vars( $extra_args = array() ) {
367 parent::_js_vars( compact( 'tab', 'type' ) );
371 * Check to see if the theme is already installed.
376 * @param object $theme - A WordPress.org Theme API object.
377 * @return string Theme status.
379 private function _get_theme_status( $theme ) {
382 $installed_theme = wp_get_theme( $theme->slug );
383 if ( $installed_theme->exists() ) {
384 if ( version_compare( $installed_theme->get('Version'), $theme->version, '=' ) )
385 $status = 'latest_installed';
386 elseif ( version_compare( $installed_theme->get('Version'), $theme->version, '>' ) )
387 $status = 'newer_installed';
389 $status = 'update_available';