Skip to content

Commit

Permalink
Global styles revisions: move from experimental to 6.3 (#51474)
Browse files Browse the repository at this point in the history
* 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 :)
  • Loading branch information
ramonjd authored Jun 14, 2023
1 parent 664a5af commit b6986e0
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -107,47 +107,70 @@ 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.
$data = array();
$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 ) ) {
Expand Down
16 changes: 1 addition & 15 deletions lib/compat/wordpress-6.3/rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' );
Expand All @@ -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 );
2 changes: 1 addition & 1 deletion lib/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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';
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down

0 comments on commit b6986e0

Please sign in to comment.