WP_Automatic_Updater::should_update( string $type, object $item, string $context ): bool

In this article

Tests to see if we can and should update a specific item.

Parameters

$typestringrequired
The type of update being checked: 'core', 'theme', 'plugin', 'translation'.
$itemobjectrequired
The update offer.
$contextstringrequired
The filesystem context (a path) against which filesystem access and status should be checked.

Return

bool True if the item should be updated, false otherwise.

Source

public function should_update( $type, $item, $context ) {
	// Used to see if WP_Filesystem is set up to allow unattended updates.
	$skin = new Automatic_Upgrader_Skin();

	if ( $this->is_disabled() ) {
		return false;
	}

	// Only relax the filesystem checks when the update doesn't include new files.
	$allow_relaxed_file_ownership = false;
	if ( 'core' === $type && isset( $item->new_files ) && ! $item->new_files ) {
		$allow_relaxed_file_ownership = true;
	}

	// If we can't do an auto core update, we may still be able to email the user.
	if ( ! $skin->request_filesystem_credentials( false, $context, $allow_relaxed_file_ownership )
		|| $this->is_vcs_checkout( $context )
	) {
		if ( 'core' === $type ) {
			$this->send_core_update_notification_email( $item );
		}
		return false;
	}

	// Next up, is this an item we can update?
	if ( 'core' === $type ) {
		$update = Core_Upgrader::should_update_to_version( $item->current );
	} elseif ( 'plugin' === $type || 'theme' === $type ) {
		$update = ! empty( $item->autoupdate );

		if ( ! $update && wp_is_auto_update_enabled_for_type( $type ) ) {
			// Check if the site admin has enabled auto-updates by default for the specific item.
			$auto_updates = (array) get_site_option( "auto_update_{$type}s", array() );
			$update       = in_array( $item->{$type}, $auto_updates, true );
		}
	} else {
		$update = ! empty( $item->autoupdate );
	}

	// If the `disable_autoupdate` flag is set, override any user-choice, but allow filters.
	if ( ! empty( $item->disable_autoupdate ) ) {
		$update = $item->disable_autoupdate;
	}

	/**
	 * Filters whether to automatically update core, a plugin, a theme, or a language.
	 *
	 * The dynamic portion of the hook name, `$type`, refers to the type of update
	 * being checked.
	 *
	 * Possible hook names include:
	 *
	 *  - `auto_update_core`
	 *  - `auto_update_plugin`
	 *  - `auto_update_theme`
	 *  - `auto_update_translation`
	 *
	 * Since WordPress 3.7, minor and development versions of core, and translations have
	 * been auto-updated by default. New installs on WordPress 5.6 or higher will also
	 * auto-update major versions by default. Starting in 5.6, older sites can opt-in to
	 * major version auto-updates, and auto-updates for plugins and themes.
	 *
	 * See the 'allow_dev_auto_core_updates', 'allow_minor_auto_core_updates',
	 * and 'allow_major_auto_core_updates' filters for a more straightforward way to
	 * adjust core updates.
	 *
	 * @since 3.7.0
	 * @since 5.5.0 The `$update` parameter accepts the value of null.
	 *
	 * @param bool|null $update Whether to update. The value of null is internally used
	 *                          to detect whether nothing has hooked into this filter.
	 * @param object    $item   The update offer.
	 */
	$update = apply_filters( "auto_update_{$type}", $update, $item );

	if ( ! $update ) {
		if ( 'core' === $type ) {
			$this->send_core_update_notification_email( $item );
		}
		return false;
	}

	// If it's a core update, are we actually compatible with its requirements?
	if ( 'core' === $type ) {
		global $wpdb;

		$php_compat = version_compare( PHP_VERSION, $item->php_version, '>=' );
		if ( file_exists( WP_CONTENT_DIR . '/db.php' ) && empty( $wpdb->is_mysql ) ) {
			$mysql_compat = true;
		} else {
			$mysql_compat = version_compare( $wpdb->db_version(), $item->mysql_version, '>=' );
		}

		if ( ! $php_compat || ! $mysql_compat ) {
			return false;
		}
	}

	// If updating a plugin or theme, ensure the minimum PHP version requirements are satisfied.
	if ( in_array( $type, array( 'plugin', 'theme' ), true ) ) {
		if ( ! empty( $item->requires_php ) && version_compare( PHP_VERSION, $item->requires_php, '<' ) ) {
			return false;
		}
	}

	return true;
}

Hooks

apply_filters( “auto_update_{$type}”, bool|null $update, object $item )

Filters whether to automatically update core, a plugin, a theme, or a language.

Changelog

VersionDescription
3.7.0Introduced.

User Contributed Notes

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