Skip to content

Commit a3078eb

Browse files
committed
First commit pulling over changes from WordPress/gutenberg#65071
1 parent 8f90a7e commit a3078eb

File tree

4 files changed

+53
-33
lines changed

4 files changed

+53
-33
lines changed

src/wp-admin/edit-form-blocks.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ static function ( $classes ) {
6767
array( '/wp/v2/settings', 'OPTIONS' ),
6868
'/wp/v2/global-styles/themes/' . get_stylesheet(),
6969
'/wp/v2/themes?context=edit&status=active',
70-
'/wp/v2/global-styles/' . WP_Theme_JSON_Resolver::get_user_global_styles_post_id() . '?context=edit',
70+
'/wp/v2/global-styles/' . WP_Theme_JSON_Resolver::get_user_global_styles_post_id(),
7171
);
7272

7373
block_editor_rest_api_preload( $preload_paths, $block_editor_context );

src/wp-includes/post.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,7 @@ function create_initial_post_types() {
489489
'revisions_rest_controller_class' => 'WP_REST_Global_Styles_Revisions_Controller',
490490
'late_route_registration' => true,
491491
'capabilities' => array(
492-
'read' => 'edit_theme_options',
492+
'read' => 'edit_posts',
493493
'create_posts' => 'edit_theme_options',
494494
'edit_posts' => 'edit_theme_options',
495495
'edit_published_posts' => 'edit_theme_options',

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

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -509,26 +509,33 @@ public function get_item_schema() {
509509
* Checks if a given request has access to read a single theme global styles config.
510510
*
511511
* @since 5.9.0
512+
* @since 6.7.0 Allow users with edit post capabilities to view theme global styles.
512513
*
513514
* @param WP_REST_Request $request Full details about the request.
514515
* @return true|WP_Error True if the request has read access for the item, WP_Error object otherwise.
515516
*/
516517
public function get_theme_item_permissions_check( $request ) {
517518
/*
518-
* Verify if the current user has edit_theme_options capability.
519-
* This capability is required to edit/view/delete global styles.
519+
* Verify if the current user has edit_posts capability.
520+
* This capability is required to view global styles.
520521
*/
521-
if ( ! current_user_can( 'edit_theme_options' ) ) {
522-
return new WP_Error(
523-
'rest_cannot_manage_global_styles',
524-
__( 'Sorry, you are not allowed to access the global styles on this site.' ),
525-
array(
526-
'status' => rest_authorization_required_code(),
527-
)
528-
);
522+
if ( current_user_can( 'edit_posts' ) ) {
523+
return true;
529524
}
530525

531-
return true;
526+
foreach ( get_post_types( array( 'show_in_rest' => true ), 'objects' ) as $post_type ) {
527+
if ( current_user_can( $post_type->cap->edit_posts ) ) {
528+
return true;
529+
}
530+
}
531+
532+
return new WP_Error(
533+
'rest_cannot_read_global_styles',
534+
__( 'Sorry, you are not allowed to access the global styles on this site.' ),
535+
array(
536+
'status' => rest_authorization_required_code(),
537+
)
538+
);
532539
}
533540

534541
/**
@@ -589,26 +596,13 @@ public function get_theme_item( $request ) {
589596
* Checks if a given request has access to read a single theme global styles config.
590597
*
591598
* @since 6.0.0
599+
* @since 6.7.0 Allow users with edit post capabilities to view theme global styles.
592600
*
593601
* @param WP_REST_Request $request Full details about the request.
594602
* @return true|WP_Error True if the request has read access for the item, WP_Error object otherwise.
595603
*/
596604
public function get_theme_items_permissions_check( $request ) {
597-
/*
598-
* Verify if the current user has edit_theme_options capability.
599-
* This capability is required to edit/view/delete global styles.
600-
*/
601-
if ( ! current_user_can( 'edit_theme_options' ) ) {
602-
return new WP_Error(
603-
'rest_cannot_manage_global_styles',
604-
__( 'Sorry, you are not allowed to access the global styles on this site.' ),
605-
array(
606-
'status' => rest_authorization_required_code(),
607-
)
608-
);
609-
}
610-
611-
return true;
605+
return $this->get_theme_item_permissions_check( $request );
612606
}
613607

614608
/**
@@ -632,7 +626,7 @@ public function get_theme_items( $request ) {
632626
);
633627
}
634628

635-
$response = array();
629+
$response = array();
636630

637631
// Register theme-defined variations e.g. from block style variation partials under `/styles`.
638632
$partials = WP_Theme_JSON_Resolver::get_style_variations( 'block' );

tests/phpunit/tests/rest-api/rest-global-styles-controller.php

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ class WP_REST_Global_Styles_Controller_Test extends WP_Test_REST_Controller_Test
1515
* @var int
1616
*/
1717
protected static $admin_id;
18-
18+
/**
19+
* @var int
20+
*/
21+
protected static $editor_id;
1922
/**
2023
* @var int
2124
*/
@@ -54,6 +57,12 @@ public static function wpSetupBeforeClass( $factory ) {
5457
)
5558
);
5659

60+
self::$editor_id = $factory->user->create(
61+
array(
62+
'role' => 'editor',
63+
)
64+
);
65+
5766
self::$subscriber_id = $factory->user->create(
5867
array(
5968
'role' => 'subscriber',
@@ -264,18 +273,35 @@ public function test_get_theme_item_no_user() {
264273
wp_set_current_user( 0 );
265274
$request = new WP_REST_Request( 'GET', '/wp/v2/global-styles/themes/tt1-blocks' );
266275
$response = rest_get_server()->dispatch( $request );
267-
$this->assertErrorResponse( 'rest_cannot_manage_global_styles', $response, 401 );
276+
$this->assertErrorResponse( 'rest_cannot_read_global_styles', $response, 401 );
268277
}
269278

270279
/**
271280
* @covers WP_REST_Global_Styles_Controller::get_theme_item
272281
* @ticket 54516
282+
* @ticket 62042
273283
*/
274-
public function test_get_theme_item_permission_check() {
284+
public function test_get_theme_item_subscriber_permission_check() {
275285
wp_set_current_user( self::$subscriber_id );
276286
$request = new WP_REST_Request( 'GET', '/wp/v2/global-styles/themes/tt1-blocks' );
277287
$response = rest_get_server()->dispatch( $request );
278-
$this->assertErrorResponse( 'rest_cannot_manage_global_styles', $response, 403 );
288+
$this->assertErrorResponse( 'rest_cannot_read_global_styles', $response, 403 );
289+
}
290+
291+
/**
292+
* @covers WP_REST_Global_Styles_Controller_Gutenberg::get_theme_item
293+
* @ticket 62042
294+
*/
295+
public function test_get_theme_item_editor_permission_check() {
296+
wp_set_current_user( self::$editor_id );
297+
$request = new WP_REST_Request( 'GET', '/wp/v2/global-styles/themes/tt1-blocks' );
298+
$response = rest_get_server()->dispatch( $request );
299+
// Checks that the response has the expected keys.
300+
$data = $response->get_data();
301+
$links = $response->get_links();
302+
$this->assertArrayHasKey( 'settings', $data, 'Data does not have "settings" key' );
303+
$this->assertArrayHasKey( 'styles', $data, 'Data does not have "styles" key' );
304+
$this->assertArrayHasKey( 'self', $links, 'Links do not have a "self" key' );
279305
}
280306

281307
/**

0 commit comments

Comments
 (0)