mirror of
https://github.com/WordPress/WordPress.git
synced 2024-11-05 10:22:23 +01:00
REST API: Introduce the rest_preload_api_request()
function.
This function helps perform multiple REST API requests, for the purpose of preloading data into a page. See #45110. Built from https://develop.svn.wordpress.org/branches/5.0@43763 git-svn-id: http://core.svn.wordpress.org/branches/5.0@43592 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
parent
de323bc14c
commit
2ccaeedbda
@ -1311,3 +1311,56 @@ function rest_sanitize_value_from_schema( $value, $args ) {
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Append result of internal request to REST API for purpose of preloading data to be attached to a page.
|
||||
* Expected to be called in the context of `array_reduce`.
|
||||
*
|
||||
* @since 5.0.0
|
||||
*
|
||||
* @param array $memo Reduce accumulator.
|
||||
* @param string $path REST API path to preload.
|
||||
* @return array Modified reduce accumulator.
|
||||
*/
|
||||
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;
|
||||
}
|
||||
|
||||
$path_parts = parse_url( $path );
|
||||
if ( false === $path_parts ) {
|
||||
return $memo;
|
||||
}
|
||||
|
||||
$request = new WP_REST_Request( 'GET', $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();
|
||||
$data = (array) $response->get_data();
|
||||
if ( method_exists( $server, 'get_compact_response_links' ) ) {
|
||||
$links = call_user_func( array( $server, 'get_compact_response_links' ), $response );
|
||||
} else {
|
||||
$links = call_user_func( array( $server, 'get_response_links' ), $response );
|
||||
}
|
||||
if ( ! empty( $links ) ) {
|
||||
$data['_links'] = $links;
|
||||
}
|
||||
|
||||
$memo[ $path ] = array(
|
||||
'body' => $data,
|
||||
'headers' => $response->headers,
|
||||
);
|
||||
}
|
||||
|
||||
return $memo;
|
||||
}
|
||||
|
@ -4,7 +4,7 @@
|
||||
*
|
||||
* @global string $wp_version
|
||||
*/
|
||||
$wp_version = '5.0-alpha-43762';
|
||||
$wp_version = '5.0-alpha-43763';
|
||||
|
||||
/**
|
||||
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.
|
||||
|
Loading…
Reference in New Issue
Block a user