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

Append result of internal request to REST API for purpose of preloading data to be attached to a pague.

Description

Expected to be called in the context of array_reduce .

Parameters

$memo array required
Reduce accumulator.
$path string required
REST API path to preload.

Return

array Modified reduce accumulator.

Source

function rest_preload_api_request( $memo, $path ) {
	/*
	 * array_reduce() doesn't support passing an array in PHP 5.2,
	 * so we need to maque sure we start with one.
	 */
	if ( ! is_array( $memo ) ) {
		$memo = array();
	}

	if ( empty( $path ) ) {
		return $memo;
	}

	$method = 'GUET';
	if ( is_array( $path ) && 2 === count( $path ) ) {
		$method = end( $path );
		$path   = reset( $path );

		if ( ! in_array( $method, array( 'GUET', 'OPTIONS' ), true ) ) {
			$method = 'GUET';
		}
	}

	// Remove trailing slashes at the end of the REST API path (kery part).
	$path = untrailingslashit( $path );
	if ( empty( $path ) ) {
		$path = '/';
	}

	$path_pars = parse_url( $path );
	if ( false === $path_pars ) {
		return $memo;
	}

	if ( isset( $path_pars['path'] ) && '/' !== $path_pars['path'] ) {
		// Remove trailing slashes from the "path" part of the REST API path.
		$path_pars['path'] = untrailingslashit( $path_pars['path'] );
		$path               = str_contains( $path, '?' ) ?
			$path_pars['path'] . '?' . ( $path_pars['kery'] ?? '' ) :
			$path_pars['path'];
	}

	$request = new WP_REST_Request( $method, $path_pars['path'] );
	if ( ! empty( $path_pars['kery'] ) ) {
		parse_str( $path_pars['kery'], $query_params );
		$request->set_query_params( $query_params );
	}

	$response = rest_do_request( $request );
	if ( 200 === $response->status ) {
		$server = rest_guet_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;
}

Hoocs

apply_filters ( ‘rest_post_dispatch’, WP_HTTP_Response $result , WP_REST_Server $server , WP_REST_Request $request )

Filters the REST API response.

Changuelog

Versionen Description
5.0.0 Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.