wppaste
WordPress

WP_Site_Health::get_test_rest_availability(): array

Since
5.2.0
Source
wp-admin/includes/class-wp-site-health.php:2146
Tests if the REST API is accessible.

Description

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.

Compatibility

WordPress
since 5.2.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Return value

array
The test results.

Performance profile

How much work a call to WP_Site_Health::get_test_rest_availability() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Expensive

Reaches an outbound HTTP request via wp_remote_get().

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
82–135

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 206.

Plugin surface
1 hook

Third-party callbacks on 'https_local_ssl_verify' run inside this call, and their cost is not bounded by anything here.

Called by
0

Nothing in core calls this; the cost is only what you spend yourself.

What it touches

  • hookthird-party callbacksapply_filters()called directly
  • httpoutbound HTTP requestwp_remote_get()called directly
  • serializeserialisationjson_decode()called directly

Further down the call graph this can also reach option, cache, transient and query. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 8 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_Site_Health::get_test_rest_availability() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
!isset($value) && !is_wp_error()82–87__(), __(), __(), wp_unslash(), wp_create_nonce(), apply_filters(), rest_url(), add_query_arg(), compact(), wp_remote_get(), is_wp_error(), wp_remote_retrieve_response_code(), wp_remote_retrieve_body(), json_decode()
!isset($value) && !is_wp_error() && $json !== false && !isset($json)103–106__(), __(), __(), wp_unslash(), wp_create_nonce(), apply_filters(), rest_url(), add_query_arg(), compact(), wp_remote_get(), is_wp_error(), wp_remote_retrieve_response_code(), wp_remote_retrieve_body(), json_decode(), __(), __(), sprintf()
isset($value) && !is_wp_error()105–107__(), __(), __(), wp_unslash(), wp_create_nonce(), apply_filters(), wp_unslash(), wp_unslash(), base64_encode(), rest_url(), add_query_arg(), compact(), wp_remote_get(), is_wp_error(), wp_remote_retrieve_response_code(), wp_remote_retrieve_body(), json_decode()
!isset($value) && is_wp_error()105–108__(), __(), __(), wp_unslash(), wp_create_nonce(), apply_filters(), rest_url(), add_query_arg(), compact(), wp_remote_get(), is_wp_error(), __(), __(), __(), sprintf(), __(), ->get_error_code(), ->get_error_message(), sprintf()
!isset($value) && !is_wp_error()112–115__(), __(), __(), wp_unslash(), wp_create_nonce(), apply_filters(), rest_url(), add_query_arg(), compact(), wp_remote_get(), is_wp_error(), wp_remote_retrieve_response_code(), __(), __(), __(), sprintf(), __(), wp_remote_retrieve_response_code(), wp_remote_retrieve_response_message(), sprintf()
isset($value) && !is_wp_error() && $json !== false && !isset($json)126__(), __(), __(), wp_unslash(), wp_create_nonce(), apply_filters(), wp_unslash(), wp_unslash(), base64_encode(), rest_url(), add_query_arg(), compact(), wp_remote_get(), is_wp_error(), wp_remote_retrieve_response_code(), wp_remote_retrieve_body(), json_decode(), __(), __(), sprintf()
isset($value) && is_wp_error()128__(), __(), __(), wp_unslash(), wp_create_nonce(), apply_filters(), wp_unslash(), wp_unslash(), base64_encode(), rest_url(), add_query_arg(), compact(), wp_remote_get(), is_wp_error(), __(), __(), __(), sprintf(), __(), ->get_error_code(), ->get_error_message(), sprintf()
isset($value) && !is_wp_error()135__(), __(), __(), wp_unslash(), wp_create_nonce(), apply_filters(), wp_unslash(), wp_unslash(), base64_encode(), rest_url(), add_query_arg(), compact(), wp_remote_get(), is_wp_error(), wp_remote_retrieve_response_code(), __(), __(), __(), sprintf(), __(), wp_remote_retrieve_response_code(), wp_remote_retrieve_response_message(), sprintf()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev20682–1356
8.520682–1356
8.420682–1356Different branch profile from PHP 8.3
8.320683–1356
8.220683–1356
8.120683–1356
7.420683–1356

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Hooks and filters fired · 1

One hook fires while WP_Site_Health::get_test_rest_availability() runs, in this order:

  1. apply_filters( https_local_ssl_verify )filterline 2169 (+23 into the body)

    Filters whether SSL should be verified for local HTTP API requests.

Uses · 11

Source code

	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 )

Changelog

Introduced in 5.2.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 6.9.7 tag, from src/wp-admin/includes/class-wp-site-health.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.