Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Global styles revisions: move from experimental to 6.3 #51474

Merged
merged 5 commits into from
Jun 14, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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';
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LOL Gutenberg_REST_Templates_Controller_6_3 was way off 😮‍💨

$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,7 +48,7 @@ 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',
Expand All @@ -63,6 +63,15 @@ public static function wpSetupBeforeClass( $factory ) {
);
}

/**
* 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