3 * List Table API: WP_Theme_Install_List_Table class
6 * @subpackage Administration
11 * Core class used to implement displaying themes to install in a list table.
16 * @see WP_Themes_List_Table
18 class WP_Theme_Install_List_Table extends WP_Themes_List_Table {
20 public $features = array();
26 public function ajax_user_can() {
27 return current_user_can( 'install_themes' );
35 * @global string $type
36 * @global array $theme_field_defaults
38 public function prepare_items() {
39 include( ABSPATH . 'wp-admin/includes/theme-install.php' );
41 global $tabs, $tab, $paged, $type, $theme_field_defaults;
42 wp_reset_vars( array( 'tab' ) );
44 $search_terms = array();
46 if ( ! empty( $_REQUEST['s'] ) ){
47 $search_string = strtolower( wp_unslash( $_REQUEST['s'] ) );
48 $search_terms = array_unique( array_filter( array_map( 'trim', explode( ',', $search_string ) ) ) );
51 if ( ! empty( $_REQUEST['features'] ) )
52 $this->features = $_REQUEST['features'];
54 $paged = $this->get_pagenum();
58 // These are the tabs which are shown on the page,
60 $tabs['dashboard'] = __( 'Search' );
61 if ( 'search' === $tab )
62 $tabs['search'] = __( 'Search Results' );
63 $tabs['upload'] = __( 'Upload' );
64 $tabs['featured'] = _x( 'Featured', 'themes' );
65 //$tabs['popular'] = _x( 'Popular', 'themes' );
66 $tabs['new'] = _x( 'Latest', 'themes' );
67 $tabs['updated'] = _x( 'Recently Updated', 'themes' );
69 $nonmenu_tabs = array( 'theme-information' ); // Valid actions to perform which do not have a Menu item.
71 /** This filter is documented in wp-admin/theme-install.php */
72 $tabs = apply_filters( 'install_themes_tabs', $tabs );
75 * Filters tabs not associated with a menu item on the Install Themes screen.
79 * @param array $nonmenu_tabs The tabs that don't have a menu item on
80 * the Install Themes screen.
82 $nonmenu_tabs = apply_filters( 'install_themes_nonmenu_tabs', $nonmenu_tabs );
84 // If a non-valid menu tab has been selected, And it's not a non-menu action.
85 if ( empty( $tab ) || ( ! isset( $tabs[ $tab ] ) && ! in_array( $tab, (array) $nonmenu_tabs ) ) )
88 $args = array( 'page' => $paged, 'per_page' => $per_page, 'fields' => $theme_field_defaults );
92 $type = isset( $_REQUEST['type'] ) ? wp_unslash( $_REQUEST['type'] ) : 'term';
95 $args['tag'] = array_map( 'sanitize_key', $search_terms );
98 $args['search'] = $search_string;
101 $args['author'] = $search_string;
105 if ( ! empty( $this->features ) ) {
106 $args['tag'] = $this->features;
107 $_REQUEST['s'] = implode( ',', $this->features );
108 $_REQUEST['type'] = 'tag';
111 add_action( 'install_themes_table_header', 'install_theme_search_form', 10, 0 );
118 $args['browse'] = $tab;
127 * Filters API request arguments for each Install Themes screen tab.
129 * The dynamic portion of the hook name, `$tab`, refers to the theme install
130 * tabs. Default tabs are 'dashboard', 'search', 'upload', 'featured',
131 * 'new', and 'updated'.
135 * @param array $args An array of themes API arguments.
137 $args = apply_filters( "install_themes_table_api_args_{$tab}", $args );
142 $api = themes_api( 'query_themes', $args );
144 if ( is_wp_error( $api ) )
145 wp_die( $api->get_error_message() . '</p> <p><a href="#" onclick="document.location.reload(); return false;">' . __( 'Try again' ) . '</a>' );
147 $this->items = $api->themes;
149 $this->set_pagination_args( array(
150 'total_items' => $api->info['results'],
151 'per_page' => $args['per_page'],
152 'infinite_scroll' => true,
159 public function no_items() {
160 _e( 'No themes match your request.' );
165 * @global array $tabs
166 * @global string $tab
169 protected function get_views() {
172 $display_tabs = array();
173 foreach ( (array) $tabs as $action => $text ) {
174 $class = ( $action === $tab ) ? ' class="current"' : '';
175 $href = self_admin_url('theme-install.php?tab=' . $action);
176 $display_tabs['theme-install-'.$action] = "<a href='$href'$class>$text</a>";
179 return $display_tabs;
185 public function display() {
186 wp_nonce_field( "fetch-list-" . get_class( $this ), '_ajax_fetch_list_nonce' );
188 <div class="tablenav top themes">
189 <div class="alignleft actions">
192 * Fires in the Install Themes list table header.
196 do_action( 'install_themes_table_header' );
199 <?php $this->pagination( 'top' ); ?>
203 <div id="availablethemes">
204 <?php $this->display_rows_or_placeholder(); ?>
208 $this->tablenav( 'bottom' );
214 public function display_rows() {
215 $themes = $this->items;
216 foreach ( $themes as $theme ) {
218 <div class="available-theme installable-theme"><?php
219 $this->single_row( $theme );
221 <?php } // end foreach $theme_names
223 $this->theme_installer();
227 * Prints a theme from the WordPress.org API.
232 * @global array $themes_allowedtags
234 * @param object $theme {
235 * An object that contains theme data returned by the WordPress.org API.
237 * @type string $name Theme name, e.g. 'Twenty Seventeen'.
238 * @type string $slug Theme slug, e.g. 'twentyseventeen'.
239 * @type string $version Theme version, e.g. '1.1'.
240 * @type string $author Theme author username, e.g. 'melchoyce'.
241 * @type string $preview_url Preview URL, e.g. 'http://2017.wordpress.net/'.
242 * @type string $screenshot_url Screenshot URL, e.g. 'https://wordpress.org/themes/twentyseventeen/'.
243 * @type float $rating Rating score.
244 * @type int $num_ratings The number of ratings.
245 * @type string $homepage Theme homepage, e.g. 'https://wordpress.org/themes/twentyseventeen/'.
246 * @type string $description Theme description.
247 * @type string $download_link Theme ZIP download URL.
250 public function single_row( $theme ) {
251 global $themes_allowedtags;
253 if ( empty( $theme ) )
256 $name = wp_kses( $theme->name, $themes_allowedtags );
257 $author = wp_kses( $theme->author, $themes_allowedtags );
259 $preview_title = sprintf( __('Preview “%s”'), $name );
260 $preview_url = add_query_arg( array(
261 'tab' => 'theme-information',
262 'theme' => $theme->slug,
263 ), self_admin_url( 'theme-install.php' ) );
267 $install_url = add_query_arg( array(
268 'action' => 'install-theme',
269 'theme' => $theme->slug,
270 ), self_admin_url( 'update.php' ) );
272 $update_url = add_query_arg( array(
273 'action' => 'upgrade-theme',
274 'theme' => $theme->slug,
275 ), self_admin_url( 'update.php' ) );
277 $status = $this->_get_theme_status( $theme );
280 case 'update_available':
281 $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>';
283 case 'newer_installed':
284 case 'latest_installed':
285 $actions[] = '<span class="install-now" title="' . esc_attr__( 'This theme is already installed and is up to date' ) . '">' . _x( 'Installed', 'theme' ) . '</span>';
289 $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>';
293 $actions[] = '<a class="install-theme-preview" href="' . esc_url( $preview_url ) . '" title="' . esc_attr( sprintf( __( 'Preview %s' ), $name ) ) . '">' . __( 'Preview' ) . '</a>';
296 * Filters the install action links for a theme in the Install Themes list table.
300 * @param array $actions An array of theme action hyperlinks. Defaults are
301 * links to Install Now, Preview, and Details.
302 * @param WP_Theme $theme Theme object.
304 $actions = apply_filters( 'theme_install_actions', $actions, $theme );
307 <a class="screenshot install-theme-preview" href="<?php echo esc_url( $preview_url ); ?>" title="<?php echo esc_attr( $preview_title ); ?>">
308 <img src="<?php echo esc_url( $theme->screenshot_url ); ?>" width="150" alt="" />
311 <h3><?php echo $name; ?></h3>
312 <div class="theme-author"><?php printf( __( 'By %s' ), $author ); ?></div>
314 <div class="action-links">
316 <?php foreach ( $actions as $action ): ?>
317 <li><?php echo $action; ?></li>
319 <li class="hide-if-no-js"><a href="#" class="theme-detail"><?php _e('Details') ?></a></li>
324 $this->install_theme_info( $theme );
328 * Prints the wrapper for the theme installer.
330 public function theme_installer() {
332 <div id="theme-installer" class="wp-full-overlay expanded">
333 <div class="wp-full-overlay-sidebar">
334 <div class="wp-full-overlay-header">
335 <a href="#" class="close-full-overlay button"><?php _e( 'Close' ); ?></a>
336 <span class="theme-install"></span>
338 <div class="wp-full-overlay-sidebar-content">
339 <div class="install-theme-info"></div>
341 <div class="wp-full-overlay-footer">
342 <button type="button" class="collapse-sidebar button" aria-expanded="true" aria-label="<?php esc_attr_e( 'Collapse Sidebar' ); ?>">
343 <span class="collapse-sidebar-arrow"></span>
344 <span class="collapse-sidebar-label"><?php _e( 'Collapse' ); ?></span>
348 <div class="wp-full-overlay-main"></div>
354 * Prints the wrapper for the theme installer with a provided theme's data.
355 * Used to make the theme installer work for no-js.
357 * @param object $theme - A WordPress.org Theme API object.
359 public function theme_installer_single( $theme ) {
361 <div id="theme-installer" class="wp-full-overlay single-theme">
362 <div class="wp-full-overlay-sidebar">
363 <?php $this->install_theme_info( $theme ); ?>
365 <div class="wp-full-overlay-main">
366 <iframe src="<?php echo esc_url( $theme->preview_url ); ?>"></iframe>
373 * Prints the info for a theme (to be used in the theme installer modal).
375 * @global array $themes_allowedtags
377 * @param object $theme - A WordPress.org Theme API object.
379 public function install_theme_info( $theme ) {
380 global $themes_allowedtags;
382 if ( empty( $theme ) )
385 $name = wp_kses( $theme->name, $themes_allowedtags );
386 $author = wp_kses( $theme->author, $themes_allowedtags );
388 $install_url = add_query_arg( array(
389 'action' => 'install-theme',
390 'theme' => $theme->slug,
391 ), self_admin_url( 'update.php' ) );
393 $update_url = add_query_arg( array(
394 'action' => 'upgrade-theme',
395 'theme' => $theme->slug,
396 ), self_admin_url( 'update.php' ) );
398 $status = $this->_get_theme_status( $theme );
401 <div class="install-theme-info"><?php
403 case 'update_available':
404 echo '<a class="theme-install button 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>';
406 case 'newer_installed':
407 case 'latest_installed':
408 echo '<span class="theme-install" title="' . esc_attr__( 'This theme is already installed and is up to date' ) . '">' . _x( 'Installed', 'theme' ) . '</span>';
412 echo '<a class="theme-install button button-primary" href="' . esc_url( wp_nonce_url( $install_url, 'install-theme_' . $theme->slug ) ) . '">' . __( 'Install' ) . '</a>';
415 <h3 class="theme-name"><?php echo $name; ?></h3>
416 <span class="theme-by"><?php printf( __( 'By %s' ), $author ); ?></span>
417 <?php if ( isset( $theme->screenshot_url ) ): ?>
418 <img class="theme-screenshot" src="<?php echo esc_url( $theme->screenshot_url ); ?>" alt="" />
420 <div class="theme-details">
421 <?php wp_star_rating( array( 'rating' => $theme->rating, 'type' => 'percent', 'number' => $theme->num_ratings ) ); ?>
422 <div class="theme-version">
423 <strong><?php _e('Version:') ?> </strong>
424 <?php echo wp_kses( $theme->version, $themes_allowedtags ); ?>
426 <div class="theme-description">
427 <?php echo wp_kses( $theme->description, $themes_allowedtags ); ?>
430 <input class="theme-preview-url" type="hidden" value="<?php echo esc_url( $theme->preview_url ); ?>" />
436 * Send required variables to JavaScript land
441 * @global string $tab Current tab within Themes->Install screen
442 * @global string $type Type of search.
444 * @param array $extra_args Unused.
446 public function _js_vars( $extra_args = array() ) {
448 parent::_js_vars( compact( 'tab', 'type' ) );
452 * Check to see if the theme is already installed.
457 * @param object $theme - A WordPress.org Theme API object.
458 * @return string Theme status.
460 private function _get_theme_status( $theme ) {
463 $installed_theme = wp_get_theme( $theme->slug );
464 if ( $installed_theme->exists() ) {
465 if ( version_compare( $installed_theme->get('Version'), $theme->version, '=' ) )
466 $status = 'latest_installed';
467 elseif ( version_compare( $installed_theme->get('Version'), $theme->version, '>' ) )
468 $status = 'newer_installed';
470 $status = 'update_available';