Block Editor: Preload wp/v2/media with OPTIONS for caps check.

Also introduces a `block_editor_preload_paths` filter for plugins and themes to preload additional data.

Merges [43833] from the 5.0 branch to trunk.

Props imath, mattheu, danielbachhuber.
Fixes #45194.



git-svn-id: https://develop.svn.wordpress.org/trunk@44172 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Gary Pendergast
2018-12-14 06:01:24 +00:00
parent ebb6b7f703
commit fb2f48d81b
3 changed files with 58 additions and 5 deletions

View File

@@ -1342,12 +1342,22 @@ function rest_preload_api_request( $memo, $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_parts = parse_url( $path );
if ( false === $path_parts ) {
return $memo;
}
$request = new WP_REST_Request( 'GET', $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 );
@@ -1366,10 +1376,19 @@ function rest_preload_api_request( $memo, $path ) {
$data['_links'] = $links;
}
$memo[ $path ] = array(
'body' => $data,
'headers' => $response->headers,
);
if ( 'OPTIONS' === $method ) {
$response = rest_send_allow_header( $response, $server, $request );
$memo[ $method ][ $path ] = array(
'body' => $data,
'headers' => $response->headers,
);
} else {
$memo[ $path ] = array(
'body' => $data,
'headers' => $response->headers,
);
}
}
return $memo;