WP_Site_Health::get_test_wordpress_version │ public │ WP 5.2.0

Tests for WordPress version and outputs it.

Gives various results depending on what kind of updates are available, if any, to encourage the user to install security updates as a priority.

Method of the class: WP_Site_Health{}

No Hooks.

Returns

array. The test result.

Usage

$WP_Site_Health = new WP_Site_Health();
$WP_Site_Health->get_test_wordpress_version();

Changelog

Since 5.2.0 Introduced.

WP_Site_Health::get_test_wordpress_version() code WP 7.1.2

public function get_test_wordpress_version() {
	$result = array(
		'label'       => '',
		'status'      => '',
		'badge'       => array(
			'label' => __( 'Performance' ),
			'color' => 'blue',
		),
		'description' => '',
		'actions'     => '',
		'test'        => 'wordpress_version',
	);

	$core_current_version = wp_get_wp_version();
	$core_updates         = get_core_updates();

	if ( ! is_array( $core_updates ) ) {
		$result['status'] = 'recommended';

		$result['label'] = sprintf(
			/* translators: %s: Your current version of WordPress. */
			__( 'WordPress version %s' ),
			$core_current_version
		);

		$result['description'] = sprintf(
			'<p>%s</p>',
			__( 'Unable to check if any new versions of WordPress are available.' )
		);

		$result['actions'] = sprintf(
			'<a href="%s">%s</a>',
			esc_url( admin_url( 'update-core.php?force-check=1' ) ),
			__( 'Check for updates manually' )
		);
	} else {
		foreach ( $core_updates as $core => $update ) {
			if ( 'upgrade' === $update->response ) {
				$current_version = explode( '.', $core_current_version );
				$new_version     = explode( '.', $update->version );

				$current_major = $current_version[0] . '.' . $current_version[1];
				$new_major     = $new_version[0] . '.' . $new_version[1];

				$result['label'] = sprintf(
					/* translators: %s: The latest version of WordPress available. */
					__( 'WordPress update available (%s)' ),
					$update->version
				);

				$result['actions'] = sprintf(
					'<a href="%s">%s</a>',
					esc_url( admin_url( 'update-core.php' ) ),
					__( 'Install the latest version of WordPress' )
				);

				if ( $current_major !== $new_major ) {
					// This is a major version mismatch.
					$result['status']      = 'recommended';
					$result['description'] = sprintf(
						'<p>%s</p>',
						__( 'A new version of WordPress is available.' )
					);
				} else {
					// This is a minor version, sometimes considered more critical.
					$result['status']         = 'critical';
					$result['badge']['label'] = __( 'Security' );
					$result['description']    = sprintf(
						'<p>%s</p>',
						__( 'A new minor update is available for your site. Because minor updates often address security, it&#8217;s important to install them.' )
					);
				}
			} else {
				$result['status'] = 'good';
				$result['label']  = sprintf(
					/* translators: %s: The current version of WordPress installed on this site. */
					__( 'Your version of WordPress (%s) is up to date' ),
					$core_current_version
				);

				$result['description'] = sprintf(
					'<p>%s</p>',
					__( 'You are currently running the latest version of WordPress available, keep it up!' )
				);
			}
		}
	}

	return $result;
}