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 if ( $parsed_args['clear_update_cache'] ) {
205 // Clear cache so wp_update_themes() knows about the new theme.
206 add_action( 'upgrader_process_complete', 'wp_clean_themes_cache', 9, 0 );
210 'package' => $package,
211 'destination' => get_theme_root(),
212 'clear_destination' => false, //Do not overwrite files.
213 'clear_working' => true,
214 'hook_extra' => array(
216 'action' => 'install',
220 remove_action( 'upgrader_process_complete', 'wp_clean_themes_cache', 9 );
221 remove_filter('upgrader_source_selection', array($this, 'check_package') );
222 remove_filter('upgrader_post_install', array($this, 'check_parent_theme_filter'));
224 if ( ! $this->result || is_wp_error($this->result) )
225 return $this->result;
227 // Refresh the Theme Update information
228 wp_clean_themes_cache( $parsed_args['clear_update_cache'] );
237 * @since 3.7.0 The `$args` parameter was added, making clearing the update cache optional.
240 * @param string $theme The theme slug.
241 * @param array $args {
242 * Optional. Other arguments for upgrading a theme. Default empty array.
244 * @type bool $clear_update_cache Whether to clear the update cache if successful.
247 * @return bool|WP_Error True if the upgrade was successful, false or a WP_Error object otherwise.
249 public function upgrade( $theme, $args = array() ) {
252 'clear_update_cache' => true,
254 $parsed_args = wp_parse_args( $args, $defaults );
257 $this->upgrade_strings();
259 // Is an update available?
260 $current = get_site_transient( 'update_themes' );
261 if ( !isset( $current->response[ $theme ] ) ) {
262 $this->skin->before();
263 $this->skin->set_result(false);
264 $this->skin->error( 'up_to_date' );
265 $this->skin->after();
269 $r = $current->response[ $theme ];
271 add_filter('upgrader_pre_install', array($this, 'current_before'), 10, 2);
272 add_filter('upgrader_post_install', array($this, 'current_after'), 10, 2);
273 add_filter('upgrader_clear_destination', array($this, 'delete_old_theme'), 10, 4);
274 if ( $parsed_args['clear_update_cache'] ) {
275 // Clear cache so wp_update_themes() knows about the new theme.
276 add_action( 'upgrader_process_complete', 'wp_clean_themes_cache', 9, 0 );
280 'package' => $r['package'],
281 'destination' => get_theme_root( $theme ),
282 'clear_destination' => true,
283 'clear_working' => true,
284 'hook_extra' => array(
287 'action' => 'update',
291 remove_action( 'upgrader_process_complete', 'wp_clean_themes_cache', 9 );
292 remove_filter('upgrader_pre_install', array($this, 'current_before'));
293 remove_filter('upgrader_post_install', array($this, 'current_after'));
294 remove_filter('upgrader_clear_destination', array($this, 'delete_old_theme'));
296 if ( ! $this->result || is_wp_error($this->result) )
297 return $this->result;
299 wp_clean_themes_cache( $parsed_args['clear_update_cache'] );
305 * Upgrade several themes at once.
308 * @since 3.7.0 The `$args` parameter was added, making clearing the update cache optional.
311 * @param array $themes The theme slugs.
312 * @param array $args {
313 * Optional. Other arguments for upgrading several themes at once. Default empty array.
315 * @type bool $clear_update_cache Whether to clear the update cache if successful.
318 * @return array[]|false An array of results, or false if unable to connect to the filesystem.
320 public function bulk_upgrade( $themes, $args = array() ) {
323 'clear_update_cache' => true,
325 $parsed_args = wp_parse_args( $args, $defaults );
329 $this->upgrade_strings();
331 $current = get_site_transient( 'update_themes' );
333 add_filter('upgrader_pre_install', array($this, 'current_before'), 10, 2);
334 add_filter('upgrader_post_install', array($this, 'current_after'), 10, 2);
335 add_filter('upgrader_clear_destination', array($this, 'delete_old_theme'), 10, 4);
337 $this->skin->header();
339 // Connect to the Filesystem first.
340 $res = $this->fs_connect( array(WP_CONTENT_DIR) );
342 $this->skin->footer();
346 $this->skin->bulk_header();
348 // Only start maintenance mode if:
349 // - running Multisite and there are one or more themes specified, OR
350 // - a theme with an update available is currently in use.
351 // @TODO: For multisite, maintenance mode should only kick in for individual sites if at all possible.
352 $maintenance = ( is_multisite() && ! empty( $themes ) );
353 foreach ( $themes as $theme )
354 $maintenance = $maintenance || $theme == get_stylesheet() || $theme == get_template();
356 $this->maintenance_mode(true);
360 $this->update_count = count($themes);
361 $this->update_current = 0;
362 foreach ( $themes as $theme ) {
363 $this->update_current++;
365 $this->skin->theme_info = $this->theme_info($theme);
367 if ( !isset( $current->response[ $theme ] ) ) {
368 $this->skin->set_result(true);
369 $this->skin->before();
370 $this->skin->feedback( 'up_to_date' );
371 $this->skin->after();
372 $results[$theme] = true;
376 // Get the URL to the zip file
377 $r = $current->response[ $theme ];
379 $result = $this->run( array(
380 'package' => $r['package'],
381 'destination' => get_theme_root( $theme ),
382 'clear_destination' => true,
383 'clear_working' => true,
385 'hook_extra' => array(
390 $results[$theme] = $this->result;
392 // Prevent credentials auth screen from displaying multiple times
393 if ( false === $result )
395 } //end foreach $plugins
397 $this->maintenance_mode(false);
399 // Refresh the Theme Update information
400 wp_clean_themes_cache( $parsed_args['clear_update_cache'] );
402 /** This action is documented in wp-admin/includes/class-wp-upgrader.php */
403 do_action( 'upgrader_process_complete', $this, array(
404 'action' => 'update',
410 $this->skin->bulk_footer();
412 $this->skin->footer();
414 // Cleanup our hooks, in case something else does a upgrade on this connection.
415 remove_filter('upgrader_pre_install', array($this, 'current_before'));
416 remove_filter('upgrader_post_install', array($this, 'current_after'));
417 remove_filter('upgrader_clear_destination', array($this, 'delete_old_theme'));
423 * Check that the package source contains a valid theme.
425 * Hooked to the {@see 'upgrader_source_selection'} filter by Theme_Upgrader::install().
426 * It will return an error if the theme doesn't have style.css or index.php
432 * @global WP_Filesystem_Base $wp_filesystem Subclass
434 * @param string $source The full path to the package source.
435 * @return string|WP_Error The source or a WP_Error.
437 public function check_package( $source ) {
438 global $wp_filesystem;
440 if ( is_wp_error($source) )
443 // Check the folder contains a valid theme
444 $working_directory = str_replace( $wp_filesystem->wp_content_dir(), trailingslashit(WP_CONTENT_DIR), $source);
445 if ( ! is_dir($working_directory) ) // Sanity check, if the above fails, let's not prevent installation.
448 // A proper archive should have a style.css file in the single subdirectory
449 if ( ! file_exists( $working_directory . 'style.css' ) ) {
450 return new WP_Error( 'incompatible_archive_theme_no_style', $this->strings['incompatible_archive'],
451 /* translators: %s: style.css */
452 sprintf( __( 'The theme is missing the %s stylesheet.' ),
453 '<code>style.css</code>'
458 $info = get_file_data( $working_directory . 'style.css', array( 'Name' => 'Theme Name', 'Template' => 'Template' ) );
460 if ( empty( $info['Name'] ) ) {
461 return new WP_Error( 'incompatible_archive_theme_no_name', $this->strings['incompatible_archive'],
462 /* translators: %s: style.css */
463 sprintf( __( 'The %s stylesheet doesn’t contain a valid theme header.' ),
464 '<code>style.css</code>'
469 // If it's not a child theme, it must have at least an index.php to be legit.
470 if ( empty( $info['Template'] ) && ! file_exists( $working_directory . 'index.php' ) ) {
471 return new WP_Error( 'incompatible_archive_theme_no_index', $this->strings['incompatible_archive'],
472 /* translators: %s: index.php */
473 sprintf( __( 'The theme is missing the %s file.' ),
474 '<code>index.php</code>'
483 * Turn on maintenance mode before attempting to upgrade the current theme.
485 * Hooked to the {@see 'upgrader_pre_install'} filter by Theme_Upgrader::upgrade() and
486 * Theme_Upgrader::bulk_upgrade().
491 * @param bool|WP_Error $return
492 * @param array $theme
493 * @return bool|WP_Error
495 public function current_before($return, $theme) {
496 if ( is_wp_error($return) )
499 $theme = isset($theme['theme']) ? $theme['theme'] : '';
501 if ( $theme != get_stylesheet() ) //If not current
503 //Change to maintenance mode now.
505 $this->maintenance_mode(true);
511 * Turn off maintenance mode after upgrading the current theme.
513 * Hooked to the {@see 'upgrader_post_install'} filter by Theme_Upgrader::upgrade()
514 * and Theme_Upgrader::bulk_upgrade().
519 * @param bool|WP_Error $return
520 * @param array $theme
521 * @return bool|WP_Error
523 public function current_after($return, $theme) {
524 if ( is_wp_error($return) )
527 $theme = isset($theme['theme']) ? $theme['theme'] : '';
529 if ( $theme != get_stylesheet() ) // If not current
532 // Ensure stylesheet name hasn't changed after the upgrade:
533 if ( $theme == get_stylesheet() && $theme != $this->result['destination_name'] ) {
534 wp_clean_themes_cache();
535 $stylesheet = $this->result['destination_name'];
536 switch_theme( $stylesheet );
539 //Time to remove maintenance mode
541 $this->maintenance_mode(false);
546 * Delete the old theme during an upgrade.
548 * Hooked to the {@see 'upgrader_clear_destination'} filter by Theme_Upgrader::upgrade()
549 * and Theme_Upgrader::bulk_upgrade().
554 * @global WP_Filesystem_Base $wp_filesystem Subclass
556 * @param bool $removed
557 * @param string $local_destination
558 * @param string $remote_destination
559 * @param array $theme
562 public function delete_old_theme( $removed, $local_destination, $remote_destination, $theme ) {
563 global $wp_filesystem;
565 if ( is_wp_error( $removed ) )
566 return $removed; // Pass errors through.
568 if ( ! isset( $theme['theme'] ) )
571 $theme = $theme['theme'];
572 $themes_dir = trailingslashit( $wp_filesystem->wp_themes_dir( $theme ) );
573 if ( $wp_filesystem->exists( $themes_dir . $theme ) ) {
574 if ( ! $wp_filesystem->delete( $themes_dir . $theme, true ) )
582 * Get the WP_Theme object for a theme.
585 * @since 3.0.0 The `$theme` argument was added.
588 * @param string $theme The directory name of the theme. This is optional, and if not supplied,
589 * the directory name from the last result will be used.
590 * @return WP_Theme|false The theme's info object, or false `$theme` is not supplied
591 * and the last result isn't set.
593 public function theme_info($theme = null) {
595 if ( empty($theme) ) {
596 if ( !empty($this->result['destination_name']) )
597 $theme = $this->result['destination_name'];
601 return wp_get_theme( $theme );