WP_Site_Health::get_test_rest_availability(): array
- Since
- 5.2.0
- Source
wp-admin/includes/class-wp-site-health.php:2122
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
- Scaling
- Constant
- Instructions
- 82–135
- Plugin surface
- 1 hook
- Called by
- 0
Reaches an outbound HTTP request via wp_remote_get().
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 206.
Third-party callbacks on 'https_local_ssl_verify' run inside this call, and their cost is not bounded by anything here.
Nothing in core calls this; the cost is only what you spend yourself.
What it touches
- hookthird-party callbacks
apply_filters()called directly - httpoutbound HTTP request
wp_remote_get()called directly - serializeserialisation
json_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.
| When | Instructions | Calls 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
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 206 | 82–135 | 6 | |
| 8.5 | 206 | 82–135 | 6 | |
| 8.4 | 206 | 82–135 | 6 | Different branch profile from PHP 8.3 |
| 8.3 | 206 | 83–135 | 6 | |
| 8.2 | 206 | 83–135 | 6 | |
| 8.1 | 206 | 83–135 | 6 | |
| 7.4 | 206 | 83–135 | 6 |
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:
- apply_filters( https_local_ssl_verify )filterline 2145 (+23 into the body)
Filters whether SSL should be verified for local HTTP API requests.
Uses · 11
- __()Retrieves the translation of $text.
- wp_unslash()Removes slashes from a string or recursively removes slashes from strings within an array.
- wp_create_nonce()Creates a cryptographic token tied to a specific action, user, user session, and window of time.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- rest_url()Retrieves the URL to a REST endpoint.
- add_query_arg()Retrieves a modified URL query string.
- wp_remote_get()Performs an HTTP request using the GET method and returns its response.
- is_wp_error()Checks whether the given variable is a WordPress Error.
- wp_remote_retrieve_response_code()Retrieves only the response code from the raw response.
- wp_remote_retrieve_response_message()Retrieves only the response message from the raw response.
- wp_remote_retrieve_body()Retrieves only the body from the raw response.
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.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 6.7.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.