3 * A File upgrader class for WordPress.
5 * This set of classes are designed to be used to upgrade/install a local set of files on the filesystem via the Filesystem Abstraction classes.
7 * @link http://trac.wordpress.org/ticket/7875 consolidate plugin/theme/core upgrade/install functions
10 * @subpackage Upgrader
14 require ABSPATH . 'wp-admin/includes/class-wp-upgrader-skins.php';
17 * WordPress Upgrader class for Upgrading/Installing a local set of files via the Filesystem Abstraction classes from a Zip file.
20 * @subpackage Upgrader
24 public $strings = array();
26 public $result = array();
28 public function __construct($skin = null) {
30 $this->skin = new WP_Upgrader_Skin();
35 public function init() {
36 $this->skin->set_upgrader($this);
37 $this->generic_strings();
40 public function generic_strings() {
41 $this->strings['bad_request'] = __('Invalid Data provided.');
42 $this->strings['fs_unavailable'] = __('Could not access filesystem.');
43 $this->strings['fs_error'] = __('Filesystem error.');
44 $this->strings['fs_no_root_dir'] = __('Unable to locate WordPress Root directory.');
45 $this->strings['fs_no_content_dir'] = __('Unable to locate WordPress Content directory (wp-content).');
46 $this->strings['fs_no_plugins_dir'] = __('Unable to locate WordPress Plugin directory.');
47 $this->strings['fs_no_themes_dir'] = __('Unable to locate WordPress Theme directory.');
48 /* translators: %s: directory name */
49 $this->strings['fs_no_folder'] = __('Unable to locate needed folder (%s).');
51 $this->strings['download_failed'] = __('Download failed.');
52 $this->strings['installing_package'] = __('Installing the latest version…');
53 $this->strings['no_files'] = __('The package contains no files.');
54 $this->strings['folder_exists'] = __('Destination folder already exists.');
55 $this->strings['mkdir_failed'] = __('Could not create directory.');
56 $this->strings['incompatible_archive'] = __('The package could not be installed.');
58 $this->strings['maintenance_start'] = __('Enabling Maintenance mode…');
59 $this->strings['maintenance_end'] = __('Disabling Maintenance mode…');
62 public function fs_connect( $directories = array() ) {
63 global $wp_filesystem;
65 if ( false === ($credentials = $this->skin->request_filesystem_credentials()) )
68 if ( ! WP_Filesystem($credentials) ) {
70 if ( is_object($wp_filesystem) && $wp_filesystem->errors->get_error_code() )
71 $error = $wp_filesystem->errors;
72 $this->skin->request_filesystem_credentials($error); //Failed to connect, Error and request again
76 if ( ! is_object($wp_filesystem) )
77 return new WP_Error('fs_unavailable', $this->strings['fs_unavailable'] );
79 if ( is_wp_error($wp_filesystem->errors) && $wp_filesystem->errors->get_error_code() )
80 return new WP_Error('fs_error', $this->strings['fs_error'], $wp_filesystem->errors);
82 foreach ( (array)$directories as $dir ) {
85 if ( ! $wp_filesystem->abspath() )
86 return new WP_Error('fs_no_root_dir', $this->strings['fs_no_root_dir']);
89 if ( ! $wp_filesystem->wp_content_dir() )
90 return new WP_Error('fs_no_content_dir', $this->strings['fs_no_content_dir']);
93 if ( ! $wp_filesystem->wp_plugins_dir() )
94 return new WP_Error('fs_no_plugins_dir', $this->strings['fs_no_plugins_dir']);
96 case get_theme_root():
97 if ( ! $wp_filesystem->wp_themes_dir() )
98 return new WP_Error('fs_no_themes_dir', $this->strings['fs_no_themes_dir']);
101 if ( ! $wp_filesystem->find_folder($dir) )
102 return new WP_Error( 'fs_no_folder', sprintf( $this->strings['fs_no_folder'], esc_html( basename( $dir ) ) ) );
107 } //end fs_connect();
109 public function download_package($package) {
112 * Filter whether to return the package.
116 * @param bool $reply Whether to bail without returning the package.
118 * @param string $package The package file name.
119 * @param WP_Upgrader $this The WP_Upgrader instance.
121 $reply = apply_filters( 'upgrader_pre_download', false, $package, $this );
122 if ( false !== $reply )
125 if ( ! preg_match('!^(http|https|ftp)://!i', $package) && file_exists($package) ) //Local file or remote?
126 return $package; //must be a local file..
128 if ( empty($package) )
129 return new WP_Error('no_package', $this->strings['no_package']);
131 $this->skin->feedback('downloading_package', $package);
133 $download_file = download_url($package);
135 if ( is_wp_error($download_file) )
136 return new WP_Error('download_failed', $this->strings['download_failed'], $download_file->get_error_message());
138 return $download_file;
141 public function unpack_package($package, $delete_package = true) {
142 global $wp_filesystem;
144 $this->skin->feedback('unpack_package');
146 $upgrade_folder = $wp_filesystem->wp_content_dir() . 'upgrade/';
148 //Clean up contents of upgrade directory beforehand.
149 $upgrade_files = $wp_filesystem->dirlist($upgrade_folder);
150 if ( !empty($upgrade_files) ) {
151 foreach ( $upgrade_files as $file )
152 $wp_filesystem->delete($upgrade_folder . $file['name'], true);
155 //We need a working directory
156 $working_dir = $upgrade_folder . basename($package, '.zip');
158 // Clean up working directory
159 if ( $wp_filesystem->is_dir($working_dir) )
160 $wp_filesystem->delete($working_dir, true);
162 // Unzip package to working directory
163 $result = unzip_file( $package, $working_dir );
165 // Once extracted, delete the package if required.
166 if ( $delete_package )
169 if ( is_wp_error($result) ) {
170 $wp_filesystem->delete($working_dir, true);
171 if ( 'incompatible_archive' == $result->get_error_code() ) {
172 return new WP_Error( 'incompatible_archive', $this->strings['incompatible_archive'], $result->get_error_data() );
180 public function install_package( $args = array() ) {
181 global $wp_filesystem, $wp_theme_directories;
184 'source' => '', // Please always pass this
185 'destination' => '', // and this
186 'clear_destination' => false,
187 'clear_working' => false,
188 'abort_if_destination_exists' => true,
189 'hook_extra' => array()
192 $args = wp_parse_args($args, $defaults);
194 // These were previously extract()'d.
195 $source = $args['source'];
196 $destination = $args['destination'];
197 $clear_destination = $args['clear_destination'];
199 @set_time_limit( 300 );
201 if ( empty( $source ) || empty( $destination ) ) {
202 return new WP_Error( 'bad_request', $this->strings['bad_request'] );
204 $this->skin->feedback( 'installing_package' );
207 * Filter the install response before the installation has started.
209 * Returning a truthy value, or one that could be evaluated as a WP_Error
210 * will effectively short-circuit the installation, returning that value
215 * @param bool|WP_Error $response Response.
216 * @param array $hook_extra Extra arguments passed to hooked filters.
218 $res = apply_filters( 'upgrader_pre_install', true, $args['hook_extra'] );
219 if ( is_wp_error( $res ) ) {
223 //Retain the Original source and destinations
224 $remote_source = $args['source'];
225 $local_destination = $destination;
227 $source_files = array_keys( $wp_filesystem->dirlist( $remote_source ) );
228 $remote_destination = $wp_filesystem->find_folder( $local_destination );
230 //Locate which directory to copy to the new folder, This is based on the actual folder holding the files.
231 if ( 1 == count( $source_files ) && $wp_filesystem->is_dir( trailingslashit( $args['source'] ) . $source_files[0] . '/' ) ) { //Only one folder? Then we want its contents.
232 $source = trailingslashit( $args['source'] ) . trailingslashit( $source_files[0] );
233 } elseif ( count( $source_files ) == 0 ) {
234 return new WP_Error( 'incompatible_archive_empty', $this->strings['incompatible_archive'], $this->strings['no_files'] ); // There are no files?
235 } else { //It's only a single file, the upgrader will use the foldername of this file as the destination folder. foldername is based on zip filename.
236 $source = trailingslashit( $args['source'] );
240 * Filter the source file location for the upgrade package.
244 * @param string $source File source location.
245 * @param string $remote_source Remove file source location.
246 * @param WP_Upgrader $this WP_Upgrader instance.
248 $source = apply_filters( 'upgrader_source_selection', $source, $remote_source, $this );
249 if ( is_wp_error( $source ) ) {
253 // Has the source location changed? If so, we need a new source_files list.
254 if ( $source !== $remote_source ) {
255 $source_files = array_keys( $wp_filesystem->dirlist( $source ) );
258 * Protection against deleting files in any important base directories.
259 * Theme_Upgrader & Plugin_Upgrader also trigger this, as they pass the
260 * destination directory (WP_PLUGIN_DIR / wp-content/themes) intending
261 * to copy the directory into the directory, whilst they pass the source
262 * as the actual files to copy.
264 $protected_directories = array( ABSPATH, WP_CONTENT_DIR, WP_PLUGIN_DIR, WP_CONTENT_DIR . '/themes' );
266 if ( is_array( $wp_theme_directories ) ) {
267 $protected_directories = array_merge( $protected_directories, $wp_theme_directories );
269 if ( in_array( $destination, $protected_directories ) ) {
270 $remote_destination = trailingslashit( $remote_destination ) . trailingslashit( basename( $source ) );
271 $destination = trailingslashit( $destination ) . trailingslashit( basename( $source ) );
274 if ( $clear_destination ) {
275 //We're going to clear the destination if there's something there
276 $this->skin->feedback('remove_old');
278 if ( $wp_filesystem->exists( $remote_destination ) ) {
279 $removed = $wp_filesystem->delete( $remote_destination, true );
283 * Filter whether the upgrader cleared the destination.
287 * @param bool $removed Whether the destination was cleared.
288 * @param string $local_destination The local package destination.
289 * @param string $remote_destination The remote package destination.
290 * @param array $hook_extra Extra arguments passed to hooked filters.
292 $removed = apply_filters( 'upgrader_clear_destination', $removed, $local_destination, $remote_destination, $args['hook_extra'] );
294 if ( is_wp_error($removed) ) {
296 } else if ( ! $removed ) {
297 return new WP_Error('remove_old_failed', $this->strings['remove_old_failed']);
299 } elseif ( $args['abort_if_destination_exists'] && $wp_filesystem->exists($remote_destination) ) {
300 //If we're not clearing the destination folder and something exists there already, Bail.
301 //But first check to see if there are actually any files in the folder.
302 $_files = $wp_filesystem->dirlist($remote_destination);
303 if ( ! empty($_files) ) {
304 $wp_filesystem->delete($remote_source, true); //Clear out the source files.
305 return new WP_Error('folder_exists', $this->strings['folder_exists'], $remote_destination );
309 //Create destination if needed
310 if ( ! $wp_filesystem->exists( $remote_destination ) ) {
311 if ( ! $wp_filesystem->mkdir( $remote_destination, FS_CHMOD_DIR ) ) {
312 return new WP_Error( 'mkdir_failed_destination', $this->strings['mkdir_failed'], $remote_destination );
315 // Copy new version of item into place.
316 $result = copy_dir($source, $remote_destination);
317 if ( is_wp_error($result) ) {
318 if ( $args['clear_working'] ) {
319 $wp_filesystem->delete( $remote_source, true );
324 //Clear the Working folder?
325 if ( $args['clear_working'] ) {
326 $wp_filesystem->delete( $remote_source, true );
329 $destination_name = basename( str_replace($local_destination, '', $destination) );
330 if ( '.' == $destination_name ) {
331 $destination_name = '';
334 $this->result = compact('local_source', 'source', 'source_name', 'source_files', 'destination', 'destination_name', 'local_destination', 'remote_destination', 'clear_destination', 'delete_source_dir');
337 * Filter the install response after the installation has finished.
341 * @param bool $response Install response.
342 * @param array $hook_extra Extra arguments passed to hooked filters.
343 * @param array $result Installation result data.
345 $res = apply_filters( 'upgrader_post_install', true, $args['hook_extra'], $this->result );
347 if ( is_wp_error($res) ) {
348 $this->result = $res;
352 //Bombard the calling function will all the info which we've just used.
353 return $this->result;
356 public function run( $options ) {
359 'package' => '', // Please always pass this.
360 'destination' => '', // And this
361 'clear_destination' => false,
362 'abort_if_destination_exists' => true, // Abort if the Destination directory exists, Pass clear_destination as false please
363 'clear_working' => true,
365 'hook_extra' => array() // Pass any extra $hook_extra args here, this will be passed to any hooked filters.
368 $options = wp_parse_args( $options, $defaults );
370 if ( ! $options['is_multi'] ) { // call $this->header separately if running multiple times
371 $this->skin->header();
374 // Connect to the Filesystem first.
375 $res = $this->fs_connect( array( WP_CONTENT_DIR, $options['destination'] ) );
376 // Mainly for non-connected filesystem.
378 if ( ! $options['is_multi'] ) {
379 $this->skin->footer();
384 $this->skin->before();
386 if ( is_wp_error($res) ) {
387 $this->skin->error($res);
388 $this->skin->after();
389 if ( ! $options['is_multi'] ) {
390 $this->skin->footer();
395 //Download the package (Note, This just returns the filename of the file if the package is a local file)
396 $download = $this->download_package( $options['package'] );
397 if ( is_wp_error($download) ) {
398 $this->skin->error($download);
399 $this->skin->after();
400 if ( ! $options['is_multi'] ) {
401 $this->skin->footer();
406 $delete_package = ( $download != $options['package'] ); // Do not delete a "local" file
408 //Unzips the file into a temporary directory
409 $working_dir = $this->unpack_package( $download, $delete_package );
410 if ( is_wp_error($working_dir) ) {
411 $this->skin->error($working_dir);
412 $this->skin->after();
413 if ( ! $options['is_multi'] ) {
414 $this->skin->footer();
419 //With the given options, this installs it to the destination directory.
420 $result = $this->install_package( array(
421 'source' => $working_dir,
422 'destination' => $options['destination'],
423 'clear_destination' => $options['clear_destination'],
424 'abort_if_destination_exists' => $options['abort_if_destination_exists'],
425 'clear_working' => $options['clear_working'],
426 'hook_extra' => $options['hook_extra']
429 $this->skin->set_result($result);
430 if ( is_wp_error($result) ) {
431 $this->skin->error($result);
432 $this->skin->feedback('process_failed');
435 $this->skin->feedback('process_success');
438 $this->skin->after();
440 if ( ! $options['is_multi'] ) {
442 /** This action is documented in wp-admin/includes/class-wp-upgrader.php */
443 do_action( 'upgrader_process_complete', $this, $options['hook_extra'] );
444 $this->skin->footer();
450 public function maintenance_mode($enable = false) {
451 global $wp_filesystem;
452 $file = $wp_filesystem->abspath() . '.maintenance';
454 $this->skin->feedback('maintenance_start');
455 // Create maintenance file to signal that we are upgrading
456 $maintenance_string = '<?php $upgrading = ' . time() . '; ?>';
457 $wp_filesystem->delete($file);
458 $wp_filesystem->put_contents($file, $maintenance_string, FS_CHMOD_FILE);
459 } else if ( !$enable && $wp_filesystem->exists($file) ) {
460 $this->skin->feedback('maintenance_end');
461 $wp_filesystem->delete($file);
468 * Plugin Upgrader class for WordPress Plugins, It is designed to upgrade/install plugins from a local zip, remote zip URL, or uploaded zip file.
471 * @subpackage Upgrader
474 class Plugin_Upgrader extends WP_Upgrader {
477 public $bulk = false;
479 public function upgrade_strings() {
480 $this->strings['up_to_date'] = __('The plugin is at the latest version.');
481 $this->strings['no_package'] = __('Update package not available.');
482 $this->strings['downloading_package'] = __('Downloading update from <span class="code">%s</span>…');
483 $this->strings['unpack_package'] = __('Unpacking the update…');
484 $this->strings['remove_old'] = __('Removing the old version of the plugin…');
485 $this->strings['remove_old_failed'] = __('Could not remove the old plugin.');
486 $this->strings['process_failed'] = __('Plugin update failed.');
487 $this->strings['process_success'] = __('Plugin updated successfully.');
490 public function install_strings() {
491 $this->strings['no_package'] = __('Install package not available.');
492 $this->strings['downloading_package'] = __('Downloading install package from <span class="code">%s</span>…');
493 $this->strings['unpack_package'] = __('Unpacking the package…');
494 $this->strings['installing_package'] = __('Installing the plugin…');
495 $this->strings['no_files'] = __('The plugin contains no files.');
496 $this->strings['process_failed'] = __('Plugin install failed.');
497 $this->strings['process_success'] = __('Plugin installed successfully.');
500 public function install( $package, $args = array() ) {
503 'clear_update_cache' => true,
505 $parsed_args = wp_parse_args( $args, $defaults );
508 $this->install_strings();
510 add_filter('upgrader_source_selection', array($this, 'check_package') );
513 'package' => $package,
514 'destination' => WP_PLUGIN_DIR,
515 'clear_destination' => false, // Do not overwrite files.
516 'clear_working' => true,
517 'hook_extra' => array(
519 'action' => 'install',
523 remove_filter('upgrader_source_selection', array($this, 'check_package') );
525 if ( ! $this->result || is_wp_error($this->result) )
526 return $this->result;
528 // Force refresh of plugin update information
529 wp_clean_plugins_cache( $parsed_args['clear_update_cache'] );
534 public function upgrade( $plugin, $args = array() ) {
537 'clear_update_cache' => true,
539 $parsed_args = wp_parse_args( $args, $defaults );
542 $this->upgrade_strings();
544 $current = get_site_transient( 'update_plugins' );
545 if ( !isset( $current->response[ $plugin ] ) ) {
546 $this->skin->before();
547 $this->skin->set_result(false);
548 $this->skin->error('up_to_date');
549 $this->skin->after();
553 // Get the URL to the zip file
554 $r = $current->response[ $plugin ];
556 add_filter('upgrader_pre_install', array($this, 'deactivate_plugin_before_upgrade'), 10, 2);
557 add_filter('upgrader_clear_destination', array($this, 'delete_old_plugin'), 10, 4);
558 //'source_selection' => array($this, 'source_selection'), //there's a trac ticket to move up the directory for zip's which are made a bit differently, useful for non-.org plugins.
561 'package' => $r->package,
562 'destination' => WP_PLUGIN_DIR,
563 'clear_destination' => true,
564 'clear_working' => true,
565 'hook_extra' => array(
568 'action' => 'update',
572 // Cleanup our hooks, in case something else does a upgrade on this connection.
573 remove_filter('upgrader_pre_install', array($this, 'deactivate_plugin_before_upgrade'));
574 remove_filter('upgrader_clear_destination', array($this, 'delete_old_plugin'));
576 if ( ! $this->result || is_wp_error($this->result) )
577 return $this->result;
579 // Force refresh of plugin update information
580 wp_clean_plugins_cache( $parsed_args['clear_update_cache'] );
585 public function bulk_upgrade( $plugins, $args = array() ) {
588 'clear_update_cache' => true,
590 $parsed_args = wp_parse_args( $args, $defaults );
594 $this->upgrade_strings();
596 $current = get_site_transient( 'update_plugins' );
598 add_filter('upgrader_clear_destination', array($this, 'delete_old_plugin'), 10, 4);
600 $this->skin->header();
602 // Connect to the Filesystem first.
603 $res = $this->fs_connect( array(WP_CONTENT_DIR, WP_PLUGIN_DIR) );
605 $this->skin->footer();
609 $this->skin->bulk_header();
611 // Only start maintenance mode if:
612 // - running Multisite and there are one or more plugins specified, OR
613 // - a plugin with an update available is currently active.
614 // @TODO: For multisite, maintenance mode should only kick in for individual sites if at all possible.
615 $maintenance = ( is_multisite() && ! empty( $plugins ) );
616 foreach ( $plugins as $plugin )
617 $maintenance = $maintenance || ( is_plugin_active( $plugin ) && isset( $current->response[ $plugin] ) );
619 $this->maintenance_mode(true);
623 $this->update_count = count($plugins);
624 $this->update_current = 0;
625 foreach ( $plugins as $plugin ) {
626 $this->update_current++;
627 $this->skin->plugin_info = get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin, false, true);
629 if ( !isset( $current->response[ $plugin ] ) ) {
630 $this->skin->set_result('up_to_date');
631 $this->skin->before();
632 $this->skin->feedback('up_to_date');
633 $this->skin->after();
634 $results[$plugin] = true;
638 // Get the URL to the zip file
639 $r = $current->response[ $plugin ];
641 $this->skin->plugin_active = is_plugin_active($plugin);
643 $result = $this->run( array(
644 'package' => $r->package,
645 'destination' => WP_PLUGIN_DIR,
646 'clear_destination' => true,
647 'clear_working' => true,
649 'hook_extra' => array(
654 $results[$plugin] = $this->result;
656 // Prevent credentials auth screen from displaying multiple times
657 if ( false === $result )
659 } //end foreach $plugins
661 $this->maintenance_mode(false);
664 * Fires when the bulk upgrader process is complete.
668 * @param Plugin_Upgrader $this Plugin_Upgrader instance. In other contexts, $this, might
669 * be a Theme_Upgrader or Core_Upgrade instance.
670 * @param array $data {
671 * Array of bulk item update data.
673 * @type string $action Type of action. Default 'update'.
674 * @type string $type Type of update process. Accepts 'plugin', 'theme', or 'core'.
675 * @type bool $bulk Whether the update process is a bulk update. Default true.
676 * @type array $packages Array of plugin, theme, or core packages to update.
679 do_action( 'upgrader_process_complete', $this, array(
680 'action' => 'update',
683 'plugins' => $plugins,
686 $this->skin->bulk_footer();
688 $this->skin->footer();
690 // Cleanup our hooks, in case something else does a upgrade on this connection.
691 remove_filter('upgrader_clear_destination', array($this, 'delete_old_plugin'));
693 // Force refresh of plugin update information
694 wp_clean_plugins_cache( $parsed_args['clear_update_cache'] );
699 public function check_package($source) {
700 global $wp_filesystem;
702 if ( is_wp_error($source) )
705 $working_directory = str_replace( $wp_filesystem->wp_content_dir(), trailingslashit(WP_CONTENT_DIR), $source);
706 if ( ! is_dir($working_directory) ) // Sanity check, if the above fails, lets not prevent installation.
709 // Check the folder contains at least 1 valid plugin.
710 $plugins_found = false;
711 foreach ( glob( $working_directory . '*.php' ) as $file ) {
712 $info = get_plugin_data($file, false, false);
713 if ( !empty( $info['Name'] ) ) {
714 $plugins_found = true;
719 if ( ! $plugins_found )
720 return new WP_Error( 'incompatible_archive_no_plugins', $this->strings['incompatible_archive'], __( 'No valid plugins were found.' ) );
725 // Return plugin info.
726 public function plugin_info() {
727 if ( ! is_array($this->result) )
729 if ( empty($this->result['destination_name']) )
732 $plugin = get_plugins('/' . $this->result['destination_name']); //Ensure to pass with leading slash
733 if ( empty($plugin) )
736 $pluginfiles = array_keys($plugin); //Assume the requested plugin is the first in the list
738 return $this->result['destination_name'] . '/' . $pluginfiles[0];
741 //Hooked to pre_install
742 public function deactivate_plugin_before_upgrade($return, $plugin) {
744 if ( is_wp_error($return) ) //Bypass.
747 // When in cron (background updates) don't deactivate the plugin, as we require a browser to reactivate it
748 if ( defined( 'DOING_CRON' ) && DOING_CRON )
751 $plugin = isset($plugin['plugin']) ? $plugin['plugin'] : '';
752 if ( empty($plugin) )
753 return new WP_Error('bad_request', $this->strings['bad_request']);
755 if ( is_plugin_active($plugin) ) {
756 //Deactivate the plugin silently, Prevent deactivation hooks from running.
757 deactivate_plugins($plugin, true);
761 //Hooked to upgrade_clear_destination
762 public function delete_old_plugin($removed, $local_destination, $remote_destination, $plugin) {
763 global $wp_filesystem;
765 if ( is_wp_error($removed) )
766 return $removed; //Pass errors through.
768 $plugin = isset($plugin['plugin']) ? $plugin['plugin'] : '';
769 if ( empty($plugin) )
770 return new WP_Error('bad_request', $this->strings['bad_request']);
772 $plugins_dir = $wp_filesystem->wp_plugins_dir();
773 $this_plugin_dir = trailingslashit( dirname($plugins_dir . $plugin) );
775 if ( ! $wp_filesystem->exists($this_plugin_dir) ) //If it's already vanished.
778 // If plugin is in its own directory, recursively delete the directory.
779 if ( strpos($plugin, '/') && $this_plugin_dir != $plugins_dir ) //base check on if plugin includes directory separator AND that it's not the root plugin folder
780 $deleted = $wp_filesystem->delete($this_plugin_dir, true);
782 $deleted = $wp_filesystem->delete($plugins_dir . $plugin);
785 return new WP_Error('remove_old_failed', $this->strings['remove_old_failed']);
792 * Theme Upgrader class for WordPress Themes, It is designed to upgrade/install themes from a local zip, remote zip URL, or uploaded zip file.
795 * @subpackage Upgrader
798 class Theme_Upgrader extends WP_Upgrader {
801 public $bulk = false;
803 public function upgrade_strings() {
804 $this->strings['up_to_date'] = __('The theme is at the latest version.');
805 $this->strings['no_package'] = __('Update package not available.');
806 $this->strings['downloading_package'] = __('Downloading update from <span class="code">%s</span>…');
807 $this->strings['unpack_package'] = __('Unpacking the update…');
808 $this->strings['remove_old'] = __('Removing the old version of the theme…');
809 $this->strings['remove_old_failed'] = __('Could not remove the old theme.');
810 $this->strings['process_failed'] = __('Theme update failed.');
811 $this->strings['process_success'] = __('Theme updated successfully.');
814 public function install_strings() {
815 $this->strings['no_package'] = __('Install package not available.');
816 $this->strings['downloading_package'] = __('Downloading install package from <span class="code">%s</span>…');
817 $this->strings['unpack_package'] = __('Unpacking the package…');
818 $this->strings['installing_package'] = __('Installing the theme…');
819 $this->strings['no_files'] = __('The theme contains no files.');
820 $this->strings['process_failed'] = __('Theme install failed.');
821 $this->strings['process_success'] = __('Theme installed successfully.');
822 /* translators: 1: theme name, 2: version */
823 $this->strings['process_success_specific'] = __('Successfully installed the theme <strong>%1$s %2$s</strong>.');
824 $this->strings['parent_theme_search'] = __('This theme requires a parent theme. Checking if it is installed…');
825 /* translators: 1: theme name, 2: version */
826 $this->strings['parent_theme_prepare_install'] = __('Preparing to install <strong>%1$s %2$s</strong>…');
827 /* translators: 1: theme name, 2: version */
828 $this->strings['parent_theme_currently_installed'] = __('The parent theme, <strong>%1$s %2$s</strong>, is currently installed.');
829 /* translators: 1: theme name, 2: version */
830 $this->strings['parent_theme_install_success'] = __('Successfully installed the parent theme, <strong>%1$s %2$s</strong>.');
831 $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.');
834 public function check_parent_theme_filter($install_result, $hook_extra, $child_result) {
835 // Check to see if we need to install a parent theme
836 $theme_info = $this->theme_info();
838 if ( ! $theme_info->parent() )
839 return $install_result;
841 $this->skin->feedback( 'parent_theme_search' );
843 if ( ! $theme_info->parent()->errors() ) {
844 $this->skin->feedback( 'parent_theme_currently_installed', $theme_info->parent()->display('Name'), $theme_info->parent()->display('Version') );
845 // We already have the theme, fall through.
846 return $install_result;
849 // We don't have the parent theme, lets install it
850 $api = themes_api('theme_information', array('slug' => $theme_info->get('Template'), 'fields' => array('sections' => false, 'tags' => false) ) ); //Save on a bit of bandwidth.
852 if ( ! $api || is_wp_error($api) ) {
853 $this->skin->feedback( 'parent_theme_not_found', $theme_info->get('Template') );
854 // Don't show activate or preview actions after install
855 add_filter('install_theme_complete_actions', array($this, 'hide_activate_preview_actions') );
856 return $install_result;
859 // Backup required data we're going to override:
860 $child_api = $this->skin->api;
861 $child_success_message = $this->strings['process_success'];
864 $this->skin->api = $api;
865 $this->strings['process_success_specific'] = $this->strings['parent_theme_install_success'];//, $api->name, $api->version);
867 $this->skin->feedback('parent_theme_prepare_install', $api->name, $api->version);
869 add_filter('install_theme_complete_actions', '__return_false', 999); // Don't show any actions after installing the theme.
871 // Install the parent theme
872 $parent_result = $this->run( array(
873 'package' => $api->download_link,
874 'destination' => get_theme_root(),
875 'clear_destination' => false, //Do not overwrite files.
876 'clear_working' => true
879 if ( is_wp_error($parent_result) )
880 add_filter('install_theme_complete_actions', array($this, 'hide_activate_preview_actions') );
882 // Start cleaning up after the parents installation
883 remove_filter('install_theme_complete_actions', '__return_false', 999);
885 // Reset child's result and data
886 $this->result = $child_result;
887 $this->skin->api = $child_api;
888 $this->strings['process_success'] = $child_success_message;
890 return $install_result;
893 public function hide_activate_preview_actions($actions) {
894 unset($actions['activate'], $actions['preview']);
898 public function install( $package, $args = array() ) {
901 'clear_update_cache' => true,
903 $parsed_args = wp_parse_args( $args, $defaults );
906 $this->install_strings();
908 add_filter('upgrader_source_selection', array($this, 'check_package') );
909 add_filter('upgrader_post_install', array($this, 'check_parent_theme_filter'), 10, 3);
912 'package' => $package,
913 'destination' => get_theme_root(),
914 'clear_destination' => false, //Do not overwrite files.
915 'clear_working' => true,
916 'hook_extra' => array(
918 'action' => 'install',
922 remove_filter('upgrader_source_selection', array($this, 'check_package') );
923 remove_filter('upgrader_post_install', array($this, 'check_parent_theme_filter'));
925 if ( ! $this->result || is_wp_error($this->result) )
926 return $this->result;
928 // Refresh the Theme Update information
929 wp_clean_themes_cache( $parsed_args['clear_update_cache'] );
934 public function upgrade( $theme, $args = array() ) {
937 'clear_update_cache' => true,
939 $parsed_args = wp_parse_args( $args, $defaults );
942 $this->upgrade_strings();
944 // Is an update available?
945 $current = get_site_transient( 'update_themes' );
946 if ( !isset( $current->response[ $theme ] ) ) {
947 $this->skin->before();
948 $this->skin->set_result(false);
949 $this->skin->error( 'up_to_date' );
950 $this->skin->after();
954 $r = $current->response[ $theme ];
956 add_filter('upgrader_pre_install', array($this, 'current_before'), 10, 2);
957 add_filter('upgrader_post_install', array($this, 'current_after'), 10, 2);
958 add_filter('upgrader_clear_destination', array($this, 'delete_old_theme'), 10, 4);
961 'package' => $r['package'],
962 'destination' => get_theme_root( $theme ),
963 'clear_destination' => true,
964 'clear_working' => true,
965 'hook_extra' => array(
968 'action' => 'update',
972 remove_filter('upgrader_pre_install', array($this, 'current_before'));
973 remove_filter('upgrader_post_install', array($this, 'current_after'));
974 remove_filter('upgrader_clear_destination', array($this, 'delete_old_theme'));
976 if ( ! $this->result || is_wp_error($this->result) )
977 return $this->result;
979 wp_clean_themes_cache( $parsed_args['clear_update_cache'] );
984 public function bulk_upgrade( $themes, $args = array() ) {
987 'clear_update_cache' => true,
989 $parsed_args = wp_parse_args( $args, $defaults );
993 $this->upgrade_strings();
995 $current = get_site_transient( 'update_themes' );
997 add_filter('upgrader_pre_install', array($this, 'current_before'), 10, 2);
998 add_filter('upgrader_post_install', array($this, 'current_after'), 10, 2);
999 add_filter('upgrader_clear_destination', array($this, 'delete_old_theme'), 10, 4);
1001 $this->skin->header();
1003 // Connect to the Filesystem first.
1004 $res = $this->fs_connect( array(WP_CONTENT_DIR) );
1006 $this->skin->footer();
1010 $this->skin->bulk_header();
1012 // Only start maintenance mode if:
1013 // - running Multisite and there are one or more themes specified, OR
1014 // - a theme with an update available is currently in use.
1015 // @TODO: For multisite, maintenance mode should only kick in for individual sites if at all possible.
1016 $maintenance = ( is_multisite() && ! empty( $themes ) );
1017 foreach ( $themes as $theme )
1018 $maintenance = $maintenance || $theme == get_stylesheet() || $theme == get_template();
1020 $this->maintenance_mode(true);
1024 $this->update_count = count($themes);
1025 $this->update_current = 0;
1026 foreach ( $themes as $theme ) {
1027 $this->update_current++;
1029 $this->skin->theme_info = $this->theme_info($theme);
1031 if ( !isset( $current->response[ $theme ] ) ) {
1032 $this->skin->set_result(true);
1033 $this->skin->before();
1034 $this->skin->feedback( 'up_to_date' );
1035 $this->skin->after();
1036 $results[$theme] = true;
1040 // Get the URL to the zip file
1041 $r = $current->response[ $theme ];
1043 $result = $this->run( array(
1044 'package' => $r['package'],
1045 'destination' => get_theme_root( $theme ),
1046 'clear_destination' => true,
1047 'clear_working' => true,
1049 'hook_extra' => array(
1054 $results[$theme] = $this->result;
1056 // Prevent credentials auth screen from displaying multiple times
1057 if ( false === $result )
1059 } //end foreach $plugins
1061 $this->maintenance_mode(false);
1063 /** This action is documented in wp-admin/includes/class-wp-upgrader.php */
1064 do_action( 'upgrader_process_complete', $this, array(
1065 'action' => 'update',
1068 'themes' => $themes,
1071 $this->skin->bulk_footer();
1073 $this->skin->footer();
1075 // Cleanup our hooks, in case something else does a upgrade on this connection.
1076 remove_filter('upgrader_pre_install', array($this, 'current_before'));
1077 remove_filter('upgrader_post_install', array($this, 'current_after'));
1078 remove_filter('upgrader_clear_destination', array($this, 'delete_old_theme'));
1080 // Refresh the Theme Update information
1081 wp_clean_themes_cache( $parsed_args['clear_update_cache'] );
1086 public function check_package($source) {
1087 global $wp_filesystem;
1089 if ( is_wp_error($source) )
1092 // Check the folder contains a valid theme
1093 $working_directory = str_replace( $wp_filesystem->wp_content_dir(), trailingslashit(WP_CONTENT_DIR), $source);
1094 if ( ! is_dir($working_directory) ) // Sanity check, if the above fails, lets not prevent installation.
1097 // A proper archive should have a style.css file in the single subdirectory
1098 if ( ! file_exists( $working_directory . 'style.css' ) )
1099 return new WP_Error( 'incompatible_archive_theme_no_style', $this->strings['incompatible_archive'], __( 'The theme is missing the <code>style.css</code> stylesheet.' ) );
1101 $info = get_file_data( $working_directory . 'style.css', array( 'Name' => 'Theme Name', 'Template' => 'Template' ) );
1103 if ( empty( $info['Name'] ) )
1104 return new WP_Error( 'incompatible_archive_theme_no_name', $this->strings['incompatible_archive'], __( "The <code>style.css</code> stylesheet doesn't contain a valid theme header." ) );
1106 // If it's not a child theme, it must have at least an index.php to be legit.
1107 if ( empty( $info['Template'] ) && ! file_exists( $working_directory . 'index.php' ) )
1108 return new WP_Error( 'incompatible_archive_theme_no_index', $this->strings['incompatible_archive'], __( 'The theme is missing the <code>index.php</code> file.' ) );
1113 public function current_before($return, $theme) {
1115 if ( is_wp_error($return) )
1118 $theme = isset($theme['theme']) ? $theme['theme'] : '';
1120 if ( $theme != get_stylesheet() ) //If not current
1122 //Change to maintenance mode now.
1123 if ( ! $this->bulk )
1124 $this->maintenance_mode(true);
1129 public function current_after($return, $theme) {
1130 if ( is_wp_error($return) )
1133 $theme = isset($theme['theme']) ? $theme['theme'] : '';
1135 if ( $theme != get_stylesheet() ) // If not current
1138 // Ensure stylesheet name hasn't changed after the upgrade:
1139 if ( $theme == get_stylesheet() && $theme != $this->result['destination_name'] ) {
1140 wp_clean_themes_cache();
1141 $stylesheet = $this->result['destination_name'];
1142 switch_theme( $stylesheet );
1145 //Time to remove maintenance mode
1146 if ( ! $this->bulk )
1147 $this->maintenance_mode(false);
1151 public function delete_old_theme( $removed, $local_destination, $remote_destination, $theme ) {
1152 global $wp_filesystem;
1154 if ( is_wp_error( $removed ) )
1155 return $removed; // Pass errors through.
1157 if ( ! isset( $theme['theme'] ) )
1160 $theme = $theme['theme'];
1161 $themes_dir = trailingslashit( $wp_filesystem->wp_themes_dir( $theme ) );
1162 if ( $wp_filesystem->exists( $themes_dir . $theme ) ) {
1163 if ( ! $wp_filesystem->delete( $themes_dir . $theme, true ) )
1170 public function theme_info($theme = null) {
1172 if ( empty($theme) ) {
1173 if ( !empty($this->result['destination_name']) )
1174 $theme = $this->result['destination_name'];
1178 return wp_get_theme( $theme );
1183 add_action( 'upgrader_process_complete', array( 'Language_Pack_Upgrader', 'async_upgrade' ), 20 );
1186 * Language pack upgrader, for updating translations of plugins, themes, and core.
1188 * @package WordPress
1189 * @subpackage Upgrader
1192 class Language_Pack_Upgrader extends WP_Upgrader {
1195 public $bulk = true;
1197 public static function async_upgrade( $upgrader = false ) {
1199 if ( $upgrader && $upgrader instanceof Language_Pack_Upgrader ) {
1204 $language_updates = wp_get_translation_updates();
1205 if ( ! $language_updates ) {
1209 // Avoid messing with VCS installs, at least for now.
1210 // Noted: this is not the ideal way to accomplish this.
1211 $check_vcs = new WP_Automatic_Updater;
1212 if ( $check_vcs->is_vcs_checkout( WP_CONTENT_DIR ) ) {
1216 foreach ( $language_updates as $key => $language_update ) {
1217 $update = ! empty( $language_update->autoupdate );
1220 * Filter whether to asynchronously update translation for core, a plugin, or a theme.
1224 * @param bool $update Whether to update.
1225 * @param object $language_update The update offer.
1227 $update = apply_filters( 'async_update_translation', $update, $language_update );
1230 unset( $language_updates[ $key ] );
1234 if ( empty( $language_updates ) ) {
1238 $skin = new Language_Pack_Upgrader_Skin( array(
1239 'skip_header_footer' => true,
1242 $lp_upgrader = new Language_Pack_Upgrader( $skin );
1243 $lp_upgrader->bulk_upgrade( $language_updates );
1246 public function upgrade_strings() {
1247 $this->strings['starting_upgrade'] = __( 'Some of your translations need updating. Sit tight for a few more seconds while we update them as well.' );
1248 $this->strings['up_to_date'] = __( 'The translation is up to date.' ); // We need to silently skip this case
1249 $this->strings['no_package'] = __( 'Update package not available.' );
1250 $this->strings['downloading_package'] = __( 'Downloading translation from <span class="code">%s</span>…' );
1251 $this->strings['unpack_package'] = __( 'Unpacking the update…' );
1252 $this->strings['process_failed'] = __( 'Translation update failed.' );
1253 $this->strings['process_success'] = __( 'Translation updated successfully.' );
1256 public function upgrade( $update = false, $args = array() ) {
1258 $update = array( $update );
1261 $results = $this->bulk_upgrade( $update, $args );
1263 if ( ! is_array( $results ) ) {
1270 public function bulk_upgrade( $language_updates = array(), $args = array() ) {
1271 global $wp_filesystem;
1274 'clear_update_cache' => true,
1276 $parsed_args = wp_parse_args( $args, $defaults );
1279 $this->upgrade_strings();
1281 if ( ! $language_updates )
1282 $language_updates = wp_get_translation_updates();
1284 if ( empty( $language_updates ) ) {
1285 $this->skin->header();
1286 $this->skin->before();
1287 $this->skin->set_result( true );
1288 $this->skin->feedback( 'up_to_date' );
1289 $this->skin->after();
1290 $this->skin->bulk_footer();
1291 $this->skin->footer();
1295 if ( 'upgrader_process_complete' == current_filter() )
1296 $this->skin->feedback( 'starting_upgrade' );
1298 // Remove any existing upgrade filters from the plugin/theme upgraders #WP29425 & #WP29230
1299 remove_all_filters( 'upgrader_pre_install' );
1300 remove_all_filters( 'upgrader_clear_destination' );
1301 remove_all_filterS( 'upgrader_post_install' );
1302 remove_all_filters( 'upgrader_source_selection' );
1304 add_filter( 'upgrader_source_selection', array( $this, 'check_package' ), 10, 2 );
1306 $this->skin->header();
1308 // Connect to the Filesystem first.
1309 $res = $this->fs_connect( array( WP_CONTENT_DIR, WP_LANG_DIR ) );
1311 $this->skin->footer();
1317 $this->update_count = count( $language_updates );
1318 $this->update_current = 0;
1321 * The filesystem's mkdir() is not recursive. Make sure WP_LANG_DIR exists,
1322 * as we then may need to create a /plugins or /themes directory inside of it.
1324 $remote_destination = $wp_filesystem->find_folder( WP_LANG_DIR );
1325 if ( ! $wp_filesystem->exists( $remote_destination ) )
1326 if ( ! $wp_filesystem->mkdir( $remote_destination, FS_CHMOD_DIR ) )
1327 return new WP_Error( 'mkdir_failed_lang_dir', $this->strings['mkdir_failed'], $remote_destination );
1329 foreach ( $language_updates as $language_update ) {
1331 $this->skin->language_update = $language_update;
1333 $destination = WP_LANG_DIR;
1334 if ( 'plugin' == $language_update->type )
1335 $destination .= '/plugins';
1336 elseif ( 'theme' == $language_update->type )
1337 $destination .= '/themes';
1339 $this->update_current++;
1342 'package' => $language_update->package,
1343 'destination' => $destination,
1344 'clear_destination' => false,
1345 'abort_if_destination_exists' => false, // We expect the destination to exist.
1346 'clear_working' => true,
1348 'hook_extra' => array(
1349 'language_update_type' => $language_update->type,
1350 'language_update' => $language_update,
1354 $result = $this->run( $options );
1356 $results[] = $this->result;
1358 // Prevent credentials auth screen from displaying multiple times.
1359 if ( false === $result )
1363 $this->skin->bulk_footer();
1365 $this->skin->footer();
1367 // Clean up our hooks, in case something else does an upgrade on this connection.
1368 remove_filter( 'upgrader_source_selection', array( $this, 'check_package' ) );
1370 if ( $parsed_args['clear_update_cache'] ) {
1371 wp_clean_themes_cache( true );
1372 wp_clean_plugins_cache( true );
1373 delete_site_transient( 'update_core' );
1379 public function check_package( $source, $remote_source ) {
1380 global $wp_filesystem;
1382 if ( is_wp_error( $source ) )
1385 // Check that the folder contains a valid language.
1386 $files = $wp_filesystem->dirlist( $remote_source );
1388 // Check to see if a .po and .mo exist in the folder.
1390 foreach ( (array) $files as $file => $filedata ) {
1391 if ( '.po' == substr( $file, -3 ) )
1393 elseif ( '.mo' == substr( $file, -3 ) )
1397 if ( ! $mo || ! $po )
1398 return new WP_Error( 'incompatible_archive_pomo', $this->strings['incompatible_archive'],
1399 __( 'The language pack is missing either the <code>.po</code> or <code>.mo</code> files.' ) );
1404 public function get_name_for_update( $update ) {
1405 switch ( $update->type ) {
1407 return 'WordPress'; // Not translated
1410 $theme = wp_get_theme( $update->slug );
1411 if ( $theme->exists() )
1412 return $theme->Get( 'Name' );
1415 $plugin_data = get_plugins( '/' . $update->slug );
1416 $plugin_data = array_shift( $plugin_data );
1418 return $plugin_data['Name'];
1427 * Core Upgrader class for WordPress. It allows for WordPress to upgrade itself in combination with the wp-admin/includes/update-core.php file
1429 * @package WordPress
1430 * @subpackage Upgrader
1433 class Core_Upgrader extends WP_Upgrader {
1435 public function upgrade_strings() {
1436 $this->strings['up_to_date'] = __('WordPress is at the latest version.');
1437 $this->strings['no_package'] = __('Update package not available.');
1438 $this->strings['downloading_package'] = __('Downloading update from <span class="code">%s</span>…');
1439 $this->strings['unpack_package'] = __('Unpacking the update…');
1440 $this->strings['copy_failed'] = __('Could not copy files.');
1441 $this->strings['copy_failed_space'] = __('Could not copy files. You may have run out of disk space.' );
1442 $this->strings['start_rollback'] = __( 'Attempting to roll back to previous version.' );
1443 $this->strings['rollback_was_required'] = __( 'Due to an error during updating, WordPress has rolled back to your previous version.' );
1446 public function upgrade( $current, $args = array() ) {
1447 global $wp_filesystem;
1449 include( ABSPATH . WPINC . '/version.php' ); // $wp_version;
1451 $start_time = time();
1454 'pre_check_md5' => true,
1455 'attempt_rollback' => false,
1456 'do_rollback' => false,
1458 $parsed_args = wp_parse_args( $args, $defaults );
1461 $this->upgrade_strings();
1463 // Is an update available?
1464 if ( !isset( $current->response ) || $current->response == 'latest' )
1465 return new WP_Error('up_to_date', $this->strings['up_to_date']);
1467 $res = $this->fs_connect( array(ABSPATH, WP_CONTENT_DIR) );
1468 if ( ! $res || is_wp_error( $res ) ) {
1472 $wp_dir = trailingslashit($wp_filesystem->abspath());
1475 if ( $parsed_args['do_rollback'] )
1477 elseif ( $parsed_args['pre_check_md5'] && ! $this->check_files() )
1481 * If partial update is returned from the API, use that, unless we're doing
1482 * a reinstall. If we cross the new_bundled version number, then use
1483 * the new_bundled zip. Don't though if the constant is set to skip bundled items.
1484 * If the API returns a no_content zip, go with it. Finally, default to the full zip.
1486 if ( $parsed_args['do_rollback'] && $current->packages->rollback )
1487 $to_download = 'rollback';
1488 elseif ( $current->packages->partial && 'reinstall' != $current->response && $wp_version == $current->partial_version && $partial )
1489 $to_download = 'partial';
1490 elseif ( $current->packages->new_bundled && version_compare( $wp_version, $current->new_bundled, '<' )
1491 && ( ! defined( 'CORE_UPGRADE_SKIP_NEW_BUNDLED' ) || ! CORE_UPGRADE_SKIP_NEW_BUNDLED ) )
1492 $to_download = 'new_bundled';
1493 elseif ( $current->packages->no_content )
1494 $to_download = 'no_content';
1496 $to_download = 'full';
1498 $download = $this->download_package( $current->packages->$to_download );
1499 if ( is_wp_error($download) )
1502 $working_dir = $this->unpack_package( $download );
1503 if ( is_wp_error($working_dir) )
1504 return $working_dir;
1506 // Copy update-core.php from the new version into place.
1507 if ( !$wp_filesystem->copy($working_dir . '/wordpress/wp-admin/includes/update-core.php', $wp_dir . 'wp-admin/includes/update-core.php', true) ) {
1508 $wp_filesystem->delete($working_dir, true);
1509 return new WP_Error( 'copy_failed_for_update_core_file', __( 'The update cannot be installed because we will be unable to copy some files. This is usually due to inconsistent file permissions.' ), 'wp-admin/includes/update-core.php' );
1511 $wp_filesystem->chmod($wp_dir . 'wp-admin/includes/update-core.php', FS_CHMOD_FILE);
1513 require_once( ABSPATH . 'wp-admin/includes/update-core.php' );
1515 if ( ! function_exists( 'update_core' ) )
1516 return new WP_Error( 'copy_failed_space', $this->strings['copy_failed_space'] );
1518 $result = update_core( $working_dir, $wp_dir );
1520 // In the event of an issue, we may be able to roll back.
1521 if ( $parsed_args['attempt_rollback'] && $current->packages->rollback && ! $parsed_args['do_rollback'] ) {
1522 $try_rollback = false;
1523 if ( is_wp_error( $result ) ) {
1524 $error_code = $result->get_error_code();
1526 * Not all errors are equal. These codes are critical: copy_failed__copy_dir,
1527 * mkdir_failed__copy_dir, copy_failed__copy_dir_retry, and disk_full.
1528 * do_rollback allows for update_core() to trigger a rollback if needed.
1530 if ( false !== strpos( $error_code, 'do_rollback' ) )
1531 $try_rollback = true;
1532 elseif ( false !== strpos( $error_code, '__copy_dir' ) )
1533 $try_rollback = true;
1534 elseif ( 'disk_full' === $error_code )
1535 $try_rollback = true;
1538 if ( $try_rollback ) {
1539 /** This filter is documented in wp-admin/includes/update-core.php */
1540 apply_filters( 'update_feedback', $result );
1542 /** This filter is documented in wp-admin/includes/update-core.php */
1543 apply_filters( 'update_feedback', $this->strings['start_rollback'] );
1545 $rollback_result = $this->upgrade( $current, array_merge( $parsed_args, array( 'do_rollback' => true ) ) );
1547 $original_result = $result;
1548 $result = new WP_Error( 'rollback_was_required', $this->strings['rollback_was_required'], (object) array( 'update' => $original_result, 'rollback' => $rollback_result ) );
1552 /** This action is documented in wp-admin/includes/class-wp-upgrader.php */
1553 do_action( 'upgrader_process_complete', $this, array( 'action' => 'update', 'type' => 'core' ) );
1555 // Clear the current updates
1556 delete_site_transient( 'update_core' );
1558 if ( ! $parsed_args['do_rollback'] ) {
1560 'update_type' => $current->response,
1562 'fs_method' => $wp_filesystem->method,
1563 'fs_method_forced' => defined( 'FS_METHOD' ) || has_filter( 'filesystem_method' ),
1564 'time_taken' => time() - $start_time,
1565 'reported' => $wp_version,
1566 'attempted' => $current->version,
1569 if ( is_wp_error( $result ) ) {
1570 $stats['success'] = false;
1571 // Did a rollback occur?
1572 if ( ! empty( $try_rollback ) ) {
1573 $stats['error_code'] = $original_result->get_error_code();
1574 $stats['error_data'] = $original_result->get_error_data();
1575 // Was the rollback successful? If not, collect its error too.
1576 $stats['rollback'] = ! is_wp_error( $rollback_result );
1577 if ( is_wp_error( $rollback_result ) ) {
1578 $stats['rollback_code'] = $rollback_result->get_error_code();
1579 $stats['rollback_data'] = $rollback_result->get_error_data();
1582 $stats['error_code'] = $result->get_error_code();
1583 $stats['error_data'] = $result->get_error_data();
1587 wp_version_check( $stats );
1593 // Determines if this WordPress Core version should update to $offered_ver or not
1594 public static function should_update_to_version( $offered_ver /* x.y.z */ ) {
1595 include( ABSPATH . WPINC . '/version.php' ); // $wp_version; // x.y.z
1597 $current_branch = implode( '.', array_slice( preg_split( '/[.-]/', $wp_version ), 0, 2 ) ); // x.y
1598 $new_branch = implode( '.', array_slice( preg_split( '/[.-]/', $offered_ver ), 0, 2 ) ); // x.y
1599 $current_is_development_version = (bool) strpos( $wp_version, '-' );
1602 $upgrade_dev = true;
1603 $upgrade_minor = true;
1604 $upgrade_major = false;
1606 // WP_AUTO_UPDATE_CORE = true (all), 'minor', false.
1607 if ( defined( 'WP_AUTO_UPDATE_CORE' ) ) {
1608 if ( false === WP_AUTO_UPDATE_CORE ) {
1609 // Defaults to turned off, unless a filter allows it
1610 $upgrade_dev = $upgrade_minor = $upgrade_major = false;
1611 } elseif ( true === WP_AUTO_UPDATE_CORE ) {
1612 // ALL updates for core
1613 $upgrade_dev = $upgrade_minor = $upgrade_major = true;
1614 } elseif ( 'minor' === WP_AUTO_UPDATE_CORE ) {
1615 // Only minor updates for core
1616 $upgrade_dev = $upgrade_major = false;
1617 $upgrade_minor = true;
1621 // 1: If we're already on that version, not much point in updating?
1622 if ( $offered_ver == $wp_version )
1625 // 2: If we're running a newer version, that's a nope
1626 if ( version_compare( $wp_version, $offered_ver, '>' ) )
1629 $failure_data = get_site_option( 'auto_core_update_failed' );
1630 if ( $failure_data ) {
1631 // If this was a critical update failure, cannot update.
1632 if ( ! empty( $failure_data['critical'] ) )
1635 // Don't claim we can update on update-core.php if we have a non-critical failure logged.
1636 if ( $wp_version == $failure_data['current'] && false !== strpos( $offered_ver, '.1.next.minor' ) )
1639 // Cannot update if we're retrying the same A to B update that caused a non-critical failure.
1640 // Some non-critical failures do allow retries, like download_failed.
1641 // 3.7.1 => 3.7.2 resulted in files_not_writable, if we are still on 3.7.1 and still trying to update to 3.7.2.
1642 if ( empty( $failure_data['retry'] ) && $wp_version == $failure_data['current'] && $offered_ver == $failure_data['attempted'] )
1646 // 3: 3.7-alpha-25000 -> 3.7-alpha-25678 -> 3.7-beta1 -> 3.7-beta2
1647 if ( $current_is_development_version ) {
1650 * Filter whether to enable automatic core updates for development versions.
1654 * @param bool $upgrade_dev Whether to enable automatic updates for
1655 * development versions.
1657 if ( ! apply_filters( 'allow_dev_auto_core_updates', $upgrade_dev ) )
1659 // Else fall through to minor + major branches below.
1662 // 4: Minor In-branch updates (3.7.0 -> 3.7.1 -> 3.7.2 -> 3.7.4)
1663 if ( $current_branch == $new_branch ) {
1666 * Filter whether to enable minor automatic core updates.
1670 * @param bool $upgrade_minor Whether to enable minor automatic core updates.
1672 return apply_filters( 'allow_minor_auto_core_updates', $upgrade_minor );
1675 // 5: Major version updates (3.7.0 -> 3.8.0 -> 3.9.1)
1676 if ( version_compare( $new_branch, $current_branch, '>' ) ) {
1679 * Filter whether to enable major automatic core updates.
1683 * @param bool $upgrade_major Whether to enable major automatic core updates.
1685 return apply_filters( 'allow_major_auto_core_updates', $upgrade_major );
1688 // If we're not sure, we don't want it
1692 public function check_files() {
1693 global $wp_version, $wp_local_package;
1695 $checksums = get_core_checksums( $wp_version, isset( $wp_local_package ) ? $wp_local_package : 'en_US' );
1697 if ( ! is_array( $checksums ) )
1700 foreach ( $checksums as $file => $checksum ) {
1701 // Skip files which get updated
1702 if ( 'wp-content' == substr( $file, 0, 10 ) )
1704 if ( ! file_exists( ABSPATH . $file ) || md5_file( ABSPATH . $file ) !== $checksum )
1713 * Upgrade Skin helper for File uploads. This class handles the upload process and passes it as if it's a local file to the Upgrade/Installer functions.
1715 * @package WordPress
1716 * @subpackage Upgrader
1719 class File_Upload_Upgrader {
1724 public function __construct($form, $urlholder) {
1726 if ( empty($_FILES[$form]['name']) && empty($_GET[$urlholder]) )
1727 wp_die(__('Please select a file'));
1729 //Handle a newly uploaded file, Else assume it's already been uploaded
1730 if ( ! empty($_FILES) ) {
1731 $overrides = array( 'test_form' => false, 'test_type' => false );
1732 $file = wp_handle_upload( $_FILES[$form], $overrides );
1734 if ( isset( $file['error'] ) )
1735 wp_die( $file['error'] );
1737 $this->filename = $_FILES[$form]['name'];
1738 $this->package = $file['file'];
1740 // Construct the object array
1742 'post_title' => $this->filename,
1743 'post_content' => $file['url'],
1744 'post_mime_type' => $file['type'],
1745 'guid' => $file['url'],
1746 'context' => 'upgrader',
1747 'post_status' => 'private'
1751 $this->id = wp_insert_attachment( $object, $file['file'] );
1753 // Schedule a cleanup for 2 hours from now in case of failed install.
1754 wp_schedule_single_event( time() + 2 * HOUR_IN_SECONDS, 'upgrader_scheduled_cleanup', array( $this->id ) );
1756 } elseif ( is_numeric( $_GET[$urlholder] ) ) {
1757 // Numeric Package = previously uploaded file, see above.
1758 $this->id = (int) $_GET[$urlholder];
1759 $attachment = get_post( $this->id );
1760 if ( empty($attachment) )
1761 wp_die(__('Please select a file'));
1763 $this->filename = $attachment->post_title;
1764 $this->package = get_attached_file( $attachment->ID );
1766 // Else, It's set to something, Back compat for plugins using the old (pre-3.3) File_Uploader handler.
1767 if ( ! ( ( $uploads = wp_upload_dir() ) && false === $uploads['error'] ) )
1768 wp_die( $uploads['error'] );
1770 $this->filename = $_GET[$urlholder];
1771 $this->package = $uploads['basedir'] . '/' . $this->filename;
1775 public function cleanup() {
1777 wp_delete_attachment( $this->id );
1779 elseif ( file_exists( $this->package ) )
1780 return @unlink( $this->package );
1787 * The WordPress automatic background updater.
1789 * @package WordPress
1790 * @subpackage Upgrader
1793 class WP_Automatic_Updater {
1796 * Tracks update results during processing.
1800 protected $update_results = array();
1803 * Whether the entire automatic updater is disabled.
1807 public function is_disabled() {
1808 // Background updates are disabled if you don't want file changes.
1809 if ( defined( 'DISALLOW_FILE_MODS' ) && DISALLOW_FILE_MODS )
1812 if ( defined( 'WP_INSTALLING' ) )
1815 // More fine grained control can be done through the WP_AUTO_UPDATE_CORE constant and filters.
1816 $disabled = defined( 'AUTOMATIC_UPDATER_DISABLED' ) && AUTOMATIC_UPDATER_DISABLED;
1819 * Filter whether to entirely disable background updates.
1821 * There are more fine-grained filters and controls for selective disabling.
1822 * This filter parallels the AUTOMATIC_UPDATER_DISABLED constant in name.
1824 * This also disables update notification emails. That may change in the future.
1828 * @param bool $disabled Whether the updater should be disabled.
1830 return apply_filters( 'automatic_updater_disabled', $disabled );
1834 * Check for version control checkouts.
1836 * Checks for Subversion, Git, Mercurial, and Bazaar. It recursively looks up the
1837 * filesystem to the top of the drive, erring on the side of detecting a VCS
1838 * checkout somewhere.
1840 * ABSPATH is always checked in addition to whatever $context is (which may be the
1841 * wp-content directory, for example). The underlying assumption is that if you are
1842 * using version control *anywhere*, then you should be making decisions for
1843 * how things get updated.
1847 * @param string $context The filesystem path to check, in addition to ABSPATH.
1849 public function is_vcs_checkout( $context ) {
1850 $context_dirs = array( untrailingslashit( $context ) );
1851 if ( $context !== ABSPATH )
1852 $context_dirs[] = untrailingslashit( ABSPATH );
1854 $vcs_dirs = array( '.svn', '.git', '.hg', '.bzr' );
1855 $check_dirs = array();
1857 foreach ( $context_dirs as $context_dir ) {
1858 // Walk up from $context_dir to the root.
1860 $check_dirs[] = $context_dir;
1862 // Once we've hit '/' or 'C:\', we need to stop. dirname will keep returning the input here.
1863 if ( $context_dir == dirname( $context_dir ) )
1866 // Continue one level at a time.
1867 } while ( $context_dir = dirname( $context_dir ) );
1870 $check_dirs = array_unique( $check_dirs );
1872 // Search all directories we've found for evidence of version control.
1873 foreach ( $vcs_dirs as $vcs_dir ) {
1874 foreach ( $check_dirs as $check_dir ) {
1875 if ( $checkout = @is_dir( rtrim( $check_dir, '\\/' ) . "/$vcs_dir" ) )
1881 * Filter whether the automatic updater should consider a filesystem
1882 * location to be potentially managed by a version control system.
1886 * @param bool $checkout Whether a VCS checkout was discovered at $context
1887 * or ABSPATH, or anywhere higher.
1888 * @param string $context The filesystem context (a path) against which
1889 * filesystem status should be checked.
1891 return apply_filters( 'automatic_updates_is_vcs_checkout', $checkout, $context );
1895 * Tests to see if we can and should update a specific item.
1899 * @param string $type The type of update being checked: 'core', 'theme',
1900 * 'plugin', 'translation'.
1901 * @param object $item The update offer.
1902 * @param string $context The filesystem context (a path) against which filesystem
1903 * access and status should be checked.
1905 public function should_update( $type, $item, $context ) {
1906 // Used to see if WP_Filesystem is set up to allow unattended updates.
1907 $skin = new Automatic_Upgrader_Skin;
1909 if ( $this->is_disabled() )
1912 // If we can't do an auto core update, we may still be able to email the user.
1913 if ( ! $skin->request_filesystem_credentials( false, $context ) || $this->is_vcs_checkout( $context ) ) {
1914 if ( 'core' == $type )
1915 $this->send_core_update_notification_email( $item );
1919 // Next up, is this an item we can update?
1920 if ( 'core' == $type )
1921 $update = Core_Upgrader::should_update_to_version( $item->current );
1923 $update = ! empty( $item->autoupdate );
1926 * Filter whether to automatically update core, a plugin, a theme, or a language.
1928 * The dynamic portion of the hook name, $type, refers to the type of update
1929 * being checked. Can be 'core', 'theme', 'plugin', or 'translation'.
1931 * Generally speaking, plugins, themes, and major core versions are not updated
1932 * by default, while translations and minor and development versions for core
1933 * are updated by default.
1935 * See the allow_dev_auto_core_updates, allow_minor_auto_core_updates, and
1936 * allow_major_auto_core_updates filters for a more straightforward way to
1937 * adjust core updates.
1941 * @param bool $update Whether to update.
1942 * @param object $item The update offer.
1944 $update = apply_filters( 'auto_update_' . $type, $update, $item );
1947 if ( 'core' == $type )
1948 $this->send_core_update_notification_email( $item );
1952 // If it's a core update, are we actually compatible with its requirements?
1953 if ( 'core' == $type ) {
1956 $php_compat = version_compare( phpversion(), $item->php_version, '>=' );
1957 if ( file_exists( WP_CONTENT_DIR . '/db.php' ) && empty( $wpdb->is_mysql ) )
1958 $mysql_compat = true;
1960 $mysql_compat = version_compare( $wpdb->db_version(), $item->mysql_version, '>=' );
1962 if ( ! $php_compat || ! $mysql_compat )
1970 * Notifies an administrator of a core update.
1974 * @param object $item The update offer.
1976 protected function send_core_update_notification_email( $item ) {
1977 $notified = get_site_option( 'auto_core_update_notified' );
1979 // Don't notify if we've already notified the same email address of the same version.
1980 if ( $notified && $notified['email'] == get_site_option( 'admin_email' ) && $notified['version'] == $item->current )
1983 // See if we need to notify users of a core update.
1984 $notify = ! empty( $item->notify_email );
1987 * Filter whether to notify the site administrator of a new core update.
1989 * By default, administrators are notified when the update offer received
1990 * from WordPress.org sets a particular flag. This allows some discretion
1991 * in if and when to notify.
1993 * This filter is only evaluated once per release. If the same email address
1994 * was already notified of the same new version, WordPress won't repeatedly
1995 * email the administrator.
1997 * This filter is also used on about.php to check if a plugin has disabled
1998 * these notifications.
2002 * @param bool $notify Whether the site administrator is notified.
2003 * @param object $item The update offer.
2005 if ( ! apply_filters( 'send_core_update_notification_email', $notify, $item ) )
2008 $this->send_email( 'manual', $item );
2013 * Update an item, if appropriate.
2017 * @param string $type The type of update being checked: 'core', 'theme', 'plugin', 'translation'.
2018 * @param object $item The update offer.
2020 public function update( $type, $item ) {
2021 $skin = new Automatic_Upgrader_Skin;
2025 // The Core upgrader doesn't use the Upgrader's skin during the actual main part of the upgrade, instead, firing a filter.
2026 add_filter( 'update_feedback', array( $skin, 'feedback' ) );
2027 $upgrader = new Core_Upgrader( $skin );
2031 $upgrader = new Plugin_Upgrader( $skin );
2032 $context = WP_PLUGIN_DIR; // We don't support custom Plugin directories, or updates for WPMU_PLUGIN_DIR
2035 $upgrader = new Theme_Upgrader( $skin );
2036 $context = get_theme_root( $item->theme );
2039 $upgrader = new Language_Pack_Upgrader( $skin );
2040 $context = WP_CONTENT_DIR; // WP_LANG_DIR;
2044 // Determine whether we can and should perform this update.
2045 if ( ! $this->should_update( $type, $item, $context ) )
2048 $upgrader_item = $item;
2051 $skin->feedback( __( 'Updating to WordPress %s' ), $item->version );
2052 $item_name = sprintf( __( 'WordPress %s' ), $item->version );
2055 $upgrader_item = $item->theme;
2056 $theme = wp_get_theme( $upgrader_item );
2057 $item_name = $theme->Get( 'Name' );
2058 $skin->feedback( __( 'Updating theme: %s' ), $item_name );
2061 $upgrader_item = $item->plugin;
2062 $plugin_data = get_plugin_data( $context . '/' . $upgrader_item );
2063 $item_name = $plugin_data['Name'];
2064 $skin->feedback( __( 'Updating plugin: %s' ), $item_name );
2067 $language_item_name = $upgrader->get_name_for_update( $item );
2068 $item_name = sprintf( __( 'Translations for %s' ), $language_item_name );
2069 $skin->feedback( sprintf( __( 'Updating translations for %1$s (%2$s)…' ), $language_item_name, $item->language ) );
2073 // Boom, This sites about to get a whole new splash of paint!
2074 $upgrade_result = $upgrader->upgrade( $upgrader_item, array(
2075 'clear_update_cache' => false,
2076 // Always use partial builds if possible for core updates.
2077 'pre_check_md5' => false,
2078 // Only available for core updates.
2079 'attempt_rollback' => true,
2082 // If the filesystem is unavailable, false is returned.
2083 if ( false === $upgrade_result ) {
2084 $upgrade_result = new WP_Error( 'fs_unavailable', __( 'Could not access filesystem.' ) );
2087 // Core doesn't output this, so lets append it so we don't get confused.
2088 if ( 'core' == $type ) {
2089 if ( is_wp_error( $upgrade_result ) ) {
2090 $skin->error( __( 'Installation Failed' ), $upgrade_result );
2092 $skin->feedback( __( 'WordPress updated successfully' ) );
2096 $this->update_results[ $type ][] = (object) array(
2098 'result' => $upgrade_result,
2099 'name' => $item_name,
2100 'messages' => $skin->get_upgrade_messages()
2103 return $upgrade_result;
2107 * Kicks off the background update process, looping through all pending updates.
2111 public function run() {
2112 global $wpdb, $wp_version;
2114 if ( $this->is_disabled() )
2117 if ( ! is_main_network() || ! is_main_site() )
2120 $lock_name = 'auto_updater.lock';
2123 $lock_result = $wpdb->query( $wpdb->prepare( "INSERT IGNORE INTO `$wpdb->options` ( `option_name`, `option_value`, `autoload` ) VALUES (%s, %s, 'no') /* LOCK */", $lock_name, time() ) );
2125 if ( ! $lock_result ) {
2126 $lock_result = get_option( $lock_name );
2128 // If we couldn't create a lock, and there isn't a lock, bail
2129 if ( ! $lock_result )
2132 // Check to see if the lock is still valid
2133 if ( $lock_result > ( time() - HOUR_IN_SECONDS ) )
2137 // Update the lock, as by this point we've definitely got a lock, just need to fire the actions
2138 update_option( $lock_name, time() );
2140 // Don't automatically run these thins, as we'll handle it ourselves
2141 remove_action( 'upgrader_process_complete', array( 'Language_Pack_Upgrader', 'async_upgrade' ), 20 );
2142 remove_action( 'upgrader_process_complete', 'wp_version_check' );
2143 remove_action( 'upgrader_process_complete', 'wp_update_plugins' );
2144 remove_action( 'upgrader_process_complete', 'wp_update_themes' );
2147 wp_update_plugins(); // Check for Plugin updates
2148 $plugin_updates = get_site_transient( 'update_plugins' );
2149 if ( $plugin_updates && !empty( $plugin_updates->response ) ) {
2150 foreach ( $plugin_updates->response as $plugin ) {
2151 $this->update( 'plugin', $plugin );
2153 // Force refresh of plugin update information
2154 wp_clean_plugins_cache();
2157 // Next, those themes we all love
2158 wp_update_themes(); // Check for Theme updates
2159 $theme_updates = get_site_transient( 'update_themes' );
2160 if ( $theme_updates && !empty( $theme_updates->response ) ) {
2161 foreach ( $theme_updates->response as $theme ) {
2162 $this->update( 'theme', (object) $theme );
2164 // Force refresh of theme update information
2165 wp_clean_themes_cache();
2168 // Next, Process any core update
2169 wp_version_check(); // Check for Core updates
2170 $core_update = find_core_auto_update();
2173 $this->update( 'core', $core_update );
2175 // Clean up, and check for any pending translations
2176 // (Core_Upgrader checks for core updates)
2177 $theme_stats = array();
2178 if ( isset( $this->update_results['theme'] ) ) {
2179 foreach ( $this->update_results['theme'] as $upgrade ) {
2180 $theme_stats[ $upgrade->item->theme ] = ( true === $upgrade->result );
2183 wp_update_themes( $theme_stats ); // Check for Theme updates
2185 $plugin_stats = array();
2186 if ( isset( $this->update_results['plugin'] ) ) {
2187 foreach ( $this->update_results['plugin'] as $upgrade ) {
2188 $plugin_stats[ $upgrade->item->plugin ] = ( true === $upgrade->result );
2191 wp_update_plugins( $plugin_stats ); // Check for Plugin updates
2193 // Finally, Process any new translations
2194 $language_updates = wp_get_translation_updates();
2195 if ( $language_updates ) {
2196 foreach ( $language_updates as $update ) {
2197 $this->update( 'translation', $update );
2200 // Clear existing caches
2201 wp_clean_plugins_cache();
2202 wp_clean_themes_cache();
2203 delete_site_transient( 'update_core' );
2205 wp_version_check(); // check for Core updates
2206 wp_update_themes(); // Check for Theme updates
2207 wp_update_plugins(); // Check for Plugin updates
2210 // Send debugging email to all development installs.
2211 if ( ! empty( $this->update_results ) ) {
2212 $development_version = false !== strpos( $wp_version, '-' );
2215 * Filter whether to send a debugging email for each automatic background update.
2219 * @param bool $development_version By default, emails are sent if the
2220 * install is a development version.
2221 * Return false to avoid the email.
2223 if ( apply_filters( 'automatic_updates_send_debug_email', $development_version ) )
2224 $this->send_debug_email();
2226 if ( ! empty( $this->update_results['core'] ) )
2227 $this->after_core_update( $this->update_results['core'][0] );
2230 * Fires after all automatic updates have run.
2234 * @param array $update_results The results of all attempted updates.
2236 do_action( 'automatic_updates_complete', $this->update_results );
2240 delete_option( $lock_name );
2244 * If we tried to perform a core update, check if we should send an email,
2245 * and if we need to avoid processing future updates.
2247 * @param object $update_result The result of the core update. Includes the update offer and result.
2249 protected function after_core_update( $update_result ) {
2252 $core_update = $update_result->item;
2253 $result = $update_result->result;
2255 if ( ! is_wp_error( $result ) ) {
2256 $this->send_email( 'success', $core_update );
2260 $error_code = $result->get_error_code();
2262 // Any of these WP_Error codes are critical failures, as in they occurred after we started to copy core files.
2263 // We should not try to perform a background update again until there is a successful one-click update performed by the user.
2265 if ( $error_code === 'disk_full' || false !== strpos( $error_code, '__copy_dir' ) ) {
2267 } elseif ( $error_code === 'rollback_was_required' && is_wp_error( $result->get_error_data()->rollback ) ) {
2268 // A rollback is only critical if it failed too.
2270 $rollback_result = $result->get_error_data()->rollback;
2271 } elseif ( false !== strpos( $error_code, 'do_rollback' ) ) {
2276 $critical_data = array(
2277 'attempted' => $core_update->current,
2278 'current' => $wp_version,
2279 'error_code' => $error_code,
2280 'error_data' => $result->get_error_data(),
2281 'timestamp' => time(),
2284 if ( isset( $rollback_result ) ) {
2285 $critical_data['rollback_code'] = $rollback_result->get_error_code();
2286 $critical_data['rollback_data'] = $rollback_result->get_error_data();
2288 update_site_option( 'auto_core_update_failed', $critical_data );
2289 $this->send_email( 'critical', $core_update, $result );
2294 * Any other WP_Error code (like download_failed or files_not_writable) occurs before
2295 * we tried to copy over core files. Thus, the failures are early and graceful.
2297 * We should avoid trying to perform a background update again for the same version.
2298 * But we can try again if another version is released.
2300 * For certain 'transient' failures, like download_failed, we should allow retries.
2301 * In fact, let's schedule a special update for an hour from now. (It's possible
2302 * the issue could actually be on WordPress.org's side.) If that one fails, then email.
2305 $transient_failures = array( 'incompatible_archive', 'download_failed', 'insane_distro' );
2306 if ( in_array( $error_code, $transient_failures ) && ! get_site_option( 'auto_core_update_failed' ) ) {
2307 wp_schedule_single_event( time() + HOUR_IN_SECONDS, 'wp_maybe_auto_update' );
2311 $n = get_site_option( 'auto_core_update_notified' );
2312 // Don't notify if we've already notified the same email address of the same version of the same notification type.
2313 if ( $n && 'fail' == $n['type'] && $n['email'] == get_site_option( 'admin_email' ) && $n['version'] == $core_update->current )
2316 update_site_option( 'auto_core_update_failed', array(
2317 'attempted' => $core_update->current,
2318 'current' => $wp_version,
2319 'error_code' => $error_code,
2320 'error_data' => $result->get_error_data(),
2321 'timestamp' => time(),
2322 'retry' => in_array( $error_code, $transient_failures ),
2326 $this->send_email( 'fail', $core_update, $result );
2330 * Sends an email upon the completion or failure of a background core update.
2334 * @param string $type The type of email to send. Can be one of 'success', 'fail', 'manual', 'critical'.
2335 * @param object $core_update The update offer that was attempted.
2336 * @param mixed $result Optional. The result for the core update. Can be WP_Error.
2338 protected function send_email( $type, $core_update, $result = null ) {
2339 update_site_option( 'auto_core_update_notified', array(
2341 'email' => get_site_option( 'admin_email' ),
2342 'version' => $core_update->current,
2343 'timestamp' => time(),
2346 $next_user_core_update = get_preferred_from_update_core();
2347 // If the update transient is empty, use the update we just performed
2348 if ( ! $next_user_core_update )
2349 $next_user_core_update = $core_update;
2350 $newer_version_available = ( 'upgrade' == $next_user_core_update->response && version_compare( $next_user_core_update->version, $core_update->version, '>' ) );
2353 * Filter whether to send an email following an automatic background core update.
2357 * @param bool $send Whether to send the email. Default true.
2358 * @param string $type The type of email to send. Can be one of
2359 * 'success', 'fail', 'critical'.
2360 * @param object $core_update The update offer that was attempted.
2361 * @param mixed $result The result for the core update. Can be WP_Error.
2363 if ( 'manual' !== $type && ! apply_filters( 'auto_core_update_send_email', true, $type, $core_update, $result ) )
2367 case 'success' : // We updated.
2368 /* translators: 1: Site name, 2: WordPress version number. */
2369 $subject = __( '[%1$s] Your site has updated to WordPress %2$s' );
2372 case 'fail' : // We tried to update but couldn't.
2373 case 'manual' : // We can't update (and made no attempt).
2374 /* translators: 1: Site name, 2: WordPress version number. */
2375 $subject = __( '[%1$s] WordPress %2$s is available. Please update!' );
2378 case 'critical' : // We tried to update, started to copy files, then things went wrong.
2379 /* translators: 1: Site name. */
2380 $subject = __( '[%1$s] URGENT: Your site may be down due to a failed update' );
2387 // If the auto update is not to the latest version, say that the current version of WP is available instead.
2388 $version = 'success' === $type ? $core_update->current : $next_user_core_update->current;
2389 $subject = sprintf( $subject, wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES ), $version );
2395 $body .= sprintf( __( 'Howdy! Your site at %1$s has been updated automatically to WordPress %2$s.' ), home_url(), $core_update->current );
2397 if ( ! $newer_version_available )
2398 $body .= __( 'No further action is needed on your part.' ) . ' ';
2400 // Can only reference the About screen if their update was successful.
2401 list( $about_version ) = explode( '-', $core_update->current, 2 );
2402 $body .= sprintf( __( "For more on version %s, see the About WordPress screen:" ), $about_version );
2403 $body .= "\n" . admin_url( 'about.php' );
2405 if ( $newer_version_available ) {
2406 $body .= "\n\n" . sprintf( __( 'WordPress %s is also now available.' ), $next_user_core_update->current ) . ' ';
2407 $body .= __( 'Updating is easy and only takes a few moments:' );
2408 $body .= "\n" . network_admin_url( 'update-core.php' );
2415 $body .= sprintf( __( 'Please update your site at %1$s to WordPress %2$s.' ), home_url(), $next_user_core_update->current );
2419 // Don't show this message if there is a newer version available.
2420 // Potential for confusion, and also not useful for them to know at this point.
2421 if ( 'fail' == $type && ! $newer_version_available )
2422 $body .= __( 'We tried but were unable to update your site automatically.' ) . ' ';
2424 $body .= __( 'Updating is easy and only takes a few moments:' );
2425 $body .= "\n" . network_admin_url( 'update-core.php' );
2429 if ( $newer_version_available )
2430 $body .= sprintf( __( 'Your site at %1$s experienced a critical failure while trying to update WordPress to version %2$s.' ), home_url(), $core_update->current );
2432 $body .= sprintf( __( 'Your site at %1$s experienced a critical failure while trying to update to the latest version of WordPress, %2$s.' ), home_url(), $core_update->current );
2434 $body .= "\n\n" . __( "This means your site may be offline or broken. Don't panic; this can be fixed." );
2436 $body .= "\n\n" . __( "Please check out your site now. It's possible that everything is working. If it says you need to update, you should do so:" );
2437 $body .= "\n" . network_admin_url( 'update-core.php' );
2441 $critical_support = 'critical' === $type && ! empty( $core_update->support_email );
2442 if ( $critical_support ) {
2443 // Support offer if available.
2444 $body .= "\n\n" . sprintf( __( "The WordPress team is willing to help you. Forward this email to %s and the team will work with you to make sure your site is working." ), $core_update->support_email );
2446 // Add a note about the support forums.
2447 $body .= "\n\n" . __( 'If you experience any issues or need support, the volunteers in the WordPress.org support forums may be able to help.' );
2448 $body .= "\n" . __( 'https://wordpress.org/support/' );
2451 // Updates are important!
2452 if ( $type != 'success' || $newer_version_available ) {
2453 $body .= "\n\n" . __( 'Keeping your site updated is important for security. It also makes the internet a safer place for you and your readers.' );
2456 if ( $critical_support ) {
2457 $body .= " " . __( "If you reach out to us, we'll also ensure you'll never have this problem again." );
2460 // If things are successful and we're now on the latest, mention plugins and themes if any are out of date.
2461 if ( $type == 'success' && ! $newer_version_available && ( get_plugin_updates() || get_theme_updates() ) ) {
2462 $body .= "\n\n" . __( 'You also have some plugins or themes with updates available. Update them now:' );
2463 $body .= "\n" . network_admin_url();
2466 $body .= "\n\n" . __( 'The WordPress Team' ) . "\n";
2468 if ( 'critical' == $type && is_wp_error( $result ) ) {
2469 $body .= "\n***\n\n";
2470 $body .= sprintf( __( 'Your site was running version %s.' ), $GLOBALS['wp_version'] );
2471 $body .= ' ' . __( 'We have some data that describes the error your site encountered.' );
2472 $body .= ' ' . __( 'Your hosting company, support forum volunteers, or a friendly developer may be able to use this information to help you:' );
2474 // If we had a rollback and we're still critical, then the rollback failed too.
2475 // Loop through all errors (the main WP_Error, the update result, the rollback result) for code, data, etc.
2476 if ( 'rollback_was_required' == $result->get_error_code() )
2477 $errors = array( $result, $result->get_error_data()->update, $result->get_error_data()->rollback );
2479 $errors = array( $result );
2481 foreach ( $errors as $error ) {
2482 if ( ! is_wp_error( $error ) )
2484 $error_code = $error->get_error_code();
2485 $body .= "\n\n" . sprintf( __( "Error code: %s" ), $error_code );
2486 if ( 'rollback_was_required' == $error_code )
2488 if ( $error->get_error_message() )
2489 $body .= "\n" . $error->get_error_message();
2490 $error_data = $error->get_error_data();
2492 $body .= "\n" . implode( ', ', (array) $error_data );
2497 $to = get_site_option( 'admin_email' );
2500 $email = compact( 'to', 'subject', 'body', 'headers' );
2503 * Filter the email sent following an automatic background core update.
2507 * @param array $email {
2508 * Array of email arguments that will be passed to wp_mail().
2510 * @type string $to The email recipient. An array of emails
2511 * can be returned, as handled by wp_mail().
2512 * @type string $subject The email's subject.
2513 * @type string $body The email message body.
2514 * @type string $headers Any email headers, defaults to no headers.
2516 * @param string $type The type of email being sent. Can be one of
2517 * 'success', 'fail', 'manual', 'critical'.
2518 * @param object $core_update The update offer that was attempted.
2519 * @param mixed $result The result for the core update. Can be WP_Error.
2521 $email = apply_filters( 'auto_core_update_email', $email, $type, $core_update, $result );
2523 wp_mail( $email['to'], wp_specialchars_decode( $email['subject'] ), $email['body'], $email['headers'] );
2527 * Prepares and sends an email of a full log of background update results, useful for debugging and geekery.
2531 protected function send_debug_email() {
2533 foreach ( $this->update_results as $type => $updates )
2534 $update_count += count( $updates );
2539 $body[] = sprintf( __( 'WordPress site: %s' ), network_home_url( '/' ) );
2542 if ( isset( $this->update_results['core'] ) ) {
2543 $result = $this->update_results['core'][0];
2544 if ( $result->result && ! is_wp_error( $result->result ) ) {
2545 $body[] = sprintf( __( 'SUCCESS: WordPress was successfully updated to %s' ), $result->name );
2547 $body[] = sprintf( __( 'FAILED: WordPress failed to update to %s' ), $result->name );
2553 // Plugins, Themes, Translations
2554 foreach ( array( 'plugin', 'theme', 'translation' ) as $type ) {
2555 if ( ! isset( $this->update_results[ $type ] ) )
2557 $success_items = wp_list_filter( $this->update_results[ $type ], array( 'result' => true ) );
2558 if ( $success_items ) {
2560 'plugin' => __( 'The following plugins were successfully updated:' ),
2561 'theme' => __( 'The following themes were successfully updated:' ),
2562 'translation' => __( 'The following translations were successfully updated:' ),
2565 $body[] = $messages[ $type ];
2566 foreach ( wp_list_pluck( $success_items, 'name' ) as $name ) {
2567 $body[] = ' * ' . sprintf( __( 'SUCCESS: %s' ), $name );
2570 if ( $success_items != $this->update_results[ $type ] ) {
2573 'plugin' => __( 'The following plugins failed to update:' ),
2574 'theme' => __( 'The following themes failed to update:' ),
2575 'translation' => __( 'The following translations failed to update:' ),
2578 $body[] = $messages[ $type ];
2579 foreach ( $this->update_results[ $type ] as $item ) {
2580 if ( ! $item->result || is_wp_error( $item->result ) ) {
2581 $body[] = ' * ' . sprintf( __( 'FAILED: %s' ), $item->name );
2589 $site_title = wp_specialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES );
2591 $body[] = trim( __( "
2595 This debugging email is sent when you are using a development version of WordPress.
2597 If you think these failures might be due to a bug in WordPress, could you report it?
2598 * Open a thread in the support forums: https://wordpress.org/support/forum/alphabeta
2599 * Or, if you're comfortable writing a bug report: http://core.trac.wordpress.org/
2601 Thanks! -- The WordPress Team" ) );
2604 $subject = sprintf( __( '[%s] There were failures during background updates' ), $site_title );
2606 $subject = sprintf( __( '[%s] Background updates have finished' ), $site_title );
2609 $body[] = trim( __( '
2614 foreach ( array( 'core', 'plugin', 'theme', 'translation' ) as $type ) {
2615 if ( ! isset( $this->update_results[ $type ] ) )
2617 foreach ( $this->update_results[ $type ] as $update ) {
2618 $body[] = $update->name;
2619 $body[] = str_repeat( '-', strlen( $update->name ) );
2620 foreach ( $update->messages as $message )
2621 $body[] = " " . html_entity_decode( str_replace( '…', '...', $message ) );
2622 if ( is_wp_error( $update->result ) ) {
2623 $results = array( 'update' => $update->result );
2624 // If we rolled back, we want to know an error that occurred then too.
2625 if ( 'rollback_was_required' === $update->result->get_error_code() )
2626 $results = (array) $update->result->get_error_data();
2627 foreach ( $results as $result_type => $result ) {
2628 if ( ! is_wp_error( $result ) )
2631 if ( 'rollback' === $result_type ) {
2632 /* translators: 1: Error code, 2: Error message. */
2633 $body[] = ' ' . sprintf( __( 'Rollback Error: [%1$s] %2$s' ), $result->get_error_code(), $result->get_error_message() );
2635 /* translators: 1: Error code, 2: Error message. */
2636 $body[] = ' ' . sprintf( __( 'Error: [%1$s] %2$s' ), $result->get_error_code(), $result->get_error_message() );
2639 if ( $result->get_error_data() )
2640 $body[] = ' ' . implode( ', ', (array) $result->get_error_data() );
2648 'to' => get_site_option( 'admin_email' ),
2649 'subject' => $subject,
2650 'body' => implode( "\n", $body ),
2655 * Filter the debug email that can be sent following an automatic
2656 * background core update.
2660 * @param array $email {
2661 * Array of email arguments that will be passed to wp_mail().
2663 * @type string $to The email recipient. An array of emails
2664 * can be returned, as handled by wp_mail().
2665 * @type string $subject Email subject.
2666 * @type string $body Email message body.
2667 * @type string $headers Any email headers. Default empty.
2669 * @param int $failures The number of failures encountered while upgrading.
2670 * @param mixed $results The results of all attempted updates.
2672 $email = apply_filters( 'automatic_updates_debug_email', $email, $failures, $this->update_results );
2674 wp_mail( $email['to'], wp_specialchars_decode( $email['subject'] ), $email['body'], $email['headers'] );