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.
52 * Filter the tabs shown on the Install Themes screen.
56 * @param array $tabs The tabs shown on the Install Themes screen. Defaults are
57 * 'dashboard', 'search', 'upload', 'featured', 'new', and 'updated'.
59 $tabs = apply_filters( 'install_themes_tabs', $tabs );
62 * Filter tabs not associated with a menu item on the Install Themes screen.
66 * @param array $nonmenu_tabs The tabs that don't have a menu item on
67 * the Install Themes screen.
69 $nonmenu_tabs = apply_filters( 'install_themes_nonmenu_tabs', $nonmenu_tabs );
71 // If a non-valid menu tab has been selected, And it's not a non-menu action.
72 if ( empty( $tab ) || ( ! isset( $tabs[ $tab ] ) && ! in_array( $tab, (array) $nonmenu_tabs ) ) )
75 $args = array( 'page' => $paged, 'per_page' => $per_page, 'fields' => $theme_field_defaults );
79 $type = isset( $_REQUEST['type'] ) ? wp_unslash( $_REQUEST['type'] ) : 'term';
82 $args['tag'] = array_map( 'sanitize_key', $search_terms );
85 $args['search'] = $search_string;
88 $args['author'] = $search_string;
92 if ( ! empty( $this->features ) ) {
93 $args['tag'] = $this->features;
94 $_REQUEST['s'] = implode( ',', $this->features );
95 $_REQUEST['type'] = 'tag';
98 add_action( 'install_themes_table_header', 'install_theme_search_form', 10, 0 );
105 $args['browse'] = $tab;
114 * Filter API request arguments for each Install Themes screen tab.
116 * The dynamic portion of the hook name, $tab, refers to the theme install
117 * tabs. Default tabs are 'dashboard', 'search', 'upload', 'featured',
118 * 'new', and 'updated'.
122 * @param array $args An array of themes API arguments.
124 $args = apply_filters( 'install_themes_table_api_args_' . $tab, $args );
129 $api = themes_api( 'query_themes', $args );
131 if ( is_wp_error( $api ) )
132 wp_die( $api->get_error_message() . '</p> <p><a href="#" onclick="document.location.reload(); return false;">' . __( 'Try again' ) . '</a>' );
134 $this->items = $api->themes;
136 $this->set_pagination_args( array(
137 'total_items' => $api->info['results'],
138 'per_page' => $args['per_page'],
139 'infinite_scroll' => true,
143 function no_items() {
144 _e( 'No themes match your request.' );
147 function get_views() {
150 $display_tabs = array();
151 foreach ( (array) $tabs as $action => $text ) {
152 $class = ( $action == $tab ) ? ' class="current"' : '';
153 $href = self_admin_url('theme-install.php?tab=' . $action);
154 $display_tabs['theme-install-'.$action] = "<a href='$href'$class>$text</a>";
157 return $display_tabs;
161 wp_nonce_field( "fetch-list-" . get_class( $this ), '_ajax_fetch_list_nonce' );
163 <div class="tablenav top themes">
164 <div class="alignleft actions">
167 * Fires in the Install Themes list table header.
171 do_action( 'install_themes_table_header' );
174 <?php $this->pagination( 'top' ); ?>
178 <div id="availablethemes">
179 <?php $this->display_rows_or_placeholder(); ?>
183 parent::tablenav( 'bottom' );
186 function display_rows() {
187 $themes = $this->items;
188 foreach ( $themes as $theme ) {
190 <div class="available-theme installable-theme"><?php
191 $this->single_row( $theme );
193 <?php } // end foreach $theme_names
195 $this->theme_installer();
199 * Prints a theme from the WordPress.org API.
201 * @param object $theme An object that contains theme data returned by the WordPress.org API.
203 * Example theme data:
204 * object(stdClass)[59]
205 * public 'name' => string 'Magazine Basic'
206 * public 'slug' => string 'magazine-basic'
207 * public 'version' => string '1.1'
208 * public 'author' => string 'tinkerpriest'
209 * public 'preview_url' => string 'http://wp-themes.com/?magazine-basic'
210 * public 'screenshot_url' => string 'http://wp-themes.com/wp-content/themes/magazine-basic/screenshot.png'
211 * public 'rating' => float 80
212 * public 'num_ratings' => int 1
213 * public 'homepage' => string 'http://wordpress.org/themes/magazine-basic'
214 * 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>.'
215 * public 'download_link' => string 'http://wordpress.org/themes/download/magazine-basic.1.1.zip'
217 function single_row( $theme ) {
218 global $themes_allowedtags;
220 if ( empty( $theme ) )
223 $name = wp_kses( $theme->name, $themes_allowedtags );
224 $author = wp_kses( $theme->author, $themes_allowedtags );
226 $preview_title = sprintf( __('Preview “%s”'), $name );
227 $preview_url = add_query_arg( array(
228 'tab' => 'theme-information',
229 'theme' => $theme->slug,
230 ), self_admin_url( 'theme-install.php' ) );
234 $install_url = add_query_arg( array(
235 'action' => 'install-theme',
236 'theme' => $theme->slug,
237 ), self_admin_url( 'update.php' ) );
239 $update_url = add_query_arg( array(
240 'action' => 'upgrade-theme',
241 'theme' => $theme->slug,
242 ), self_admin_url( 'update.php' ) );
244 $status = $this->_get_theme_status( $theme );
249 $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>';
251 case 'update_available':
252 $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>';
254 case 'newer_installed':
255 case 'latest_installed':
256 $actions[] = '<span class="install-now" title="' . esc_attr__( 'This theme is already installed and is up to date' ) . '">' . _x( 'Installed', 'theme' ) . '</span>';
260 $actions[] = '<a class="install-theme-preview" href="' . esc_url( $preview_url ) . '" title="' . esc_attr( sprintf( __( 'Preview %s' ), $name ) ) . '">' . __( 'Preview' ) . '</a>';
263 * Filter the install action links for a theme in the Install Themes list table.
267 * @param array $actions An array of theme action hyperlinks. Defaults are
268 * links to Install Now, Preview, and Details.
269 * @param WP_Theme $theme Theme object.
271 $actions = apply_filters( 'theme_install_actions', $actions, $theme );
274 <a class="screenshot install-theme-preview" href="<?php echo esc_url( $preview_url ); ?>" title="<?php echo esc_attr( $preview_title ); ?>">
275 <img src='<?php echo esc_url( $theme->screenshot_url ); ?>' width='150' />
278 <h3><?php echo $name; ?></h3>
279 <div class="theme-author"><?php printf( __( 'By %s' ), $author ); ?></div>
281 <div class="action-links">
283 <?php foreach ( $actions as $action ): ?>
284 <li><?php echo $action; ?></li>
286 <li class="hide-if-no-js"><a href="#" class="theme-detail"><?php _e('Details') ?></a></li>
291 $this->install_theme_info( $theme );
295 * Prints the wrapper for the theme installer.
297 function theme_installer() {
299 <div id="theme-installer" class="wp-full-overlay expanded">
300 <div class="wp-full-overlay-sidebar">
301 <div class="wp-full-overlay-header">
302 <a href="#" class="close-full-overlay button-secondary"><?php _e( 'Close' ); ?></a>
303 <span class="theme-install"></span>
305 <div class="wp-full-overlay-sidebar-content">
306 <div class="install-theme-info"></div>
308 <div class="wp-full-overlay-footer">
309 <a href="#" class="collapse-sidebar" title="<?php esc_attr_e('Collapse Sidebar'); ?>">
310 <span class="collapse-sidebar-label"><?php _e('Collapse'); ?></span>
311 <span class="collapse-sidebar-arrow"></span>
315 <div class="wp-full-overlay-main"></div>
321 * Prints the wrapper for the theme installer with a provided theme's data.
322 * Used to make the theme installer work for no-js.
324 * @param object $theme - A WordPress.org Theme API object.
326 function theme_installer_single( $theme ) {
328 <div id="theme-installer" class="wp-full-overlay single-theme">
329 <div class="wp-full-overlay-sidebar">
330 <?php $this->install_theme_info( $theme ); ?>
332 <div class="wp-full-overlay-main">
333 <iframe src="<?php echo esc_url( $theme->preview_url ); ?>"></iframe>
340 * Prints the info for a theme (to be used in the theme installer modal).
342 * @param object $theme - A WordPress.org Theme API object.
344 function install_theme_info( $theme ) {
345 global $themes_allowedtags;
347 if ( empty( $theme ) )
350 $name = wp_kses( $theme->name, $themes_allowedtags );
351 $author = wp_kses( $theme->author, $themes_allowedtags );
353 $num_ratings = sprintf( _n( '(based on %s rating)', '(based on %s ratings)', $theme->num_ratings ), number_format_i18n( $theme->num_ratings ) );
355 $install_url = add_query_arg( array(
356 'action' => 'install-theme',
357 'theme' => $theme->slug,
358 ), self_admin_url( 'update.php' ) );
360 $update_url = add_query_arg( array(
361 'action' => 'upgrade-theme',
362 'theme' => $theme->slug,
363 ), self_admin_url( 'update.php' ) );
365 $status = $this->_get_theme_status( $theme );
368 <div class="install-theme-info"><?php
372 echo '<a class="theme-install button-primary" href="' . esc_url( wp_nonce_url( $install_url, 'install-theme_' . $theme->slug ) ) . '">' . __( 'Install' ) . '</a>';
374 case 'update_available':
375 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>';
377 case 'newer_installed':
378 case 'latest_installed':
379 echo '<span class="theme-install" title="' . esc_attr__( 'This theme is already installed and is up to date' ) . '">' . _x( 'Installed', 'theme' ) . '</span>';
382 <h3 class="theme-name"><?php echo $name; ?></h3>
383 <span class="theme-by"><?php printf( __( 'By %s' ), $author ); ?></span>
384 <?php if ( isset( $theme->screenshot_url ) ): ?>
385 <img class="theme-screenshot" src="<?php echo esc_url( $theme->screenshot_url ); ?>" />
387 <div class="theme-details">
388 <?php wp_star_rating( array( 'rating' => $theme->rating, 'type' => 'percent', 'number' => $theme->num_ratings ) ); ?>
389 <div class="theme-version">
390 <strong><?php _e('Version:') ?> </strong>
391 <?php echo wp_kses( $theme->version, $themes_allowedtags ); ?>
393 <div class="theme-description">
394 <?php echo wp_kses( $theme->description, $themes_allowedtags ); ?>
397 <input class="theme-preview-url" type="hidden" value="<?php echo esc_url( $theme->preview_url ); ?>" />
403 * Send required variables to JavaScript land
408 * @uses $tab Global; current tab within Themes->Install screen
409 * @uses $type Global; type of search.
411 function _js_vars( $extra_args = array() ) {
413 parent::_js_vars( compact( 'tab', 'type' ) );
417 * Check to see if the theme is already installed.
422 * @param object $theme - A WordPress.org Theme API object.
423 * @return string Theme status.
425 private function _get_theme_status( $theme ) {
428 $installed_theme = wp_get_theme( $theme->slug );
429 if ( $installed_theme->exists() ) {
430 if ( version_compare( $installed_theme->get('Version'), $theme->version, '=' ) )
431 $status = 'latest_installed';
432 elseif ( version_compare( $installed_theme->get('Version'), $theme->version, '>' ) )
433 $status = 'newer_installed';
435 $status = 'update_available';