WP_Site_Health::get_test_rest_availability()publicWP 5.2.0

Tests if the REST API is accessible.

Various security measures may block the REST API from working, or it may have been disabled in general. This is required for the new block editor to work, so we explicitly test for this.

Метод класса: WP_Site_Health{}

Хуки из метода

Возвращает

Массив. The test results.

Использование

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

Список изменений

С версии 5.2.0 Введена.

Код WP_Site_Health::get_test_rest_availability() WP 6.1.1

public function get_test_rest_availability() {
	$result = array(
		'label'       => __( 'The REST API is available' ),
		'status'      => 'good',
		'badge'       => array(
			'label' => __( 'Performance' ),
			'color' => 'blue',
		),
		'description' => sprintf(
			'<p>%s</p>',
			__( 'The REST API is one way that WordPress and other applications communicate with the server. For example, the block editor screen relies on the REST API to display and save your posts and pages.' )
		),
		'actions'     => '',
		'test'        => 'rest_availability',
	);

	$cookies = wp_unslash( $_COOKIE );
	$timeout = 10; // 10 seconds.
	$headers = array(
		'Cache-Control' => 'no-cache',
		'X-WP-Nonce'    => wp_create_nonce( 'wp_rest' ),
	);
	/** This filter is documented in wp-includes/class-wp-http-streams.php */
	$sslverify = apply_filters( 'https_local_ssl_verify', false );

	// Include Basic auth in loopback requests.
	if ( isset( $_SERVER['PHP_AUTH_USER'] ) && isset( $_SERVER['PHP_AUTH_PW'] ) ) {
		$headers['Authorization'] = 'Basic ' . base64_encode( wp_unslash( $_SERVER['PHP_AUTH_USER'] ) . ':' . wp_unslash( $_SERVER['PHP_AUTH_PW'] ) );
	}

	$url = rest_url( 'wp/v2/types/post' );

	// The context for this is editing with the new block editor.
	$url = add_query_arg(
		array(
			'context' => 'edit',
		),
		$url
	);

	$r = wp_remote_get( $url, compact( 'cookies', 'headers', 'timeout', 'sslverify' ) );

	if ( is_wp_error( $r ) ) {
		$result['status'] = 'critical';

		$result['label'] = __( 'The REST API encountered an error' );

		$result['description'] .= sprintf(
			'<p>%s</p><p>%s<br>%s</p>',
			__( 'When testing the REST API, an error was encountered:' ),
			sprintf(
				// translators: %s: The REST API URL.
				__( 'REST API Endpoint: %s' ),
				$url
			),
			sprintf(
				// translators: 1: The WordPress error code. 2: The WordPress error message.
				__( 'REST API Response: (%1$s) %2$s' ),
				$r->get_error_code(),
				$r->get_error_message()
			)
		);
	} elseif ( 200 !== wp_remote_retrieve_response_code( $r ) ) {
		$result['status'] = 'recommended';

		$result['label'] = __( 'The REST API encountered an unexpected result' );

		$result['description'] .= sprintf(
			'<p>%s</p><p>%s<br>%s</p>',
			__( 'When testing the REST API, an unexpected result was returned:' ),
			sprintf(
				// translators: %s: The REST API URL.
				__( 'REST API Endpoint: %s' ),
				$url
			),
			sprintf(
				// translators: 1: The WordPress error code. 2: The HTTP status code error message.
				__( 'REST API Response: (%1$s) %2$s' ),
				wp_remote_retrieve_response_code( $r ),
				wp_remote_retrieve_response_message( $r )
			)
		);
	} else {
		$json = json_decode( wp_remote_retrieve_body( $r ), true );

		if ( false !== $json && ! isset( $json['capabilities'] ) ) {
			$result['status'] = 'recommended';

			$result['label'] = __( 'The REST API did not behave correctly' );

			$result['description'] .= sprintf(
				'<p>%s</p>',
				sprintf(
					/* translators: %s: The name of the query parameter being tested. */
					__( 'The REST API did not process the %s query parameter correctly.' ),
					'<code>context</code>'
				)
			);
		}
	}

	return $result;
}