3 * Theme Installer List Table class.
6 * @subpackage List_Table
10 class WP_Theme_Install_List_Table extends WP_Themes_List_Table {
12 public $features = array();
18 public function ajax_user_can() {
19 return current_user_can( 'install_themes' );
27 * @global string $type
28 * @global array $theme_field_defaults
30 public function prepare_items() {
31 include( ABSPATH . 'wp-admin/includes/theme-install.php' );
33 global $tabs, $tab, $paged, $type, $theme_field_defaults;
34 wp_reset_vars( array( 'tab' ) );
36 $search_terms = array();
38 if ( ! empty( $_REQUEST['s'] ) ){
39 $search_string = strtolower( wp_unslash( $_REQUEST['s'] ) );
40 $search_terms = array_unique( array_filter( array_map( 'trim', explode( ',', $search_string ) ) ) );
43 if ( ! empty( $_REQUEST['features'] ) )
44 $this->features = $_REQUEST['features'];
46 $paged = $this->get_pagenum();
50 // These are the tabs which are shown on the page,
52 $tabs['dashboard'] = __( 'Search' );
53 if ( 'search' == $tab )
54 $tabs['search'] = __( 'Search Results' );
55 $tabs['upload'] = __( 'Upload' );
56 $tabs['featured'] = _x( 'Featured', 'themes' );
57 //$tabs['popular'] = _x( 'Popular', 'themes' );
58 $tabs['new'] = _x( 'Latest', 'themes' );
59 $tabs['updated'] = _x( 'Recently Updated', 'themes' );
61 $nonmenu_tabs = array( 'theme-information' ); // Valid actions to perform which do not have a Menu item.
63 /** This filter is documented in wp-admin/theme-install.php */
64 $tabs = apply_filters( 'install_themes_tabs', $tabs );
67 * Filter tabs not associated with a menu item on the Install Themes screen.
71 * @param array $nonmenu_tabs The tabs that don't have a menu item on
72 * the Install Themes screen.
74 $nonmenu_tabs = apply_filters( 'install_themes_nonmenu_tabs', $nonmenu_tabs );
76 // If a non-valid menu tab has been selected, And it's not a non-menu action.
77 if ( empty( $tab ) || ( ! isset( $tabs[ $tab ] ) && ! in_array( $tab, (array) $nonmenu_tabs ) ) )
80 $args = array( 'page' => $paged, 'per_page' => $per_page, 'fields' => $theme_field_defaults );
84 $type = isset( $_REQUEST['type'] ) ? wp_unslash( $_REQUEST['type'] ) : 'term';
87 $args['tag'] = array_map( 'sanitize_key', $search_terms );
90 $args['search'] = $search_string;
93 $args['author'] = $search_string;
97 if ( ! empty( $this->features ) ) {
98 $args['tag'] = $this->features;
99 $_REQUEST['s'] = implode( ',', $this->features );
100 $_REQUEST['type'] = 'tag';
103 add_action( 'install_themes_table_header', 'install_theme_search_form', 10, 0 );
110 $args['browse'] = $tab;
119 * Filter API request arguments for each Install Themes screen tab.
121 * The dynamic portion of the hook name, `$tab`, refers to the theme install
122 * tabs. Default tabs are 'dashboard', 'search', 'upload', 'featured',
123 * 'new', and 'updated'.
127 * @param array $args An array of themes API arguments.
129 $args = apply_filters( 'install_themes_table_api_args_' . $tab, $args );
134 $api = themes_api( 'query_themes', $args );
136 if ( is_wp_error( $api ) )
137 wp_die( $api->get_error_message() . '</p> <p><a href="#" onclick="document.location.reload(); return false;">' . __( 'Try again' ) . '</a>' );
139 $this->items = $api->themes;
141 $this->set_pagination_args( array(
142 'total_items' => $api->info['results'],
143 'per_page' => $args['per_page'],
144 'infinite_scroll' => true,
151 public function no_items() {
152 _e( 'No themes match your request.' );
157 * @global array $tabs
158 * @global string $tab
161 protected function get_views() {
164 $display_tabs = array();
165 foreach ( (array) $tabs as $action => $text ) {
166 $class = ( $action == $tab ) ? ' class="current"' : '';
167 $href = self_admin_url('theme-install.php?tab=' . $action);
168 $display_tabs['theme-install-'.$action] = "<a href='$href'$class>$text</a>";
171 return $display_tabs;
177 public function display() {
178 wp_nonce_field( "fetch-list-" . get_class( $this ), '_ajax_fetch_list_nonce' );
180 <div class="tablenav top themes">
181 <div class="alignleft actions">
184 * Fires in the Install Themes list table header.
188 do_action( 'install_themes_table_header' );
191 <?php $this->pagination( 'top' ); ?>
195 <div id="availablethemes">
196 <?php $this->display_rows_or_placeholder(); ?>
200 $this->tablenav( 'bottom' );
206 public function display_rows() {
207 $themes = $this->items;
208 foreach ( $themes as $theme ) {
210 <div class="available-theme installable-theme"><?php
211 $this->single_row( $theme );
213 <?php } // end foreach $theme_names
215 $this->theme_installer();
219 * Prints a theme from the WordPress.org API.
221 * @global array $themes_allowedtags
223 * @param object $theme An object that contains theme data returned by the WordPress.org API.
225 * Example theme data:
226 * object(stdClass)[59]
227 * public 'name' => string 'Magazine Basic'
228 * public 'slug' => string 'magazine-basic'
229 * public 'version' => string '1.1'
230 * public 'author' => string 'tinkerpriest'
231 * public 'preview_url' => string 'http://wp-themes.com/?magazine-basic'
232 * public 'screenshot_url' => string 'http://wp-themes.com/wp-content/themes/magazine-basic/screenshot.png'
233 * public 'rating' => float 80
234 * public 'num_ratings' => int 1
235 * public 'homepage' => string 'http://wordpress.org/themes/magazine-basic'
236 * 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>.'
237 * public 'download_link' => string 'http://wordpress.org/themes/download/magazine-basic.1.1.zip'
239 public function single_row( $theme ) {
240 global $themes_allowedtags;
242 if ( empty( $theme ) )
245 $name = wp_kses( $theme->name, $themes_allowedtags );
246 $author = wp_kses( $theme->author, $themes_allowedtags );
248 $preview_title = sprintf( __('Preview “%s”'), $name );
249 $preview_url = add_query_arg( array(
250 'tab' => 'theme-information',
251 'theme' => $theme->slug,
252 ), self_admin_url( 'theme-install.php' ) );
256 $install_url = add_query_arg( array(
257 'action' => 'install-theme',
258 'theme' => $theme->slug,
259 ), self_admin_url( 'update.php' ) );
261 $update_url = add_query_arg( array(
262 'action' => 'upgrade-theme',
263 'theme' => $theme->slug,
264 ), self_admin_url( 'update.php' ) );
266 $status = $this->_get_theme_status( $theme );
269 case 'update_available':
270 $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>';
272 case 'newer_installed':
273 case 'latest_installed':
274 $actions[] = '<span class="install-now" title="' . esc_attr__( 'This theme is already installed and is up to date' ) . '">' . _x( 'Installed', 'theme' ) . '</span>';
278 $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>';
282 $actions[] = '<a class="install-theme-preview" href="' . esc_url( $preview_url ) . '" title="' . esc_attr( sprintf( __( 'Preview %s' ), $name ) ) . '">' . __( 'Preview' ) . '</a>';
285 * Filter the install action links for a theme in the Install Themes list table.
289 * @param array $actions An array of theme action hyperlinks. Defaults are
290 * links to Install Now, Preview, and Details.
291 * @param WP_Theme $theme Theme object.
293 $actions = apply_filters( 'theme_install_actions', $actions, $theme );
296 <a class="screenshot install-theme-preview" href="<?php echo esc_url( $preview_url ); ?>" title="<?php echo esc_attr( $preview_title ); ?>">
297 <img src="<?php echo esc_url( $theme->screenshot_url ); ?>" width="150" />
300 <h3><?php echo $name; ?></h3>
301 <div class="theme-author"><?php printf( __( 'By %s' ), $author ); ?></div>
303 <div class="action-links">
305 <?php foreach ( $actions as $action ): ?>
306 <li><?php echo $action; ?></li>
308 <li class="hide-if-no-js"><a href="#" class="theme-detail"><?php _e('Details') ?></a></li>
313 $this->install_theme_info( $theme );
317 * Prints the wrapper for the theme installer.
319 public function theme_installer() {
321 <div id="theme-installer" class="wp-full-overlay expanded">
322 <div class="wp-full-overlay-sidebar">
323 <div class="wp-full-overlay-header">
324 <a href="#" class="close-full-overlay button-secondary"><?php _e( 'Close' ); ?></a>
325 <span class="theme-install"></span>
327 <div class="wp-full-overlay-sidebar-content">
328 <div class="install-theme-info"></div>
330 <div class="wp-full-overlay-footer">
331 <button type="button" class="collapse-sidebar button-secondary" aria-expanded="true" aria-label="<?php esc_attr_e( 'Collapse Sidebar' ); ?>">
332 <span class="collapse-sidebar-arrow"></span>
333 <span class="collapse-sidebar-label"><?php _e( 'Collapse' ); ?></span>
337 <div class="wp-full-overlay-main"></div>
343 * Prints the wrapper for the theme installer with a provided theme's data.
344 * Used to make the theme installer work for no-js.
346 * @param object $theme - A WordPress.org Theme API object.
348 public function theme_installer_single( $theme ) {
350 <div id="theme-installer" class="wp-full-overlay single-theme">
351 <div class="wp-full-overlay-sidebar">
352 <?php $this->install_theme_info( $theme ); ?>
354 <div class="wp-full-overlay-main">
355 <iframe src="<?php echo esc_url( $theme->preview_url ); ?>"></iframe>
362 * Prints the info for a theme (to be used in the theme installer modal).
364 * @global array $themes_allowedtags
366 * @param object $theme - A WordPress.org Theme API object.
368 public function install_theme_info( $theme ) {
369 global $themes_allowedtags;
371 if ( empty( $theme ) )
374 $name = wp_kses( $theme->name, $themes_allowedtags );
375 $author = wp_kses( $theme->author, $themes_allowedtags );
377 $install_url = add_query_arg( array(
378 'action' => 'install-theme',
379 'theme' => $theme->slug,
380 ), self_admin_url( 'update.php' ) );
382 $update_url = add_query_arg( array(
383 'action' => 'upgrade-theme',
384 'theme' => $theme->slug,
385 ), self_admin_url( 'update.php' ) );
387 $status = $this->_get_theme_status( $theme );
390 <div class="install-theme-info"><?php
392 case 'update_available':
393 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>';
395 case 'newer_installed':
396 case 'latest_installed':
397 echo '<span class="theme-install" title="' . esc_attr__( 'This theme is already installed and is up to date' ) . '">' . _x( 'Installed', 'theme' ) . '</span>';
401 echo '<a class="theme-install button-primary" href="' . esc_url( wp_nonce_url( $install_url, 'install-theme_' . $theme->slug ) ) . '">' . __( 'Install' ) . '</a>';
404 <h3 class="theme-name"><?php echo $name; ?></h3>
405 <span class="theme-by"><?php printf( __( 'By %s' ), $author ); ?></span>
406 <?php if ( isset( $theme->screenshot_url ) ): ?>
407 <img class="theme-screenshot" src="<?php echo esc_url( $theme->screenshot_url ); ?>" />
409 <div class="theme-details">
410 <?php wp_star_rating( array( 'rating' => $theme->rating, 'type' => 'percent', 'number' => $theme->num_ratings ) ); ?>
411 <div class="theme-version">
412 <strong><?php _e('Version:') ?> </strong>
413 <?php echo wp_kses( $theme->version, $themes_allowedtags ); ?>
415 <div class="theme-description">
416 <?php echo wp_kses( $theme->description, $themes_allowedtags ); ?>
419 <input class="theme-preview-url" type="hidden" value="<?php echo esc_url( $theme->preview_url ); ?>" />
425 * Send required variables to JavaScript land
430 * @global string $tab Current tab within Themes->Install screen
431 * @global string $type Type of search.
433 * @param array $extra_args Unused.
435 public function _js_vars( $extra_args = array() ) {
437 parent::_js_vars( compact( 'tab', 'type' ) );
441 * Check to see if the theme is already installed.
446 * @param object $theme - A WordPress.org Theme API object.
447 * @return string Theme status.
449 private function _get_theme_status( $theme ) {
452 $installed_theme = wp_get_theme( $theme->slug );
453 if ( $installed_theme->exists() ) {
454 if ( version_compare( $installed_theme->get('Version'), $theme->version, '=' ) )
455 $status = 'latest_installed';
456 elseif ( version_compare( $installed_theme->get('Version'), $theme->version, '>' ) )
457 $status = 'newer_installed';
459 $status = 'update_available';