WP_Automatic_Updater::after_core_update( object $update_result )

In this article

Checks whether to send an email and avoid processing future updates after attempting a core update.

Parameters

$update_resultobjectrequired
The result of the core update. Includes the update offer and result.

Source

protected function after_core_update( $update_result ) {
	$wp_version = get_bloginfo( 'version' );

	$core_update = $update_result->item;
	$result      = $update_result->result;

	if ( ! is_wp_error( $result ) ) {
		$this->send_email( 'success', $core_update );
		return;
	}

	$error_code = $result->get_error_code();

	/*
	 * Any of these WP_Error codes are critical failures, as in they occurred after we started to copy core files.
	 * We should not try to perform a background update again until there is a successful one-click update performed by the user.
	 */
	$critical = false;
	if ( 'disk_full' === $error_code || str_contains( $error_code, '__copy_dir' ) ) {
		$critical = true;
	} elseif ( 'rollback_was_required' === $error_code && is_wp_error( $result->get_error_data()->rollback ) ) {
		// A rollback is only critical if it failed too.
		$critical        = true;
		$rollback_result = $result->get_error_data()->rollback;
	} elseif ( str_contains( $error_code, 'do_rollback' ) ) {
		$critical = true;
	}

	if ( $critical ) {
		$critical_data = array(
			'attempted'  => $core_update->current,
			'current'    => $wp_version,
			'error_code' => $error_code,
			'error_data' => $result->get_error_data(),
			'timestamp'  => time(),
			'critical'   => true,
		);
		if ( isset( $rollback_result ) ) {
			$critical_data['rollback_code'] = $rollback_result->get_error_code();
			$critical_data['rollback_data'] = $rollback_result->get_error_data();
		}
		update_site_option( 'auto_core_update_failed', $critical_data );
		$this->send_email( 'critical', $core_update, $result );
		return;
	}

	/*
	 * Any other WP_Error code (like download_failed or files_not_writable) occurs before
	 * we tried to copy over core files. Thus, the failures are early and graceful.
	 *
	 * We should avoid trying to perform a background update again for the same version.
	 * But we can try again if another version is released.
	 *
	 * For certain 'transient' failures, like download_failed, we should allow retries.
	 * In fact, let's schedule a special update for an hour from now. (It's possible
	 * the issue could actually be on WordPress.org's side.) If that one fails, then email.
	 */
	$send               = true;
	$transient_failures = array( 'incompatible_archive', 'download_failed', 'insane_distro', 'locked' );
	if ( in_array( $error_code, $transient_failures, true ) && ! get_site_option( 'auto_core_update_failed' ) ) {
		wp_schedule_single_event( time() + HOUR_IN_SECONDS, 'wp_maybe_auto_update' );
		$send = false;
	}

	$notified = get_site_option( 'auto_core_update_notified' );

	// Don't notify if we've already notified the same email address of the same version of the same notification type.
	if ( $notified
		&& 'fail' === $notified['type']
		&& get_site_option( 'admin_email' ) === $notified['email']
		&& $notified['version'] === $core_update->current
	) {
		$send = false;
	}

	update_site_option(
		'auto_core_update_failed',
		array(
			'attempted'  => $core_update->current,
			'current'    => $wp_version,
			'error_code' => $error_code,
			'error_data' => $result->get_error_data(),
			'timestamp'  => time(),
			'retry'      => in_array( $error_code, $transient_failures, true ),
		)
	);

	if ( $send ) {
		$this->send_email( 'fail', $core_update, $result );
	}
}

Changelog

VersionDescription
3.7.0Introduced.

User Contributed Notes

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