3 * Upgrade API: WP_Upgrader class
5 * Requires skin classes and WP_Upgrader subclasses for backward compatibility.
12 /** WP_Upgrader_Skin class */
13 require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader-skin.php';
15 /** Plugin_Upgrader_Skin class */
16 require_once ABSPATH . 'wp-admin/includes/class-plugin-upgrader-skin.php';
18 /** Theme_Upgrader_Skin class */
19 require_once ABSPATH . 'wp-admin/includes/class-theme-upgrader-skin.php';
21 /** Bulk_Upgrader_Skin class */
22 require_once ABSPATH . 'wp-admin/includes/class-bulk-upgrader-skin.php';
24 /** Bulk_Plugin_Upgrader_Skin class */
25 require_once ABSPATH . 'wp-admin/includes/class-bulk-plugin-upgrader-skin.php';
27 /** Bulk_Theme_Upgrader_Skin class */
28 require_once ABSPATH . 'wp-admin/includes/class-bulk-theme-upgrader-skin.php';
30 /** Plugin_Installer_Skin class */
31 require_once ABSPATH . 'wp-admin/includes/class-plugin-installer-skin.php';
33 /** Theme_Installer_Skin class */
34 require_once ABSPATH . 'wp-admin/includes/class-theme-installer-skin.php';
36 /** Language_Pack_Upgrader_Skin class */
37 require_once ABSPATH . 'wp-admin/includes/class-language-pack-upgrader-skin.php';
39 /** Automatic_Upgrader_Skin class */
40 require_once ABSPATH . 'wp-admin/includes/class-automatic-upgrader-skin.php';
42 /** WP_Ajax_Upgrader_Skin class */
43 require_once ABSPATH . 'wp-admin/includes/class-wp-ajax-upgrader-skin.php';
46 * Core class used for upgrading/installing a local set of files via
47 * the Filesystem Abstraction classes from a Zip file.
54 * The error/notification strings used to update the user on the progress.
60 public $strings = array();
63 * The upgrader skin being used.
67 * @var Automatic_Upgrader_Skin|WP_Upgrader_Skin $skin
72 * The result of the installation.
74 * This is set by WP_Upgrader::install_package(), only when the package is installed
75 * successfully. It will then be an array, unless a WP_Error is returned by the
76 * {@see 'upgrader_post_install'} filter. In that case, the WP_Error will be assigned to
82 * @var WP_Error|array $result {
83 * @type string $source The full path to the source the files were installed from.
84 * @type string $source_files List of all the files in the source directory.
85 * @type string $destination The full path to the install destination folder.
86 * @type string $destination_name The name of the destination folder, or empty if `$destination`
87 * and `$local_destination` are the same.
88 * @type string $local_destination The full local path to the destination folder. This is usually
89 * the same as `$destination`.
90 * @type string $remote_destination The full remote path to the destination folder
91 * (i.e., from `$wp_filesystem`).
92 * @type bool $clear_destination Whether the destination folder was cleared.
95 public $result = array();
98 * The total number of updates being performed.
100 * Set by the bulk update methods.
104 * @var int $update_count
106 public $update_count = 0;
109 * The current update if multiple updates are being performed.
111 * Used by the bulk update methods, and incremented for each update.
117 public $update_current = 0;
120 * Construct the upgrader with a skin.
125 * @param WP_Upgrader_Skin $skin The upgrader skin to use. Default is a WP_Upgrader_Skin.
128 public function __construct( $skin = null ) {
130 $this->skin = new WP_Upgrader_Skin();
136 * Initialize the upgrader.
138 * This will set the relationship between the skin being used and this upgrader,
139 * and also add the generic strings to `WP_Upgrader::$strings`.
144 public function init() {
145 $this->skin->set_upgrader($this);
146 $this->generic_strings();
150 * Add the generic strings to WP_Upgrader::$strings.
155 public function generic_strings() {
156 $this->strings['bad_request'] = __('Invalid data provided.');
157 $this->strings['fs_unavailable'] = __('Could not access filesystem.');
158 $this->strings['fs_error'] = __('Filesystem error.');
159 $this->strings['fs_no_root_dir'] = __('Unable to locate WordPress root directory.');
160 $this->strings['fs_no_content_dir'] = __('Unable to locate WordPress content directory (wp-content).');
161 $this->strings['fs_no_plugins_dir'] = __('Unable to locate WordPress plugin directory.');
162 $this->strings['fs_no_themes_dir'] = __('Unable to locate WordPress theme directory.');
163 /* translators: %s: directory name */
164 $this->strings['fs_no_folder'] = __('Unable to locate needed folder (%s).');
166 $this->strings['download_failed'] = __('Download failed.');
167 $this->strings['installing_package'] = __('Installing the latest version…');
168 $this->strings['no_files'] = __('The package contains no files.');
169 $this->strings['folder_exists'] = __('Destination folder already exists.');
170 $this->strings['mkdir_failed'] = __('Could not create directory.');
171 $this->strings['incompatible_archive'] = __('The package could not be installed.');
172 $this->strings['files_not_writable'] = __( 'The update cannot be installed because we will be unable to copy some files. This is usually due to inconsistent file permissions.' );
174 $this->strings['maintenance_start'] = __('Enabling Maintenance mode…');
175 $this->strings['maintenance_end'] = __('Disabling Maintenance mode…');
179 * Connect to the filesystem.
184 * @global WP_Filesystem_Base $wp_filesystem Subclass
186 * @param array $directories Optional. A list of directories. If any of these do
187 * not exist, a WP_Error object will be returned.
188 * Default empty array.
189 * @param bool $allow_relaxed_file_ownership Whether to allow relaxed file ownership.
191 * @return bool|WP_Error True if able to connect, false or a WP_Error otherwise.
193 public function fs_connect( $directories = array(), $allow_relaxed_file_ownership = false ) {
194 global $wp_filesystem;
196 if ( false === ( $credentials = $this->skin->request_filesystem_credentials( false, $directories[0], $allow_relaxed_file_ownership ) ) ) {
200 if ( ! WP_Filesystem( $credentials, $directories[0], $allow_relaxed_file_ownership ) ) {
202 if ( is_object($wp_filesystem) && $wp_filesystem->errors->get_error_code() )
203 $error = $wp_filesystem->errors;
204 // Failed to connect, Error and request again
205 $this->skin->request_filesystem_credentials( $error, $directories[0], $allow_relaxed_file_ownership );
209 if ( ! is_object($wp_filesystem) )
210 return new WP_Error('fs_unavailable', $this->strings['fs_unavailable'] );
212 if ( is_wp_error($wp_filesystem->errors) && $wp_filesystem->errors->get_error_code() )
213 return new WP_Error('fs_error', $this->strings['fs_error'], $wp_filesystem->errors);
215 foreach ( (array)$directories as $dir ) {
218 if ( ! $wp_filesystem->abspath() )
219 return new WP_Error('fs_no_root_dir', $this->strings['fs_no_root_dir']);
222 if ( ! $wp_filesystem->wp_content_dir() )
223 return new WP_Error('fs_no_content_dir', $this->strings['fs_no_content_dir']);
226 if ( ! $wp_filesystem->wp_plugins_dir() )
227 return new WP_Error('fs_no_plugins_dir', $this->strings['fs_no_plugins_dir']);
229 case get_theme_root():
230 if ( ! $wp_filesystem->wp_themes_dir() )
231 return new WP_Error('fs_no_themes_dir', $this->strings['fs_no_themes_dir']);
234 if ( ! $wp_filesystem->find_folder($dir) )
235 return new WP_Error( 'fs_no_folder', sprintf( $this->strings['fs_no_folder'], esc_html( basename( $dir ) ) ) );
240 } //end fs_connect();
243 * Download a package.
248 * @param string $package The URI of the package. If this is the full path to an
249 * existing local file, it will be returned untouched.
250 * @return string|WP_Error The full path to the downloaded package file, or a WP_Error object.
252 public function download_package( $package ) {
255 * Filters whether to return the package.
260 * @param bool $reply Whether to bail without returning the package.
262 * @param string $package The package file name.
263 * @param WP_Upgrader $this The WP_Upgrader instance.
265 $reply = apply_filters( 'upgrader_pre_download', false, $package, $this );
266 if ( false !== $reply )
269 if ( ! preg_match('!^(http|https|ftp)://!i', $package) && file_exists($package) ) //Local file or remote?
270 return $package; //must be a local file..
272 if ( empty($package) )
273 return new WP_Error('no_package', $this->strings['no_package']);
275 $this->skin->feedback('downloading_package', $package);
277 $download_file = download_url($package);
279 if ( is_wp_error($download_file) )
280 return new WP_Error('download_failed', $this->strings['download_failed'], $download_file->get_error_message());
282 return $download_file;
286 * Unpack a compressed package file.
291 * @global WP_Filesystem_Base $wp_filesystem Subclass
293 * @param string $package Full path to the package file.
294 * @param bool $delete_package Optional. Whether to delete the package file after attempting
295 * to unpack it. Default true.
296 * @return string|WP_Error The path to the unpacked contents, or a WP_Error on failure.
298 public function unpack_package( $package, $delete_package = true ) {
299 global $wp_filesystem;
301 $this->skin->feedback('unpack_package');
303 $upgrade_folder = $wp_filesystem->wp_content_dir() . 'upgrade/';
305 //Clean up contents of upgrade directory beforehand.
306 $upgrade_files = $wp_filesystem->dirlist($upgrade_folder);
307 if ( !empty($upgrade_files) ) {
308 foreach ( $upgrade_files as $file )
309 $wp_filesystem->delete($upgrade_folder . $file['name'], true);
312 // We need a working directory - Strip off any .tmp or .zip suffixes
313 $working_dir = $upgrade_folder . basename( basename( $package, '.tmp' ), '.zip' );
315 // Clean up working directory
316 if ( $wp_filesystem->is_dir($working_dir) )
317 $wp_filesystem->delete($working_dir, true);
319 // Unzip package to working directory
320 $result = unzip_file( $package, $working_dir );
322 // Once extracted, delete the package if required.
323 if ( $delete_package )
326 if ( is_wp_error($result) ) {
327 $wp_filesystem->delete($working_dir, true);
328 if ( 'incompatible_archive' == $result->get_error_code() ) {
329 return new WP_Error( 'incompatible_archive', $this->strings['incompatible_archive'], $result->get_error_data() );
338 * Clears the directory where this item is going to be installed into.
343 * @global WP_Filesystem_Base $wp_filesystem Subclass
345 * @param string $remote_destination The location on the remote filesystem to be cleared
346 * @return bool|WP_Error True upon success, WP_Error on failure.
348 public function clear_destination( $remote_destination ) {
349 global $wp_filesystem;
351 if ( ! $wp_filesystem->exists( $remote_destination ) ) {
355 // Check all files are writable before attempting to clear the destination.
356 $unwritable_files = array();
358 $_files = $wp_filesystem->dirlist( $remote_destination, true, true );
360 // Flatten the resulting array, iterate using each as we append to the array during iteration.
361 while ( $f = each( $_files ) ) {
365 if ( ! isset( $file['files'] ) ) {
369 foreach ( $file['files'] as $filename => $details ) {
370 $_files[ $name . '/' . $filename ] = $details;
374 // Check writability.
375 foreach ( $_files as $filename => $file_details ) {
376 if ( ! $wp_filesystem->is_writable( $remote_destination . $filename ) ) {
378 // Attempt to alter permissions to allow writes and try again.
379 $wp_filesystem->chmod( $remote_destination . $filename, ( 'd' == $file_details['type'] ? FS_CHMOD_DIR : FS_CHMOD_FILE ) );
380 if ( ! $wp_filesystem->is_writable( $remote_destination . $filename ) ) {
381 $unwritable_files[] = $filename;
386 if ( ! empty( $unwritable_files ) ) {
387 return new WP_Error( 'files_not_writable', $this->strings['files_not_writable'], implode( ', ', $unwritable_files ) );
390 if ( ! $wp_filesystem->delete( $remote_destination, true ) ) {
391 return new WP_Error( 'remove_old_failed', $this->strings['remove_old_failed'] );
400 * Copies the contents of a package form a source directory, and installs them in
401 * a destination directory. Optionally removes the source. It can also optionally
402 * clear out the destination folder if it already exists.
407 * @global WP_Filesystem_Base $wp_filesystem Subclass
408 * @global array $wp_theme_directories
410 * @param array|string $args {
411 * Optional. Array or string of arguments for installing a package. Default empty array.
413 * @type string $source Required path to the package source. Default empty.
414 * @type string $destination Required path to a folder to install the package in.
416 * @type bool $clear_destination Whether to delete any files already in the destination
417 * folder. Default false.
418 * @type bool $clear_working Whether to delete the files form the working directory
419 * after copying to the destination. Default false.
420 * @type bool $abort_if_destination_exists Whether to abort the installation if
421 * the destination folder already exists. Default true.
422 * @type array $hook_extra Extra arguments to pass to the filter hooks called by
423 * WP_Upgrader::install_package(). Default empty array.
426 * @return array|WP_Error The result (also stored in `WP_Upgrader::$result`), or a WP_Error on failure.
428 public function install_package( $args = array() ) {
429 global $wp_filesystem, $wp_theme_directories;
432 'source' => '', // Please always pass this
433 'destination' => '', // and this
434 'clear_destination' => false,
435 'clear_working' => false,
436 'abort_if_destination_exists' => true,
437 'hook_extra' => array()
440 $args = wp_parse_args($args, $defaults);
442 // These were previously extract()'d.
443 $source = $args['source'];
444 $destination = $args['destination'];
445 $clear_destination = $args['clear_destination'];
447 @set_time_limit( 300 );
449 if ( empty( $source ) || empty( $destination ) ) {
450 return new WP_Error( 'bad_request', $this->strings['bad_request'] );
452 $this->skin->feedback( 'installing_package' );
455 * Filters the install response before the installation has started.
457 * Returning a truthy value, or one that could be evaluated as a WP_Error
458 * will effectively short-circuit the installation, returning that value
463 * @param bool|WP_Error $response Response.
464 * @param array $hook_extra Extra arguments passed to hooked filters.
466 $res = apply_filters( 'upgrader_pre_install', true, $args['hook_extra'] );
468 if ( is_wp_error( $res ) ) {
472 //Retain the Original source and destinations
473 $remote_source = $args['source'];
474 $local_destination = $destination;
476 $source_files = array_keys( $wp_filesystem->dirlist( $remote_source ) );
477 $remote_destination = $wp_filesystem->find_folder( $local_destination );
479 //Locate which directory to copy to the new folder, This is based on the actual folder holding the files.
480 if ( 1 == count( $source_files ) && $wp_filesystem->is_dir( trailingslashit( $args['source'] ) . $source_files[0] . '/' ) ) { //Only one folder? Then we want its contents.
481 $source = trailingslashit( $args['source'] ) . trailingslashit( $source_files[0] );
482 } elseif ( count( $source_files ) == 0 ) {
483 return new WP_Error( 'incompatible_archive_empty', $this->strings['incompatible_archive'], $this->strings['no_files'] ); // There are no files?
484 } else { // It's only a single file, the upgrader will use the folder name of this file as the destination folder. Folder name is based on zip filename.
485 $source = trailingslashit( $args['source'] );
489 * Filters the source file location for the upgrade package.
492 * @since 4.4.0 The $hook_extra parameter became available.
494 * @param string $source File source location.
495 * @param string $remote_source Remote file source location.
496 * @param WP_Upgrader $this WP_Upgrader instance.
497 * @param array $hook_extra Extra arguments passed to hooked filters.
499 $source = apply_filters( 'upgrader_source_selection', $source, $remote_source, $this, $args['hook_extra'] );
501 if ( is_wp_error( $source ) ) {
505 // Has the source location changed? If so, we need a new source_files list.
506 if ( $source !== $remote_source ) {
507 $source_files = array_keys( $wp_filesystem->dirlist( $source ) );
511 * Protection against deleting files in any important base directories.
512 * Theme_Upgrader & Plugin_Upgrader also trigger this, as they pass the
513 * destination directory (WP_PLUGIN_DIR / wp-content/themes) intending
514 * to copy the directory into the directory, whilst they pass the source
515 * as the actual files to copy.
517 $protected_directories = array( ABSPATH, WP_CONTENT_DIR, WP_PLUGIN_DIR, WP_CONTENT_DIR . '/themes' );
519 if ( is_array( $wp_theme_directories ) ) {
520 $protected_directories = array_merge( $protected_directories, $wp_theme_directories );
523 if ( in_array( $destination, $protected_directories ) ) {
524 $remote_destination = trailingslashit( $remote_destination ) . trailingslashit( basename( $source ) );
525 $destination = trailingslashit( $destination ) . trailingslashit( basename( $source ) );
528 if ( $clear_destination ) {
529 // We're going to clear the destination if there's something there.
530 $this->skin->feedback('remove_old');
532 $removed = $this->clear_destination( $remote_destination );
535 * Filters whether the upgrader cleared the destination.
539 * @param mixed $removed Whether the destination was cleared. true on success, WP_Error on failure
540 * @param string $local_destination The local package destination.
541 * @param string $remote_destination The remote package destination.
542 * @param array $hook_extra Extra arguments passed to hooked filters.
544 $removed = apply_filters( 'upgrader_clear_destination', $removed, $local_destination, $remote_destination, $args['hook_extra'] );
546 if ( is_wp_error( $removed ) ) {
549 } elseif ( $args['abort_if_destination_exists'] && $wp_filesystem->exists($remote_destination) ) {
550 //If we're not clearing the destination folder and something exists there already, Bail.
551 //But first check to see if there are actually any files in the folder.
552 $_files = $wp_filesystem->dirlist($remote_destination);
553 if ( ! empty($_files) ) {
554 $wp_filesystem->delete($remote_source, true); //Clear out the source files.
555 return new WP_Error('folder_exists', $this->strings['folder_exists'], $remote_destination );
559 //Create destination if needed
560 if ( ! $wp_filesystem->exists( $remote_destination ) ) {
561 if ( ! $wp_filesystem->mkdir( $remote_destination, FS_CHMOD_DIR ) ) {
562 return new WP_Error( 'mkdir_failed_destination', $this->strings['mkdir_failed'], $remote_destination );
565 // Copy new version of item into place.
566 $result = copy_dir($source, $remote_destination);
567 if ( is_wp_error($result) ) {
568 if ( $args['clear_working'] ) {
569 $wp_filesystem->delete( $remote_source, true );
574 //Clear the Working folder?
575 if ( $args['clear_working'] ) {
576 $wp_filesystem->delete( $remote_source, true );
579 $destination_name = basename( str_replace($local_destination, '', $destination) );
580 if ( '.' == $destination_name ) {
581 $destination_name = '';
584 $this->result = compact( 'source', 'source_files', 'destination', 'destination_name', 'local_destination', 'remote_destination', 'clear_destination' );
587 * Filters the install response after the installation has finished.
591 * @param bool $response Install response.
592 * @param array $hook_extra Extra arguments passed to hooked filters.
593 * @param array $result Installation result data.
595 $res = apply_filters( 'upgrader_post_install', true, $args['hook_extra'], $this->result );
597 if ( is_wp_error($res) ) {
598 $this->result = $res;
602 //Bombard the calling function will all the info which we've just used.
603 return $this->result;
607 * Run an upgrade/install.
609 * Attempts to download the package (if it is not a local file), unpack it, and
610 * install it in the destination folder.
615 * @param array $options {
616 * Array or string of arguments for upgrading/installing a package.
618 * @type string $package The full path or URI of the package to install.
620 * @type string $destination The full path to the destination folder.
622 * @type bool $clear_destination Whether to delete any files already in the
623 * destination folder. Default false.
624 * @type bool $clear_working Whether to delete the files form the working
625 * directory after copying to the destination.
627 * @type bool $abort_if_destination_exists Whether to abort the installation if the destination
628 * folder already exists. When true, `$clear_destination`
629 * should be false. Default true.
630 * @type bool $is_multi Whether this run is one of multiple upgrade/install
631 * actions being performed in bulk. When true, the skin
632 * WP_Upgrader::header() and WP_Upgrader::footer()
633 * aren't called. Default false.
634 * @type array $hook_extra Extra arguments to pass to the filter hooks called by
635 * WP_Upgrader::run().
637 * @return array|false|WP_error The result from self::install_package() on success, otherwise a WP_Error,
638 * or false if unable to connect to the filesystem.
640 public function run( $options ) {
643 'package' => '', // Please always pass this.
644 'destination' => '', // And this
645 'clear_destination' => false,
646 'abort_if_destination_exists' => true, // Abort if the Destination directory exists, Pass clear_destination as false please
647 'clear_working' => true,
649 'hook_extra' => array() // Pass any extra $hook_extra args here, this will be passed to any hooked filters.
652 $options = wp_parse_args( $options, $defaults );
655 * Filters the package options before running an update.
657 * See also {@see 'upgrader_process_complete'}.
661 * @param array $options {
662 * Options used by the upgrader.
664 * @type string $package Package for update.
665 * @type string $destination Update location.
666 * @type bool $clear_destination Clear the destination resource.
667 * @type bool $clear_working Clear the working resource.
668 * @type bool $abort_if_destination_exists Abort if the Destination directory exists.
669 * @type bool $is_multi Whether the upgrader is running multiple times.
670 * @type array $hook_extra {
671 * Extra hook arguments.
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 string $plugin The base plugin path from the plugins directory.
677 * @type string $theme The stylesheet or template name of the theme.
678 * @type string $language_update_type The language pack update type. Accepts 'plugin', 'theme',
680 * @type object $language_update The language pack update offer.
684 $options = apply_filters( 'upgrader_package_options', $options );
686 if ( ! $options['is_multi'] ) { // call $this->header separately if running multiple times
687 $this->skin->header();
690 // Connect to the Filesystem first.
691 $res = $this->fs_connect( array( WP_CONTENT_DIR, $options['destination'] ) );
692 // Mainly for non-connected filesystem.
694 if ( ! $options['is_multi'] ) {
695 $this->skin->footer();
700 $this->skin->before();
702 if ( is_wp_error($res) ) {
703 $this->skin->error($res);
704 $this->skin->after();
705 if ( ! $options['is_multi'] ) {
706 $this->skin->footer();
712 * Download the package (Note, This just returns the filename
713 * of the file if the package is a local file)
715 $download = $this->download_package( $options['package'] );
716 if ( is_wp_error($download) ) {
717 $this->skin->error($download);
718 $this->skin->after();
719 if ( ! $options['is_multi'] ) {
720 $this->skin->footer();
725 $delete_package = ( $download != $options['package'] ); // Do not delete a "local" file
727 // Unzips the file into a temporary directory.
728 $working_dir = $this->unpack_package( $download, $delete_package );
729 if ( is_wp_error($working_dir) ) {
730 $this->skin->error($working_dir);
731 $this->skin->after();
732 if ( ! $options['is_multi'] ) {
733 $this->skin->footer();
738 // With the given options, this installs it to the destination directory.
739 $result = $this->install_package( array(
740 'source' => $working_dir,
741 'destination' => $options['destination'],
742 'clear_destination' => $options['clear_destination'],
743 'abort_if_destination_exists' => $options['abort_if_destination_exists'],
744 'clear_working' => $options['clear_working'],
745 'hook_extra' => $options['hook_extra']
748 $this->skin->set_result($result);
749 if ( is_wp_error($result) ) {
750 $this->skin->error($result);
751 $this->skin->feedback('process_failed');
753 // Install succeeded.
754 $this->skin->feedback('process_success');
757 $this->skin->after();
759 if ( ! $options['is_multi'] ) {
762 * Fires when the upgrader process is complete.
764 * See also {@see 'upgrader_package_options'}.
767 * @since 3.7.0 Added to WP_Upgrader::run().
768 * @since 4.6.0 `$translations` was added as a possible argument to `$hook_extra`.
770 * @param WP_Upgrader $this WP_Upgrader instance. In other contexts, $this, might be a
771 * Theme_Upgrader, Plugin_Upgrader, Core_Upgrade, or Language_Pack_Upgrader instance.
772 * @param array $hook_extra {
773 * Array of bulk item update data.
775 * @type string $action Type of action. Default 'update'.
776 * @type string $type Type of update process. Accepts 'plugin', 'theme', 'translation', or 'core'.
777 * @type bool $bulk Whether the update process is a bulk update. Default true.
778 * @type array $plugins Array of the basename paths of the plugins' main files.
779 * @type array $themes The theme slugs.
780 * @type array $translations {
781 * Array of translations update data.
783 * @type string $language The locale the translation is for.
784 * @type string $type Type of translation. Accepts 'plugin', 'theme', or 'core'.
785 * @type string $slug Text domain the translation is for. The slug of a theme/plugin or
786 * 'default' for core translations.
787 * @type string $version The version of a theme, plugin, or core.
791 do_action( 'upgrader_process_complete', $this, $options['hook_extra'] );
793 $this->skin->footer();
800 * Toggle maintenance mode for the site.
802 * Creates/deletes the maintenance file to enable/disable maintenance mode.
807 * @global WP_Filesystem_Base $wp_filesystem Subclass
809 * @param bool $enable True to enable maintenance mode, false to disable.
811 public function maintenance_mode( $enable = false ) {
812 global $wp_filesystem;
813 $file = $wp_filesystem->abspath() . '.maintenance';
815 $this->skin->feedback('maintenance_start');
816 // Create maintenance file to signal that we are upgrading
817 $maintenance_string = '<?php $upgrading = ' . time() . '; ?>';
818 $wp_filesystem->delete($file);
819 $wp_filesystem->put_contents($file, $maintenance_string, FS_CHMOD_FILE);
820 } elseif ( ! $enable && $wp_filesystem->exists( $file ) ) {
821 $this->skin->feedback('maintenance_end');
822 $wp_filesystem->delete($file);
827 * Creates a lock using WordPress options.
833 * @param string $lock_name The name of this unique lock.
834 * @param int $release_timeout Optional. The duration in seconds to respect an existing lock.
836 * @return bool False if a lock couldn't be created or if the lock is no longer valid. True otherwise.
838 public static function create_lock( $lock_name, $release_timeout = null ) {
840 if ( ! $release_timeout ) {
841 $release_timeout = HOUR_IN_SECONDS;
843 $lock_option = $lock_name . '.lock';
846 $lock_result = $wpdb->query( $wpdb->prepare( "INSERT IGNORE INTO `$wpdb->options` ( `option_name`, `option_value`, `autoload` ) VALUES (%s, %s, 'no') /* LOCK */", $lock_option, time() ) );
848 if ( ! $lock_result ) {
849 $lock_result = get_option( $lock_option );
851 // If a lock couldn't be created, and there isn't a lock, bail.
852 if ( ! $lock_result ) {
856 // Check to see if the lock is still valid. If not, bail.
857 if ( $lock_result > ( time() - $release_timeout ) ) {
861 // There must exist an expired lock, clear it and re-gain it.
862 WP_Upgrader::release_lock( $lock_name );
864 return WP_Upgrader::create_lock( $lock_name, $release_timeout );
867 // Update the lock, as by this point we've definitely got a lock, just need to fire the actions.
868 update_option( $lock_option, time() );
874 * Releases an upgrader lock.
880 * @see WP_Upgrader::create_lock()
882 * @param string $lock_name The name of this unique lock.
883 * @return bool True if the lock was successfully released. False on failure.
885 public static function release_lock( $lock_name ) {
886 return delete_option( $lock_name . '.lock' );
891 /** Plugin_Upgrader class */
892 require_once ABSPATH . 'wp-admin/includes/class-plugin-upgrader.php';
894 /** Theme_Upgrader class */
895 require_once ABSPATH . 'wp-admin/includes/class-theme-upgrader.php';
897 /** Language_Pack_Upgrader class */
898 require_once ABSPATH . 'wp-admin/includes/class-language-pack-upgrader.php';
900 /** Core_Upgrader class */
901 require_once ABSPATH . 'wp-admin/includes/class-core-upgrader.php';
903 /** File_Upload_Upgrader class */
904 require_once ABSPATH . 'wp-admin/includes/class-file-upload-upgrader.php';
906 /** WP_Automatic_Updater class */
907 require_once ABSPATH . 'wp-admin/includes/class-wp-automatic-updater.php';