From c739cebddae1a3d66fbf195d8a0434d188fe2ce2 Mon Sep 17 00:00:00 2001 From: ntsekouras Date: Fri, 21 Jan 2022 09:52:31 +0200 Subject: [PATCH 1/6] [Global Styles]: Add REST API endpoint to fetch variations --- ...utenberg-rest-global-styles-controller.php | 448 ------------- ...class-wp-rest-global-styles-controller.php | 587 ++++++++++++++++++ ...utenberg-rest-global-styles-controller.php | 96 +++ lib/compat/wordpress-6.0/rest-api.php | 15 + lib/load.php | 4 +- lib/rest-api.php | 9 - 6 files changed, 701 insertions(+), 458 deletions(-) delete mode 100644 lib/compat/wordpress-5.9/class-gutenberg-rest-global-styles-controller.php create mode 100644 lib/compat/wordpress-5.9/class-wp-rest-global-styles-controller.php create mode 100644 lib/compat/wordpress-6.0/class-gutenberg-rest-global-styles-controller.php create mode 100644 lib/compat/wordpress-6.0/rest-api.php diff --git a/lib/compat/wordpress-5.9/class-gutenberg-rest-global-styles-controller.php b/lib/compat/wordpress-5.9/class-gutenberg-rest-global-styles-controller.php deleted file mode 100644 index 67b48ed402b69f..00000000000000 --- a/lib/compat/wordpress-5.9/class-gutenberg-rest-global-styles-controller.php +++ /dev/null @@ -1,448 +0,0 @@ -namespace = 'wp/v2'; - $this->rest_base = 'global-styles'; - } - - /** - * Registers the controllers routes. - * - * @return void - */ - public function register_routes() { - // List themes global styles. - register_rest_route( - $this->namespace, - '/' . $this->rest_base . '/themes/(?P[\/\s%\w\.\(\)\[\]\@_\-]+)', - array( - array( - 'methods' => WP_REST_Server::READABLE, - 'callback' => array( $this, 'get_theme_item' ), - 'permission_callback' => array( $this, 'get_theme_item_permissions_check' ), - 'args' => array( - 'stylesheet' => array( - 'description' => __( 'The theme identifier', 'gutenberg' ), - 'type' => 'string', - ), - ), - ), - ) - ); - - // Lists/updates a single global style variation based on the given id. - register_rest_route( - $this->namespace, - '/' . $this->rest_base . '/(?P[\/\s%\w\.\(\)\[\]\@_\-]+)', - array( - array( - 'methods' => WP_REST_Server::READABLE, - 'callback' => array( $this, 'get_item' ), - 'permission_callback' => array( $this, 'get_item_permissions_check' ), - 'args' => array( - 'id' => array( - 'description' => __( 'The id of the global style variation', 'gutenberg' ), - 'type' => 'string', - 'sanitize_callback' => array( $this, '_sanitize_global_styles_callback' ), - ), - ), - ), - array( - 'methods' => WP_REST_Server::EDITABLE, - 'callback' => array( $this, 'update_item' ), - 'permission_callback' => array( $this, 'update_item_permissions_check' ), - 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ), - ), - 'schema' => array( $this, 'get_public_item_schema' ), - ) - ); - } - - /** - * Sanitize the global styles ID or stylesheet to decode endpoint. - * For example, `wp/v2/global-styles/templatetwentytwo%200.4.0` - * would be decoded to `templatetwentytwo 0.4.0`. - * - * @since 5.9.0 - * - * @param string $id_or_stylesheet Global styles ID or stylesheet. - * @return string Sanitized global styles ID or stylesheet. - */ - public function _sanitize_global_styles_callback( $id_or_stylesheet ) { - return urldecode( $id_or_stylesheet ); - } - - /** - * Checks if the user has permissions to make the request. - * - * @return true|WP_Error True if the request has read access, WP_Error object otherwise. - */ - protected function permissions_check() { - // Verify if the current user has edit_theme_options capability. - // This capability is required to edit/view/delete templates. - if ( ! current_user_can( 'edit_theme_options' ) ) { - return new WP_Error( - 'rest_cannot_manage_global_styles', - __( 'Sorry, you are not allowed to access the global styles on this site.', 'gutenberg' ), - array( - 'status' => rest_authorization_required_code(), - ) - ); - } - - return true; - } - - /** - * Checks if a given request has access to read a single global styles config. - * - * @param WP_REST_Request $request Full details about the request. - * @return true|WP_Error True if the request has read access for the item, WP_Error object otherwise. - */ - public function get_item_permissions_check( $request ) { - return $this->permissions_check( $request ); - } - - /** - * Returns the given global styles config. - * - * @param WP_REST_Request $request The request instance. - * - * @return WP_REST_Response|WP_Error - */ - public function get_item( $request ) { - $post = get_post( $request['id'] ); - if ( ! $post || 'wp_global_styles' !== $post->post_type ) { - return new WP_Error( 'rest_global_styles_not_found', __( 'No global styles config exist with that id.', 'gutenberg' ), array( 'status' => 404 ) ); - } - - return $this->prepare_item_for_response( $post, $request ); - } - - /** - * Checks if a given request has access to write a single global styles config. - * - * @param WP_REST_Request $request Full details about the request. - * @return true|WP_Error True if the request has write access for the item, WP_Error object otherwise. - */ - public function update_item_permissions_check( $request ) { - return $this->permissions_check( $request ); - } - - /** - * Updates a single global style config. - * - * @param WP_REST_Request $request Full details about the request. - * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. - */ - public function update_item( $request ) { - $post = get_post( $request['id'] ); - if ( ! $post || 'wp_global_styles' !== $post->post_type ) { - return new WP_Error( 'rest_global_styles_not_found', __( 'No global styles config exist with that id.', 'gutenberg' ), array( 'status' => 404 ) ); - } - - $changes = $this->prepare_item_for_database( $request ); - $result = wp_update_post( wp_slash( (array) $changes ), true ); - if ( is_wp_error( $result ) ) { - return $result; - } - - $post = get_post( $request['id'] ); - $fields_update = $this->update_additional_fields_for_object( $post, $request ); - if ( is_wp_error( $fields_update ) ) { - return $fields_update; - } - - return $this->prepare_item_for_response( - get_post( $request['id'] ), - $request - ); - } - - /** - * Prepares a single global styles config for update. - * - * @param WP_REST_Request $request Request object. - * @return stdClass Changes to pass to wp_update_post. - */ - protected function prepare_item_for_database( $request ) { - $changes = new stdClass(); - $changes->ID = $request['id']; - - $post = get_post( $request['id'] ); - $existing_config = array(); - if ( $post ) { - $existing_config = json_decode( $post->post_content, true ); - $json_decoding_error = json_last_error(); - if ( JSON_ERROR_NONE !== $json_decoding_error || ! isset( $existing_config['isGlobalStylesUserThemeJSON'] ) || - ! $existing_config['isGlobalStylesUserThemeJSON'] ) { - $existing_config = array(); - } - } - - if ( isset( $request['styles'] ) || isset( $request['settings'] ) ) { - $config = array(); - if ( isset( $request['styles'] ) ) { - $config['styles'] = $request['styles']; - } elseif ( isset( $existing_config['styles'] ) ) { - $config['styles'] = $existing_config['styles']; - } - if ( isset( $request['settings'] ) ) { - $config['settings'] = $request['settings']; - } elseif ( isset( $existing_config['settings'] ) ) { - $config['settings'] = $existing_config['settings']; - } - $config['isGlobalStylesUserThemeJSON'] = true; - $config['version'] = WP_Theme_JSON_Gutenberg::LATEST_SCHEMA; - $changes->post_content = wp_json_encode( $config ); - } - - // Post title. - if ( isset( $request['title'] ) ) { - if ( is_string( $request['title'] ) ) { - $changes->post_title = $request['title']; - } elseif ( ! empty( $request['title']['raw'] ) ) { - $changes->post_title = $request['title']['raw']; - } - } - - return $changes; - } - - /** - * Prepare a global styles config output for response. - * - * @param WP_Post $post Global Styles post object. - * @param WP_REST_Request $request Request object. - * - * @return WP_REST_Response $data - */ - public function prepare_item_for_response( $post, $request ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable - $raw_config = json_decode( $post->post_content, true ); - $is_global_styles_user_theme_json = isset( $raw_config['isGlobalStylesUserThemeJSON'] ) && true === $raw_config['isGlobalStylesUserThemeJSON']; - $config = array(); - if ( $is_global_styles_user_theme_json ) { - $config = ( new WP_Theme_JSON_Gutenberg( $raw_config, 'custom' ) )->get_raw_data(); - } - - $fields = $this->get_fields_for_response( $request ); - - // Base fields for every post. - $data = array(); - - if ( rest_is_field_included( 'id', $fields ) ) { - $data['id'] = $post->ID; - } - - if ( rest_is_field_included( 'title', $fields ) ) { - $data['title'] = array(); - } - if ( rest_is_field_included( 'title.raw', $fields ) ) { - $data['title']['raw'] = $post->post_title; - } - if ( rest_is_field_included( 'title.rendered', $fields ) ) { - add_filter( 'protected_title_format', array( $this, 'protected_title_format' ) ); - - $data['title']['rendered'] = get_the_title( $post->ID ); - - remove_filter( 'protected_title_format', array( $this, 'protected_title_format' ) ); - } - - if ( rest_is_field_included( 'settings', $fields ) ) { - $data['settings'] = ! empty( $config['settings'] ) && $is_global_styles_user_theme_json ? $config['settings'] : new stdClass(); - } - - if ( rest_is_field_included( 'styles', $fields ) ) { - $data['styles'] = ! empty( $config['styles'] ) && $is_global_styles_user_theme_json ? $config['styles'] : new stdClass(); - } - - $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; - $data = $this->add_additional_fields_to_object( $data, $request ); - $data = $this->filter_response_by_context( $data, $context ); - - // Wrap the data in a response object. - $response = rest_ensure_response( $data ); - - $links = $this->prepare_links( $post->ID ); - $response->add_links( $links ); - if ( ! empty( $links['self']['href'] ) ) { - $actions = $this->get_available_actions(); - $self = $links['self']['href']; - foreach ( $actions as $rel ) { - $response->add_link( $rel, $self ); - } - } - - return $response; - } - - - /** - * Prepares links for the request. - * - * @param integer $id ID. - * @return array Links for the given post. - */ - protected function prepare_links( $id ) { - $base = sprintf( '%s/%s', $this->namespace, $this->rest_base ); - - $links = array( - 'self' => array( - 'href' => rest_url( trailingslashit( $base ) . $id ), - ), - 'collection' => array( - 'href' => rest_url( $base ), - ), - ); - - return $links; - } - - /** - * Get the link relations available for the post and current user. - * - * @return array List of link relations. - */ - protected function get_available_actions() { - $rels = array(); - - $post_type = get_post_type_object( 'wp_global_styles' ); - if ( current_user_can( $post_type->cap->publish_posts ) ) { - $rels[] = 'https://api.w.org/action-publish'; - } - - 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. - * - * @return string Protected title format. - */ - public function protected_title_format() { - return '%s'; - } - - /** - * Retrieves the query params for the global styles collection. - * - * @return array Collection parameters. - */ - public function get_collection_params() { - return array(); - } - - /** - * Retrieves the global styles type' schema, conforming to JSON Schema. - * - * @return array Item schema data. - */ - public function get_item_schema() { - if ( $this->schema ) { - return $this->add_additional_fields_schema( $this->schema ); - } - - $schema = array( - '$schema' => 'http://json-schema.org/draft-04/schema#', - 'title' => 'wp_global_styles', - 'type' => 'object', - 'properties' => array( - 'id' => array( - 'description' => __( 'ID of global styles config.', 'gutenberg' ), - 'type' => 'string', - 'context' => array( 'embed', 'view', 'edit' ), - 'readonly' => true, - ), - 'styles' => array( - 'description' => __( 'Global styles.', 'gutenberg' ), - 'type' => array( 'object' ), - 'context' => array( 'view', 'edit' ), - ), - 'settings' => array( - 'description' => __( 'Global settings.', 'gutenberg' ), - 'type' => array( 'object' ), - 'context' => array( 'view', 'edit' ), - ), - 'title' => array( - 'description' => __( 'Title of the global styles variation.', 'gutenberg' ), - 'type' => array( 'object', 'string' ), - 'default' => '', - 'context' => array( 'embed', 'view', 'edit' ), - 'properties' => array( - 'raw' => array( - 'description' => __( 'Title for the global styles variation, as it exists in the database.', 'gutenberg' ), - 'type' => 'string', - 'context' => array( 'view', 'edit', 'embed' ), - ), - 'rendered' => array( - 'description' => __( 'HTML title for the post, transformed for display.', 'gutenberg' ), - 'type' => 'string', - 'context' => array( 'view', 'edit', 'embed' ), - 'readonly' => true, - ), - ), - ), - ), - ); - - $this->schema = $schema; - - return $this->add_additional_fields_schema( $this->schema ); - } - - /** - * Checks if a given request has access to read a single theme global styles config. - * - * @param WP_REST_Request $request Full details about the request. - * @return true|WP_Error True if the request has read access for the item, WP_Error object otherwise. - */ - public function get_theme_item_permissions_check( $request ) { - return $this->permissions_check( $request ); - } - - /** - * Returns the given theme global styles config. - * - * @param WP_REST_Request $request The request instance. - * - * @return WP_REST_Response|WP_Error - */ - public function get_theme_item( $request ) { - if ( wp_get_theme()->get_stylesheet() !== $request['stylesheet'] ) { - // This endpoint only supports the active theme for now. - return new WP_Error( - 'rest_theme_not_found', - __( 'Theme not found.', 'gutenberg' ), - array( 'status' => 404 ) - ); - } - - $theme = WP_Theme_JSON_Resolver_Gutenberg::get_merged_data( 'theme' ); - $styles = $theme->get_raw_data()['styles']; - $settings = $theme->get_settings(); - $result = array( - 'settings' => $settings, - 'styles' => $styles, - ); - $response = rest_ensure_response( $result ); - - return $response; - } -} diff --git a/lib/compat/wordpress-5.9/class-wp-rest-global-styles-controller.php b/lib/compat/wordpress-5.9/class-wp-rest-global-styles-controller.php new file mode 100644 index 00000000000000..9f5bb3fcf2a8da --- /dev/null +++ b/lib/compat/wordpress-5.9/class-wp-rest-global-styles-controller.php @@ -0,0 +1,587 @@ +namespace = 'wp/v2'; + $this->rest_base = 'global-styles'; + $this->post_type = 'wp_global_styles'; + } + + /** + * Registers the controllers routes. + * + * @since 5.9.0 + * + * @return void + */ + public function register_routes() { + // List themes global styles. + register_rest_route( + $this->namespace, + // The route. + sprintf( + '/%s/themes/(?P%s)', + $this->rest_base, + // Matches theme's directory: `/themes///` or `/themes//`. + // Excludes invalid directory name characters: `/:<>*?"|`. + '[^\/:<>\*\?"\|]+(?:\/[^\/:<>\*\?"\|]+)?' + ), + array( + array( + 'methods' => WP_REST_Server::READABLE, + 'callback' => array( $this, 'get_theme_item' ), + 'permission_callback' => array( $this, 'get_theme_item_permissions_check' ), + 'args' => array( + 'stylesheet' => array( + 'description' => __( 'The theme identifier' ), + 'type' => 'string', + 'sanitize_callback' => array( $this, '_sanitize_global_styles_callback' ), + ), + ), + ), + ) + ); + + // Lists/updates a single global style variation based on the given id. + register_rest_route( + $this->namespace, + '/' . $this->rest_base . '/(?P[\/\w-]+)', + array( + array( + 'methods' => WP_REST_Server::READABLE, + 'callback' => array( $this, 'get_item' ), + 'permission_callback' => array( $this, 'get_item_permissions_check' ), + 'args' => array( + 'id' => array( + 'description' => __( 'The id of a template' ), + 'type' => 'string', + 'sanitize_callback' => array( $this, '_sanitize_global_styles_callback' ), + ), + ), + ), + array( + 'methods' => WP_REST_Server::EDITABLE, + 'callback' => array( $this, 'update_item' ), + 'permission_callback' => array( $this, 'update_item_permissions_check' ), + 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ), + ), + 'schema' => array( $this, 'get_public_item_schema' ), + ) + ); + } + + /** + * Sanitize the global styles ID or stylesheet to decode endpoint. + * For example, `wp/v2/global-styles/twentytwentytwo%200.4.0` + * would be decoded to `twentytwentytwo 0.4.0`. + * + * @since 5.9.0 + * + * @param string $id_or_stylesheet Global styles ID or stylesheet. + * @return string Sanitized global styles ID or stylesheet. + */ + public function _sanitize_global_styles_callback( $id_or_stylesheet ) { + return urldecode( $id_or_stylesheet ); + } + + /** + * Checks if a given request has access to read a single global style. + * + * @since 5.9.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 ) { + $post = $this->get_post( $request['id'] ); + if ( is_wp_error( $post ) ) { + return $post; + } + + if ( 'edit' === $request['context'] && $post && ! $this->check_update_permission( $post ) ) { + return new WP_Error( + 'rest_forbidden_context', + __( 'Sorry, you are not allowed to edit this global style.' ), + array( 'status' => rest_authorization_required_code() ) + ); + } + + if ( ! $this->check_read_permission( $post ) ) { + return new WP_Error( + 'rest_cannot_view', + __( 'Sorry, you are not allowed to view this global style.' ), + array( 'status' => rest_authorization_required_code() ) + ); + } + + return true; + } + + /** + * Checks if a global style can be read. + * + * @since 5.9.0 + * + * @param WP_Post $post Post object. + * @return bool Whether the post can be read. + */ + protected function check_read_permission( $post ) { + return current_user_can( 'read_post', $post->ID ); + } + + /** + * Returns the given global styles config. + * + * @since 5.9.0 + * + * @param WP_REST_Request $request The request instance. + * + * @return WP_REST_Response|WP_Error + */ + public function get_item( $request ) { + $post = $this->get_post( $request['id'] ); + if ( is_wp_error( $post ) ) { + return $post; + } + + return $this->prepare_item_for_response( $post, $request ); + } + + /** + * Checks if a given request has access to write a single global styles config. + * + * @since 5.9.0 + * + * @param WP_REST_Request $request Full details about the request. + * @return true|WP_Error True if the request has write access for the item, WP_Error object otherwise. + */ + public function update_item_permissions_check( $request ) { + $post = $this->get_post( $request['id'] ); + if ( is_wp_error( $post ) ) { + return $post; + } + + if ( $post && ! $this->check_update_permission( $post ) ) { + return new WP_Error( + 'rest_cannot_edit', + __( 'Sorry, you are not allowed to edit this global style.' ), + array( 'status' => rest_authorization_required_code() ) + ); + } + + 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. + * + * @since 5.9.0 + * + * @param WP_REST_Request $request Full details about the request. + * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. + */ + public function update_item( $request ) { + $post_before = $this->get_post( $request['id'] ); + if ( is_wp_error( $post_before ) ) { + return $post_before; + } + + $changes = $this->prepare_item_for_database( $request ); + $result = wp_update_post( wp_slash( (array) $changes ), true, false ); + if ( is_wp_error( $result ) ) { + return $result; + } + + $post = get_post( $request['id'] ); + $fields_update = $this->update_additional_fields_for_object( $post, $request ); + if ( is_wp_error( $fields_update ) ) { + return $fields_update; + } + + wp_after_insert_post( $post, true, $post_before ); + + $response = $this->prepare_item_for_response( $post, $request ); + + return rest_ensure_response( $response ); + } + + /** + * Prepares a single global styles config for update. + * + * @since 5.9.0 + * + * @param WP_REST_Request $request Request object. + * @return stdClass Changes to pass to wp_update_post. + */ + protected function prepare_item_for_database( $request ) { + $changes = new stdClass(); + $changes->ID = $request['id']; + + $post = get_post( $request['id'] ); + $existing_config = array(); + if ( $post ) { + $existing_config = json_decode( $post->post_content, true ); + $json_decoding_error = json_last_error(); + if ( JSON_ERROR_NONE !== $json_decoding_error || ! isset( $existing_config['isGlobalStylesUserThemeJSON'] ) || + ! $existing_config['isGlobalStylesUserThemeJSON'] ) { + $existing_config = array(); + } + } + + if ( isset( $request['styles'] ) || isset( $request['settings'] ) ) { + $config = array(); + if ( isset( $request['styles'] ) ) { + $config['styles'] = $request['styles']; + } elseif ( isset( $existing_config['styles'] ) ) { + $config['styles'] = $existing_config['styles']; + } + if ( isset( $request['settings'] ) ) { + $config['settings'] = $request['settings']; + } elseif ( isset( $existing_config['settings'] ) ) { + $config['settings'] = $existing_config['settings']; + } + $config['isGlobalStylesUserThemeJSON'] = true; + $config['version'] = WP_Theme_JSON::LATEST_SCHEMA; + $changes->post_content = wp_json_encode( $config ); + } + + // Post title. + if ( isset( $request['title'] ) ) { + if ( is_string( $request['title'] ) ) { + $changes->post_title = $request['title']; + } elseif ( ! empty( $request['title']['raw'] ) ) { + $changes->post_title = $request['title']['raw']; + } + } + + return $changes; + } + + /** + * Prepare a global styles config output for response. + * + * @since 5.9.0 + * + * @param WP_Post $post Global Styles post object. + * @param WP_REST_Request $request Request object. + * @return WP_REST_Response Response object. + */ + public function prepare_item_for_response( $post, $request ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable + $raw_config = json_decode( $post->post_content, true ); + $is_global_styles_user_theme_json = isset( $raw_config['isGlobalStylesUserThemeJSON'] ) && true === $raw_config['isGlobalStylesUserThemeJSON']; + $config = array(); + if ( $is_global_styles_user_theme_json ) { + $config = ( new WP_Theme_JSON( $raw_config, 'custom' ) )->get_raw_data(); + } + + // Base fields for every post. + $data = array(); + $fields = $this->get_fields_for_response( $request ); + + if ( rest_is_field_included( 'id', $fields ) ) { + $data['id'] = $post->ID; + } + + if ( rest_is_field_included( 'title', $fields ) ) { + $data['title'] = array(); + } + if ( rest_is_field_included( 'title.raw', $fields ) ) { + $data['title']['raw'] = $post->post_title; + } + if ( rest_is_field_included( 'title.rendered', $fields ) ) { + add_filter( 'protected_title_format', array( $this, 'protected_title_format' ) ); + + $data['title']['rendered'] = get_the_title( $post->ID ); + + remove_filter( 'protected_title_format', array( $this, 'protected_title_format' ) ); + } + + if ( rest_is_field_included( 'settings', $fields ) ) { + $data['settings'] = ! empty( $config['settings'] ) && $is_global_styles_user_theme_json ? $config['settings'] : new stdClass(); + } + + if ( rest_is_field_included( 'styles', $fields ) ) { + $data['styles'] = ! empty( $config['styles'] ) && $is_global_styles_user_theme_json ? $config['styles'] : new stdClass(); + } + + $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; + $data = $this->add_additional_fields_to_object( $data, $request ); + $data = $this->filter_response_by_context( $data, $context ); + + // Wrap the data in a response object. + $response = rest_ensure_response( $data ); + + $links = $this->prepare_links( $post->ID ); + $response->add_links( $links ); + if ( ! empty( $links['self']['href'] ) ) { + $actions = $this->get_available_actions(); + $self = $links['self']['href']; + foreach ( $actions as $rel ) { + $response->add_link( $rel, $self ); + } + } + + return $response; + } + + /** + * 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; + } + + + /** + * Prepares links for the request. + * + * @since 5.9.0 + * + * @param integer $id ID. + * @return array Links for the given post. + */ + protected function prepare_links( $id ) { + $base = sprintf( '%s/%s', $this->namespace, $this->rest_base ); + + $links = array( + 'self' => array( + 'href' => rest_url( trailingslashit( $base ) . $id ), + ), + ); + + return $links; + } + + /** + * Get the link relations available for the post and current user. + * + * @since 5.9.0 + * + * @return array List of link relations. + */ + protected function get_available_actions() { + $rels = array(); + + $post_type = get_post_type_object( $this->post_type ); + if ( current_user_can( $post_type->cap->publish_posts ) ) { + $rels[] = 'https://api.w.org/action-publish'; + } + + 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. + * + * @since 5.9.0 + * + * @return array Collection parameters. + */ + public function get_collection_params() { + return array(); + } + + /** + * Retrieves the global styles type' schema, conforming to JSON Schema. + * + * @since 5.9.0 + * + * @return array Item schema data. + */ + public function get_item_schema() { + if ( $this->schema ) { + return $this->add_additional_fields_schema( $this->schema ); + } + + $schema = array( + '$schema' => 'http://json-schema.org/draft-04/schema#', + 'title' => $this->post_type, + 'type' => 'object', + 'properties' => array( + 'id' => array( + 'description' => __( 'ID of global styles config.' ), + 'type' => 'string', + 'context' => array( 'embed', 'view', 'edit' ), + 'readonly' => true, + ), + 'styles' => array( + 'description' => __( 'Global styles.' ), + 'type' => array( 'object' ), + 'context' => array( 'view', 'edit' ), + ), + 'settings' => array( + 'description' => __( 'Global settings.' ), + 'type' => array( 'object' ), + 'context' => array( 'view', 'edit' ), + ), + 'title' => array( + 'description' => __( 'Title of the global styles variation.' ), + 'type' => array( 'object', 'string' ), + 'default' => '', + 'context' => array( 'embed', 'view', 'edit' ), + 'properties' => array( + 'raw' => array( + 'description' => __( 'Title for the global styles variation, as it exists in the database.' ), + 'type' => 'string', + 'context' => array( 'view', 'edit', 'embed' ), + ), + 'rendered' => array( + 'description' => __( 'HTML title for the post, transformed for display.' ), + 'type' => 'string', + 'context' => array( 'view', 'edit', 'embed' ), + 'readonly' => true, + ), + ), + ), + ), + ); + + $this->schema = $schema; + + return $this->add_additional_fields_schema( $this->schema ); + } + + /** + * Checks if a given request has access to read a single theme global styles config. + * + * @since 5.9.0 + * + * @param WP_REST_Request $request Full details about the request. + * @return true|WP_Error True if the request has read access for the item, WP_Error object otherwise. + */ + public function get_theme_item_permissions_check( $request ) { + // Verify if the current user has edit_theme_options capability. + // This capability is required to edit/view/delete templates. + if ( ! current_user_can( 'edit_theme_options' ) ) { + return new WP_Error( + 'rest_cannot_manage_global_styles', + __( 'Sorry, you are not allowed to access the global styles on this site.' ), + array( + 'status' => rest_authorization_required_code(), + ) + ); + } + + return true; + } + + /** + * Returns the given theme global styles config. + * + * @since 5.9.0 + * + * @param WP_REST_Request $request The request instance. + * @return WP_REST_Response|WP_Error + */ + public function get_theme_item( $request ) { + if ( wp_get_theme()->get_stylesheet() !== $request['stylesheet'] ) { + // This endpoint only supports the active theme for now. + return new WP_Error( + 'rest_theme_not_found', + __( 'Theme not found.' ), + array( 'status' => 404 ) + ); + } + + $theme = WP_Theme_JSON_Resolver::get_merged_data( 'theme' ); + $data = array(); + $fields = $this->get_fields_for_response( $request ); + + if ( rest_is_field_included( 'settings', $fields ) ) { + $data['settings'] = $theme->get_settings(); + } + + if ( rest_is_field_included( 'styles', $fields ) ) { + $data['styles'] = $theme->get_raw_data()['styles']; + } + + $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; + $data = $this->add_additional_fields_to_object( $data, $request ); + $data = $this->filter_response_by_context( $data, $context ); + + $response = rest_ensure_response( $data ); + + $links = array( + 'self' => array( + 'href' => rest_url( sprintf( '%s/%s/themes/%s', $this->namespace, $this->rest_base, $request['stylesheet'] ) ), + ), + ); + + $response->add_links( $links ); + + return $response; + } + } +} diff --git a/lib/compat/wordpress-6.0/class-gutenberg-rest-global-styles-controller.php b/lib/compat/wordpress-6.0/class-gutenberg-rest-global-styles-controller.php new file mode 100644 index 00000000000000..b51f24bb98a576 --- /dev/null +++ b/lib/compat/wordpress-6.0/class-gutenberg-rest-global-styles-controller.php @@ -0,0 +1,96 @@ +namespace, + '/' . $this->rest_base . '/themes/(?P[\/\s%\w\.\(\)\[\]\@_\-]+)/variations', + array( + array( + 'methods' => WP_REST_Server::READABLE, + 'callback' => array( $this, 'get_theme_items' ), + 'permission_callback' => array( $this, 'get_theme_items_permissions_check' ), + 'args' => array( + 'stylesheet' => array( + 'description' => __( 'The theme identifier', 'gutenberg' ), + 'type' => 'string', + ), + ), + ), + ) + ); + + parent::register_routes(); + } + + /** + * Checks if a given request has access to read a single theme global styles config. + * + * @param WP_REST_Request $request Full details about the request. + * @return true|WP_Error True if the request has read access for the item, WP_Error object otherwise. + */ + public function get_theme_items_permissions_check( $request ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable + // Verify if the current user has edit_theme_options capability. + // This capability is required to edit/view/delete templates. + if ( ! current_user_can( 'edit_theme_options' ) ) { + return new WP_Error( + 'rest_cannot_manage_global_styles', + __( 'Sorry, you are not allowed to access the global styles on this site.', 'gutenberg' ), + array( + 'status' => rest_authorization_required_code(), + ) + ); + } + + return true; + } + + /** + * Returns the given theme global styles variations. + * + * @param WP_REST_Request $request The request instance. + * + * @return WP_REST_Response|WP_Error + */ + public function get_theme_items( $request ) { + if ( wp_get_theme()->get_stylesheet() !== $request['stylesheet'] ) { + // This endpoint only supports the active theme for now. + return new WP_Error( + 'rest_theme_not_found', + __( 'Theme not found.', 'gutenberg' ), + array( 'status' => 404 ) + ); + } + + $variations = array(); + $base_directory = get_stylesheet_directory() . '/styles'; + if ( file_exists( $base_directory ) ) { + $nested_files = new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $base_directory ) ); + $nested_html_files = new RegexIterator( $nested_files, '/^.+\.json$/i', RecursiveRegexIterator::GET_MATCH ); + foreach ( $nested_html_files as $path => $file ) { + $decoded_file = wp_json_file_decode( $path, array( 'associative' => true ) ); + if ( is_array( $decoded_file ) ) { + $variations[] = ( new WP_Theme_JSON_Gutenberg( $decoded_file ) )->get_raw_data(); + } + } + } + $response = rest_ensure_response( $variations ); + + return $response; + } +} diff --git a/lib/compat/wordpress-6.0/rest-api.php b/lib/compat/wordpress-6.0/rest-api.php new file mode 100644 index 00000000000000..5816f518d64784 --- /dev/null +++ b/lib/compat/wordpress-6.0/rest-api.php @@ -0,0 +1,15 @@ +register_routes(); +} +add_action( 'rest_api_init', 'gutenberg_register_global_styles_endpoints' ); diff --git a/lib/load.php b/lib/load.php index 2b1b5d4839162a..0ad62385f9f2a7 100644 --- a/lib/load.php +++ b/lib/load.php @@ -123,11 +123,13 @@ function gutenberg_is_experiment_enabled( $name ) { require __DIR__ . '/compat/wordpress-5.9/block-template.php'; require __DIR__ . '/compat/wordpress-5.9/wp-theme-get-post-templates.php'; require __DIR__ . '/compat/wordpress-5.9/default-theme-supports.php'; -require __DIR__ . '/compat/wordpress-5.9/class-gutenberg-rest-global-styles-controller.php'; +require __DIR__ . '/compat/wordpress-5.9/class-wp-rest-global-styles-controller.php'; require __DIR__ . '/compat/wordpress-5.9/rest-active-global-styles.php'; require __DIR__ . '/compat/wordpress-5.9/move-theme-editor-menu-item.php'; require __DIR__ . '/compat/wordpress-6.0/post-lock.php'; require __DIR__ . '/compat/wordpress-6.0/blocks.php'; +require __DIR__ . '/compat/wordpress-6.0/class-gutenberg-rest-global-styles-controller.php'; +require __DIR__ . '/compat/wordpress-6.0/rest-api.php'; require __DIR__ . '/compat/experimental/blocks.php'; require __DIR__ . '/blocks.php'; diff --git a/lib/rest-api.php b/lib/rest-api.php index 49f82e2eb4b0e1..647b9bd3b70dc3 100644 --- a/lib/rest-api.php +++ b/lib/rest-api.php @@ -234,15 +234,6 @@ function gutenberg_rest_user_collection_params_has_published_posts( $query_param } add_filter( 'rest_user_collection_params', 'gutenberg_rest_user_collection_params_has_published_posts' ); -/** - * Registers the Global Styles REST API routes. - */ -function gutenberg_register_global_styles_endpoints() { - $editor_settings = new Gutenberg_REST_Global_Styles_Controller(); - $editor_settings->register_routes(); -} -add_action( 'rest_api_init', 'gutenberg_register_global_styles_endpoints' ); - /** * Registers the Edit Site's Export REST API routes. * From 362774f79531dc23b869de6ee11d8e14e034d391 Mon Sep 17 00:00:00 2001 From: ntsekouras Date: Fri, 21 Jan 2022 13:05:08 +0200 Subject: [PATCH 2/6] [Global Styles]: Add REST API endpoint to fetch variations --- ...erg-rest-global-styles-controller-test.php | 66 ++++++++++++++++++- phpunit/fixtures/theme-json-variation.json | 23 +++++++ 2 files changed, 87 insertions(+), 2 deletions(-) create mode 100644 phpunit/fixtures/theme-json-variation.json diff --git a/phpunit/class-gutenberg-rest-global-styles-controller-test.php b/phpunit/class-gutenberg-rest-global-styles-controller-test.php index f546d88dbb7c50..656edac4daba6c 100644 --- a/phpunit/class-gutenberg-rest-global-styles-controller-test.php +++ b/phpunit/class-gutenberg-rest-global-styles-controller-test.php @@ -54,18 +54,25 @@ public static function wpSetupBeforeClass( $factory ) { ); } + public function test_register_routes() { $routes = rest_get_server()->get_routes(); $this->assertArrayHasKey( - '/wp/v2/global-styles/(?P[\/\s%\w\.\(\)\[\]\@_\-]+)', + // '/wp/v2/global-styles/(?P[\/\s%\w\.\(\)\[\]\@_\-]+)', + '/wp/v2/global-styles/(?P[\/\w-]+)', $routes, 'Single global style based on the given ID route does not exist' ); $this->assertArrayHasKey( - '/wp/v2/global-styles/themes/(?P[\/\s%\w\.\(\)\[\]\@_\-]+)', + '/wp/v2/global-styles/themes/(?P[^\/:<>\*\?"\|]+(?:\/[^\/:<>\*\?"\|]+)?)', $routes, 'Theme global styles route does not exist' ); + $this->assertArrayHasKey( + '/wp/v2/global-styles/themes/(?P[\/\s%\w\.\(\)\[\]\@_\-]+)/variations', + $routes, + 'Theme global styles variations route does not exist' + ); } public function test_context_param() { @@ -73,6 +80,61 @@ public function test_context_param() { $this->markTestIncomplete(); } + /** + * @group tsek + */ + public function test_get_theme_items() { + wp_set_current_user( self::$admin_id ); + $request = new WP_REST_Request( 'GET', '/wp/v2/global-styles/themes/emptytheme/variations' ); + $response = rest_get_server()->dispatch( $request ); + $data = $response->get_data(); + $this->assertEmpty( $data ); + + // We create a global style variation by creating a `styles` folder with + // a `theme.json` variation. + $styles_path = gutenberg_dir_path() . 'test/emptytheme/styles'; + $fixture_name = 'theme-json-variation.json'; + $fixture = gutenberg_dir_path() . "phpunit/fixtures/$fixture_name"; + if ( ! file_exists( $styles_path ) ) { + mkdir( $styles_path, 0777, true ); + } + copy( $fixture, "$styles_path/$fixture_name" ); + $response = rest_get_server()->dispatch( $request ); + $data = $response->get_data(); + $expected = array( + array( + 'version' => 2, + 'settings' => array( + 'color' => array( + 'palette' => array( + 'theme' => array( + array( + 'slug' => 'foreground', + 'color' => '#3F67C6', + 'name' => 'Foreground', + ), + ), + ), + ), + ), + 'styles' => array( + 'blocks' => array( + 'core/post-title' => array( + 'typography' => array( + 'fontWeight' => '700', + ), + ), + ), + ), + ), + ); + $this->assertSameSetsWithIndex( $data, $expected ); + + // Delete copied files. + array_map( 'unlink', glob( "$styles_path/*.*" ) ); + rmdir( $styles_path ); + } + public function test_get_items() { $this->markTestIncomplete(); } diff --git a/phpunit/fixtures/theme-json-variation.json b/phpunit/fixtures/theme-json-variation.json new file mode 100644 index 00000000000000..0ab221d087035a --- /dev/null +++ b/phpunit/fixtures/theme-json-variation.json @@ -0,0 +1,23 @@ +{ + "version": 2, + "settings": { + "color": { + "palette": [ + { + "slug": "foreground", + "color": "#3F67C6", + "name": "Foreground" + } + ] + } + }, + "styles": { + "blocks": { + "core/post-title": { + "typography": { + "fontWeight": "700" + } + } + } + } +} From 38a8a39f6f8da71b099889ebe42c9170bd4168ef Mon Sep 17 00:00:00 2001 From: Nik Tsekouras Date: Fri, 21 Jan 2022 13:11:52 +0200 Subject: [PATCH 3/6] Update lib/compat/wordpress-6.0/class-gutenberg-rest-global-styles-controller.php Co-authored-by: George Mamadashvili --- .../class-gutenberg-rest-global-styles-controller.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/compat/wordpress-6.0/class-gutenberg-rest-global-styles-controller.php b/lib/compat/wordpress-6.0/class-gutenberg-rest-global-styles-controller.php index b51f24bb98a576..eafd0bee4a3b01 100644 --- a/lib/compat/wordpress-6.0/class-gutenberg-rest-global-styles-controller.php +++ b/lib/compat/wordpress-6.0/class-gutenberg-rest-global-styles-controller.php @@ -79,7 +79,7 @@ public function get_theme_items( $request ) { $variations = array(); $base_directory = get_stylesheet_directory() . '/styles'; - if ( file_exists( $base_directory ) ) { + if ( is_dir( $base_directory ) ) { $nested_files = new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $base_directory ) ); $nested_html_files = new RegexIterator( $nested_files, '/^.+\.json$/i', RecursiveRegexIterator::GET_MATCH ); foreach ( $nested_html_files as $path => $file ) { From cdd3ccc3c890343bddb2cdfc7b7cc4d5e27fb96f Mon Sep 17 00:00:00 2001 From: ntsekouras Date: Fri, 21 Jan 2022 13:13:13 +0200 Subject: [PATCH 4/6] remove test @group --- phpunit/class-gutenberg-rest-global-styles-controller-test.php | 3 --- 1 file changed, 3 deletions(-) diff --git a/phpunit/class-gutenberg-rest-global-styles-controller-test.php b/phpunit/class-gutenberg-rest-global-styles-controller-test.php index 656edac4daba6c..a1cb876717b05f 100644 --- a/phpunit/class-gutenberg-rest-global-styles-controller-test.php +++ b/phpunit/class-gutenberg-rest-global-styles-controller-test.php @@ -80,9 +80,6 @@ public function test_context_param() { $this->markTestIncomplete(); } - /** - * @group tsek - */ public function test_get_theme_items() { wp_set_current_user( self::$admin_id ); $request = new WP_REST_Request( 'GET', '/wp/v2/global-styles/themes/emptytheme/variations' ); From f94845cf901c1d616bb7a283f33fcdd749e6d168 Mon Sep 17 00:00:00 2001 From: ntsekouras Date: Fri, 21 Jan 2022 15:08:40 +0200 Subject: [PATCH 5/6] add variation to empty theme --- .../class-wp-rest-global-styles-controller.php | 3 ++- ...nberg-rest-global-styles-controller-test.php | 17 ----------------- .../emptytheme/styles/variation.json | 0 3 files changed, 2 insertions(+), 18 deletions(-) rename phpunit/fixtures/theme-json-variation.json => test/emptytheme/styles/variation.json (100%) diff --git a/lib/compat/wordpress-5.9/class-wp-rest-global-styles-controller.php b/lib/compat/wordpress-5.9/class-wp-rest-global-styles-controller.php index 9f5bb3fcf2a8da..c2e6e900e17134 100644 --- a/lib/compat/wordpress-5.9/class-wp-rest-global-styles-controller.php +++ b/lib/compat/wordpress-5.9/class-wp-rest-global-styles-controller.php @@ -564,7 +564,8 @@ public function get_theme_item( $request ) { } if ( rest_is_field_included( 'styles', $fields ) ) { - $data['styles'] = $theme->get_raw_data()['styles']; + $raw_data = $theme->get_raw_data(); + $data['styles'] = isset( $raw_data['styles'] ) ? $raw_data['styles'] : array(); } $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; diff --git a/phpunit/class-gutenberg-rest-global-styles-controller-test.php b/phpunit/class-gutenberg-rest-global-styles-controller-test.php index a1cb876717b05f..4c2ad39d555214 100644 --- a/phpunit/class-gutenberg-rest-global-styles-controller-test.php +++ b/phpunit/class-gutenberg-rest-global-styles-controller-test.php @@ -85,19 +85,6 @@ public function test_get_theme_items() { $request = new WP_REST_Request( 'GET', '/wp/v2/global-styles/themes/emptytheme/variations' ); $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); - $this->assertEmpty( $data ); - - // We create a global style variation by creating a `styles` folder with - // a `theme.json` variation. - $styles_path = gutenberg_dir_path() . 'test/emptytheme/styles'; - $fixture_name = 'theme-json-variation.json'; - $fixture = gutenberg_dir_path() . "phpunit/fixtures/$fixture_name"; - if ( ! file_exists( $styles_path ) ) { - mkdir( $styles_path, 0777, true ); - } - copy( $fixture, "$styles_path/$fixture_name" ); - $response = rest_get_server()->dispatch( $request ); - $data = $response->get_data(); $expected = array( array( 'version' => 2, @@ -126,10 +113,6 @@ public function test_get_theme_items() { ), ); $this->assertSameSetsWithIndex( $data, $expected ); - - // Delete copied files. - array_map( 'unlink', glob( "$styles_path/*.*" ) ); - rmdir( $styles_path ); } public function test_get_items() { diff --git a/phpunit/fixtures/theme-json-variation.json b/test/emptytheme/styles/variation.json similarity index 100% rename from phpunit/fixtures/theme-json-variation.json rename to test/emptytheme/styles/variation.json From 3751224ecf760088a67fd53656e02e370b6947f1 Mon Sep 17 00:00:00 2001 From: ntsekouras Date: Fri, 21 Jan 2022 15:30:18 +0200 Subject: [PATCH 6/6] fix php linting issues --- ...class-wp-rest-global-styles-controller.php | 30 +++++++++---------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/lib/compat/wordpress-5.9/class-wp-rest-global-styles-controller.php b/lib/compat/wordpress-5.9/class-wp-rest-global-styles-controller.php index c2e6e900e17134..5e7be742b86db4 100644 --- a/lib/compat/wordpress-5.9/class-wp-rest-global-styles-controller.php +++ b/lib/compat/wordpress-5.9/class-wp-rest-global-styles-controller.php @@ -57,7 +57,7 @@ public function register_routes() { 'permission_callback' => array( $this, 'get_theme_item_permissions_check' ), 'args' => array( 'stylesheet' => array( - 'description' => __( 'The theme identifier' ), + 'description' => __( 'The theme identifier', 'gutenberg' ), 'type' => 'string', 'sanitize_callback' => array( $this, '_sanitize_global_styles_callback' ), ), @@ -77,7 +77,7 @@ public function register_routes() { 'permission_callback' => array( $this, 'get_item_permissions_check' ), 'args' => array( 'id' => array( - 'description' => __( 'The id of a template' ), + 'description' => __( 'The id of a template', 'gutenberg' ), 'type' => 'string', 'sanitize_callback' => array( $this, '_sanitize_global_styles_callback' ), ), @@ -125,7 +125,7 @@ public function get_item_permissions_check( $request ) { if ( 'edit' === $request['context'] && $post && ! $this->check_update_permission( $post ) ) { return new WP_Error( 'rest_forbidden_context', - __( 'Sorry, you are not allowed to edit this global style.' ), + __( 'Sorry, you are not allowed to edit this global style.', 'gutenberg' ), array( 'status' => rest_authorization_required_code() ) ); } @@ -133,7 +133,7 @@ public function get_item_permissions_check( $request ) { if ( ! $this->check_read_permission( $post ) ) { return new WP_Error( 'rest_cannot_view', - __( 'Sorry, you are not allowed to view this global style.' ), + __( 'Sorry, you are not allowed to view this global style.', 'gutenberg' ), array( 'status' => rest_authorization_required_code() ) ); } @@ -188,7 +188,7 @@ public function update_item_permissions_check( $request ) { if ( $post && ! $this->check_update_permission( $post ) ) { return new WP_Error( 'rest_cannot_edit', - __( 'Sorry, you are not allowed to edit this global style.' ), + __( 'Sorry, you are not allowed to edit this global style.', 'gutenberg' ), array( 'status' => rest_authorization_required_code() ) ); } @@ -371,7 +371,7 @@ public function prepare_item_for_response( $post, $request ) { // phpcs:ignore V protected function get_post( $id ) { $error = new WP_Error( 'rest_global_styles_not_found', - __( 'No global styles config exist with that id.' ), + __( 'No global styles config exist with that id.', 'gutenberg' ), array( 'status' => 404 ) ); @@ -471,34 +471,34 @@ public function get_item_schema() { 'type' => 'object', 'properties' => array( 'id' => array( - 'description' => __( 'ID of global styles config.' ), + 'description' => __( 'ID of global styles config.', 'gutenberg' ), 'type' => 'string', 'context' => array( 'embed', 'view', 'edit' ), 'readonly' => true, ), 'styles' => array( - 'description' => __( 'Global styles.' ), + 'description' => __( 'Global styles.', 'gutenberg' ), 'type' => array( 'object' ), 'context' => array( 'view', 'edit' ), ), 'settings' => array( - 'description' => __( 'Global settings.' ), + 'description' => __( 'Global settings.', 'gutenberg' ), 'type' => array( 'object' ), 'context' => array( 'view', 'edit' ), ), 'title' => array( - 'description' => __( 'Title of the global styles variation.' ), + 'description' => __( 'Title of the global styles variation.', 'gutenberg' ), 'type' => array( 'object', 'string' ), 'default' => '', 'context' => array( 'embed', 'view', 'edit' ), 'properties' => array( 'raw' => array( - 'description' => __( 'Title for the global styles variation, as it exists in the database.' ), + 'description' => __( 'Title for the global styles variation, as it exists in the database.', 'gutenberg' ), 'type' => 'string', 'context' => array( 'view', 'edit', 'embed' ), ), 'rendered' => array( - 'description' => __( 'HTML title for the post, transformed for display.' ), + 'description' => __( 'HTML title for the post, transformed for display.', 'gutenberg' ), 'type' => 'string', 'context' => array( 'view', 'edit', 'embed' ), 'readonly' => true, @@ -521,13 +521,13 @@ public function get_item_schema() { * @param WP_REST_Request $request Full details about the request. * @return true|WP_Error True if the request has read access for the item, WP_Error object otherwise. */ - public function get_theme_item_permissions_check( $request ) { + public function get_theme_item_permissions_check( $request ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable // Verify if the current user has edit_theme_options capability. // This capability is required to edit/view/delete templates. if ( ! current_user_can( 'edit_theme_options' ) ) { return new WP_Error( 'rest_cannot_manage_global_styles', - __( 'Sorry, you are not allowed to access the global styles on this site.' ), + __( 'Sorry, you are not allowed to access the global styles on this site.', 'gutenberg' ), array( 'status' => rest_authorization_required_code(), ) @@ -550,7 +550,7 @@ public function get_theme_item( $request ) { // This endpoint only supports the active theme for now. return new WP_Error( 'rest_theme_not_found', - __( 'Theme not found.' ), + __( 'Theme not found.', 'gutenberg' ), array( 'status' => 404 ) ); }