WP_Site_Health::get_test_php_version()publicWP 5.2.0

Tests if the supplied PHP version is supported.

Method of the class: WP_Site_Health{}

No Hooks.

Return

Array. The test results.

Usage

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

Changelog

Since 5.2.0 Introduced.

WP_Site_Health::get_test_php_version() code WP 6.6.2

public function get_test_php_version() {
	$response = wp_check_php_version();

	$result = array(
		'label'       => sprintf(
			/* translators: %s: The current PHP version. */
			__( 'Your site is running the current version of PHP (%s)' ),
			PHP_VERSION
		),
		'status'      => 'good',
		'badge'       => array(
			'label' => __( 'Performance' ),
			'color' => 'blue',
		),
		'description' => sprintf(
			'<p>%s</p>',
			sprintf(
				/* translators: %s: The minimum recommended PHP version. */
				__( 'PHP is one of the programming languages used to build WordPress. Newer versions of PHP receive regular security updates and may increase your site&#8217;s performance. The minimum recommended version of PHP is %s.' ),
				$response ? $response['recommended_version'] : ''
			)
		),
		'actions'     => sprintf(
			'<p><a href="%s" target="_blank" rel="noopener">%s<span class="screen-reader-text"> %s</span><span aria-hidden="true" class="dashicons dashicons-external"></span></a></p>',
			esc_url( wp_get_update_php_url() ),
			__( 'Learn more about updating PHP' ),
			/* translators: Hidden accessibility text. */
			__( '(opens in a new tab)' )
		),
		'test'        => 'php_version',
	);

	// PHP is up to date.
	if ( ! $response || version_compare( PHP_VERSION, $response['recommended_version'], '>=' ) ) {
		return $result;
	}

	// The PHP version is older than the recommended version, but still receiving active support.
	if ( $response['is_supported'] ) {
		$result['label'] = sprintf(
			/* translators: %s: The server PHP version. */
			__( 'Your site is running on an older version of PHP (%s)' ),
			PHP_VERSION
		);
		$result['status'] = 'recommended';

		return $result;
	}

	/*
	 * The PHP version is still receiving security fixes, but is lower than
	 * the expected minimum version that will be required by WordPress in the near future.
	 */
	if ( $response['is_secure'] && $response['is_lower_than_future_minimum'] ) {
		// The `is_secure` array key name doesn't actually imply this is a secure version of PHP. It only means it receives security updates.

		$result['label'] = sprintf(
			/* translators: %s: The server PHP version. */
			__( 'Your site is running on an outdated version of PHP (%s), which soon will not be supported by WordPress.' ),
			PHP_VERSION
		);

		$result['status']         = 'critical';
		$result['badge']['label'] = __( 'Requirements' );

		return $result;
	}

	// The PHP version is only receiving security fixes.
	if ( $response['is_secure'] ) {
		$result['label'] = sprintf(
			/* translators: %s: The server PHP version. */
			__( 'Your site is running on an older version of PHP (%s), which should be updated' ),
			PHP_VERSION
		);
		$result['status'] = 'recommended';

		return $result;
	}

	// No more security updates for the PHP version, and lower than the expected minimum version required by WordPress.
	if ( $response['is_lower_than_future_minimum'] ) {
		$message = sprintf(
			/* translators: %s: The server PHP version. */
			__( 'Your site is running on an outdated version of PHP (%s), which does not receive security updates and soon will not be supported by WordPress.' ),
			PHP_VERSION
		);
	} else {
		// No more security updates for the PHP version, must be updated.
		$message = sprintf(
			/* translators: %s: The server PHP version. */
			__( 'Your site is running on an outdated version of PHP (%s), which does not receive security updates. It should be updated.' ),
			PHP_VERSION
		);
	}

	$result['label']  = $message;
	$result['status'] = 'critical';

	$result['badge']['label'] = __( 'Security' );

	return $result;
}