diff --git a/src/wp-admin/edit-form-blocks.php b/src/wp-admin/edit-form-blocks.php
index d998a11dad..b3deab00d9 100644
a
|
b
|
$preload_paths = array( |
49 | 49 | sprintf( '/wp/v2/%s/%s?context=edit', $rest_base, $post->ID ), |
50 | 50 | sprintf( '/wp/v2/types/%s?context=edit', $post_type ), |
51 | 51 | sprintf( '/wp/v2/users/me?post_type=%s&context=edit', $post_type ), |
| 52 | array( '/wp/v2/media', 'OPTIONS' ), |
52 | 53 | ); |
53 | 54 | |
| 55 | /** |
| 56 | * Preload common data by specifying an array of REST API paths that will be preloaded. |
| 57 | * |
| 58 | * Filters the array of paths that will be preloaded. |
| 59 | * |
| 60 | * @since 5.0.0 |
| 61 | * |
| 62 | * @param array $preload_paths Array of paths to preload. |
| 63 | * @param object $post The post resource data. |
| 64 | */ |
| 65 | $preload_paths = apply_filters( 'block_editor_preload_paths', $preload_paths, $post ); |
| 66 | |
54 | 67 | /* |
55 | 68 | * Ensure the global $post remains the same after API data is preloaded. |
56 | 69 | * Because API preloading can call the_content and other filters, plugins |
diff --git a/src/wp-includes/rest-api.php b/src/wp-includes/rest-api.php
index e1d8919c8d..f1892c10c4 100644
a
|
b
|
function rest_preload_api_request( $memo, $path ) { |
1343 | 1343 | return $memo; |
1344 | 1344 | } |
1345 | 1345 | |
| 1346 | $method = 'GET'; |
| 1347 | if ( is_array( $path ) && 2 === count( $path ) ) { |
| 1348 | $method = end( $path ); |
| 1349 | $path = reset( $path ); |
| 1350 | |
| 1351 | if ( ! in_array( $method, array( 'GET', 'OPTIONS' ), true ) ) { |
| 1352 | $method = 'GET'; |
| 1353 | } |
| 1354 | } |
| 1355 | |
1346 | 1356 | $path_parts = parse_url( $path ); |
1347 | 1357 | if ( false === $path_parts ) { |
1348 | 1358 | return $memo; |
1349 | 1359 | } |
1350 | 1360 | |
1351 | | $request = new WP_REST_Request( 'GET', $path_parts['path'] ); |
| 1361 | $request = new WP_REST_Request( $method, $path_parts['path'] ); |
1352 | 1362 | if ( ! empty( $path_parts['query'] ) ) { |
1353 | 1363 | parse_str( $path_parts['query'], $query_params ); |
1354 | 1364 | $request->set_query_params( $query_params ); |
… |
… |
function rest_preload_api_request( $memo, $path ) { |
1367 | 1377 | $data['_links'] = $links; |
1368 | 1378 | } |
1369 | 1379 | |
1370 | | $memo[ $path ] = array( |
1371 | | 'body' => $data, |
1372 | | 'headers' => $response->headers, |
1373 | | ); |
| 1380 | if ( 'OPTIONS' === $method ) { |
| 1381 | $response = rest_send_allow_header( $response, $server, $request ); |
| 1382 | |
| 1383 | $memo[ $method ][ $path ] = array( |
| 1384 | 'body' => $data, |
| 1385 | 'headers' => $response->headers, |
| 1386 | ); |
| 1387 | } else { |
| 1388 | $memo[ $path ] = array( |
| 1389 | 'body' => $data, |
| 1390 | 'headers' => $response->headers, |
| 1391 | ); |
| 1392 | } |
1374 | 1393 | } |
1375 | 1394 | |
1376 | 1395 | return $memo; |
diff --git a/tests/phpunit/tests/rest-api.php b/tests/phpunit/tests/rest-api.php
index d30eb421c5..c82d94fff7 100644
a
|
b
|
class Tests_REST_API extends WP_UnitTestCase { |
629 | 629 | function test_rest_preload_api_request_no_notices_php_52() { |
630 | 630 | $this->assertTrue( is_array( rest_preload_api_request( 0, '/' ) ) ); |
631 | 631 | } |
| 632 | |
| 633 | function test_rest_preload_api_request_with_method() { |
| 634 | $rest_server = $GLOBALS['wp_rest_server']; |
| 635 | $GLOBALS['wp_rest_server'] = null; |
| 636 | |
| 637 | $preload_paths = array( |
| 638 | '/wp/v2/types', |
| 639 | array( '/wp/v2/media', 'OPTIONS' ), |
| 640 | ); |
| 641 | |
| 642 | $preload_data = array_reduce( |
| 643 | $preload_paths, |
| 644 | 'rest_preload_api_request', |
| 645 | array() |
| 646 | ); |
| 647 | |
| 648 | $this->assertSame( array_keys( $preload_data ), array( '/wp/v2/types', 'OPTIONS' ) ); |
| 649 | $this->assertTrue( isset( $preload_data['OPTIONS']['/wp/v2/media'] ) ); |
| 650 | |
| 651 | $GLOBALS['wp_rest_server'] = $rest_server; |
| 652 | } |
632 | 653 | } |