WP_Automatic_Updater::update( string $type, object $item ): null|WP_Error

In this article

Updates an item, if appropriate.

Parameters

$typestringrequired
The type of update being checked: 'core', 'theme', 'plugin', 'translation'.
$itemobjectrequired
The update offer.

Return

null|WP_Error

Source

public function update( $type, $item ) {
	$skin = new Automatic_Upgrader_Skin();

	switch ( $type ) {
		case 'core':
			// The Core upgrader doesn't use the Upgrader's skin during the actual main part of the upgrade, instead, firing a filter.
			add_filter( 'update_feedback', array( $skin, 'feedback' ) );
			$upgrader = new Core_Upgrader( $skin );
			$context  = ABSPATH;
			break;
		case 'plugin':
			$upgrader = new Plugin_Upgrader( $skin );
			$context  = WP_PLUGIN_DIR; // We don't support custom Plugin directories, or updates for WPMU_PLUGIN_DIR.
			break;
		case 'theme':
			$upgrader = new Theme_Upgrader( $skin );
			$context  = get_theme_root( $item->theme );
			break;
		case 'translation':
			$upgrader = new Language_Pack_Upgrader( $skin );
			$context  = WP_CONTENT_DIR; // WP_LANG_DIR;
			break;
	}

	// Determine whether we can and should perform this update.
	if ( ! $this->should_update( $type, $item, $context ) ) {
		return false;
	}

	/**
	 * Fires immediately prior to an auto-update.
	 *
	 * @since 4.4.0
	 *
	 * @param string $type    The type of update being checked: 'core', 'theme', 'plugin', or 'translation'.
	 * @param object $item    The update offer.
	 * @param string $context The filesystem context (a path) against which filesystem access and status
	 *                        should be checked.
	 */
	do_action( 'pre_auto_update', $type, $item, $context );

	$upgrader_item = $item;
	switch ( $type ) {
		case 'core':
			/* translators: %s: WordPress version. */
			$skin->feedback( __( 'Updating to WordPress %s' ), $item->version );
			/* translators: %s: WordPress version. */
			$item_name = sprintf( __( 'WordPress %s' ), $item->version );
			break;
		case 'theme':
			$upgrader_item = $item->theme;
			$theme         = wp_get_theme( $upgrader_item );
			$item_name     = $theme->Get( 'Name' );
			// Add the current version so that it can be reported in the notification email.
			$item->current_version = $theme->get( 'Version' );
			if ( empty( $item->current_version ) ) {
				$item->current_version = false;
			}
			/* translators: %s: Theme name. */
			$skin->feedback( __( 'Updating theme: %s' ), $item_name );
			break;
		case 'plugin':
			$upgrader_item = $item->plugin;
			$plugin_data   = get_plugin_data( $context . '/' . $upgrader_item );
			$item_name     = $plugin_data['Name'];
			// Add the current version so that it can be reported in the notification email.
			$item->current_version = $plugin_data['Version'];
			if ( empty( $item->current_version ) ) {
				$item->current_version = false;
			}
			/* translators: %s: Plugin name. */
			$skin->feedback( __( 'Updating plugin: %s' ), $item_name );
			break;
		case 'translation':
			$language_item_name = $upgrader->get_name_for_update( $item );
			/* translators: %s: Project name (plugin, theme, or WordPress). */
			$item_name = sprintf( __( 'Translations for %s' ), $language_item_name );
			/* translators: 1: Project name (plugin, theme, or WordPress), 2: Language. */
			$skin->feedback( sprintf( __( 'Updating translations for %1$s (%2$s)…' ), $language_item_name, $item->language ) );
			break;
	}

	$allow_relaxed_file_ownership = false;
	if ( 'core' === $type && isset( $item->new_files ) && ! $item->new_files ) {
		$allow_relaxed_file_ownership = true;
	}

	$is_debug = WP_DEBUG && WP_DEBUG_LOG;
	if ( 'plugin' === $type ) {
		$was_active = is_plugin_active( $upgrader_item );
		if ( $is_debug ) {
			error_log( '    Upgrading plugin ' . var_export( $item->slug, true ) . '...' );
		}
	}

	if ( 'theme' === $type && $is_debug ) {
		error_log( '    Upgrading theme ' . var_export( $item->theme, true ) . '...' );
	}

	/*
	 * Enable maintenance mode before upgrading the plugin or theme.
	 *
	 * This avoids potential non-fatal errors being detected
	 * while scraping for a fatal error if some files are still
	 * being moved.
	 *
	 * While these checks are intended only for plugins,
	 * maintenance mode is enabled for all upgrade types as any
	 * update could contain an error or warning, which could cause
	 * the scrape to miss a fatal error in the plugin update.
	 */
	if ( 'translation' !== $type ) {
		$upgrader->maintenance_mode( true );
	}

	// Boom, this site's about to get a whole new splash of paint!
	$upgrade_result = $upgrader->upgrade(
		$upgrader_item,
		array(
			'clear_update_cache'           => false,
			// Always use partial builds if possible for core updates.
			'pre_check_md5'                => false,
			// Only available for core updates.
			'attempt_rollback'             => true,
			// Allow relaxed file ownership in some scenarios.
			'allow_relaxed_file_ownership' => $allow_relaxed_file_ownership,
		)
	);

	/*
	 * After WP_Upgrader::upgrade() completes, maintenance mode is disabled.
	 *
	 * Re-enable maintenance mode while attempting to detect fatal errors
	 * and potentially rolling back.
	 *
	 * This avoids errors if the site is visited while fatal errors exist
	 * or while files are still being moved.
	 */
	if ( 'translation' !== $type ) {
		$upgrader->maintenance_mode( true );
	}

	// If the filesystem is unavailable, false is returned.
	if ( false === $upgrade_result ) {
		$upgrade_result = new WP_Error( 'fs_unavailable', __( 'Could not access filesystem.' ) );
	}

	if ( 'core' === $type ) {
		if ( is_wp_error( $upgrade_result )
			&& ( 'up_to_date' === $upgrade_result->get_error_code()
				|| 'locked' === $upgrade_result->get_error_code() )
		) {
			// Allow visitors to browse the site again.
			$upgrader->maintenance_mode( false );

			/*
			 * These aren't actual errors, treat it as a skipped-update instead
			 * to avoid triggering the post-core update failure routines.
			 */
			return false;
		}

		// Core doesn't output this, so let's append it, so we don't get confused.
		if ( is_wp_error( $upgrade_result ) ) {
			$upgrade_result->add( 'installation_failed', __( 'Installation failed.' ) );
			$skin->error( $upgrade_result );
		} else {
			$skin->feedback( __( 'WordPress updated successfully.' ) );
		}
	}

	$is_debug = WP_DEBUG && WP_DEBUG_LOG;

	if ( 'theme' === $type && $is_debug ) {
		error_log( '    Theme ' . var_export( $item->theme, true ) . ' has been upgraded.' );
	}

	if ( 'plugin' === $type ) {
		if ( $is_debug ) {
			error_log( '    Plugin ' . var_export( $item->slug, true ) . ' has been upgraded.' );
			if ( is_plugin_inactive( $upgrader_item ) ) {
				error_log( '    ' . var_export( $upgrader_item, true ) . ' is inactive and will not be checked for fatal errors.' );
			}
		}

		if ( $was_active && ! is_wp_error( $upgrade_result ) ) {

			/*
			 * The usual time limit is five minutes. However, as a loopback request
			 * is about to be performed, increase the time limit to account for this.
			 */
			if ( function_exists( 'set_time_limit' ) ) {
				set_time_limit( 10 * MINUTE_IN_SECONDS );
			}

			/*
			 * Avoids a race condition when there are 2 sequential plugins that have
			 * fatal errors. It seems a slight delay is required for the loopback to
			 * use the updated plugin code in the request. This can cause the second
			 * plugin's fatal error checking to be inaccurate, and may also affect
			 * subsequent plugin checks.
			 */
			sleep( 2 );

			if ( $this->has_fatal_error() ) {
				$upgrade_result = new WP_Error();
				$temp_backup    = array(
					array(
						'dir'  => 'plugins',
						'slug' => $item->slug,
						'src'  => WP_PLUGIN_DIR,
					),
				);

				$backup_restored = $upgrader->restore_temp_backup( $temp_backup );
				if ( is_wp_error( $backup_restored ) ) {
					$upgrade_result->add(
						'plugin_update_fatal_error_rollback_failed',
						sprintf(
							/* translators: %s: The plugin's slug. */
							__( "The update for '%s' contained a fatal error. The previously installed version could not be restored." ),
							$item->slug
						)
					);

					$upgrade_result->merge_from( $backup_restored );
				} else {
					$upgrade_result->add(
						'plugin_update_fatal_error_rollback_successful',
						sprintf(
							/* translators: %s: The plugin's slug. */
							__( "The update for '%s' contained a fatal error. The previously installed version has been restored." ),
							$item->slug
						)
					);

					$backup_deleted = $upgrader->delete_temp_backup( $temp_backup );
					if ( is_wp_error( $backup_deleted ) ) {
						$upgrade_result->merge_from( $backup_deleted );
					}
				}

				/*
				 * Should emails not be working, log the message(s) so that
				 * the log file contains context for the fatal error,
				 * and whether a rollback was performed.
				 *
				 * `trigger_error()` is not used as it outputs a stack trace
				 * to this location rather than to the fatal error, which will
				 * appear above this entry in the log file.
				 */
				if ( $is_debug ) {
					error_log( '    ' . implode( "\n", $upgrade_result->get_error_messages() ) );
				}
			} elseif ( $is_debug ) {
				error_log( '    The update for ' . var_export( $item->slug, true ) . ' has no fatal errors.' );
			}
		}
	}

	// All processes are complete. Allow visitors to browse the site again.
	if ( 'translation' !== $type ) {
		$upgrader->maintenance_mode( false );
	}

	$this->update_results[ $type ][] = (object) array(
		'item'     => $item,
		'result'   => $upgrade_result,
		'name'     => $item_name,
		'messages' => $skin->get_upgrade_messages(),
	);

	return $upgrade_result;
}

Hooks

do_action( ‘pre_auto_update’, string $type, object $item, string $context )

Fires immediately prior to an auto-update.

Changelog

VersionDescription
3.7.0Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.