rest_preload_api_request( array $memo, string $path ): array
- Since
- 5.0.0
- Source
wp-includes/rest-api.php:2918
Description
Expected to be called in the context of array_reduce.
Compatibility
- WordPress
- since 5.0.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.
Parameters
$memoarray- Reduce accumulator.
$pathstring- REST API path to preload.
Return value
array- Modified reduce accumulator.
Performance profile
How much work a call to rest_preload_api_request() 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
- Trivial
- Scaling
- Constant
- Instructions
- 7–100
- Plugin surface
- 1 hook
- Called by
- 0
Touches nothing outside its own arguments.
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 109.
Third-party callbacks on 'rest_post_dispatch' 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
What one call costs · 15 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost rest_preload_api_request() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
empty($path) | 7–8 | none |
!empty($path) && $path_parts === false | 22–27 | untrailingslashit(), parse_url() |
!empty($path) && is_array($path) && $path_parts === false | 35–38 | end(), reset(), untrailingslashit(), parse_url() |
!empty($path) && $path_parts !== false && empty($path_parts) | 38–43 | untrailingslashit(), parse_url(), rest_do_request() |
!empty($path) && $path_parts !== false && !empty($path_parts) | 46–51 | untrailingslashit(), parse_url(), parse_str(), ->set_query_params(), rest_do_request() |
!empty($path) && is_array($path) && $path_parts !== false && empty($path_parts) | 51–54 | end(), reset(), untrailingslashit(), parse_url(), rest_do_request() |
!empty($path) && is_array($path) && $path_parts !== false && !empty($path_parts) | 59–62 | end(), reset(), untrailingslashit(), parse_url(), parse_str(), ->set_query_params(), rest_do_request() |
!empty($path) && $path_parts !== false && empty($path_parts) && !->has_param() | 69–75 | untrailingslashit(), parse_url(), rest_do_request(), rest_get_server(), rest_ensure_response(), apply_filters(), ->has_param(), ->response_to_data() |
!empty($path) && $path_parts !== false && empty($path_parts) && ->has_param() | 75–81 | untrailingslashit(), parse_url(), rest_do_request(), rest_get_server(), rest_ensure_response(), apply_filters(), ->has_param(), rest_parse_embed_param(), ->response_to_data() |
!empty($path) && $path_parts !== false && !empty($path_parts) && !->has_param() | 77–83 | untrailingslashit(), parse_url(), parse_str(), ->set_query_params(), rest_do_request(), rest_get_server(), rest_ensure_response(), apply_filters(), ->has_param(), ->response_to_data() |
!empty($path) && is_array($path) && $path_parts !== false && empty($path_parts) && !->has_param() | 82–86 | end(), reset(), untrailingslashit(), parse_url(), rest_do_request(), rest_get_server(), rest_ensure_response(), apply_filters(), ->has_param(), ->response_to_data() |
!empty($path) && $path_parts !== false && !empty($path_parts) && ->has_param() | 83–89 | untrailingslashit(), parse_url(), parse_str(), ->set_query_params(), rest_do_request(), rest_get_server(), rest_ensure_response(), apply_filters(), ->has_param(), rest_parse_embed_param(), ->response_to_data() |
3 further outcomes, up to 100 instructions
!empty($path) && is_array($path) && $path_parts !== false && empty($path_parts) && ->has_param() | 88–92 | end(), reset(), untrailingslashit(), parse_url(), rest_do_request(), rest_get_server(), rest_ensure_response(), apply_filters(), ->has_param(), rest_parse_embed_param(), ->response_to_data() |
!empty($path) && is_array($path) && $path_parts !== false && !empty($path_parts) && !->has_param() | 90–94 | end(), reset(), untrailingslashit(), parse_url(), parse_str(), ->set_query_params(), rest_do_request(), rest_get_server(), rest_ensure_response(), apply_filters(), ->has_param(), ->response_to_data() |
!empty($path) && is_array($path) && $path_parts !== false && !empty($path_parts) && ->has_param() | 96–100 | end(), reset(), untrailingslashit(), parse_url(), parse_str(), ->set_query_params(), rest_do_request(), rest_get_server(), rest_ensure_response(), apply_filters(), ->has_param(), rest_parse_embed_param(), ->response_to_data() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 108 | 7–99 | 11 | 1 fewer instruction than PHP 8.5 |
| 8.5 | 109 | 7–100 | 11 | |
| 8.4 | 109 | 7–100 | 11 | |
| 8.3 | 109 | 7–100 | 11 | |
| 8.2 | 109 | 7–100 | 11 | |
| 8.1 | 109 | 7–100 | 11 | |
| 7.4 | 109 | 7–100 | 11 |
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 rest_preload_api_request() runs, in this order:
- apply_filters( rest_post_dispatch )filterline 2961 (+43 into the body)
Filters the REST API response.
Uses · 7
- untrailingslashit()Removes trailing forward slashes and backslashes if they exist.
- rest_do_request()Do a REST request.
- rest_get_server()Retrieves the current REST server instance.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- rest_ensure_response()Ensures a REST response is a response object (for consistency).
- rest_parse_embed_param()Parses the "_embed" parameter into the list of resources to embed.
- WP_REST_Request::__construct()Constructor.
Source code
function rest_preload_api_request( $memo, $path ) { /* * array_reduce() doesn't support passing an array in PHP 5.2, * so we need to make sure we start with one. */ if ( ! is_array( $memo ) ) { $memo = array(); } if ( empty( $path ) ) { return $memo; } $method = 'GET'; if ( is_array( $path ) && 2 === count( $path ) ) { $method = end( $path ); $path = reset( $path ); if ( ! in_array( $method, array( 'GET', 'OPTIONS' ), true ) ) { $method = 'GET'; } } $path = untrailingslashit( $path ); if ( empty( $path ) ) { $path = '/'; } $path_parts = parse_url( $path ); if ( false === $path_parts ) { return $memo; } $request = new WP_REST_Request( $method, $path_parts['path'] ); if ( ! empty( $path_parts['query'] ) ) { parse_str( $path_parts['query'], $query_params ); $request->set_query_params( $query_params ); } $response = rest_do_request( $request ); if ( 200 === $response->status ) { $server = rest_get_server(); /** This filter is documented in wp-includes/rest-api/class-wp-rest-server.php */ $response = apply_filters( 'rest_post_dispatch', rest_ensure_response( $response ), $server, $request ); $embed = $request->has_param( '_embed' ) ? rest_parse_embed_param( $request['_embed'] ) : false; $data = (array) $server->response_to_data( $response, $embed ); if ( 'OPTIONS' === $method ) { $memo[ $method ][ $path ] = array( 'body' => $data, 'headers' => $response->headers, ); } else { $memo[ $path ] = array( 'body' => $data, 'headers' => $response->headers, ); } } return $memo;}Changelog
Introduced in 5.0.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-includes/rest-api.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.