WP_Automatic_Updater::after_core_update()protectedWP 3.7.0

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

Method of the class: WP_Automatic_Updater{}

No Hooks.

Return

null. Nothing (null).

Usage

// protected - for code of main (parent) or child class
$result = $this->after_core_update( $update_result );
$update_result(object) (required)
The result of the core update. Includes the update offer and result.

Changelog

Since 3.7.0 Introduced.

WP_Automatic_Updater::after_core_update() code WP 6.6.2

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 );
	}
}