WP_Site_Health::enqueue_scripts()

Enqueues the site health scripts.


Description Description


Source Source

File: wp-admin/includes/class-wp-site-health.php

	public function enqueue_scripts() {
		$screen = get_current_screen();
		if ( 'site-health' !== $screen->id ) {
			return;
		}

		$health_check_js_variables = array(
			'screen'      => $screen->id,
			'nonce'       => array(
				'site_status'        => wp_create_nonce( 'health-check-site-status' ),
				'site_status_result' => wp_create_nonce( 'health-check-site-status-result' ),
			),
			'site_status' => array(
				'direct' => array(),
				'async'  => array(),
				'issues' => array(
					'good'        => 0,
					'recommended' => 0,
					'critical'    => 0,
				),
			),
		);

		$issue_counts = get_transient( 'health-check-site-status-result' );

		if ( false !== $issue_counts ) {
			$issue_counts = json_decode( $issue_counts );

			$health_check_js_variables['site_status']['issues'] = $issue_counts;
		}

		if ( 'site-health' === $screen->id && ! isset( $_GET['tab'] ) ) {
			$tests = WP_Site_Health::get_tests();

			// Don't run https test on localhost
			if ( 'localhost' === preg_replace( '|https?://|', '', get_site_url() ) ) {
				unset( $tests['direct']['https_status'] );
			}

			foreach ( $tests['direct'] as $test ) {
				if ( is_string( $test['test'] ) ) {
					$test_function = sprintf(
						'get_test_%s',
						$test['test']
					);

					if ( method_exists( $this, $test_function ) && is_callable( array( $this, $test_function ) ) ) {
						/**
						 * Filter the output of a finished Site Health test.
						 *
						 * @since 5.3.0
						 *
						 * @param array $test_result {
						 *     An associated array of test result data.
						 *
						 *     @param string $label  A label describing the test, and is used as a header in the output.
						 *     @param string $status The status of the test, which can be a value of `good`, `recommended` or `critical`.
						 *     @param array  $badge {
						 *         Tests are put into categories which have an associated badge shown, these can be modified and assigned here.
						 *
						 *         @param string $label The test label, for example `Performance`.
						 *         @param string $color Default `blue`. A string representing a color to use for the label.
						 *     }
						 *     @param string $description A more descriptive explanation of what the test looks for, and why it is important for the end user.
						 *     @param string $actions     An action to direct the user to where they can resolve the issue, if one exists.
						 *     @param string $test        The name of the test being ran, used as a reference point.
						 * }
						 */
						$health_check_js_variables['site_status']['direct'][] = apply_filters( 'site_status_test_result', call_user_func( array( $this, $test_function ) ) );
						continue;
					}
				}

				if ( is_callable( $test['test'] ) ) {
					/** This filter is documented in wp-admin/includes/class-wp-site-health.php */
					$health_check_js_variables['site_status']['direct'][] = apply_filters( 'site_status_test_result', call_user_func( $test['test'] ) );
				}
			}

			foreach ( $tests['async'] as $test ) {
				if ( is_string( $test['test'] ) ) {
					$health_check_js_variables['site_status']['async'][] = array(
						'test'      => $test['test'],
						'completed' => false,
					);
				}
			}
		}

		wp_localize_script( 'site-health', 'SiteHealth', $health_check_js_variables );
	}

Top ↑

Changelog Changelog

Changelog
Version Description
5.2.0 Introduced.


Top ↑

User Contributed Notes User Contributed Notes

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