wppaste
WordPress

rest_preload_api_request( array $memo, string $path ): array

Since
5.0.0
Source
wp-includes/rest-api.php:2946
Append result of internal request to REST API for purpose of preloading data to be attached to a page.

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

Touches nothing outside its own arguments.

Scaling
Constant

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

Instructions
7–122

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

Plugin surface
1 hook

Third-party callbacks on 'rest_post_dispatch' 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

What one call costs · 27 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.

WhenInstructionsCalls it makes
empty($path)7–8none
!empty($path) && $path_parts === false22–27untrailingslashit(), parse_url()
!empty($path) && is_array($path) && $path_parts === false35–38end(), reset(), untrailingslashit(), parse_url()
!empty($path) && $path_parts !== false && empty($path_parts)40–48untrailingslashit(), parse_url(), rest_do_request()
!empty($path) && $path_parts !== false && !empty($path_parts)48–56untrailingslashit(), parse_url(), parse_str(), ->set_query_params(), rest_do_request()
!empty($path) && is_array($path) && $path_parts !== false && empty($path_parts)53–59end(), reset(), untrailingslashit(), parse_url(), rest_do_request()
!empty($path) && $path_parts !== false && isset($path_parts) && empty($path_parts)54–65untrailingslashit(), parse_url(), untrailingslashit(), rest_do_request()
!empty($path) && is_array($path) && $path_parts !== false && !empty($path_parts)61–67end(), reset(), untrailingslashit(), parse_url(), parse_str(), ->set_query_params(), rest_do_request()
!empty($path) && $path_parts !== false && isset($path_parts) && !empty($path_parts)62–73untrailingslashit(), parse_url(), untrailingslashit(), parse_str(), ->set_query_params(), rest_do_request()
!empty($path) && is_array($path) && $path_parts !== false && isset($path_parts) && empty($path_parts)67–76end(), reset(), untrailingslashit(), parse_url(), untrailingslashit(), rest_do_request()
!empty($path) && $path_parts !== false && empty($path_parts) && !->has_param()71–80untrailingslashit(), parse_url(), rest_do_request(), rest_get_server(), rest_ensure_response(), apply_filters(), ->has_param(), ->response_to_data()
!empty($path) && is_array($path) && $path_parts !== false && isset($path_parts) && !empty($path_parts)75–84end(), reset(), untrailingslashit(), parse_url(), untrailingslashit(), parse_str(), ->set_query_params(), rest_do_request()
15 further outcomes, up to 122 instructions
!empty($path) && $path_parts !== false && empty($path_parts) && ->has_param()77–86untrailingslashit(), 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()79–88untrailingslashit(), 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()84–91end(), 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 && isset($path_parts) && empty($path_parts) && !->has_param()85–97untrailingslashit(), parse_url(), untrailingslashit(), 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()85–94untrailingslashit(), 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()
!empty($path) && is_array($path) && $path_parts !== false && empty($path_parts) && ->has_param()90–97end(), 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) && $path_parts !== false && isset($path_parts) && empty($path_parts) && ->has_param()91–103untrailingslashit(), parse_url(), untrailingslashit(), 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()92–99end(), 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) && $path_parts !== false && isset($path_parts) && !empty($path_parts) && !->has_param()93–105untrailingslashit(), parse_url(), untrailingslashit(), 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 && isset($path_parts) && empty($path_parts) && !->has_param()98–108end(), reset(), untrailingslashit(), parse_url(), untrailingslashit(), 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()98–105end(), 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()
!empty($path) && $path_parts !== false && isset($path_parts) && !empty($path_parts) && ->has_param()99–111untrailingslashit(), parse_url(), untrailingslashit(), 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()
!empty($path) && is_array($path) && $path_parts !== false && isset($path_parts) && empty($path_parts) && ->has_param()104–114end(), reset(), untrailingslashit(), parse_url(), untrailingslashit(), 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 && isset($path_parts) && !empty($path_parts) && !->has_param()106–116end(), reset(), untrailingslashit(), parse_url(), untrailingslashit(), 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 && isset($path_parts) && !empty($path_parts) && ->has_param()112–122end(), reset(), untrailingslashit(), parse_url(), untrailingslashit(), 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

PHPCompiledExecutedBranchesNotes
8.6-dev1317–121151 fewer instruction than PHP 8.5
8.51327–12215
8.41327–122153 fewer instructions than PHP 8.3
8.31357–12515
8.21357–12515
8.11357–125152 fewer instructions than PHP 7.4
7.41377–12615

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:

  1. apply_filters( rest_post_dispatch )filterline 2998 (+52 into the body)

    Filters the REST API response.

Uses · 8

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';		}	} 	// Remove trailing slashes at the end of the REST API path (query part).	$path = untrailingslashit( $path );	if ( empty( $path ) ) {		$path = '/';	} 	$path_parts = parse_url( $path );	if ( false === $path_parts ) {		return $memo;	} 	if ( isset( $path_parts['path'] ) && '/' !== $path_parts['path'] ) {		// Remove trailing slashes from the "path" part of the REST API path.		$path_parts['path'] = untrailingslashit( $path_parts['path'] );		$path               = str_contains( $path, '?' ) ?			$path_parts['path'] . '?' . ( $path_parts['query'] ?? '' ) :			$path_parts['path'];	} 	$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.

  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 7.0.4 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.