-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add media upload capability check #9301
Changes from all commits
f1d9073
592137d
36d7d58
4d234ed
27fa4fc
de838cc
790a643
1131d6a
4b4b4d6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -762,28 +762,28 @@ function gutenberg_register_scripts_and_styles() { | |
* data to be attached to the page. Expected to be called in the context of | ||
* `array_reduce`. | ||
* | ||
* @param array $memo Reduce accumulator. | ||
* @param string $path REST API path to preload. | ||
* @return array Modified reduce accumulator. | ||
* @param array $memo Reduce accumulator. | ||
* @param array $args REST API path and method to preload. | ||
* @return array Modified reduce accumulator. | ||
*/ | ||
function gutenberg_preload_api_request( $memo, $path ) { | ||
function gutenberg_preload_api_request( $memo, $args ) { | ||
|
||
// 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 ) ) { | ||
if ( empty( $args ) || empty( $args['path'] ) || empty( $args['method'] ) ) { | ||
return $memo; | ||
} | ||
|
||
$path_parts = parse_url( $path ); | ||
$path_parts = parse_url( $args['path'] ); | ||
if ( false === $path_parts ) { | ||
return $memo; | ||
} | ||
|
||
$request = new WP_REST_Request( 'GET', $path_parts['path'] ); | ||
$request = new WP_REST_Request( $args['method'], $path_parts['path'] ); | ||
if ( ! empty( $path_parts['query'] ) ) { | ||
parse_str( $path_parts['query'], $query_params ); | ||
$request->set_query_params( $query_params ); | ||
|
@@ -802,7 +802,11 @@ function gutenberg_preload_api_request( $memo, $path ) { | |
$data['_links'] = $links; | ||
} | ||
|
||
$memo[ $path ] = array( | ||
if ( 'OPTIONS' === $args['method'] ) { | ||
$response = rest_send_allow_header( $response, $server, $request ); | ||
} | ||
|
||
$memo[ $args['method'] ][ $args['path'] ] = array( | ||
'body' => $data, | ||
'headers' => $response->headers, | ||
); | ||
|
@@ -1278,12 +1282,34 @@ function gutenberg_editor_scripts_and_styles( $hook ) { | |
|
||
// Preload common data. | ||
$preload_paths = array( | ||
'/', | ||
'/wp/v2/types?context=edit', | ||
'/wp/v2/taxonomies?per_page=-1&context=edit', | ||
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( | ||
'path' => '/', | ||
'method' => 'GET', | ||
), | ||
array( | ||
'path' => '/wp/v2/types?context=edit', | ||
'method' => 'GET', | ||
), | ||
array( | ||
'path' => '/wp/v2/taxonomies?per_page=-1&context=edit', | ||
'method' => 'GET', | ||
), | ||
array( | ||
'path' => sprintf( '/wp/v2/%s/%s?context=edit', $rest_base, $post->ID ), | ||
'method' => 'GET', | ||
), | ||
array( | ||
'path' => sprintf( '/wp/v2/types/%s?context=edit', $post_type ), | ||
'method' => 'GET', | ||
), | ||
array( | ||
'path' => sprintf( '/wp/v2/users/me?post_type=%s&context=edit', $post_type ), | ||
'method' => 'GET', | ||
), | ||
array( | ||
'path' => '/wp/v2/media', | ||
'method' => 'OPTIONS', | ||
), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Related #10268 If we change the function signature, there may be some compatibility concerns. |
||
); | ||
|
||
// Ensure the global $post remains the same after | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,8 +32,9 @@ const createPreloadingMiddleware = ( preloadedData ) => ( options, next ) => { | |
const method = options.method || 'GET'; | ||
const path = getStablePath( options.path ); | ||
|
||
if ( 'GET' === method && preloadedData[ path ] ) { | ||
return Promise.resolve( preloadedData[ path ].body ); | ||
if ( preloadedData[ method ] && preloadedData[ method ][ path ] ) { | ||
const response = 'OPTIONS' === method ? preloadedData[ method ][ path ] : preloadedData[ method ][ path ].body; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Coment from old PR
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can see that this would be quite a big refactor, so maybe worth splitting into a separate PR? I will add the tests though. |
||
return Promise.resolve( response ); | ||
} | ||
} | ||
|
||
|
This file was deleted.
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Comment from old PR
https://github.com/WordPress/gutenberg/pull/4155/files#r201324762
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not a fan of the idea of mixing types in the array. I feel like at least this way it's obvious what's happening.
I would maybe say that this could throw an error if the args are not provided, at the moment it just skips preloading the request when really it is an obvious user error if they're not provided.
Also not sure if we'd consider splitting some code out of client-assets, we might be able to make this a bit more expressive.