diff --git a/src/wp-includes/post.php b/src/wp-includes/post.php index 69a63625524d1..fc97423e64888 100644 --- a/src/wp-includes/post.php +++ b/src/wp-includes/post.php @@ -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', @@ -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', diff --git a/src/wp-includes/rest-api.php b/src/wp-includes/rest-api.php index 2631a6663fe92..2dead4ad8f2f0 100644 --- a/src/wp-includes/rest-api.php +++ b/src/wp-includes/rest-api.php @@ -323,14 +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(); - // Settings. $controller = new WP_REST_Settings_Controller(); $controller->register_routes(); 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 9837f535e268d..670dd401efd14 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 @@ -10,26 +10,7 @@ /** * Base Global Styles REST API Controller. */ -class WP_REST_Global_Styles_Controller extends WP_REST_Controller { - - /** - * Post type. - * - * @since 5.9.0 - * @var string - */ - protected $post_type; - - /** - * Constructor. - * @since 5.9.0 - */ - public function __construct() { - $this->namespace = 'wp/v2'; - $this->rest_base = 'global-styles'; - $this->post_type = 'wp_global_styles'; - } - +class WP_REST_Global_Styles_Controller extends WP_REST_Posts_Controller { /** * Registers the controllers routes. * @@ -125,34 +106,6 @@ public function _sanitize_global_styles_callback( $id_or_stylesheet ) { return urldecode( $id_or_stylesheet ); } - /** - * Get the post, if the ID is valid. - * - * @since 5.9.0 - * - * @param int $id Supplied ID. - * @return WP_Post|WP_Error Post object if ID is valid, WP_Error otherwise. - */ - protected function get_post( $id ) { - $error = new WP_Error( - 'rest_global_styles_not_found', - __( 'No global styles config exist with that id.' ), - array( 'status' => 404 ) - ); - - $id = (int) $id; - if ( $id <= 0 ) { - return $error; - } - - $post = get_post( $id ); - if ( empty( $post ) || empty( $post->ID ) || $this->post_type !== $post->post_type ) { - return $error; - } - - return $post; - } - /** * Checks if a given request has access to read a single global style. * @@ -194,7 +147,7 @@ public function get_item_permissions_check( $request ) { * @param WP_Post $post Post object. * @return bool Whether the post can be read. */ - protected function check_read_permission( $post ) { + public function check_read_permission( $post ) { return current_user_can( 'read_post', $post->ID ); } @@ -241,18 +194,6 @@ public function update_item_permissions_check( $request ) { return true; } - /** - * Checks if a global style can be edited. - * - * @since 5.9.0 - * - * @param WP_Post $post Post object. - * @return bool Whether the post can be edited. - */ - protected function check_update_permission( $post ) { - return current_user_can( 'edit_post', $post->ID ); - } - /** * Updates a single global style config. * @@ -407,7 +348,7 @@ public function prepare_item_for_response( $post, $request ) { $links = $this->prepare_links( $post->ID ); $response->add_links( $links ); if ( ! empty( $links['self']['href'] ) ) { - $actions = $this->get_available_actions(); + $actions = $this->get_available_actions( $post, $request ); $self = $links['self']['href']; foreach ( $actions as $rel ) { $response->add_link( $rel, $self ); @@ -431,9 +372,12 @@ protected function prepare_links( $id ) { $base = sprintf( '%s/%s', $this->namespace, $this->rest_base ); $links = array( - 'self' => array( + 'self' => array( 'href' => rest_url( trailingslashit( $base ) . $id ), ), + 'about' => array( + 'href' => rest_url( 'wp/v2/types/' . $this->post_type ), + ), ); if ( post_type_supports( $this->post_type, 'revisions' ) ) { @@ -457,10 +401,10 @@ protected function prepare_links( $id ) { * * @return array List of link relations. */ - protected function get_available_actions() { + protected function get_available_actions( $post, $request ) { $rels = array(); - $post_type = get_post_type_object( $this->post_type ); + $post_type = get_post_type_object( $post->post_type ); if ( current_user_can( $post_type->cap->publish_posts ) ) { $rels[] = 'https://api.w.org/action-publish'; } @@ -472,21 +416,6 @@ protected function get_available_actions() { return $rels; } - /** - * Overwrites the default protected title format. - * - * By default, WordPress will show password protected posts with a title of - * "Protected: %s", as the REST API communicates the protected status of a post - * in a machine readable format, we remove the "Protected: " prefix. - * - * @since 5.9.0 - * - * @return string Protected title format. - */ - public function protected_title_format() { - return '%s'; - } - /** * Retrieves the query params for the global styles collection. * 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 07050a0515e17..8f3734347f165 100644 --- a/tests/phpunit/tests/rest-api/rest-global-styles-controller.php +++ b/tests/phpunit/tests/rest-api/rest-global-styles-controller.php @@ -225,7 +225,7 @@ public function data_get_theme_item_invalid_theme_dirname() { // Themes deep in subdirectories. '2 subdirectories deep' => array( 'theme_dirname' => 'subdir/subsubdir/mytheme', - 'expected' => 'rest_global_styles_not_found', + 'expected' => 'rest_post_invalid_id', ), ); } @@ -322,7 +322,7 @@ public function test_get_item_invalid_post() { wp_set_current_user( self::$admin_id ); $request = new WP_REST_Request( 'GET', '/wp/v2/global-styles/' . self::$post_id ); $response = rest_get_server()->dispatch( $request ); - $this->assertErrorResponse( 'rest_global_styles_not_found', $response, 404 ); + $this->assertErrorResponse( 'rest_post_invalid_id', $response, 404 ); } /** @@ -431,7 +431,7 @@ public function test_update_item_invalid_post() { wp_set_current_user( self::$admin_id ); $request = new WP_REST_Request( 'PUT', '/wp/v2/global-styles/' . self::$post_id ); $response = rest_get_server()->dispatch( $request ); - $this->assertErrorResponse( 'rest_global_styles_not_found', $response, 404 ); + $this->assertErrorResponse( 'rest_post_invalid_id', $response, 404 ); } /** diff --git a/tests/phpunit/tests/rest-api/rest-schema-setup.php b/tests/phpunit/tests/rest-api/rest-schema-setup.php index 3ff35260f0fb8..e2d34c559ffe2 100644 --- a/tests/phpunit/tests/rest-api/rest-schema-setup.php +++ b/tests/phpunit/tests/rest-api/rest-schema-setup.php @@ -134,6 +134,8 @@ public function test_expected_routes_in_schema() { '/wp/v2/comments', '/wp/v2/comments/(?P[\\d]+)', '/wp/v2/global-styles/(?P[\/\w-]+)', + '/wp/v2/global-styles/(?P[\d]+)/autosaves', + '/wp/v2/global-styles/(?P[\d]+)/autosaves/(?P[\d]+)', '/wp/v2/global-styles/(?P[\d]+)/revisions', '/wp/v2/global-styles/(?P[\d]+)/revisions/(?P[\d]+)', '/wp/v2/global-styles/themes/(?P[\/\s%\w\.\(\)\[\]\@_\-]+)/variations',