diff --git a/lib/experimental/class-gutenberg-rest-global-styles-revisions-controller.php b/lib/compat/wordpress-6.3/class-gutenberg-rest-global-styles-revisions-controller-6-3.php similarity index 85% rename from lib/experimental/class-gutenberg-rest-global-styles-revisions-controller.php rename to lib/compat/wordpress-6.3/class-gutenberg-rest-global-styles-revisions-controller-6-3.php index c5d5a5f0dff1a4..356b435b71f1e8 100644 --- a/lib/experimental/class-gutenberg-rest-global-styles-revisions-controller.php +++ b/lib/compat/wordpress-6.3/class-gutenberg-rest-global-styles-revisions-controller-6-3.php @@ -14,7 +14,7 @@ * * @see WP_REST_Controller */ -class Gutenberg_REST_Global_Styles_Revisions_Controller extends WP_REST_Controller { +class Gutenberg_REST_Global_Styles_Revisions_Controller_6_3 extends WP_REST_Controller { /** * Parent post type. * @@ -107,19 +107,42 @@ public function get_items( $request ) { return rest_ensure_response( $response ); } + /** + * A direct copy of WP_REST_Revisions_Controller->prepare_date_response(). + * Checks the post_date_gmt or modified_gmt and prepare any post or + * modified date for single post output. + * + * @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 $item Post revision object. + * @param WP_Post $post Post revision object. * @param WP_REST_Request $request Request object. * @return WP_REST_Response Response object. */ - public function prepare_item_for_response( $item, $request ) { + public function prepare_item_for_response( $post, $request ) { $parent = $this->get_parent( $request['parent'] ); // Retrieves global styles config as JSON. - $raw_revision_config = json_decode( $item->post_content, true ); + $raw_revision_config = json_decode( $post->post_content, true ); $config = ( new WP_Theme_JSON_Gutenberg( $raw_revision_config, 'custom' ) )->get_raw_data(); // Prepares item data. @@ -127,27 +150,27 @@ public function prepare_item_for_response( $item, $request ) { $fields = $this->get_fields_for_response( $request ); if ( rest_is_field_included( 'author', $fields ) ) { - $data['author'] = (int) $item->post_author; + $data['author'] = (int) $post->post_author; } if ( rest_is_field_included( 'date', $fields ) ) { - $data['date'] = $item->post_date; + $data['date'] = $this->prepare_date_response( $post->post_date_gmt, $post->post_date ); } if ( rest_is_field_included( 'date_gmt', $fields ) ) { - $data['date_gmt'] = $item->post_date_gmt; + $data['date_gmt'] = $this->prepare_date_response( $post->post_date_gmt ); } if ( rest_is_field_included( 'id', $fields ) ) { - $data['id'] = (int) $item->ID; + $data['id'] = (int) $post->ID; } if ( rest_is_field_included( 'modified', $fields ) ) { - $data['modified'] = $item->post_modified; + $data['modified'] = $this->prepare_date_response( $post->post_modified_gmt, $post->post_modified ); } if ( rest_is_field_included( 'modified_gmt', $fields ) ) { - $data['modified_gmt'] = $item->post_modified_gmt; + $data['modified_gmt'] = $this->prepare_date_response( $post->post_modified_gmt ); } if ( rest_is_field_included( 'parent', $fields ) ) { diff --git a/lib/compat/wordpress-6.3/rest-api.php b/lib/compat/wordpress-6.3/rest-api.php index 50f8eeff6c3230..d571e56dae7628 100644 --- a/lib/compat/wordpress-6.3/rest-api.php +++ b/lib/compat/wordpress-6.3/rest-api.php @@ -39,7 +39,7 @@ function gutenberg_update_templates_template_parts_rest_controller( $args, $post * Registers the Global Styles Revisions REST API routes. */ function gutenberg_register_global_styles_revisions_endpoints() { - $global_styles_revisions_controller = new Gutenberg_REST_Global_Styles_Revisions_Controller(); + $global_styles_revisions_controller = new Gutenberg_REST_Global_Styles_Revisions_Controller_6_3(); $global_styles_revisions_controller->register_routes(); } add_action( 'rest_api_init', 'gutenberg_register_global_styles_revisions_endpoints' ); @@ -53,17 +53,3 @@ function gutenberg_register_global_styles_endpoints() { } add_action( 'rest_api_init', 'gutenberg_register_global_styles_endpoints' ); -/** - * Update `wp_global_styles` post type to use Gutenberg's REST controller. - * - * @param array $args Array of arguments for registering a post type. - * @param string $post_type Post type key. - */ -function gutenberg_update_global_styles_rest_controller( $args, $post_type ) { - if ( in_array( $post_type, array( 'wp_global_styles' ), true ) ) { - $args['rest_controller_class'] = 'Gutenberg_REST_Templates_Controller_6_3'; - $args['rest_base'] = 'global-styles'; - } - return $args; -} -add_filter( 'register_post_type_args', 'gutenberg_update_global_styles_rest_controller', 10, 2 ); diff --git a/lib/load.php b/lib/load.php index f5c1f25db52522..1fd251bd19e9b0 100644 --- a/lib/load.php +++ b/lib/load.php @@ -46,6 +46,7 @@ function gutenberg_is_experiment_enabled( $name ) { // WordPress 6.3 compat. require_once __DIR__ . '/compat/wordpress-6.3/class-gutenberg-rest-templates-controller-6-3.php'; require_once __DIR__ . '/compat/wordpress-6.3/class-gutenberg-rest-global-styles-controller-6-3.php'; + require_once __DIR__ . '/compat/wordpress-6.3/class-gutenberg-rest-global-styles-revisions-controller-6-3.php'; require_once __DIR__ . '/compat/wordpress-6.3/rest-api.php'; require_once __DIR__ . '/compat/wordpress-6.3/theme-previews.php'; require_once __DIR__ . '/compat/wordpress-6.3/navigation-block-preloading.php'; @@ -57,7 +58,6 @@ function gutenberg_is_experiment_enabled( $name ) { require_once __DIR__ . '/experimental/class-wp-rest-customizer-nonces.php'; } - require_once __DIR__ . '/experimental/class-gutenberg-rest-global-styles-revisions-controller.php'; require_once __DIR__ . '/experimental/class-wp-rest-navigation-fallback-controller.php'; require_once __DIR__ . '/experimental/rest-api.php'; } diff --git a/phpunit/class-gutenberg-rest-global-styles-revisions-controller-test.php b/phpunit/class-gutenberg-rest-global-styles-revisions-controller-test.php index 6856638bf8754e..3e7f8763c5c765 100644 --- a/phpunit/class-gutenberg-rest-global-styles-revisions-controller-test.php +++ b/phpunit/class-gutenberg-rest-global-styles-revisions-controller-test.php @@ -48,21 +48,29 @@ public static function wpSetupBeforeClass( $factory ) { ) ); // This creates the global styles for the current theme. - self::$global_styles_id = wp_insert_post( + self::$global_styles_id = $factory->post->create( array( 'post_content' => '{"version": ' . WP_Theme_JSON_Gutenberg::LATEST_SCHEMA . ', "isGlobalStylesUserThemeJSON": true }', 'post_status' => 'publish', 'post_title' => __( 'Custom Styles', 'default' ), 'post_type' => 'wp_global_styles', - 'post_name' => 'wp-global-styles-emptytheme', + 'post_name' => 'wp-global-styles-emptytheme-revisions', 'tax_input' => array( 'wp_theme' => 'emptytheme', ), - ), - true + ) ); } + /** + * 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 ); + } + /** * @covers Gutenberg_REST_Global_Styles_Revisions_Controller::register_routes */