From 36da826901d82535ecb14591fe59d3f596653726 Mon Sep 17 00:00:00 2001 From: Tonya Mork Date: Wed, 1 Feb 2023 18:36:08 +0000 Subject: [PATCH] REST API: Declare 'edit_css' capability in links within `WP_REST_Global_Styles_Controller`. Updates the Global Styles endpoint to expose the `'edit_css'` capability via action links. References: * [https://github.com/WordPress/gutenberg/pull/46815 Gutenberg PR 46815] Part of an effort to hide custom CSS setting for users without `'edit_css'` capability. Follow-up to [52342], [52051]. Props mamaduka, dsas, glendaviesnz, mmtr86, talldanwp, timothyblynjacobs. Fixes #57526. git-svn-id: https://develop.svn.wordpress.org/trunk@55177 602fd350-edb4-49c9-b593-d223f7449a82 --- ...class-wp-rest-global-styles-controller.php | 5 +++++ .../rest-global-styles-controller.php | 19 +++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-global-styles-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-global-styles-controller.php index 9ce931398f383..b504e23c35ba6 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-global-styles-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-global-styles-controller.php @@ -431,6 +431,7 @@ protected function prepare_links( $id ) { * Get the link relations available for the post and current user. * * @since 5.9.0 + * @since 6.2.0 Added 'edit-css' action. * * @return array List of link relations. */ @@ -442,6 +443,10 @@ protected function get_available_actions() { $rels[] = 'https://api.w.org/action-publish'; } + if ( current_user_can( 'edit_css' ) ) { + $rels[] = 'https://api.w.org/action-edit-css'; + } + return $rels; } diff --git a/tests/phpunit/tests/rest-api/rest-global-styles-controller.php b/tests/phpunit/tests/rest-api/rest-global-styles-controller.php index b7a5617a11d9c..8fc86d2205c7f 100644 --- a/tests/phpunit/tests/rest-api/rest-global-styles-controller.php +++ b/tests/phpunit/tests/rest-api/rest-global-styles-controller.php @@ -513,4 +513,23 @@ public function test_get_theme_items() { ); $this->assertSameSetsWithIndex( $data, $expected ); } + + /** + * @covers WP_REST_Global_Styles_Controller::get_available_actions + */ + public function test_assign_edit_css_action_admin() { + wp_set_current_user( self::$admin_id ); + + $request = new WP_REST_Request( 'GET', '/wp/v2/global-styles/' . self::$global_styles_id ); + $request->set_param( 'context', 'edit' ); + $response = rest_do_request( $request ); + $links = $response->get_links(); + + // Admins can only edit css on single site. + if ( is_multisite() ) { + $this->assertArrayNotHasKey( 'https://api.w.org/action-edit-css', $links ); + } else { + $this->assertArrayHasKey( 'https://api.w.org/action-edit-css', $links ); + } + } }