Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
7d192d8
Initial commit.
ramonjd Nov 23, 2023
d7b4c1c
Adding title to schema.
ramonjd Nov 23, 2023
41c98a5
Remove permissions checks and favour parent's
ramonjd Nov 23, 2023
59d6953
Remove duplicated tests
ramonjd Nov 23, 2023
dbcc3b5
Reverting post type registration changes as it assigns an autosave co…
ramonjd Nov 23, 2023
25cbe83
Regenerated fixture
ramonjd Nov 23, 2023
e9d1f10
Revert introducing title in schema
ramonjd Nov 23, 2023
ec4229f
Reinstating global styles revision controller tests that were duplica…
ramonjd Jan 3, 2024
ca524b2
Update src/wp-includes/rest-api/endpoints/class-wp-rest-global-styles…
ramonjd Dec 22, 2023
93190ae
Excluding search, include and exclude from the collection query params
ramonjd Jan 3, 2024
b43344d
Now that https://github.com/WordPress/wordpress-develop/pull/5655 has…
ramonjd Jan 31, 2024
dac8e0b
Pulling across changes from https://github.com/WordPress/wordpress-de…
ramonjd Feb 7, 2024
acd2590
Implementing latest round of feedback:
ramonjd Feb 8, 2024
dfa02f8
Apply suggestions from code review
spacedmonkey Feb 13, 2024
f6f45e3
Fix lints
jonnynews Feb 13, 2024
038331b
Remove ideas
jonnynews Feb 13, 2024
6c7a149
Merge branch 'trunk' into update/global-styles-revision-extends-revis…
jonnynews Feb 13, 2024
95db1e9
Fix
jonnynews Feb 13, 2024
9fb3e30
Add allow_batch parameter.
jonnynews Feb 13, 2024
319bdff
Be overly safe with `__construct` method.
jonnynews Feb 13, 2024
095c7d0
Apply suggestions from code review
spacedmonkey Feb 13, 2024
1fef997
Add 'allow_batch' option to API endpoints in WP API
jonnynews Feb 13, 2024
70d68da
Update version annotations for wp_global_styles related changes
spacedmonkey May 20, 2024
c62fed9
Simplify unset function calls in global styles revisions controller
spacedmonkey May 20, 2024
cc916ac
Merge branch 'trunk' into update/global-styles-revision-extends-revis…
spacedmonkey May 23, 2024
7c526bd
Disable autosave for global styles in WordPress
spacedmonkey May 23, 2024
fe4fc65
Remove redundant comment in wp-post-type.php
spacedmonkey May 23, 2024
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
Next Next commit
Initial commit.
WP_REST_Global_Styles_Revisions_Controller extends WP_REST_Controller
  • Loading branch information
ramonjd committed Feb 7, 2024
commit 7d192d8adfa0860430e5652a95b9e48f3fe911fa
26 changes: 15 additions & 11 deletions src/wp-includes/post.php
Original file line number Diff line number Diff line change
Expand Up @@ -473,15 +473,19 @@ function create_initial_post_types() {
register_post_type(
'wp_global_styles',
array(
'label' => _x( 'Global Styles', 'post type general name' ),
'description' => __( 'Global styles to include in themes.' ),
'public' => false,
'_builtin' => true, /* internal use only. don't use this when registering your own post type. */
'_edit_link' => '/site-editor.php?canvas=edit', /* internal use only. don't use this when registering your own post type. */
'show_ui' => false,
'show_in_rest' => false,
'rewrite' => false,
'capabilities' => array(
'label' => _x( 'Global Styles', 'post type general name' ),
'description' => __( 'Global styles to include in themes.' ),
'public' => false,
'_builtin' => true, /* internal use only. don't use this when registering your own post type. */
'_edit_link' => '/site-editor.php?canvas=edit', /* internal use only. don't use this when registering your own post type. */
'show_ui' => false,
'show_in_rest' => true,
'rewrite' => false,
'rest_base' => 'global-styles',
'rest_controller_class' => 'WP_REST_Global_Styles_Controller',
'revisions_rest_controller_class' => 'WP_REST_Global_Styles_Revisions_Controller',
'late_route_registration' => true,
'capabilities' => array(
'read' => 'edit_theme_options',
'create_posts' => 'edit_theme_options',
'edit_posts' => 'edit_theme_options',
Expand All @@ -490,8 +494,8 @@ function create_initial_post_types() {
'edit_others_posts' => 'edit_theme_options',
'delete_others_posts' => 'edit_theme_options',
),
'map_meta_cap' => true,
'supports' => array(
'map_meta_cap' => true,
'supports' => array(
'title',
'editor',
'revisions',
Expand Down
4 changes: 0 additions & 4 deletions src/wp-includes/rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -323,10 +323,6 @@ function create_initial_rest_routes() {
$controller = new WP_REST_Block_Types_Controller();
$controller->register_routes();

// Global Styles revisions.
$controller = new WP_REST_Global_Styles_Revisions_Controller();
$controller->register_routes();

// Global Styles.
$controller = new WP_REST_Global_Styles_Controller();
$controller->register_routes();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,22 @@
*
* @see WP_REST_Controller
*/
class WP_REST_Global_Styles_Revisions_Controller extends WP_REST_Controller {
class WP_REST_Global_Styles_Revisions_Controller extends WP_REST_Revisions_Controller {
/**
* Parent post type.
*
* @since 6.3.0
* @var string
*/
protected $parent_post_type;
private $parent_post_type;

/**
* Parent controller.
*
* @since 6.5.0
* @var WP_REST_Controller
*/
private $parent_controller;

/**
* The base of the parent controller's route.
Expand All @@ -35,12 +43,23 @@ class WP_REST_Global_Styles_Revisions_Controller extends WP_REST_Controller {
* Constructor.
*
* @since 6.3.0
*
* @param string $parent_post_type Post type of the parent.
*/
public function __construct() {
$this->parent_post_type = 'wp_global_styles';
$this->rest_base = 'revisions';
$this->parent_base = 'global-styles';
$this->namespace = 'wp/v2';
public function __construct( $parent_post_type ) {
parent::__construct( $parent_post_type );
$this->parent_post_type = $parent_post_type;
$post_type_object = get_post_type_object( $parent_post_type );
$parent_controller = $post_type_object->get_rest_controller();

if ( ! $parent_controller ) {
$parent_controller = new WP_REST_Global_Styles_Controller();
}

$this->parent_controller = $parent_controller;
$this->rest_base = 'revisions';
$this->parent_base = ! empty( $post_type_object->rest_base ) ? $post_type_object->rest_base : $post_type_object->name;
$this->namespace = ! empty( $post_type_object->rest_namespace ) ? $post_type_object->rest_namespace : 'wp/v2';
}

/**
Expand All @@ -63,7 +82,7 @@ public function register_routes() {
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_items' ),
'permission_callback' => array( $this, 'get_item_permissions_check' ),
'permission_callback' => array( $this, 'get_items_permissions_check' ),
'args' => $this->get_collection_params(),
),
'schema' => array( $this, 'get_public_item_schema' ),
Expand Down Expand Up @@ -97,29 +116,6 @@ public function register_routes() {
);
}

/**
* Retrieves the query params for collections.
*
* Inherits from WP_REST_Controller::get_collection_params(),
* also reflects changes to return value WP_REST_Revisions_Controller::get_collection_params().
*
* @since 6.3.0
*
* @return array Collection parameters.
*/
public function get_collection_params() {
$collection_params = parent::get_collection_params();
$collection_params['context']['default'] = 'view';
$collection_params['offset'] = array(
'description' => __( 'Offset the result set by a specific number of items.' ),
'type' => 'integer',
);
unset( $collection_params['search'] );
unset( $collection_params['per_page']['default'] );

return $collection_params;
}

/**
* Returns decoded JSON from post content string,
* or a 404 if not found.
Expand Down Expand Up @@ -318,30 +314,6 @@ protected function get_revision( $id ) {
return $revision;
}

/**
* Checks the post_date_gmt or modified_gmt and prepare any post or
* modified date for single post output.
*
* Duplicate of WP_REST_Revisions_Controller::prepare_date_response.
*
* @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.
*
Expand Down Expand Up @@ -419,85 +391,39 @@ public function get_item_schema() {
return $this->add_additional_fields_schema( $this->schema );
}

$schema = array(
'$schema' => 'http://json-schema.org/draft-04/schema#',
'title' => "{$this->parent_post_type}-revision",
'type' => 'object',
// Base properties for every revision.
'properties' => array(

/*
* Adds settings and styles from the WP_REST_Revisions_Controller item fields.
* Leaves out GUID as global styles shouldn't be accessible via URL.
*/
'author' => array(
'description' => __( 'The ID for the author of the revision.' ),
'type' => 'integer',
'context' => array( 'view', 'edit', 'embed' ),
),
'date' => array(
'description' => __( "The date the revision was published, in the site's timezone." ),
'type' => 'string',
'format' => 'date-time',
'context' => array( 'view', 'edit', 'embed' ),
),
'date_gmt' => array(
'description' => __( 'The date the revision was published, as GMT.' ),
'type' => 'string',
'format' => 'date-time',
'context' => array( 'view', 'edit' ),
),
'id' => array(
'description' => __( 'Unique identifier for the revision.' ),
'type' => 'integer',
'context' => array( 'view', 'edit', 'embed' ),
),
'modified' => array(
'description' => __( "The date the revision was last modified, in the site's timezone." ),
'type' => 'string',
'format' => 'date-time',
'context' => array( 'view', 'edit' ),
),
'modified_gmt' => array(
'description' => __( 'The date the revision was last modified, as GMT.' ),
'type' => 'string',
'format' => 'date-time',
'context' => array( 'view', 'edit' ),
),
'parent' => array(
'description' => __( 'The ID for the parent of the revision.' ),
'type' => 'integer',
'context' => array( 'view', 'edit', 'embed' ),
),
// Adds settings and styles from the WP_REST_Revisions_Controller item fields.
$schema = parent::get_item_schema();

// Adds settings and styles from the WP_REST_Global_Styles_Controller parent schema.
'styles' => array(
'description' => __( 'Global styles.' ),
'type' => array( 'object' ),
'context' => array( 'view', 'edit' ),
),
'settings' => array(
'description' => __( 'Global settings.' ),
'type' => array( 'object' ),
'context' => array( 'view', 'edit' ),
),
),
// Adds settings and styles from the WP_REST_Global_Styles_Controller parent schema.
$schema['properties']['styles'] = array(
'description' => __( 'Global styles.' ),
'type' => array( 'object' ),
'context' => array( 'view', 'edit' ),
);

$schema['properties']['settings'] = array(
'description' => __( 'Global settings.' ),
'type' => array( 'object' ),
'context' => array( 'view', 'edit' ),
);

unset( $schema['properties']['guid'] );
unset( $schema['properties']['slug'] );

$this->schema = $schema;

return $this->add_additional_fields_schema( $this->schema );
}

/**
* Checks if a given request has access to read a single global style.
* Checks if a given request has access to get global styles revisions.
*
* @since 6.3.0
* @since 6.5.0
*
* @param WP_REST_Request $request Full details about the request.
* @return true|WP_Error True if the request has read access, WP_Error object otherwise.
*/
public function get_item_permissions_check( $request ) {
public function get_items_permissions_check( $request ) {
$post = $this->get_parent( $request['parent'] );
if ( is_wp_error( $post ) ) {
return $post;
Expand All @@ -518,34 +444,14 @@ public function get_item_permissions_check( $request ) {
}

/**
* Gets the parent post, if the ID is valid.
*
* Duplicate of WP_REST_Revisions_Controller::get_parent.
* Checks if a given request has access to read a single global style revision.
*
* @since 6.3.0
*
* @param int $parent_post_id Supplied ID.
* @return WP_Post|WP_Error Post object if ID is valid, WP_Error otherwise.
* @param WP_REST_Request $request Full details about the request.
* @return true|WP_Error True if the request has read access, WP_Error object otherwise.
*/
protected function get_parent( $parent_post_id ) {
$error = new WP_Error(
'rest_post_invalid_parent',
__( 'Invalid post parent ID.' ),
array( 'status' => 404 )
);

if ( (int) $parent_post_id <= 0 ) {
return $error;
}

$parent_post = get_post( (int) $parent_post_id );

if ( empty( $parent_post ) || empty( $parent_post->ID )
|| $this->parent_post_type !== $parent_post->post_type
) {
return $error;
}

return $parent_post;
public function get_item_permissions_check( $request ) {
return $this->get_items_permissions_check( $request );
}
}
2 changes: 1 addition & 1 deletion src/wp-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -276,10 +276,10 @@
require ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-posts-controller.php';
require ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-attachments-controller.php';
require ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-global-styles-controller.php';
require ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-global-styles-revisions-controller.php';
require ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-post-types-controller.php';
require ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-post-statuses-controller.php';
require ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-revisions-controller.php';
require ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-global-styles-revisions-controller.php';
require ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-template-revisions-controller.php';
require ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-autosaves-controller.php';
require ABSPATH . WPINC . '/rest-api/endpoints/class-wp-rest-template-autosaves-controller.php';
Expand Down