From eea69421645ad91c22f2ffbeace83b50a38056d3 Mon Sep 17 00:00:00 2001 From: danielbachhuber Date: Mon, 29 Oct 2018 00:25:38 +0000 Subject: [PATCH] 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. Props imath, mattheu. Fixes #45194. Built from https://develop.svn.wordpress.org/branches/5.0@43833 git-svn-id: http://core.svn.wordpress.org/branches/5.0@43662 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-admin/edit-form-blocks.php | 13 +++++++++++++ wp-includes/rest-api.php | 29 ++++++++++++++++++++++++----- wp-includes/version.php | 2 +- 3 files changed, 38 insertions(+), 6 deletions(-) diff --git a/wp-admin/edit-form-blocks.php b/wp-admin/edit-form-blocks.php index d998a11dad..b3deab00d9 100644 --- a/wp-admin/edit-form-blocks.php +++ b/wp-admin/edit-form-blocks.php @@ -49,8 +49,21 @@ $preload_paths = array( sprintf( '/wp/v2/%s/%s?context=edit', $rest_base, $post->ID ), sprintf( '/wp/v2/types/%s?context=edit', $post_type ), sprintf( '/wp/v2/users/me?post_type=%s&context=edit', $post_type ), + array( '/wp/v2/media', 'OPTIONS' ), ); +/** + * Preload common data by specifying an array of REST API paths that will be preloaded. + * + * Filters the array of paths that will be preloaded. + * + * @since 5.0.0 + * + * @param array $preload_paths Array of paths to preload. + * @param object $post The post resource data. + */ +$preload_paths = apply_filters( 'block_editor_preload_paths', $preload_paths, $post ); + /* * Ensure the global $post remains the same after API data is preloaded. * Because API preloading can call the_content and other filters, plugins diff --git a/wp-includes/rest-api.php b/wp-includes/rest-api.php index e1d8919c8d..f1892c10c4 100644 --- a/wp-includes/rest-api.php +++ b/wp-includes/rest-api.php @@ -1343,12 +1343,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 ); @@ -1367,10 +1377,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; diff --git a/wp-includes/version.php b/wp-includes/version.php index c00a71bbdf..954e91aceb 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -4,7 +4,7 @@ * * @global string $wp_version */ -$wp_version = '5.0-beta1-43832'; +$wp_version = '5.0-beta1-43833'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.