3 * Upgrade API: Theme_Upgrader class
11 * Core class used for upgrading/installing themes.
13 * It is designed to upgrade/install themes from a local zip, remote zip URL,
14 * or uploaded zip file.
17 * @since 4.6.0 Moved to its own file from wp-admin/includes/class-wp-upgrader.php.
21 class Theme_Upgrader extends WP_Upgrader {
24 * Result of the theme upgrade offer.
28 * @var array|WP_Error $result
29 * @see WP_Upgrader::$result
34 * Whether multiple themes are being upgraded/installed in bulk.
43 * Initialize the upgrade strings.
48 public function upgrade_strings() {
49 $this->strings['up_to_date'] = __('The theme is at the latest version.');
50 $this->strings['no_package'] = __('Update package not available.');
51 $this->strings['downloading_package'] = __('Downloading update from <span class="code">%s</span>…');
52 $this->strings['unpack_package'] = __('Unpacking the update…');
53 $this->strings['remove_old'] = __('Removing the old version of the theme…');
54 $this->strings['remove_old_failed'] = __('Could not remove the old theme.');
55 $this->strings['process_failed'] = __('Theme update failed.');
56 $this->strings['process_success'] = __('Theme updated successfully.');
60 * Initialize the install strings.
65 public function install_strings() {
66 $this->strings['no_package'] = __('Install package not available.');
67 $this->strings['downloading_package'] = __('Downloading install package from <span class="code">%s</span>…');
68 $this->strings['unpack_package'] = __('Unpacking the package…');
69 $this->strings['installing_package'] = __('Installing the theme…');
70 $this->strings['no_files'] = __('The theme contains no files.');
71 $this->strings['process_failed'] = __('Theme install failed.');
72 $this->strings['process_success'] = __('Theme installed successfully.');
73 /* translators: 1: theme name, 2: version */
74 $this->strings['process_success_specific'] = __('Successfully installed the theme <strong>%1$s %2$s</strong>.');
75 $this->strings['parent_theme_search'] = __('This theme requires a parent theme. Checking if it is installed…');
76 /* translators: 1: theme name, 2: version */
77 $this->strings['parent_theme_prepare_install'] = __('Preparing to install <strong>%1$s %2$s</strong>…');
78 /* translators: 1: theme name, 2: version */
79 $this->strings['parent_theme_currently_installed'] = __('The parent theme, <strong>%1$s %2$s</strong>, is currently installed.');
80 /* translators: 1: theme name, 2: version */
81 $this->strings['parent_theme_install_success'] = __('Successfully installed the parent theme, <strong>%1$s %2$s</strong>.');
82 $this->strings['parent_theme_not_found'] = __('<strong>The parent theme could not be found.</strong> You will need to install the parent theme, <strong>%s</strong>, before you can use this child theme.');
86 * Check if a child theme is being installed and we need to install its parent.
88 * Hooked to the {@see 'upgrader_post_install'} filter by Theme_Upgrader::install().
93 * @param bool $install_result
94 * @param array $hook_extra
95 * @param array $child_result
98 public function check_parent_theme_filter( $install_result, $hook_extra, $child_result ) {
99 // Check to see if we need to install a parent theme
100 $theme_info = $this->theme_info();
102 if ( ! $theme_info->parent() )
103 return $install_result;
105 $this->skin->feedback( 'parent_theme_search' );
107 if ( ! $theme_info->parent()->errors() ) {
108 $this->skin->feedback( 'parent_theme_currently_installed', $theme_info->parent()->display('Name'), $theme_info->parent()->display('Version') );
109 // We already have the theme, fall through.
110 return $install_result;
113 // We don't have the parent theme, let's install it.
114 $api = themes_api('theme_information', array('slug' => $theme_info->get('Template'), 'fields' => array('sections' => false, 'tags' => false) ) ); //Save on a bit of bandwidth.
116 if ( ! $api || is_wp_error($api) ) {
117 $this->skin->feedback( 'parent_theme_not_found', $theme_info->get('Template') );
118 // Don't show activate or preview actions after install
119 add_filter('install_theme_complete_actions', array($this, 'hide_activate_preview_actions') );
120 return $install_result;
123 // Backup required data we're going to override:
124 $child_api = $this->skin->api;
125 $child_success_message = $this->strings['process_success'];
128 $this->skin->api = $api;
129 $this->strings['process_success_specific'] = $this->strings['parent_theme_install_success'];//, $api->name, $api->version);
131 $this->skin->feedback('parent_theme_prepare_install', $api->name, $api->version);
133 add_filter('install_theme_complete_actions', '__return_false', 999); // Don't show any actions after installing the theme.
135 // Install the parent theme
136 $parent_result = $this->run( array(
137 'package' => $api->download_link,
138 'destination' => get_theme_root(),
139 'clear_destination' => false, //Do not overwrite files.
140 'clear_working' => true
143 if ( is_wp_error($parent_result) )
144 add_filter('install_theme_complete_actions', array($this, 'hide_activate_preview_actions') );
146 // Start cleaning up after the parents installation
147 remove_filter('install_theme_complete_actions', '__return_false', 999);
149 // Reset child's result and data
150 $this->result = $child_result;
151 $this->skin->api = $child_api;
152 $this->strings['process_success'] = $child_success_message;
154 return $install_result;
158 * Don't display the activate and preview actions to the user.
160 * Hooked to the {@see 'install_theme_complete_actions'} filter by
161 * Theme_Upgrader::check_parent_theme_filter() when installing
162 * a child theme and installing the parent theme fails.
167 * @param array $actions Preview actions.
170 public function hide_activate_preview_actions( $actions ) {
171 unset($actions['activate'], $actions['preview']);
176 * Install a theme package.
179 * @since 3.7.0 The `$args` parameter was added, making clearing the update cache optional.
182 * @param string $package The full local path or URI of the package.
183 * @param array $args {
184 * Optional. Other arguments for installing a theme package. Default empty array.
186 * @type bool $clear_update_cache Whether to clear the updates cache if successful.
190 * @return bool|WP_Error True if the install was successful, false or a WP_Error object otherwise.
192 public function install( $package, $args = array() ) {
195 'clear_update_cache' => true,
197 $parsed_args = wp_parse_args( $args, $defaults );
200 $this->install_strings();
202 add_filter('upgrader_source_selection', array($this, 'check_package') );
203 add_filter('upgrader_post_install', array($this, 'check_parent_theme_filter'), 10, 3);
204 // Clear cache so wp_update_themes() knows about the new theme.
205 add_action( 'upgrader_process_complete', 'wp_clean_themes_cache', 9, 0 );
208 'package' => $package,
209 'destination' => get_theme_root(),
210 'clear_destination' => false, //Do not overwrite files.
211 'clear_working' => true,
212 'hook_extra' => array(
214 'action' => 'install',
218 remove_action( 'upgrader_process_complete', 'wp_clean_themes_cache', 9 );
219 remove_filter('upgrader_source_selection', array($this, 'check_package') );
220 remove_filter('upgrader_post_install', array($this, 'check_parent_theme_filter'));
222 if ( ! $this->result || is_wp_error($this->result) )
223 return $this->result;
225 // Refresh the Theme Update information
226 wp_clean_themes_cache( $parsed_args['clear_update_cache'] );
235 * @since 3.7.0 The `$args` parameter was added, making clearing the update cache optional.
238 * @param string $theme The theme slug.
239 * @param array $args {
240 * Optional. Other arguments for upgrading a theme. Default empty array.
242 * @type bool $clear_update_cache Whether to clear the update cache if successful.
245 * @return bool|WP_Error True if the upgrade was successful, false or a WP_Error object otherwise.
247 public function upgrade( $theme, $args = array() ) {
250 'clear_update_cache' => true,
252 $parsed_args = wp_parse_args( $args, $defaults );
255 $this->upgrade_strings();
257 // Is an update available?
258 $current = get_site_transient( 'update_themes' );
259 if ( !isset( $current->response[ $theme ] ) ) {
260 $this->skin->before();
261 $this->skin->set_result(false);
262 $this->skin->error( 'up_to_date' );
263 $this->skin->after();
267 $r = $current->response[ $theme ];
269 add_filter('upgrader_pre_install', array($this, 'current_before'), 10, 2);
270 add_filter('upgrader_post_install', array($this, 'current_after'), 10, 2);
271 add_filter('upgrader_clear_destination', array($this, 'delete_old_theme'), 10, 4);
272 // Clear cache so wp_update_themes() knows about the new theme.
273 add_action( 'upgrader_process_complete', 'wp_clean_themes_cache', 9, 0 );
276 'package' => $r['package'],
277 'destination' => get_theme_root( $theme ),
278 'clear_destination' => true,
279 'clear_working' => true,
280 'hook_extra' => array(
283 'action' => 'update',
287 remove_action( 'upgrader_process_complete', 'wp_clean_themes_cache', 9 );
288 remove_filter('upgrader_pre_install', array($this, 'current_before'));
289 remove_filter('upgrader_post_install', array($this, 'current_after'));
290 remove_filter('upgrader_clear_destination', array($this, 'delete_old_theme'));
292 if ( ! $this->result || is_wp_error($this->result) )
293 return $this->result;
295 wp_clean_themes_cache( $parsed_args['clear_update_cache'] );
301 * Upgrade several themes at once.
304 * @since 3.7.0 The `$args` parameter was added, making clearing the update cache optional.
307 * @param array $themes The theme slugs.
308 * @param array $args {
309 * Optional. Other arguments for upgrading several themes at once. Default empty array.
311 * @type bool $clear_update_cache Whether to clear the update cache if successful.
314 * @return array[]|false An array of results, or false if unable to connect to the filesystem.
316 public function bulk_upgrade( $themes, $args = array() ) {
319 'clear_update_cache' => true,
321 $parsed_args = wp_parse_args( $args, $defaults );
325 $this->upgrade_strings();
327 $current = get_site_transient( 'update_themes' );
329 add_filter('upgrader_pre_install', array($this, 'current_before'), 10, 2);
330 add_filter('upgrader_post_install', array($this, 'current_after'), 10, 2);
331 add_filter('upgrader_clear_destination', array($this, 'delete_old_theme'), 10, 4);
332 // Clear cache so wp_update_themes() knows about the new theme.
333 add_action( 'upgrader_process_complete', 'wp_clean_themes_cache', 9, 0 );
335 $this->skin->header();
337 // Connect to the Filesystem first.
338 $res = $this->fs_connect( array(WP_CONTENT_DIR) );
340 $this->skin->footer();
344 $this->skin->bulk_header();
346 // Only start maintenance mode if:
347 // - running Multisite and there are one or more themes specified, OR
348 // - a theme with an update available is currently in use.
349 // @TODO: For multisite, maintenance mode should only kick in for individual sites if at all possible.
350 $maintenance = ( is_multisite() && ! empty( $themes ) );
351 foreach ( $themes as $theme )
352 $maintenance = $maintenance || $theme == get_stylesheet() || $theme == get_template();
354 $this->maintenance_mode(true);
358 $this->update_count = count($themes);
359 $this->update_current = 0;
360 foreach ( $themes as $theme ) {
361 $this->update_current++;
363 $this->skin->theme_info = $this->theme_info($theme);
365 if ( !isset( $current->response[ $theme ] ) ) {
366 $this->skin->set_result(true);
367 $this->skin->before();
368 $this->skin->feedback( 'up_to_date' );
369 $this->skin->after();
370 $results[$theme] = true;
374 // Get the URL to the zip file
375 $r = $current->response[ $theme ];
377 $result = $this->run( array(
378 'package' => $r['package'],
379 'destination' => get_theme_root( $theme ),
380 'clear_destination' => true,
381 'clear_working' => true,
383 'hook_extra' => array(
388 $results[$theme] = $this->result;
390 // Prevent credentials auth screen from displaying multiple times
391 if ( false === $result )
393 } //end foreach $plugins
395 $this->maintenance_mode(false);
397 /** This action is documented in wp-admin/includes/class-wp-upgrader.php */
398 do_action( 'upgrader_process_complete', $this, array(
399 'action' => 'update',
405 $this->skin->bulk_footer();
407 $this->skin->footer();
409 // Cleanup our hooks, in case something else does a upgrade on this connection.
410 remove_action( 'upgrader_process_complete', 'wp_clean_themes_cache', 9 );
411 remove_filter('upgrader_pre_install', array($this, 'current_before'));
412 remove_filter('upgrader_post_install', array($this, 'current_after'));
413 remove_filter('upgrader_clear_destination', array($this, 'delete_old_theme'));
415 // Refresh the Theme Update information
416 wp_clean_themes_cache( $parsed_args['clear_update_cache'] );
422 * Check that the package source contains a valid theme.
424 * Hooked to the {@see 'upgrader_source_selection'} filter by Theme_Upgrader::install().
425 * It will return an error if the theme doesn't have style.css or index.php
431 * @global WP_Filesystem_Base $wp_filesystem Subclass
433 * @param string $source The full path to the package source.
434 * @return string|WP_Error The source or a WP_Error.
436 public function check_package( $source ) {
437 global $wp_filesystem;
439 if ( is_wp_error($source) )
442 // Check the folder contains a valid theme
443 $working_directory = str_replace( $wp_filesystem->wp_content_dir(), trailingslashit(WP_CONTENT_DIR), $source);
444 if ( ! is_dir($working_directory) ) // Sanity check, if the above fails, let's not prevent installation.
447 // A proper archive should have a style.css file in the single subdirectory
448 if ( ! file_exists( $working_directory . 'style.css' ) ) {
449 return new WP_Error( 'incompatible_archive_theme_no_style', $this->strings['incompatible_archive'],
450 /* translators: %s: style.css */
451 sprintf( __( 'The theme is missing the %s stylesheet.' ),
452 '<code>style.css</code>'
457 $info = get_file_data( $working_directory . 'style.css', array( 'Name' => 'Theme Name', 'Template' => 'Template' ) );
459 if ( empty( $info['Name'] ) ) {
460 return new WP_Error( 'incompatible_archive_theme_no_name', $this->strings['incompatible_archive'],
461 /* translators: %s: style.css */
462 sprintf( __( 'The %s stylesheet doesn’t contain a valid theme header.' ),
463 '<code>style.css</code>'
468 // If it's not a child theme, it must have at least an index.php to be legit.
469 if ( empty( $info['Template'] ) && ! file_exists( $working_directory . 'index.php' ) ) {
470 return new WP_Error( 'incompatible_archive_theme_no_index', $this->strings['incompatible_archive'],
471 /* translators: %s: index.php */
472 sprintf( __( 'The theme is missing the %s file.' ),
473 '<code>index.php</code>'
482 * Turn on maintenance mode before attempting to upgrade the current theme.
484 * Hooked to the {@see 'upgrader_pre_install'} filter by Theme_Upgrader::upgrade() and
485 * Theme_Upgrader::bulk_upgrade().
490 * @param bool|WP_Error $return
491 * @param array $theme
492 * @return bool|WP_Error
494 public function current_before($return, $theme) {
495 if ( is_wp_error($return) )
498 $theme = isset($theme['theme']) ? $theme['theme'] : '';
500 if ( $theme != get_stylesheet() ) //If not current
502 //Change to maintenance mode now.
504 $this->maintenance_mode(true);
510 * Turn off maintenance mode after upgrading the current theme.
512 * Hooked to the {@see 'upgrader_post_install'} filter by Theme_Upgrader::upgrade()
513 * and Theme_Upgrader::bulk_upgrade().
518 * @param bool|WP_Error $return
519 * @param array $theme
520 * @return bool|WP_Error
522 public function current_after($return, $theme) {
523 if ( is_wp_error($return) )
526 $theme = isset($theme['theme']) ? $theme['theme'] : '';
528 if ( $theme != get_stylesheet() ) // If not current
531 // Ensure stylesheet name hasn't changed after the upgrade:
532 if ( $theme == get_stylesheet() && $theme != $this->result['destination_name'] ) {
533 wp_clean_themes_cache();
534 $stylesheet = $this->result['destination_name'];
535 switch_theme( $stylesheet );
538 //Time to remove maintenance mode
540 $this->maintenance_mode(false);
545 * Delete the old theme during an upgrade.
547 * Hooked to the {@see 'upgrader_clear_destination'} filter by Theme_Upgrader::upgrade()
548 * and Theme_Upgrader::bulk_upgrade().
553 * @global WP_Filesystem_Base $wp_filesystem Subclass
555 * @param bool $removed
556 * @param string $local_destination
557 * @param string $remote_destination
558 * @param array $theme
561 public function delete_old_theme( $removed, $local_destination, $remote_destination, $theme ) {
562 global $wp_filesystem;
564 if ( is_wp_error( $removed ) )
565 return $removed; // Pass errors through.
567 if ( ! isset( $theme['theme'] ) )
570 $theme = $theme['theme'];
571 $themes_dir = trailingslashit( $wp_filesystem->wp_themes_dir( $theme ) );
572 if ( $wp_filesystem->exists( $themes_dir . $theme ) ) {
573 if ( ! $wp_filesystem->delete( $themes_dir . $theme, true ) )
581 * Get the WP_Theme object for a theme.
584 * @since 3.0.0 The `$theme` argument was added.
587 * @param string $theme The directory name of the theme. This is optional, and if not supplied,
588 * the directory name from the last result will be used.
589 * @return WP_Theme|false The theme's info object, or false `$theme` is not supplied
590 * and the last result isn't set.
592 public function theme_info($theme = null) {
594 if ( empty($theme) ) {
595 if ( !empty($this->result['destination_name']) )
596 $theme = $this->result['destination_name'];
600 return wp_get_theme( $theme );