forked from WordPress/gutenberg
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Core Data: Resolve entity collection user permissions (WordPress#64504)
Co-authored-by: Mamaduka <mamaduka@git.wordpress.org> Co-authored-by: TimothyBJacobs <timothyblynjacobs@git.wordpress.org> Co-authored-by: swissspidy <swissspidy@git.wordpress.org> Co-authored-by: youknowriad <youknowriad@git.wordpress.org> Co-authored-by: tyxla <tyxla@git.wordpress.org> Co-authored-by: spacedmonkey <spacedmonkey@git.wordpress.org>
- Loading branch information
Showing
9 changed files
with
322 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
169 changes: 169 additions & 0 deletions
169
lib/compat/wordpress-6.7/class-gutenberg-rest-server.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
|
||
// 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 ) { | ||
$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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.