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

Rest API: add /revisions endpoint for global styles #49974

Merged
merged 6 commits into from
Apr 26, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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
9 changes: 0 additions & 9 deletions lib/compat/wordpress-6.2/rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,6 @@ function gutenberg_pattern_directory_collection_params_6_2( $query_params ) {
}
add_filter( 'rest_pattern_directory_collection_params', 'gutenberg_pattern_directory_collection_params_6_2' );

/**
* Registers the Global Styles REST API routes.
*/
function gutenberg_register_global_styles_endpoints() {
$editor_settings = new Gutenberg_REST_Global_Styles_Controller_6_2();
$editor_settings->register_routes();
}
add_action( 'rest_api_init', 'gutenberg_register_global_styles_endpoints' );

/**
* Updates REST API response for the sidebars and marks them as 'inactive'.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php
/**
* REST API: Gutenberg_REST_Global_Styles_Controller class
*
* @package Gutenberg
* @subpackage REST_API
*/

/**
* Base Global Styles REST API Controller.
*/
class Gutenberg_REST_Global_Styles_Controller_6_3 extends Gutenberg_REST_Global_Styles_Controller_6_2 {
ramonjd marked this conversation as resolved.
Show resolved Hide resolved
/**
* Revision controller.
*
* @since 6.3.0
* @var WP_REST_Revisions_Controller
*/
private $revisions_controller;

/**
* Prepares links for the request.
*
* @since 5.9.0
* @since 6.3 Adds revisions to version-history.
*
* @param integer $id ID.
* @return array Links for the given post.
*/
protected function prepare_links( $id ) {
$base = sprintf( '%s/%s', $this->namespace, $this->rest_base );

$links = array(
'self' => array(
'href' => rest_url( trailingslashit( $base ) . $id ),
),
);

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/%s/%d/revisions', $this->namespace, $this->rest_base, $id );
$links['version-history'] = array(
'href' => rest_url( $revisions_base ),
'count' => $revisions_count,
);
}

return $links;
}
}
34 changes: 34 additions & 0 deletions lib/compat/wordpress-6.3/rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,37 @@ function gutenberg_update_templates_template_parts_rest_controller( $args, $post
return $args;
}
add_filter( 'register_post_type_args', 'gutenberg_update_templates_template_parts_rest_controller', 10, 2 );


/**
* 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->register_routes();
}
add_action( 'rest_api_init', 'gutenberg_register_global_styles_revisions_endpoints' );

/**
* Registers the Global Styles REST API routes.
*/
function gutenberg_register_global_styles_endpoints() {
$global_styles_controller = new Gutenberg_REST_Global_Styles_Controller_6_3();
$global_styles_controller->register_routes();
}
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 );
Loading