<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[56082] trunk: REST API: add revisions endpoint for global styles.</title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { white-space: pre-line; overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/56082">56082</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/56082","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>isabel_brison</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2023-06-28 03:33:23 +0000 (Wed, 28 Jun 2023)</dd>
</dl>
<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>REST API: add revisions endpoint for global styles.
Adds an endpoint that returns revisions to the global styles custom post.
Props ramonopoly, peterwilsoncc, spacedmonkey, mukesh27, timothyblynjacobs.
Fixes <a href="https://core.trac.wordpress.org/ticket/58524">#58524</a>.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkpackagelockjson">trunk/package-lock.json</a></li>
<li><a href="#trunksrcwpincludesrestapiendpointsclasswprestglobalstylescontrollerphp">trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-global-styles-controller.php</a></li>
<li><a href="#trunksrcwpincludesrestapiphp">trunk/src/wp-includes/rest-api.php</a></li>
<li><a href="#trunksrcwpsettingsphp">trunk/src/wp-settings.php</a></li>
<li><a href="#trunktestsphpunittestsrestapirestschemasetupphp">trunk/tests/phpunit/tests/rest-api/rest-schema-setup.php</a></li>
<li><a href="#trunktestsqunitfixtureswpapigeneratedjs">trunk/tests/qunit/fixtures/wp-api-generated.js</a></li>
</ul>
<h3>Added Paths</h3>
<ul>
<li><a href="#trunksrcwpincludesrestapiendpointsclasswprestglobalstylesrevisionscontrollerphp">trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-global-styles-revisions-controller.php</a></li>
<li><a href="#trunktestsphpunittestsrestapirestglobalstylesrevisionscontrollerphp">trunk/tests/phpunit/tests/rest-api/rest-global-styles-revisions-controller.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkpackagelockjson"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/package-lock.json</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/package-lock.json 2023-06-27 23:31:01 UTC (rev 56081)
+++ trunk/package-lock.json 2023-06-28 03:33:23 UTC (rev 56082)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -12728,7 +12728,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> "from2-string": {
</span><span class="cx" style="display: block; padding: 0 10px"> "version": "1.1.0",
</span><span class="cx" style="display: block; padding: 0 10px"> "resolved": "https://registry.npmjs.org/from2-string/-/from2-string-1.1.0.tgz",
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- "integrity": "sha1-GCgrJ9CKJnyzAwzSuLSw8hKvdSo=",
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ "integrity": "sha512-m8vCh+KnXXXBtfF2VUbiYlQ+nczLcntB0BrtNgpmLkHylhObe9WF1b2LZjBBzrZzA6P4mkEla6ZYQoOUTG8cYA==",
</ins><span class="cx" style="display: block; padding: 0 10px"> "requires": {
</span><span class="cx" style="display: block; padding: 0 10px"> "from2": "^2.0.3"
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -21799,7 +21799,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> "promise-polyfill": {
</span><span class="cx" style="display: block; padding: 0 10px"> "version": "1.1.6",
</span><span class="cx" style="display: block; padding: 0 10px"> "resolved": "https://registry.npmjs.org/promise-polyfill/-/promise-polyfill-1.1.6.tgz",
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- "integrity": "sha1-zQTv9G9clcOn0EVZHXm14+AfEtc="
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ "integrity": "sha512-7rrONfyLkDEc7OJ5QBkqa4KI4EBhCd340xRuIUPGCfu13znS+vx+VDdrT9ODAJHlXm7w4lbxN3DRjyv58EuzDg=="
</ins><span class="cx" style="display: block; padding: 0 10px"> },
</span><span class="cx" style="display: block; padding: 0 10px"> "prompts": {
</span><span class="cx" style="display: block; padding: 0 10px"> "version": "2.4.2",
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -23986,7 +23986,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> "stream-from-promise": {
</span><span class="cx" style="display: block; padding: 0 10px"> "version": "1.0.0",
</span><span class="cx" style="display: block; padding: 0 10px"> "resolved": "https://registry.npmjs.org/stream-from-promise/-/stream-from-promise-1.0.0.tgz",
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- "integrity": "sha1-djaH9913fkyJT2QIMz/Gs/yKYbs="
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ "integrity": "sha512-j84KLkudt+gr8KJ21RB02btPLx61uGbrLnewsWz6QKmsz8/c4ZFqXw6mJh5+G4oRN7DgDxdbjPxnpySpg1mUig=="
</ins><span class="cx" style="display: block; padding: 0 10px"> },
</span><span class="cx" style="display: block; padding: 0 10px"> "stream-to-string": {
</span><span class="cx" style="display: block; padding: 0 10px"> "version": "1.2.0",
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -25374,7 +25374,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> "toposort": {
</span><span class="cx" style="display: block; padding: 0 10px"> "version": "2.0.2",
</span><span class="cx" style="display: block; padding: 0 10px"> "resolved": "https://registry.npmjs.org/toposort/-/toposort-2.0.2.tgz",
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- "integrity": "sha1-riF2gXXRVZ1IvvNUILL0li8JwzA="
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ "integrity": "sha512-0a5EOkAUp8D4moMi2W8ZF8jcga7BgZd91O/yabJCFY8az+XSzeGyTKs0Aoo897iV1Nj6guFq8orWDS96z91oGg=="
</ins><span class="cx" style="display: block; padding: 0 10px"> },
</span><span class="cx" style="display: block; padding: 0 10px"> "totalist": {
</span><span class="cx" style="display: block; padding: 0 10px"> "version": "1.1.0",
</span></span></pre></div>
<a id="trunksrcwpincludesrestapiendpointsclasswprestglobalstylescontrollerphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-global-styles-controller.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-global-styles-controller.php 2023-06-27 23:31:01 UTC (rev 56081)
+++ trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-global-styles-controller.php 2023-06-28 03:33:23 UTC (rev 56082)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -420,6 +420,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * Prepares links for the request.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 5.9.0
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 6.3.0 Adds revisions count and rest URL href to version-history.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @param integer $id ID.
</span><span class="cx" style="display: block; padding: 0 10px"> * @return array Links for the given post.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -433,6 +434,16 @@
</span><span class="cx" style="display: block; padding: 0 10px"> ),
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( post_type_supports( $this->post_type, 'revisions' ) ) {
+ $revisions = wp_get_latest_revision_id_and_total_count( $id );
+ $revisions_count = ! is_wp_error( $revisions ) ? $revisions['count'] : 0;
+ $revisions_base = sprintf( '/%s/%d/revisions', $base, $id );
+ $links['version-history'] = array(
+ 'href' => rest_url( $revisions_base ),
+ 'count' => $revisions_count,
+ );
+ }
+
</ins><span class="cx" style="display: block; padding: 0 10px"> return $links;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span></span></pre></div>
<a id="trunksrcwpincludesrestapiendpointsclasswprestglobalstylesrevisionscontrollerphp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-global-styles-revisions-controller.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-global-styles-revisions-controller.php (rev 0)
+++ trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-global-styles-revisions-controller.php 2023-06-28 03:33:23 UTC (rev 56082)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,474 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+/**
+ * REST API: WP_REST_Global_Styles_Revisions_Controller class
+ *
+ * @package WordPress
+ * @subpackage REST_API
+ * @since 6.3.0
+ */
+
+/**
+ * Core class used to access global styles revisions via the REST API.
+ *
+ * @since 6.3.0
+ *
+ * @see WP_REST_Controller
+ */
+class WP_REST_Global_Styles_Revisions_Controller extends WP_REST_Controller {
+ /**
+ * Parent post type.
+ *
+ * @since 6.3.0
+ * @var string
+ */
+ private $parent_post_type;
+
+ /**
+ * The base of the parent controller's route.
+ *
+ * @since 6.3.0
+ * @var string
+ */
+ private $parent_base;
+
+ /**
+ * Constructor.
+ *
+ * @since 6.3.0
+ */
+ public function __construct() {
+ $this->parent_post_type = 'wp_global_styles';
+ $this->rest_base = 'revisions';
+ $this->parent_base = 'global-styles';
+ $this->namespace = 'wp/v2';
+ }
+
+ /**
+ * Registers the controller's routes.
+ *
+ * @since 6.3.0
+ */
+ public function register_routes() {
+ register_rest_route(
+ $this->namespace,
+ '/' . $this->parent_base . '/(?P<parent>[\d]+)/' . $this->rest_base,
+ array(
+ 'args' => array(
+ 'parent' => array(
+ 'description' => __( 'The ID for the parent of the revision.' ),
+ 'type' => 'integer',
+ ),
+ ),
+ array(
+ 'methods' => WP_REST_Server::READABLE,
+ 'callback' => array( $this, 'get_items' ),
+ 'permission_callback' => array( $this, 'get_item_permissions_check' ),
+ 'args' => $this->get_collection_params(),
+ ),
+ 'schema' => array( $this, 'get_public_item_schema' ),
+ )
+ );
+ }
+
+ /**
+ * Retrieves the query params for collections.
+ *
+ * Inherits from WP_REST_Controller::get_collection_params(),
+ * also reflects changes to return value WP_REST_Revisions_Controller::get_collection_params().
+ *
+ * @since 6.3.0
+ *
+ * @return array Collection parameters.
+ */
+ public function get_collection_params() {
+ $collection_params = parent::get_collection_params();
+ $collection_params['context']['default'] = 'view';
+ $collection_params['offset'] = array(
+ 'description' => __( 'Offset the result set by a specific number of items.' ),
+ 'type' => 'integer',
+ );
+ unset( $collection_params['search'] );
+ unset( $collection_params['per_page']['default'] );
+
+ return $collection_params;
+ }
+
+ /**
+ * Returns decoded JSON from post content string,
+ * or a 404 if not found.
+ *
+ * @since 6.3.0
+ *
+ * @param string $raw_json Encoded JSON from global styles custom post content.
+ * @return Array|WP_Error
+ */
+ private function get_decoded_global_styles_json( $raw_json ) {
+ $decoded_json = json_decode( $raw_json, true );
+
+ if ( is_array( $decoded_json ) && isset( $decoded_json['isGlobalStylesUserThemeJSON'] ) && true === $decoded_json['isGlobalStylesUserThemeJSON'] ) {
+ return $decoded_json;
+ }
+
+ return new WP_Error(
+ 'rest_global_styles_not_found',
+ __( 'Cannot find user global styles revisions.' ),
+ array( 'status' => 404 )
+ );
+ }
+
+ /**
+ * Returns paginated revisions of the given global styles config custom post type.
+ *
+ * The bulk of the body is taken from WP_REST_Revisions_Controller->get_items,
+ * but global styles does not require as many parameters.
+ *
+ * @since 6.3.0
+ *
+ * @param WP_REST_Request $request The request instance.
+ * @return WP_REST_Response|WP_Error
+ */
+ public function get_items( $request ) {
+ $parent = $this->get_parent( $request['parent'] );
+
+ if ( is_wp_error( $parent ) ) {
+ return $parent;
+ }
+
+ $global_styles_config = $this->get_decoded_global_styles_json( $parent->post_content );
+
+ if ( is_wp_error( $global_styles_config ) ) {
+ return $global_styles_config;
+ }
+
+ if ( wp_revisions_enabled( $parent ) ) {
+ $registered = $this->get_collection_params();
+ $query_args = array(
+ 'post_parent' => $parent->ID,
+ 'post_type' => 'revision',
+ 'post_status' => 'inherit',
+ 'posts_per_page' => -1,
+ 'orderby' => 'date ID',
+ 'order' => 'DESC',
+ );
+
+ $parameter_mappings = array(
+ 'offset' => 'offset',
+ 'page' => 'paged',
+ 'per_page' => 'posts_per_page',
+ );
+
+ foreach ( $parameter_mappings as $api_param => $wp_param ) {
+ if ( isset( $registered[ $api_param ], $request[ $api_param ] ) ) {
+ $query_args[ $wp_param ] = $request[ $api_param ];
+ }
+ }
+
+ $revisions_query = new WP_Query();
+ $revisions = $revisions_query->query( $query_args );
+ $offset = isset( $query_args['offset'] ) ? (int) $query_args['offset'] : 0;
+ $page = (int) $query_args['paged'];
+ $total_revisions = $revisions_query->found_posts;
+
+ if ( $total_revisions < 1 ) {
+ // Out-of-bounds, run the query again without LIMIT for total count.
+ unset( $query_args['paged'], $query_args['offset'] );
+ $count_query = new WP_Query();
+ $count_query->query( $query_args );
+
+ $total_revisions = $count_query->found_posts;
+ }
+
+ if ( $revisions_query->query_vars['posts_per_page'] > 0 ) {
+ $max_pages = ceil( $total_revisions / (int) $revisions_query->query_vars['posts_per_page'] );
+ } else {
+ $max_pages = $total_revisions > 0 ? 1 : 0;
+ }
+ if ( $total_revisions > 0 ) {
+ if ( $offset >= $total_revisions ) {
+ return new WP_Error(
+ 'rest_revision_invalid_offset_number',
+ __( 'The offset number requested is larger than or equal to the number of available revisions.' ),
+ array( 'status' => 400 )
+ );
+ } elseif ( ! $offset && $page > $max_pages ) {
+ return new WP_Error(
+ 'rest_revision_invalid_page_number',
+ __( 'The page number requested is larger than the number of pages available.' ),
+ array( 'status' => 400 )
+ );
+ }
+ }
+ } else {
+ $revisions = array();
+ $total_revisions = 0;
+ $max_pages = 0;
+ $page = (int) $request['page'];
+ }
+
+ $response = array();
+
+ foreach ( $revisions as $revision ) {
+ $data = $this->prepare_item_for_response( $revision, $request );
+ $response[] = $this->prepare_response_for_collection( $data );
+ }
+
+ $response = rest_ensure_response( $response );
+
+ $response->header( 'X-WP-Total', (int) $total_revisions );
+ $response->header( 'X-WP-TotalPages', (int) $max_pages );
+
+ $request_params = $request->get_query_params();
+ $base_path = rest_url( sprintf( '%s/%s/%d/%s', $this->namespace, $this->parent_base, $request['parent'], $this->rest_base ) );
+ $base = add_query_arg( urlencode_deep( $request_params ), $base_path );
+
+ if ( $page > 1 ) {
+ $prev_page = $page - 1;
+
+ if ( $prev_page > $max_pages ) {
+ $prev_page = $max_pages;
+ }
+
+ $prev_link = add_query_arg( 'page', $prev_page, $base );
+ $response->link_header( 'prev', $prev_link );
+ }
+ if ( $max_pages > $page ) {
+ $next_page = $page + 1;
+ $next_link = add_query_arg( 'page', $next_page, $base );
+
+ $response->link_header( 'next', $next_link );
+ }
+
+ return $response;
+ }
+
+ /**
+ * Checks the post_date_gmt or modified_gmt and prepare any post or
+ * modified date for single post output.
+ *
+ * Duplicate of WP_REST_Revisions_Controller::prepare_date_response
+ *
+ * @since 6.3.0
+ *
+ * @param string $date_gmt GMT publication time.
+ * @param string|null $date Optional. Local publication time. Default null.
+ * @return string|null ISO8601/RFC3339 formatted datetime, otherwise null.
+ */
+ protected function prepare_date_response( $date_gmt, $date = null ) {
+ if ( '0000-00-00 00:00:00' === $date_gmt ) {
+ return null;
+ }
+
+ if ( isset( $date ) ) {
+ return mysql_to_rfc3339( $date );
+ }
+
+ return mysql_to_rfc3339( $date_gmt );
+ }
+
+ /**
+ * Prepares the revision for the REST response.
+ *
+ * @since 6.3.0
+ *
+ * @param WP_Post $post Post revision object.
+ * @param WP_REST_Request $request Request object.
+ * @return WP_REST_Response|WP_Error Response object.
+ */
+ public function prepare_item_for_response( $post, $request ) {
+ $parent = $this->get_parent( $request['parent'] );
+ $global_styles_config = $this->get_decoded_global_styles_json( $post->post_content );
+
+ if ( is_wp_error( $global_styles_config ) ) {
+ return $global_styles_config;
+ }
+
+ $fields = $this->get_fields_for_response( $request );
+ $data = array();
+
+ if ( ! empty( $global_styles_config['styles'] ) || ! empty( $global_styles_config['settings'] ) ) {
+ $global_styles_config = ( new WP_Theme_JSON( $global_styles_config, 'custom' ) )->get_raw_data();
+ if ( rest_is_field_included( 'settings', $fields ) ) {
+ $data['settings'] = ! empty( $global_styles_config['settings'] ) ? $global_styles_config['settings'] : new stdClass();
+ }
+ if ( rest_is_field_included( 'styles', $fields ) ) {
+ $data['styles'] = ! empty( $global_styles_config['styles'] ) ? $global_styles_config['styles'] : new stdClass();
+ }
+ }
+
+ if ( rest_is_field_included( 'author', $fields ) ) {
+ $data['author'] = (int) $post->post_author;
+ }
+
+ if ( rest_is_field_included( 'date', $fields ) ) {
+ $data['date'] = $this->prepare_date_response( $post->post_date_gmt, $post->post_date );
+ }
+
+ if ( rest_is_field_included( 'date_gmt', $fields ) ) {
+ $data['date_gmt'] = $this->prepare_date_response( $post->post_date_gmt );
+ }
+
+ if ( rest_is_field_included( 'id', $fields ) ) {
+ $data['id'] = (int) $post->ID;
+ }
+
+ if ( rest_is_field_included( 'modified', $fields ) ) {
+ $data['modified'] = $this->prepare_date_response( $post->post_modified_gmt, $post->post_modified );
+ }
+
+ if ( rest_is_field_included( 'modified_gmt', $fields ) ) {
+ $data['modified_gmt'] = $this->prepare_date_response( $post->post_modified_gmt );
+ }
+
+ if ( rest_is_field_included( 'parent', $fields ) ) {
+ $data['parent'] = (int) $parent->ID;
+ }
+
+ $context = ! empty( $request['context'] ) ? $request['context'] : 'view';
+ $data = $this->add_additional_fields_to_object( $data, $request );
+ $data = $this->filter_response_by_context( $data, $context );
+
+ return rest_ensure_response( $data );
+ }
+
+ /**
+ * Retrieves the revision's schema, conforming to JSON Schema.
+ *
+ * @since 6.3.0
+ *
+ * @return array Item schema data.
+ */
+ public function get_item_schema() {
+ if ( $this->schema ) {
+ return $this->add_additional_fields_schema( $this->schema );
+ }
+
+ $schema = array(
+ '$schema' => 'http://json-schema.org/draft-04/schema#',
+ 'title' => "{$this->parent_post_type}-revision",
+ 'type' => 'object',
+ // Base properties for every revision.
+ 'properties' => array(
+
+ /*
+ * Adds settings and styles from the WP_REST_Revisions_Controller item fields.
+ * Leaves out GUID as global styles shouldn't be accessible via URL.
+ */
+ 'author' => array(
+ 'description' => __( 'The ID for the author of the revision.' ),
+ 'type' => 'integer',
+ 'context' => array( 'view', 'edit', 'embed' ),
+ ),
+ 'date' => array(
+ 'description' => __( "The date the revision was published, in the site's timezone." ),
+ 'type' => 'string',
+ 'format' => 'date-time',
+ 'context' => array( 'view', 'edit', 'embed' ),
+ ),
+ 'date_gmt' => array(
+ 'description' => __( 'The date the revision was published, as GMT.' ),
+ 'type' => 'string',
+ 'format' => 'date-time',
+ 'context' => array( 'view', 'edit' ),
+ ),
+ 'id' => array(
+ 'description' => __( 'Unique identifier for the revision.' ),
+ 'type' => 'integer',
+ 'context' => array( 'view', 'edit', 'embed' ),
+ ),
+ 'modified' => array(
+ 'description' => __( "The date the revision was last modified, in the site's timezone." ),
+ 'type' => 'string',
+ 'format' => 'date-time',
+ 'context' => array( 'view', 'edit' ),
+ ),
+ 'modified_gmt' => array(
+ 'description' => __( 'The date the revision was last modified, as GMT.' ),
+ 'type' => 'string',
+ 'format' => 'date-time',
+ 'context' => array( 'view', 'edit' ),
+ ),
+ 'parent' => array(
+ 'description' => __( 'The ID for the parent of the revision.' ),
+ 'type' => 'integer',
+ 'context' => array( 'view', 'edit', 'embed' ),
+ ),
+
+ // Adds settings and styles from the WP_REST_Global_Styles_Controller parent schema.
+ 'styles' => array(
+ 'description' => __( 'Global styles.' ),
+ 'type' => array( 'object' ),
+ 'context' => array( 'view', 'edit' ),
+ ),
+ 'settings' => array(
+ 'description' => __( 'Global settings.' ),
+ 'type' => array( 'object' ),
+ 'context' => array( 'view', 'edit' ),
+ ),
+ ),
+ );
+
+ $this->schema = $schema;
+
+ return $this->add_additional_fields_schema( $this->schema );
+ }
+
+ /**
+ * Checks if a given request has access to read a single global style.
+ *
+ * @since 6.3.0
+ *
+ * @param WP_REST_Request $request Full details about the request.
+ * @return true|WP_Error True if the request has read access, WP_Error object otherwise.
+ */
+ public function get_item_permissions_check( $request ) {
+ $post = $this->get_parent( $request['parent'] );
+ if ( is_wp_error( $post ) ) {
+ return $post;
+ }
+
+ /*
+ * The same check as WP_REST_Global_Styles_Controller::get_item_permissions_check.
+ */
+ if ( ! current_user_can( 'read_post', $post->ID ) ) {
+ return new WP_Error(
+ 'rest_cannot_view',
+ __( 'Sorry, you are not allowed to view revisions for this global style.' ),
+ array( 'status' => rest_authorization_required_code() )
+ );
+ }
+
+ return true;
+ }
+
+ /**
+ * Get the parent post, if the ID is valid.
+ *
+ * Duplicate of WP_REST_Revisions_Controller::get_parent
+ *
+ * @since 6.3.0
+ *
+ * @param int $parent_post_id Supplied ID.
+ * @return WP_Post|WP_Error Post object if ID is valid, WP_Error otherwise.
+ */
+ protected function get_parent( $parent_post_id ) {
+ $error = new WP_Error(
+ 'rest_post_invalid_parent',
+ __( 'Invalid post parent ID.' ),
+ array( 'status' => 404 )
+ );
+
+ if ( (int) $parent_post_id <= 0 ) {
+ return $error;
+ }
+
+ $parent_post = get_post( (int) $parent_post_id );
+
+ if ( empty( $parent_post ) || empty( $parent_post->ID )
+ || $this->parent_post_type !== $parent_post->post_type
+ ) {
+ return $error;
+ }
+
+ return $parent_post;
+ }
+}
</ins><span class="cx" style="display: block; padding: 0 10px">Property changes on: trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-global-styles-revisions-controller.php
</span><span class="cx" style="display: block; padding: 0 10px">___________________________________________________________________
</span></span></pre></div>
<a id="svneolstyle"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: svn:eol-style</h4></div>
<ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+native
</ins><span class="cx" style="display: block; padding: 0 10px">\ No newline at end of property
</span><a id="trunksrcwpincludesrestapiphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/rest-api.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/rest-api.php 2023-06-27 23:31:01 UTC (rev 56081)
+++ trunk/src/wp-includes/rest-api.php 2023-06-28 03:33:23 UTC (rev 56082)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -317,6 +317,10 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $controller = new WP_REST_Block_Types_Controller();
</span><span class="cx" style="display: block; padding: 0 10px"> $controller->register_routes();
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // Global Styles revisions.
+ $controller = new WP_REST_Global_Styles_Revisions_Controller();
+ $controller->register_routes();
+
</ins><span class="cx" style="display: block; padding: 0 10px"> // Global Styles.
</span><span class="cx" style="display: block; padding: 0 10px"> $controller = new WP_REST_Global_Styles_Controller();
</span><span class="cx" style="display: block; padding: 0 10px"> $controller->register_routes();
</span></span></pre></div>
<a id="trunksrcwpsettingsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-settings.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-settings.php 2023-06-27 23:31:01 UTC (rev 56081)
+++ trunk/src/wp-settings.php 2023-06-28 03:33:23 UTC (rev 56082)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -261,6 +261,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> require ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-posts-controller.php';
</span><span class="cx" style="display: block; padding: 0 10px"> require ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-attachments-controller.php';
</span><span class="cx" style="display: block; padding: 0 10px"> require ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-global-styles-controller.php';
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+require ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-global-styles-revisions-controller.php';
</ins><span class="cx" style="display: block; padding: 0 10px"> require ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-post-types-controller.php';
</span><span class="cx" style="display: block; padding: 0 10px"> require ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-post-statuses-controller.php';
</span><span class="cx" style="display: block; padding: 0 10px"> require ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-revisions-controller.php';
</span></span></pre></div>
<a id="trunktestsphpunittestsrestapirestglobalstylesrevisionscontrollerphp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: trunk/tests/phpunit/tests/rest-api/rest-global-styles-revisions-controller.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/rest-api/rest-global-styles-revisions-controller.php (rev 0)
+++ trunk/tests/phpunit/tests/rest-api/rest-global-styles-revisions-controller.php 2023-06-28 03:33:23 UTC (rev 56082)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,806 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+/**
+ * Unit tests covering WP_REST_Global_Styles_Revisions_Controller functionality.
+ *
+ * @package WordPress
+ * @subpackage REST API
+ *
+ * @covers WP_REST_Global_Styles_Revisions_Controller
+ *
+ * @group restapi-global-styles
+ * @group restapi
+ */
+class WP_REST_Global_Styles_Revisions_Controller_Test extends WP_Test_REST_Controller_Testcase {
+ /**
+ * @var int
+ */
+ protected static $admin_id;
+
+ /**
+ * @var int
+ */
+ protected static $second_admin_id;
+
+ /**
+ * @var int
+ */
+ protected static $author_id;
+
+ /**
+ * @var int
+ */
+ protected static $global_styles_id;
+
+ /**
+ * @var int
+ */
+ private $total_revisions;
+
+ /**
+ * @var array
+ */
+ private $revision_1;
+
+ /**
+ * @var int
+ */
+ private $revision_1_id;
+
+ /**
+ * @var array
+ */
+ private $revision_2;
+
+ /**
+ * @var int
+ */
+ private $revision_2_id;
+
+ /**
+ * @var array
+ */
+ private $revision_3;
+
+ /**
+ * @var int
+ */
+ private $revision_3_id;
+
+
+ /**
+ * Create fake data before our tests run.
+ *
+ * @param WP_UnitTest_Factory $factory Helper that lets us create fake data.
+ */
+ public static function wpSetupBeforeClass( $factory ) {
+ self::$admin_id = $factory->user->create(
+ array(
+ 'role' => 'administrator',
+ )
+ );
+ self::$second_admin_id = $factory->user->create(
+ array(
+ 'role' => 'administrator',
+ )
+ );
+ self::$author_id = $factory->user->create(
+ array(
+ 'role' => 'author',
+ )
+ );
+
+ wp_set_current_user( self::$admin_id );
+ // This creates the global styles for the current theme.
+ self::$global_styles_id = $factory->post->create(
+ array(
+ 'post_content' => '{"version": ' . WP_Theme_JSON::LATEST_SCHEMA . ', "isGlobalStylesUserThemeJSON": true }',
+ 'post_status' => 'publish',
+ 'post_title' => __( 'Custom Styles', 'default' ),
+ 'post_type' => 'wp_global_styles',
+ 'post_name' => 'wp-global-styles-tt1-blocks-revisions',
+ 'tax_input' => array(
+ 'wp_theme' => 'tt1-blocks',
+ ),
+ )
+ );
+
+ // Update post to create a new revisions.
+ $new_styles_post = array(
+ 'ID' => self::$global_styles_id,
+ 'post_content' => wp_json_encode(
+ array(
+ 'version' => WP_Theme_JSON::LATEST_SCHEMA,
+ 'isGlobalStylesUserThemeJSON' => true,
+ 'styles' => array(
+ 'color' => array(
+ 'background' => 'hotpink',
+ ),
+ ),
+ 'settings' => array(
+ 'color' => array(
+ 'palette' => array(
+ 'custom' => array(
+ array(
+ 'name' => 'Ghost',
+ 'slug' => 'ghost',
+ 'color' => 'ghost',
+ ),
+ ),
+ ),
+ ),
+ ),
+ )
+ ),
+ );
+
+ wp_update_post( $new_styles_post, true, false );
+
+ $new_styles_post = array(
+ 'ID' => self::$global_styles_id,
+ 'post_content' => wp_json_encode(
+ array(
+ 'version' => WP_Theme_JSON::LATEST_SCHEMA,
+ 'isGlobalStylesUserThemeJSON' => true,
+ 'styles' => array(
+ 'color' => array(
+ 'background' => 'lemonchiffon',
+ ),
+ ),
+ 'settings' => array(
+ 'color' => array(
+ 'palette' => array(
+ 'custom' => array(
+ array(
+ 'name' => 'Gwanda',
+ 'slug' => 'gwanda',
+ 'color' => 'gwanda',
+ ),
+ ),
+ ),
+ ),
+ ),
+ )
+ ),
+ );
+
+ wp_update_post( $new_styles_post, true, false );
+
+ $new_styles_post = array(
+ 'ID' => self::$global_styles_id,
+ 'post_content' => wp_json_encode(
+ array(
+ 'version' => WP_Theme_JSON::LATEST_SCHEMA,
+ 'isGlobalStylesUserThemeJSON' => true,
+ 'styles' => array(
+ 'color' => array(
+ 'background' => 'chocolate',
+ ),
+ ),
+ 'settings' => array(
+ 'color' => array(
+ 'palette' => array(
+ 'custom' => array(
+ array(
+ 'name' => 'Stacy',
+ 'slug' => 'stacy',
+ 'color' => 'stacy',
+ ),
+ ),
+ ),
+ ),
+ ),
+ )
+ ),
+ );
+
+ wp_update_post( $new_styles_post, true, false );
+ wp_set_current_user( 0 );
+ }
+
+ /**
+ * Removes users after our tests run.
+ */
+ public static function wpTearDownAfterClass() {
+ self::delete_user( self::$admin_id );
+ self::delete_user( self::$second_admin_id );
+ self::delete_user( self::$author_id );
+ }
+
+ /**
+ * Sets up before tests.
+ */
+ public function set_up() {
+ parent::set_up();
+ switch_theme( 'tt1-blocks' );
+ $revisions = wp_get_post_revisions( self::$global_styles_id );
+ $this->total_revisions = count( $revisions );
+
+ $this->revision_1 = array_pop( $revisions );
+ $this->revision_1_id = $this->revision_1->ID;
+
+ $this->revision_2 = array_pop( $revisions );
+ $this->revision_2_id = $this->revision_2->ID;
+
+ $this->revision_3 = array_pop( $revisions );
+ $this->revision_3_id = $this->revision_3->ID;
+ }
+
+
+ /**
+ * @covers WP_REST_Global_Styles_Controller::register_routes
+ *
+ * @ticket 58524
+ */
+ public function test_register_routes() {
+ $routes = rest_get_server()->get_routes();
+ $this->assertArrayHasKey(
+ '/wp/v2/global-styles/(?P<parent>[\d]+)/revisions',
+ $routes,
+ 'Global style revisions based on the given parentID route does not exist.'
+ );
+ }
+
+ /**
+ * @covers WP_REST_Global_Styles_Controller::get_items
+ *
+ * @ticket 58524
+ */
+ public function test_get_items_missing_parent() {
+ wp_set_current_user( self::$admin_id );
+ $request = new WP_REST_Request( 'GET', '/wp/v2/global-styles/' . REST_TESTS_IMPOSSIBLY_HIGH_NUMBER . '/revisions' );
+ $response = rest_get_server()->dispatch( $request );
+ $this->assertErrorResponse( 'rest_post_invalid_parent', $response, 404 );
+ }
+
+ /**
+ * Utility function to check the items in WP_REST_Global_Styles_Controller::get_items
+ * against the expected values.
+ *
+ * @ticket 58524
+ */
+ protected function check_get_revision_response( $response_revision_item, $revision_expected_item ) {
+ $this->assertSame( (int) $revision_expected_item->post_author, $response_revision_item['author'], 'Check that the revision item `author` exists.' );
+ $this->assertSame( mysql_to_rfc3339( $revision_expected_item->post_date ), $response_revision_item['date'], 'Check that the revision item `date` exists.' );
+ $this->assertSame( mysql_to_rfc3339( $revision_expected_item->post_date_gmt ), $response_revision_item['date_gmt'], 'Check that the revision item `date_gmt` exists.' );
+ $this->assertSame( mysql_to_rfc3339( $revision_expected_item->post_modified ), $response_revision_item['modified'], 'Check that the revision item `modified` exists.' );
+ $this->assertSame( mysql_to_rfc3339( $revision_expected_item->post_modified_gmt ), $response_revision_item['modified_gmt'], 'Check that the revision item `modified_gmt` exists.' );
+ $this->assertSame( $revision_expected_item->post_parent, $response_revision_item['parent'], 'Check that an id for the parent exists.' );
+
+ // Global styles.
+ $config = ( new WP_Theme_JSON( json_decode( $revision_expected_item->post_content, true ), 'custom' ) )->get_raw_data();
+ $this->assertEquals(
+ $config['settings'],
+ $response_revision_item['settings'],
+ 'Check that the revision settings exist in the response.'
+ );
+ $this->assertEquals(
+ $config['styles'],
+ $response_revision_item['styles'],
+ 'Check that the revision styles match the updated styles.'
+ );
+ }
+
+ /**
+ * @covers WP_REST_Global_Styles_Controller::get_items
+ *
+ * @ticket 58524
+ */
+ public function test_get_items() {
+ wp_set_current_user( self::$admin_id );
+
+ $request = new WP_REST_Request( 'GET', '/wp/v2/global-styles/' . self::$global_styles_id . '/revisions' );
+ $response = rest_get_server()->dispatch( $request );
+ $data = $response->get_data();
+
+ $this->assertSame( 200, $response->get_status(), 'Response status is 200.' );
+ $this->assertCount( $this->total_revisions, $data, 'Check that correct number of revisions exists.' );
+
+ // Reverse chronology.
+ $this->assertSame( $this->revision_3_id, $data[0]['id'] );
+ $this->check_get_revision_response( $data[0], $this->revision_3 );
+
+ $this->assertSame( $this->revision_2_id, $data[1]['id'] );
+ $this->check_get_revision_response( $data[1], $this->revision_2 );
+
+ $this->assertSame( $this->revision_1_id, $data[2]['id'] );
+ $this->check_get_revision_response( $data[2], $this->revision_1 );
+ }
+
+ /**
+ * @covers WP_REST_Global_Styles_Controller::get_items
+ *
+ * @ticket 58524
+ */
+ public function test_get_items_eligible_roles() {
+ wp_set_current_user( self::$second_admin_id );
+ $config = array(
+ 'version' => WP_Theme_JSON::LATEST_SCHEMA,
+ 'isGlobalStylesUserThemeJSON' => true,
+ 'styles' => array(
+ 'color' => array(
+ 'background' => 'whitesmoke',
+ ),
+ ),
+ 'settings' => array(),
+ );
+ $updated_styles_post = array(
+ 'ID' => self::$global_styles_id,
+ 'post_content' => wp_json_encode( $config ),
+ );
+
+ wp_update_post( $updated_styles_post, true, false );
+
+ $request = new WP_REST_Request( 'GET', '/wp/v2/global-styles/' . self::$global_styles_id . '/revisions' );
+ $response = rest_get_server()->dispatch( $request );
+ $data = $response->get_data();
+
+ $this->assertCount( $this->total_revisions + 1, $data, 'Check that extra revision exist' );
+ $this->assertEquals( self::$second_admin_id, $data[0]['author'], 'Check that second author id returns expected value.' );
+ }
+
+ /**
+ * @covers WP_REST_Global_Styles_Controller::get_items with context arg.
+ *
+ * @ticket 58524
+ */
+ public function test_get_item_embed_context() {
+ wp_set_current_user( self::$admin_id );
+
+ $request = new WP_REST_Request( 'GET', '/wp/v2/global-styles/' . self::$global_styles_id . '/revisions' );
+ $request->set_param( 'context', 'embed' );
+ $response = rest_get_server()->dispatch( $request );
+ $fields = array(
+ 'author',
+ 'date',
+ 'id',
+ 'parent',
+ );
+ $data = $response->get_data();
+ $this->assertSameSets( $fields, array_keys( $data[0] ) );
+ }
+
+ /**
+ * @covers WP_REST_Global_Styles_Controller::get_item_schema
+ *
+ * @ticket 58524
+ */
+ public function test_get_item_schema() {
+ $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/global-styles/' . self::$global_styles_id . '/revisions' );
+ $response = rest_get_server()->dispatch( $request );
+ $data = $response->get_data();
+ $properties = $data['schema']['properties'];
+
+ $this->assertCount( 9, $properties, 'Schema properties array has exactly 9 elements.' );
+ $this->assertArrayHasKey( 'id', $properties, 'Schema properties array has "id" key.' );
+ $this->assertArrayHasKey( 'styles', $properties, 'Schema properties array has "styles" key.' );
+ $this->assertArrayHasKey( 'settings', $properties, 'Schema properties array has "settings" key.' );
+ $this->assertArrayHasKey( 'parent', $properties, 'Schema properties array has "parent" key.' );
+ $this->assertArrayHasKey( 'author', $properties, 'Schema properties array has "author" key.' );
+ $this->assertArrayHasKey( 'date', $properties, 'Schema properties array has "date" key.' );
+ $this->assertArrayHasKey( 'date_gmt', $properties, 'Schema properties array has "date_gmt" key.' );
+ $this->assertArrayHasKey( 'modified', $properties, 'Schema properties array has "modified" key.' );
+ $this->assertArrayHasKey( 'modified_gmt', $properties, 'Schema properties array has "modified_gmt" key.' );
+ }
+
+ /**
+ * @covers WP_REST_Global_Styles_Controller::get_item_permissions_check
+ *
+ * @ticket 58524
+ */
+ public function test_get_item_permissions_check() {
+ wp_set_current_user( self::$author_id );
+ $request = new WP_REST_Request( 'GET', '/wp/v2/global-styles/' . self::$global_styles_id . '/revisions' );
+ $response = rest_get_server()->dispatch( $request );
+
+ $this->assertErrorResponse( 'rest_cannot_view', $response, 403 );
+ }
+
+ /**
+ * Tests the pagination header of the first page.
+ *
+ * Duplicate of WP_Test_REST_Revisions_Controller::test_get_items_pagination_header_of_the_first_page
+ *
+ * @ticket 58524
+ */
+ public function test_get_items_pagination_header_of_the_first_page() {
+ wp_set_current_user( self::$admin_id );
+
+ $rest_route = '/wp/v2/global-styles/' . self::$global_styles_id . '/revisions';
+ $per_page = 2;
+ $total_pages = (int) ceil( $this->total_revisions / $per_page );
+ $page = 1; // First page.
+
+ $request = new WP_REST_Request( 'GET', $rest_route );
+ $request->set_query_params(
+ array(
+ 'per_page' => $per_page,
+ 'page' => $page,
+ )
+ );
+ $response = rest_get_server()->dispatch( $request );
+ $headers = $response->get_headers();
+ $this->assertSame( $this->total_revisions, $headers['X-WP-Total'] );
+ $this->assertSame( $total_pages, $headers['X-WP-TotalPages'] );
+ $next_link = add_query_arg(
+ array(
+ 'per_page' => $per_page,
+ 'page' => $page + 1,
+ ),
+ rest_url( $rest_route )
+ );
+ $this->assertStringNotContainsString( 'rel="prev"', $headers['Link'] );
+ $this->assertStringContainsString( '<' . $next_link . '>; rel="next"', $headers['Link'] );
+ }
+
+ /**
+ * Tests the pagination header of the last page.
+ *
+ * Duplicate of WP_Test_REST_Revisions_Controller::test_get_items_pagination_header_of_the_last_page
+ *
+ * @ticket 58524
+ */
+ public function test_get_items_pagination_header_of_the_last_page() {
+ wp_set_current_user( self::$admin_id );
+
+ $rest_route = '/wp/v2/global-styles/' . self::$global_styles_id . '/revisions';
+ $per_page = 2;
+ $total_pages = (int) ceil( $this->total_revisions / $per_page );
+ $page = 2; // Last page.
+
+ $request = new WP_REST_Request( 'GET', $rest_route );
+ $request->set_query_params(
+ array(
+ 'per_page' => $per_page,
+ 'page' => $page,
+ )
+ );
+ $response = rest_get_server()->dispatch( $request );
+ $headers = $response->get_headers();
+ $this->assertSame( $this->total_revisions, $headers['X-WP-Total'] );
+ $this->assertSame( $total_pages, $headers['X-WP-TotalPages'] );
+ $prev_link = add_query_arg(
+ array(
+ 'per_page' => $per_page,
+ 'page' => $page - 1,
+ ),
+ rest_url( $rest_route )
+ );
+ $this->assertStringContainsString( '<' . $prev_link . '>; rel="prev"', $headers['Link'] );
+ }
+
+ /**
+ * Tests that invalid 'per_page' query should error.
+ *
+ * Duplicate of WP_Test_REST_Revisions_Controller::test_get_items_invalid_per_page_should_error
+ *
+ * @ticket 58524
+ */
+ public function test_get_items_invalid_per_page_should_error() {
+ wp_set_current_user( self::$admin_id );
+
+ $per_page = -1; // Invalid number.
+ $expected_error = 'rest_invalid_param';
+ $expected_status = 400;
+
+ $request = new WP_REST_Request( 'GET', '/wp/v2/global-styles/' . self::$global_styles_id . '/revisions' );
+ $request->set_param( 'per_page', $per_page );
+ $response = rest_get_server()->dispatch( $request );
+ $this->assertErrorResponse( $expected_error, $response, $expected_status );
+ }
+
+ /**
+ * Tests that out of bounds 'page' query should error.
+ *
+ * Duplicate of WP_Test_REST_Revisions_Controller::test_get_items_out_of_bounds_page_should_error
+ *
+ * @ticket 58524
+ */
+ public function test_get_items_out_of_bounds_page_should_error() {
+ wp_set_current_user( self::$admin_id );
+
+ $per_page = 2;
+ $total_pages = (int) ceil( $this->total_revisions / $per_page );
+ $page = $total_pages + 1; // Out of bound page.
+ $expected_error = 'rest_revision_invalid_page_number';
+ $expected_status = 400;
+
+ $request = new WP_REST_Request( 'GET', '/wp/v2/global-styles/' . self::$global_styles_id . '/revisions' );
+ $request->set_query_params(
+ array(
+ 'per_page' => $per_page,
+ 'page' => $page,
+ )
+ );
+ $response = rest_get_server()->dispatch( $request );
+ $this->assertErrorResponse( $expected_error, $response, $expected_status );
+ }
+
+ /**
+ * Tests that impossibly high 'page' query should error.
+ *
+ * Duplicate of WP_Test_REST_Revisions_Controller::test_get_items_invalid_max_pages_should_error
+ *
+ * @ticket 58524
+ */
+ public function test_get_items_invalid_max_pages_should_error() {
+ wp_set_current_user( self::$admin_id );
+
+ $per_page = 2;
+ $page = REST_TESTS_IMPOSSIBLY_HIGH_NUMBER; // Invalid number.
+ $expected_error = 'rest_revision_invalid_page_number';
+ $expected_status = 400;
+
+ $request = new WP_REST_Request( 'GET', '/wp/v2/global-styles/' . self::$global_styles_id . '/revisions' );
+ $request->set_query_params(
+ array(
+ 'per_page' => $per_page,
+ 'page' => $page,
+ )
+ );
+ $response = rest_get_server()->dispatch( $request );
+ $this->assertErrorResponse( $expected_error, $response, $expected_status );
+ }
+
+ /**
+ * Tests that the default query should fetch all revisions.
+ *
+ * Duplicate of WP_Test_REST_Revisions_Controller::test_get_items_default_query_should_fetch_all_revisons
+ *
+ * @ticket 58524
+ */
+ public function test_get_items_default_query_should_fetch_all_revisons() {
+ wp_set_current_user( self::$admin_id );
+
+ $expected_count = $this->total_revisions;
+
+ $request = new WP_REST_Request( 'GET', '/wp/v2/global-styles/' . self::$global_styles_id . '/revisions' );
+ $response = rest_get_server()->dispatch( $request );
+ $this->assertCount( $expected_count, $response->get_data() );
+ }
+
+
+ /**
+ * Tests that 'offset' query shouldn't work without 'per_page' (fallback -1).
+ *
+ * Duplicate of WP_Test_REST_Revisions_Controller::test_get_items_offset_should_not_work_without_per_page
+ *
+ * @ticket 58524
+ */
+ public function test_get_items_offset_should_not_work_without_per_page() {
+ wp_set_current_user( self::$admin_id );
+
+ $offset = 1;
+ $expected_count = $this->total_revisions;
+
+ $request = new WP_REST_Request( 'GET', '/wp/v2/global-styles/' . self::$global_styles_id . '/revisions' );
+ $request->set_param( 'offset', $offset );
+ $response = rest_get_server()->dispatch( $request );
+ $this->assertCount( $expected_count, $response->get_data() );
+ }
+
+ /**
+ * Tests that 'offset' query should work with 'per_page'.
+ *
+ * Duplicate of WP_Test_REST_Revisions_Controller::test_get_items_offset_should_work_with_per_page
+ *
+ * @ticket 58524
+ */
+ public function test_get_items_offset_should_work_with_per_page() {
+ wp_set_current_user( self::$admin_id );
+
+ $per_page = 2;
+ $offset = 1;
+ $expected_count = 2;
+
+ $request = new WP_REST_Request( 'GET', '/wp/v2/global-styles/' . self::$global_styles_id . '/revisions' );
+ $request->set_query_params(
+ array(
+ 'offset' => $offset,
+ 'per_page' => $per_page,
+ )
+ );
+ $response = rest_get_server()->dispatch( $request );
+ $this->assertCount( $expected_count, $response->get_data() );
+ }
+
+ /**
+ * Tests that 'offset' query should take priority over 'page'.
+ *
+ * Duplicate of WP_Test_REST_Revisions_Controller::test_get_items_offset_should_take_priority_over_page
+ *
+ * @ticket 58524
+ */
+ public function test_get_items_offset_should_take_priority_over_page() {
+ wp_set_current_user( self::$admin_id );
+
+ $per_page = 2;
+ $offset = 1;
+ $page = 1;
+ $expected_count = 2;
+
+ $request = new WP_REST_Request( 'GET', '/wp/v2/global-styles/' . self::$global_styles_id . '/revisions' );
+ $request->set_query_params(
+ array(
+ 'offset' => $offset,
+ 'per_page' => $per_page,
+ 'page' => $page,
+ )
+ );
+ $response = rest_get_server()->dispatch( $request );
+ $this->assertCount( $expected_count, $response->get_data() );
+ }
+
+ /**
+ * Tests that 'offset' query, as the total revisions count, should return empty data.
+ *
+ * Duplicate of WP_Test_REST_Revisions_Controller::test_get_items_total_revisions_offset_should_return_empty_data
+ *
+ * @ticket 58524
+ */
+ public function test_get_items_total_revisions_offset_should_return_empty_data() {
+ wp_set_current_user( self::$admin_id );
+
+ $per_page = 2;
+ $offset = $this->total_revisions;
+ $expected_error = 'rest_revision_invalid_offset_number';
+ $expected_status = 400;
+
+ $request = new WP_REST_Request( 'GET', '/wp/v2/global-styles/' . self::$global_styles_id . '/revisions' );
+ $request->set_query_params(
+ array(
+ 'offset' => $offset,
+ 'per_page' => $per_page,
+ )
+ );
+ $response = rest_get_server()->dispatch( $request );
+ $this->assertErrorResponse( $expected_error, $response, $expected_status );
+ }
+
+ /**
+ * Tests that out of bound 'offset' query should error.
+ *
+ * Duplicate of WP_Test_REST_Revisions_Controller::test_get_items_out_of_bound_offset_should_error
+ *
+ * @ticket 58524
+ */
+ public function test_get_items_out_of_bound_offset_should_error() {
+ wp_set_current_user( self::$admin_id );
+
+ $per_page = 2;
+ $offset = $this->total_revisions + 1;
+ $expected_error = 'rest_revision_invalid_offset_number';
+ $expected_status = 400;
+
+ $request = new WP_REST_Request( 'GET', '/wp/v2/global-styles/' . self::$global_styles_id . '/revisions' );
+ $request->set_query_params(
+ array(
+ 'offset' => $offset,
+ 'per_page' => $per_page,
+ )
+ );
+ $response = rest_get_server()->dispatch( $request );
+ $this->assertErrorResponse( $expected_error, $response, $expected_status );
+ }
+
+ /**
+ * Tests that impossible high number for 'offset' query should error.
+ *
+ * Duplicate of WP_Test_REST_Revisions_Controller::test_get_items_impossible_high_number_offset_should_error
+ *
+ * @ticket 58524
+ */
+ public function test_get_items_impossible_high_number_offset_should_error() {
+ wp_set_current_user( self::$admin_id );
+
+ $per_page = 2;
+ $offset = REST_TESTS_IMPOSSIBLY_HIGH_NUMBER;
+ $expected_error = 'rest_revision_invalid_offset_number';
+ $expected_status = 400;
+
+ $request = new WP_REST_Request( 'GET', '/wp/v2/global-styles/' . self::$global_styles_id . '/revisions' );
+ $request->set_query_params(
+ array(
+ 'offset' => $offset,
+ 'per_page' => $per_page,
+ )
+ );
+ $response = rest_get_server()->dispatch( $request );
+ $this->assertErrorResponse( $expected_error, $response, $expected_status );
+ }
+
+ /**
+ * Tests that invalid 'offset' query should error.
+ *
+ * Duplicate of WP_Test_REST_Revisions_Controller::test_get_items_invalid_offset_should_error
+ *
+ * @ticket 58524
+ */
+ public function test_get_items_invalid_offset_should_error() {
+ wp_set_current_user( self::$admin_id );
+
+ $per_page = 2;
+ $offset = 'moreplease';
+ $expected_error = 'rest_invalid_param';
+ $expected_status = 400;
+
+ $request = new WP_REST_Request( 'GET', '/wp/v2/global-styles/' . self::$global_styles_id . '/revisions' );
+ $request->set_query_params(
+ array(
+ 'offset' => $offset,
+ 'per_page' => $per_page,
+ )
+ );
+ $response = rest_get_server()->dispatch( $request );
+ $this->assertErrorResponse( $expected_error, $response, $expected_status );
+ }
+
+ /**
+ * Tests that out of bounds 'page' query should not error when offset is provided,
+ * because it takes precedence.
+ *
+ * Duplicate of WP_Test_REST_Revisions_Controller::test_get_items_out_of_bounds_page_should_not_error_if_offset
+ *
+ * @ticket 58524
+ */
+ public function test_get_items_out_of_bounds_page_should_not_error_if_offset() {
+ wp_set_current_user( self::$admin_id );
+
+ $per_page = 2;
+ $total_pages = (int) ceil( $this->total_revisions / $per_page );
+ $page = $total_pages + 1; // Out of bound page.
+ $expected_count = 2;
+
+ $request = new WP_REST_Request( 'GET', '/wp/v2/global-styles/' . self::$global_styles_id . '/revisions' );
+ $request->set_query_params(
+ array(
+ 'offset' => 1,
+ 'per_page' => $per_page,
+ 'page' => $page,
+ )
+ );
+ $response = rest_get_server()->dispatch( $request );
+ $this->assertCount( $expected_count, $response->get_data() );
+ }
+
+ /**
+ * @doesNotPerformAssertions
+ */
+ public function test_context_param() {
+ // Controller does not implement test_context_param().
+ }
+
+ /**
+ * @doesNotPerformAssertions
+ */
+ public function test_get_item() {
+ // Controller does not implement get_item().
+ }
+
+ /**
+ * @doesNotPerformAssertions
+ */
+ public function test_create_item() {
+ // Controller does not implement create_item().
+ }
+
+ /**
+ * @doesNotPerformAssertions
+ */
+ public function test_delete_item() {
+ // Controller does not implement delete_item().
+ }
+
+ /**
+ * @doesNotPerformAssertions
+ */
+ public function test_prepare_item() {
+ // Controller does not implement prepare_item().
+ }
+
+ /**
+ * @doesNotPerformAssertions
+ */
+ public function test_update_item() {
+ // Controller does not implement update_item().
+ }
+}
</ins><span class="cx" style="display: block; padding: 0 10px">Property changes on: trunk/tests/phpunit/tests/rest-api/rest-global-styles-revisions-controller.php
</span><span class="cx" style="display: block; padding: 0 10px">___________________________________________________________________
</span></span></pre></div>
<a id="svneolstyle"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: svn:eol-style</h4></div>
<ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+native
</ins><span class="cx" style="display: block; padding: 0 10px">\ No newline at end of property
</span><a id="trunktestsphpunittestsrestapirestschemasetupphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/rest-api/rest-schema-setup.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/rest-api/rest-schema-setup.php 2023-06-27 23:31:01 UTC (rev 56081)
+++ trunk/tests/phpunit/tests/rest-api/rest-schema-setup.php 2023-06-28 03:33:23 UTC (rev 56082)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -134,6 +134,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> '/wp/v2/comments',
</span><span class="cx" style="display: block; padding: 0 10px"> '/wp/v2/comments/(?P<id>[\\d]+)',
</span><span class="cx" style="display: block; padding: 0 10px"> '/wp/v2/global-styles/(?P<id>[\/\w-]+)',
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ '/wp/v2/global-styles/(?P<parent>[\d]+)/revisions',
</ins><span class="cx" style="display: block; padding: 0 10px"> '/wp/v2/global-styles/themes/(?P<stylesheet>[\/\s%\w\.\(\)\[\]\@_\-]+)/variations',
</span><span class="cx" style="display: block; padding: 0 10px"> '/wp/v2/global-styles/themes/(?P<stylesheet>[^\/:<>\*\?"\|]+(?:\/[^\/:<>\*\?"\|]+)?)',
</span><span class="cx" style="display: block; padding: 0 10px"> '/wp/v2/search',
</span></span></pre></div>
<a id="trunktestsqunitfixtureswpapigeneratedjs"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/qunit/fixtures/wp-api-generated.js</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/qunit/fixtures/wp-api-generated.js 2023-06-27 23:31:01 UTC (rev 56081)
+++ trunk/tests/qunit/fixtures/wp-api-generated.js 2023-06-28 03:33:23 UTC (rev 56082)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -9618,6 +9618,56 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> ]
</span><span class="cx" style="display: block; padding: 0 10px"> },
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ "/wp/v2/global-styles/(?P<parent>[\\d]+)/revisions": {
+ "namespace": "wp/v2",
+ "methods": [
+ "GET"
+ ],
+ "endpoints": [
+ {
+ "methods": [
+ "GET"
+ ],
+ "args": {
+ "parent": {
+ "description": "The ID for the parent of the revision.",
+ "type": "integer",
+ "required": false
+ },
+ "context": {
+ "description": "Scope under which the request is made; determines fields present in response.",
+ "type": "string",
+ "enum": [
+ "view",
+ "embed",
+ "edit"
+ ],
+ "default": "view",
+ "required": false
+ },
+ "page": {
+ "description": "Current page of the collection.",
+ "type": "integer",
+ "default": 1,
+ "minimum": 1,
+ "required": false
+ },
+ "per_page": {
+ "description": "Maximum number of items to be returned in result set.",
+ "type": "integer",
+ "minimum": 1,
+ "maximum": 100,
+ "required": false
+ },
+ "offset": {
+ "description": "Offset the result set by a specific number of items.",
+ "type": "integer",
+ "required": false
+ }
+ }
+ }
+ ]
+ },
</ins><span class="cx" style="display: block; padding: 0 10px"> "/wp/v2/global-styles/themes/(?P<stylesheet>[\\/\\s%\\w\\.\\(\\)\\[\\]\\@_\\-]+)/variations": {
</span><span class="cx" style="display: block; padding: 0 10px"> "namespace": "wp/v2",
</span><span class="cx" style="display: block; padding: 0 10px"> "methods": [
</span></span></pre>
</div>
</div>
</body>
</html>