Skip to content

Commit b6986e0

Browse files
authored
Global styles revisions: move from experimental to 6.3 (#51474)
* Removing `gutenberg_update_global_styles_rest_controller` hook as it's not required for 6.3 and was unrelated to the addition of the /revisions endpoint in the first place Moving global styles revisions endpoint file to 6-3 since it'll be included in 6.3 and is not experimental * Class needs to be named after file name, hence the 6_3 suffix * Backporting changes from core to gutenberg: adding prepare_date_response to parse dates * Tearing down after class in tests * Post name should be different to the one created in the global styles endpoint test :)
1 parent 664a5af commit b6986e0

File tree

4 files changed

+47
-30
lines changed

4 files changed

+47
-30
lines changed

lib/experimental/class-gutenberg-rest-global-styles-revisions-controller.php renamed to lib/compat/wordpress-6.3/class-gutenberg-rest-global-styles-revisions-controller-6-3.php

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
*
1515
* @see WP_REST_Controller
1616
*/
17-
class Gutenberg_REST_Global_Styles_Revisions_Controller extends WP_REST_Controller {
17+
class Gutenberg_REST_Global_Styles_Revisions_Controller_6_3 extends WP_REST_Controller {
1818
/**
1919
* Parent post type.
2020
*
@@ -107,47 +107,70 @@ public function get_items( $request ) {
107107
return rest_ensure_response( $response );
108108
}
109109

110+
/**
111+
* A direct copy of WP_REST_Revisions_Controller->prepare_date_response().
112+
* Checks the post_date_gmt or modified_gmt and prepare any post or
113+
* modified date for single post output.
114+
*
115+
* @since 6.3.0
116+
*
117+
* @param string $date_gmt GMT publication time.
118+
* @param string|null $date Optional. Local publication time. Default null.
119+
* @return string|null ISO8601/RFC3339 formatted datetime, otherwise null.
120+
*/
121+
protected function prepare_date_response( $date_gmt, $date = null ) {
122+
if ( '0000-00-00 00:00:00' === $date_gmt ) {
123+
return null;
124+
}
125+
126+
if ( isset( $date ) ) {
127+
return mysql_to_rfc3339( $date );
128+
}
129+
130+
return mysql_to_rfc3339( $date_gmt );
131+
}
132+
110133
/**
111134
* Prepares the revision for the REST response.
112135
*
113136
* @since 6.3.0
114137
*
115-
* @param WP_Post $item Post revision object.
138+
* @param WP_Post $post Post revision object.
116139
* @param WP_REST_Request $request Request object.
117140
* @return WP_REST_Response Response object.
118141
*/
119-
public function prepare_item_for_response( $item, $request ) {
142+
public function prepare_item_for_response( $post, $request ) {
120143
$parent = $this->get_parent( $request['parent'] );
121144
// Retrieves global styles config as JSON.
122-
$raw_revision_config = json_decode( $item->post_content, true );
145+
$raw_revision_config = json_decode( $post->post_content, true );
123146
$config = ( new WP_Theme_JSON_Gutenberg( $raw_revision_config, 'custom' ) )->get_raw_data();
124147

125148
// Prepares item data.
126149
$data = array();
127150
$fields = $this->get_fields_for_response( $request );
128151

129152
if ( rest_is_field_included( 'author', $fields ) ) {
130-
$data['author'] = (int) $item->post_author;
153+
$data['author'] = (int) $post->post_author;
131154
}
132155

133156
if ( rest_is_field_included( 'date', $fields ) ) {
134-
$data['date'] = $item->post_date;
157+
$data['date'] = $this->prepare_date_response( $post->post_date_gmt, $post->post_date );
135158
}
136159

137160
if ( rest_is_field_included( 'date_gmt', $fields ) ) {
138-
$data['date_gmt'] = $item->post_date_gmt;
161+
$data['date_gmt'] = $this->prepare_date_response( $post->post_date_gmt );
139162
}
140163

141164
if ( rest_is_field_included( 'id', $fields ) ) {
142-
$data['id'] = (int) $item->ID;
165+
$data['id'] = (int) $post->ID;
143166
}
144167

145168
if ( rest_is_field_included( 'modified', $fields ) ) {
146-
$data['modified'] = $item->post_modified;
169+
$data['modified'] = $this->prepare_date_response( $post->post_modified_gmt, $post->post_modified );
147170
}
148171

149172
if ( rest_is_field_included( 'modified_gmt', $fields ) ) {
150-
$data['modified_gmt'] = $item->post_modified_gmt;
173+
$data['modified_gmt'] = $this->prepare_date_response( $post->post_modified_gmt );
151174
}
152175

153176
if ( rest_is_field_included( 'parent', $fields ) ) {

lib/compat/wordpress-6.3/rest-api.php

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ function gutenberg_update_templates_template_parts_rest_controller( $args, $post
3939
* Registers the Global Styles Revisions REST API routes.
4040
*/
4141
function gutenberg_register_global_styles_revisions_endpoints() {
42-
$global_styles_revisions_controller = new Gutenberg_REST_Global_Styles_Revisions_Controller();
42+
$global_styles_revisions_controller = new Gutenberg_REST_Global_Styles_Revisions_Controller_6_3();
4343
$global_styles_revisions_controller->register_routes();
4444
}
4545
add_action( 'rest_api_init', 'gutenberg_register_global_styles_revisions_endpoints' );
@@ -53,17 +53,3 @@ function gutenberg_register_global_styles_endpoints() {
5353
}
5454
add_action( 'rest_api_init', 'gutenberg_register_global_styles_endpoints' );
5555

56-
/**
57-
* Update `wp_global_styles` post type to use Gutenberg's REST controller.
58-
*
59-
* @param array $args Array of arguments for registering a post type.
60-
* @param string $post_type Post type key.
61-
*/
62-
function gutenberg_update_global_styles_rest_controller( $args, $post_type ) {
63-
if ( in_array( $post_type, array( 'wp_global_styles' ), true ) ) {
64-
$args['rest_controller_class'] = 'Gutenberg_REST_Templates_Controller_6_3';
65-
$args['rest_base'] = 'global-styles';
66-
}
67-
return $args;
68-
}
69-
add_filter( 'register_post_type_args', 'gutenberg_update_global_styles_rest_controller', 10, 2 );

lib/load.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ function gutenberg_is_experiment_enabled( $name ) {
4646
// WordPress 6.3 compat.
4747
require_once __DIR__ . '/compat/wordpress-6.3/class-gutenberg-rest-templates-controller-6-3.php';
4848
require_once __DIR__ . '/compat/wordpress-6.3/class-gutenberg-rest-global-styles-controller-6-3.php';
49+
require_once __DIR__ . '/compat/wordpress-6.3/class-gutenberg-rest-global-styles-revisions-controller-6-3.php';
4950
require_once __DIR__ . '/compat/wordpress-6.3/rest-api.php';
5051
require_once __DIR__ . '/compat/wordpress-6.3/theme-previews.php';
5152
require_once __DIR__ . '/compat/wordpress-6.3/navigation-block-preloading.php';
@@ -57,7 +58,6 @@ function gutenberg_is_experiment_enabled( $name ) {
5758
require_once __DIR__ . '/experimental/class-wp-rest-customizer-nonces.php';
5859
}
5960

60-
require_once __DIR__ . '/experimental/class-gutenberg-rest-global-styles-revisions-controller.php';
6161
require_once __DIR__ . '/experimental/class-wp-rest-navigation-fallback-controller.php';
6262
require_once __DIR__ . '/experimental/rest-api.php';
6363
}

phpunit/class-gutenberg-rest-global-styles-revisions-controller-test.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,21 +48,29 @@ public static function wpSetupBeforeClass( $factory ) {
4848
)
4949
);
5050
// This creates the global styles for the current theme.
51-
self::$global_styles_id = wp_insert_post(
51+
self::$global_styles_id = $factory->post->create(
5252
array(
5353
'post_content' => '{"version": ' . WP_Theme_JSON_Gutenberg::LATEST_SCHEMA . ', "isGlobalStylesUserThemeJSON": true }',
5454
'post_status' => 'publish',
5555
'post_title' => __( 'Custom Styles', 'default' ),
5656
'post_type' => 'wp_global_styles',
57-
'post_name' => 'wp-global-styles-emptytheme',
57+
'post_name' => 'wp-global-styles-emptytheme-revisions',
5858
'tax_input' => array(
5959
'wp_theme' => 'emptytheme',
6060
),
61-
),
62-
true
61+
)
6362
);
6463
}
6564

65+
/**
66+
* Removes users after our tests run.
67+
*/
68+
public static function wpTearDownAfterClass() {
69+
self::delete_user( self::$admin_id );
70+
self::delete_user( self::$second_admin_id );
71+
self::delete_user( self::$author_id );
72+
}
73+
6674
/**
6775
* @covers Gutenberg_REST_Global_Styles_Revisions_Controller::register_routes
6876
*/

0 commit comments

Comments
 (0)