Skip to content

Commit

Permalink
Rest API: add /revisions endpoint for global styles (#49974)
Browse files Browse the repository at this point in the history
* Initial commit:
- adding /revisions endpoint to global styles API
- tests

* Updating tests to account for author and is_latest response properties

* There's an existing protected method called `prepare_links` in Gutenberg_REST_Global_Styles_Controller that we can overwrite in the subclass. It makes for less code and a neater abstraction.

* Refactored to extract revisions endpoint registration and associated classes into Gutenberg_REST_Global_Styles_Revisions_Controller
Returning revisions URL in prepare_links

* Update tests

* Removing unused comment and method.
  • Loading branch information
ramonjd authored Apr 26, 2023
1 parent 13a2d09 commit 832da50
Show file tree
Hide file tree
Showing 6 changed files with 624 additions and 9 deletions.
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 {
/**
* 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

0 comments on commit 832da50

Please sign in to comment.