-
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
Core Data: Resolve entity collection user permissions #64504
Changes from all commits
c03bb3b
6e09661
22c9597
6931c7e
e898dbe
55e08c7
357dfaf
ad0ea0a
2d421da
b8cf431
24cd272
76cf2bd
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
https://github.com/WordPress/wordpress-develop/pull/7139 | ||
|
||
* https://github.com/WordPress/gutenberg/pull/64504 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
<?php | ||
/** | ||
* A custom REST server for Gutenberg. | ||
* | ||
* @package gutenberg | ||
* @since 6.7.0 | ||
*/ | ||
|
||
class Gutenberg_REST_Server extends WP_REST_Server { | ||
/** | ||
* Converts a response to data to send. | ||
* | ||
* @since 4.4.0 | ||
* @since 5.4.0 The `$embed` parameter can now contain a list of link relations to include. | ||
* | ||
* @param WP_REST_Response $response Response object. | ||
* @param bool|string[] $embed Whether to embed all links, a filtered list of link relations, or no links. | ||
* @return array { | ||
* Data with sub-requests embedded. | ||
* | ||
* @type array $_links Links. | ||
* @type array $_embedded Embedded objects. | ||
* } | ||
*/ | ||
// @core-merge: Do not merge. The method is copied here to fix the inheritance issue. | ||
public function response_to_data( $response, $embed ) { | ||
$data = $response->get_data(); | ||
$links = static::get_compact_response_links( $response ); | ||
|
||
if ( ! empty( $links ) ) { | ||
// Convert links to part of the data. | ||
$data['_links'] = $links; | ||
} | ||
|
||
if ( $embed ) { | ||
$this->embed_cache = array(); | ||
// Determine if this is a numeric array. | ||
if ( wp_is_numeric_array( $data ) ) { | ||
foreach ( $data as $key => $item ) { | ||
$data[ $key ] = $this->embed_links( $item, $embed ); | ||
} | ||
} else { | ||
$data = $this->embed_links( $data, $embed ); | ||
} | ||
$this->embed_cache = array(); | ||
} | ||
|
||
return $data; | ||
} | ||
|
||
/** | ||
* Retrieves links from a response. | ||
* | ||
* Extracts the links from a response into a structured hash, suitable for | ||
* direct output. | ||
* | ||
* @since 4.4.0 | ||
* @since 6.7.0 The `targetHints` property to the `self` link object was added. | ||
* | ||
* @param WP_REST_Response $response Response to extract links from. | ||
* @return array Map of link relation to list of link hashes. | ||
*/ | ||
public static function get_response_links( $response ) { | ||
$links = $response->get_links(); | ||
|
||
if ( empty( $links ) ) { | ||
return array(); | ||
} | ||
|
||
$server = rest_get_server(); | ||
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. If this method is not 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 think we need an instantiated server here (which I copied this from WordPress/wordpress-develop#7139, so I might be bit wrong about reasoning 😅 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. Right, this is not a new method, but an overriede of an existing Core method which is |
||
|
||
// Convert links to part of the data. | ||
$data = array(); | ||
foreach ( $links as $rel => $items ) { | ||
$data[ $rel ] = array(); | ||
|
||
foreach ( $items as $item ) { | ||
$attributes = $item['attributes']; | ||
$attributes['href'] = $item['href']; | ||
|
||
if ( 'self' !== $rel ) { | ||
$data[ $rel ][] = $attributes; | ||
continue; | ||
} | ||
|
||
// Prefer targetHints that were specifically designated by the developer. | ||
if ( isset( $attributes['targetHints']['allow'] ) ) { | ||
$data[ $rel ][] = $attributes; | ||
continue; | ||
} | ||
|
||
$request = WP_REST_Request::from_url( $item['href'] ); | ||
if ( ! $request ) { | ||
$data[ $rel ][] = $attributes; | ||
continue; | ||
} | ||
|
||
$match = $server->match_request_to_handler( $request ); | ||
if ( ! is_wp_error( $match ) ) { | ||
$response = new WP_REST_Response(); | ||
$response->set_matched_route( $match[0] ); | ||
$response->set_matched_handler( $match[1] ); | ||
$headers = rest_send_allow_header( $response, $server, $request )->get_headers(); | ||
|
||
foreach ( $headers as $name => $value ) { | ||
$name = WP_REST_Request::canonicalize_header_name( $name ); | ||
$attributes['targetHints'][ $name ] = array_map( 'trim', explode( ',', $value ) ); | ||
} | ||
} | ||
|
||
$data[ $rel ][] = $attributes; | ||
} | ||
} | ||
|
||
return $data; | ||
} | ||
|
||
/** | ||
* Retrieves the CURIEs (compact URIs) used for relations. | ||
* | ||
* Extracts the links from a response into a structured hash, suitable for | ||
* direct output. | ||
* | ||
* @since 4.5.0 | ||
* | ||
* @param WP_REST_Response $response Response to extract links from. | ||
* @return array Map of link relation to list of link hashes. | ||
*/ | ||
// @core-merge: Do not merge. The method is copied here to fix the inheritance issue. | ||
public static function get_compact_response_links( $response ) { | ||
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. Is there a good reason to keep these methods static? 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 think they've been static since REST API got merged in the core. They need to remain static; otherwise, we'll break backward compatibility. P.S. I had to copy two extra methods because of how the inheritance of 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. Yep, exactly that. |
||
$links = static::get_response_links( $response ); | ||
|
||
if ( empty( $links ) ) { | ||
return array(); | ||
} | ||
|
||
$curies = $response->get_curies(); | ||
$used_curies = array(); | ||
|
||
foreach ( $links as $rel => $items ) { | ||
|
||
// Convert $rel URIs to their compact versions if they exist. | ||
foreach ( $curies as $curie ) { | ||
$href_prefix = substr( $curie['href'], 0, strpos( $curie['href'], '{rel}' ) ); | ||
if ( ! str_starts_with( $rel, $href_prefix ) ) { | ||
continue; | ||
} | ||
|
||
// Relation now changes from '$uri' to '$curie:$relation'. | ||
$rel_regex = str_replace( '\{rel\}', '(.+)', preg_quote( $curie['href'], '!' ) ); | ||
preg_match( '!' . $rel_regex . '!', $rel, $matches ); | ||
if ( $matches ) { | ||
$new_rel = $curie['name'] . ':' . $matches[1]; | ||
$used_curies[ $curie['name'] ] = $curie; | ||
$links[ $new_rel ] = $items; | ||
unset( $links[ $rel ] ); | ||
break; | ||
} | ||
} | ||
} | ||
|
||
// Push the curies onto the start of the links array. | ||
if ( $used_curies ) { | ||
$links['curies'] = array_values( $used_curies ); | ||
} | ||
|
||
return $links; | ||
} | ||
} |
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.
Any tests for this? When this should get into core, there need to be tests.
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.
100%. I would like to do that, though I'm unsure what the best approach is. Maybe @TimothyBJacobs has some suggestions.
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 pushed a commit with some tests.