Skip to content

Commit d4d7e82

Browse files
ramonjdmukeshpanchal27spacedmonkey
committed
Pulling across changes from #6022
Co-authored-by: ramonjd <ramonopoly@git.wordpress.org> Co-authored-by: mukeshpanchal27 <mukesh27@git.wordpress.org> Co-authored-by: spacedmonkey <spacedmonkey@git.wordpress.org>
1 parent f7cf55a commit d4d7e82

File tree

6 files changed

+300
-1048
lines changed

6 files changed

+300
-1048
lines changed

src/wp-includes/class-wp-post-type.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -913,6 +913,7 @@ public function get_revisions_rest_controller() {
913913
* Will only instantiate the controller class once per request.
914914
*
915915
* @since 6.4.0
916+
* @since 6.5.0 Prevents autosave class instantiation for wp_global_styles post types.
916917
*
917918
* @return WP_REST_Controller|null The controller instance, or null if the post type
918919
* is set not to show in rest.
@@ -922,7 +923,7 @@ public function get_autosave_rest_controller() {
922923
return null;
923924
}
924925

925-
if ( 'attachment' === $this->name ) {
926+
if ( in_array( $this->name, array( 'attachment', 'wp_global_styles' ), true ) ) {
926927
return null;
927928
}
928929

src/wp-includes/post.php

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -473,15 +473,19 @@ function create_initial_post_types() {
473473
register_post_type(
474474
'wp_global_styles',
475475
array(
476-
'label' => _x( 'Global Styles', 'post type general name' ),
477-
'description' => __( 'Global styles to include in themes.' ),
478-
'public' => false,
479-
'_builtin' => true, /* internal use only. don't use this when registering your own post type. */
480-
'_edit_link' => '/site-editor.php?canvas=edit', /* internal use only. don't use this when registering your own post type. */
481-
'show_ui' => false,
482-
'show_in_rest' => false,
483-
'rewrite' => false,
484-
'capabilities' => array(
476+
'label' => _x( 'Global Styles', 'post type general name' ),
477+
'description' => __( 'Global styles to include in themes.' ),
478+
'public' => false,
479+
'_builtin' => true, /* internal use only. don't use this when registering your own post type. */
480+
'_edit_link' => '/site-editor.php?canvas=edit', /* internal use only. don't use this when registering your own post type. */
481+
'show_ui' => false,
482+
'show_in_rest' => true,
483+
'rewrite' => false,
484+
'rest_base' => 'global-styles',
485+
'rest_controller_class' => 'WP_REST_Global_Styles_Controller',
486+
'revisions_rest_controller_class' => 'WP_REST_Global_Styles_Revisions_Controller',
487+
'late_route_registration' => true,
488+
'capabilities' => array(
485489
'read' => 'edit_theme_options',
486490
'create_posts' => 'edit_theme_options',
487491
'edit_posts' => 'edit_theme_options',
@@ -490,8 +494,8 @@ function create_initial_post_types() {
490494
'edit_others_posts' => 'edit_theme_options',
491495
'delete_others_posts' => 'edit_theme_options',
492496
),
493-
'map_meta_cap' => true,
494-
'supports' => array(
497+
'map_meta_cap' => true,
498+
'supports' => array(
495499
'title',
496500
'editor',
497501
'revisions',

src/wp-includes/rest-api.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -323,14 +323,6 @@ function create_initial_rest_routes() {
323323
$controller = new WP_REST_Block_Types_Controller();
324324
$controller->register_routes();
325325

326-
// Global Styles revisions.
327-
$controller = new WP_REST_Global_Styles_Revisions_Controller();
328-
$controller->register_routes();
329-
330-
// Global Styles.
331-
$controller = new WP_REST_Global_Styles_Controller();
332-
$controller->register_routes();
333-
334326
// Settings.
335327
$controller = new WP_REST_Settings_Controller();
336328
$controller->register_routes();

src/wp-includes/rest-api/endpoints/class-wp-rest-global-styles-controller.php

Lines changed: 18 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,20 @@
1010
/**
1111
* Base Global Styles REST API Controller.
1212
*/
13-
class WP_REST_Global_Styles_Controller extends WP_REST_Controller {
14-
15-
/**
16-
* Post type.
17-
*
18-
* @since 5.9.0
19-
* @var string
20-
*/
21-
protected $post_type;
22-
13+
class WP_REST_Global_Styles_Controller extends WP_REST_Posts_Controller {
2314
/**
2415
* Constructor.
16+
*
2517
* @since 5.9.0
18+
* @since 6.5.0 Extends class from WP_REST_Posts_Controller.
19+
*
20+
* @param string $post_type Post type.
2621
*/
27-
public function __construct() {
28-
$this->namespace = 'wp/v2';
29-
$this->rest_base = 'global-styles';
30-
$this->post_type = 'wp_global_styles';
22+
public function __construct( $post_type ) {
23+
$this->post_type = $post_type;
24+
$obj = get_post_type_object( $post_type );
25+
$this->rest_base = ! empty( $obj->rest_base ) ? $obj->rest_base : $obj->name;
26+
$this->namespace = ! empty( $obj->rest_namespace ) ? $obj->rest_namespace : 'wp/v2';
3127
}
3228

3329
/**
@@ -194,7 +190,7 @@ public function get_item_permissions_check( $request ) {
194190
* @param WP_Post $post Post object.
195191
* @return bool Whether the post can be read.
196192
*/
197-
protected function check_read_permission( $post ) {
193+
public function check_read_permission( $post ) {
198194
return current_user_can( 'read_post', $post->ID );
199195
}
200196

@@ -241,18 +237,6 @@ public function update_item_permissions_check( $request ) {
241237
return true;
242238
}
243239

244-
/**
245-
* Checks if a global style can be edited.
246-
*
247-
* @since 5.9.0
248-
*
249-
* @param WP_Post $post Post object.
250-
* @return bool Whether the post can be edited.
251-
*/
252-
protected function check_update_permission( $post ) {
253-
return current_user_can( 'edit_post', $post->ID );
254-
}
255-
256240
/**
257241
* Updates a single global style config.
258242
*
@@ -407,7 +391,7 @@ public function prepare_item_for_response( $post, $request ) {
407391
$links = $this->prepare_links( $post->ID );
408392
$response->add_links( $links );
409393
if ( ! empty( $links['self']['href'] ) ) {
410-
$actions = $this->get_available_actions();
394+
$actions = $this->get_available_actions( $post, $request );
411395
$self = $links['self']['href'];
412396
foreach ( $actions as $rel ) {
413397
$response->add_link( $rel, $self );
@@ -431,9 +415,12 @@ protected function prepare_links( $id ) {
431415
$base = sprintf( '%s/%s', $this->namespace, $this->rest_base );
432416

433417
$links = array(
434-
'self' => array(
418+
'self' => array(
435419
'href' => rest_url( trailingslashit( $base ) . $id ),
436420
),
421+
'about' => array(
422+
'href' => rest_url( 'wp/v2/types/' . $this->post_type ),
423+
),
437424
);
438425

439426
if ( post_type_supports( $this->post_type, 'revisions' ) ) {
@@ -457,10 +444,10 @@ protected function prepare_links( $id ) {
457444
*
458445
* @return array List of link relations.
459446
*/
460-
protected function get_available_actions() {
447+
protected function get_available_actions( $post, $request ) {
461448
$rels = array();
462449

463-
$post_type = get_post_type_object( $this->post_type );
450+
$post_type = get_post_type_object( $post->post_type );
464451
if ( current_user_can( $post_type->cap->publish_posts ) ) {
465452
$rels[] = 'https://api.w.org/action-publish';
466453
}
@@ -472,21 +459,6 @@ protected function get_available_actions() {
472459
return $rels;
473460
}
474461

475-
/**
476-
* Overwrites the default protected title format.
477-
*
478-
* By default, WordPress will show password protected posts with a title of
479-
* "Protected: %s", as the REST API communicates the protected status of a post
480-
* in a machine readable format, we remove the "Protected: " prefix.
481-
*
482-
* @since 5.9.0
483-
*
484-
* @return string Protected title format.
485-
*/
486-
public function protected_title_format() {
487-
return '%s';
488-
}
489-
490462
/**
491463
* Retrieves the query params for the global styles collection.
492464
*

src/wp-includes/rest-api/endpoints/class-wp-rest-global-styles-revisions-controller.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,21 @@ class WP_REST_Global_Styles_Revisions_Controller extends WP_REST_Revisions_Contr
3535
* Constructor.
3636
*
3737
* @since 6.3.0
38+
* @since 6.5.0 Extends class from WP_REST_Revisions_Controller.
3839
*/
39-
public function __construct() {
40-
parent::__construct( 'wp_global_styles' );
41-
$this->parent_controller = new WP_REST_Global_Styles_Controller();
40+
public function __construct( $parent_post_type ) {
41+
parent::__construct( $parent_post_type );
42+
$post_type_object = get_post_type_object( $parent_post_type );
43+
$parent_controller = $post_type_object->get_rest_controller();
44+
45+
if ( ! $parent_controller ) {
46+
$parent_controller = new WP_REST_Global_Styles_Controller( $parent_post_type );
47+
}
48+
49+
$this->parent_controller = $parent_controller;
4250
$this->rest_base = 'revisions';
43-
$this->parent_base = $this->parent_controller->rest_base;
44-
$this->namespace = $this->parent_controller->namespace;
51+
$this->parent_base = ! empty( $post_type_object->rest_base ) ? $post_type_object->rest_base : $post_type_object->name;
52+
$this->namespace = ! empty( $post_type_object->rest_namespace ) ? $post_type_object->rest_namespace : 'wp/v2';
4553
}
4654

4755
/**

0 commit comments

Comments
 (0)