summaryrefslogtreecommitdiffstats
path: root/wp-includes/rest-api/endpoints/class-wp-rest-global-styles-controller.php
diff options
context:
space:
mode:
Diffstat (limited to 'wp-includes/rest-api/endpoints/class-wp-rest-global-styles-controller.php')
-rw-r--r--wp-includes/rest-api/endpoints/class-wp-rest-global-styles-controller.php174
1 files changed, 72 insertions, 102 deletions
diff --git a/wp-includes/rest-api/endpoints/class-wp-rest-global-styles-controller.php b/wp-includes/rest-api/endpoints/class-wp-rest-global-styles-controller.php
index 9837f53..0cb54c9 100644
--- a/wp-includes/rest-api/endpoints/class-wp-rest-global-styles-controller.php
+++ b/wp-includes/rest-api/endpoints/class-wp-rest-global-styles-controller.php
@@ -10,24 +10,24 @@
/**
* Base Global Styles REST API Controller.
*/
-class WP_REST_Global_Styles_Controller extends WP_REST_Controller {
-
+class WP_REST_Global_Styles_Controller extends WP_REST_Posts_Controller {
/**
- * Post type.
+ * Whether the controller supports batching.
*
- * @since 5.9.0
- * @var string
+ * @since 6.6.0
+ * @var array
*/
- protected $post_type;
+ protected $allow_batch = array( 'v1' => false );
/**
* Constructor.
- * @since 5.9.0
+ *
+ * @since 6.6.0
+ *
+ * @param string $post_type Post type.
*/
- public function __construct() {
- $this->namespace = 'wp/v2';
- $this->rest_base = 'global-styles';
- $this->post_type = 'wp_global_styles';
+ public function __construct( $post_type = 'wp_global_styles' ) {
+ parent::__construct( $post_type );
}
/**
@@ -50,6 +50,7 @@ class WP_REST_Global_Styles_Controller extends WP_REST_Controller {
'type' => 'string',
),
),
+ 'allow_batch' => $this->allow_batch,
),
)
);
@@ -79,6 +80,7 @@ class WP_REST_Global_Styles_Controller extends WP_REST_Controller {
'sanitize_callback' => array( $this, '_sanitize_global_styles_callback' ),
),
),
+ 'allow_batch' => $this->allow_batch,
),
)
);
@@ -106,7 +108,8 @@ class WP_REST_Global_Styles_Controller extends WP_REST_Controller {
'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' ),
+ 'schema' => array( $this, 'get_public_item_schema' ),
+ 'allow_batch' => $this->allow_batch,
)
);
}
@@ -194,29 +197,11 @@ class WP_REST_Global_Styles_Controller extends WP_REST_Controller {
* @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 );
}
/**
- * 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
@@ -242,59 +227,11 @@ class WP_REST_Global_Styles_Controller extends WP_REST_Controller {
}
/**
- * 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 );
- if ( is_wp_error( $changes ) ) {
- return $changes;
- }
-
- $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
* @since 6.2.0 Added validation of styles.css property.
+ * @since 6.6.0 Added registration of block style variations from theme.json sources (theme.json, user theme.json, partials).
*
* @param WP_REST_Request $request Request object.
* @return stdClass|WP_Error Prepared item on success. WP_Error on when the custom CSS is not valid.
@@ -327,6 +264,11 @@ class WP_REST_Global_Styles_Controller extends WP_REST_Controller {
} elseif ( isset( $existing_config['styles'] ) ) {
$config['styles'] = $existing_config['styles'];
}
+
+ // Register theme-defined variations e.g. from block style variation partials under `/styles`.
+ $variations = WP_Theme_JSON_Resolver::get_style_variations( 'block' );
+ wp_register_block_style_variations_from_theme_json_partials( $variations );
+
if ( isset( $request['settings'] ) ) {
$config['settings'] = $request['settings'];
} elseif ( isset( $existing_config['settings'] ) ) {
@@ -353,6 +295,7 @@ class WP_REST_Global_Styles_Controller extends WP_REST_Controller {
* Prepare a global styles config output for response.
*
* @since 5.9.0
+ * @since 6.6.0 Added custom relative theme file URIs to `_links`.
*
* @param WP_Post $post Global Styles post object.
* @param WP_REST_Request $request Request object.
@@ -362,8 +305,10 @@ class WP_REST_Global_Styles_Controller extends WP_REST_Controller {
$raw_config = json_decode( $post->post_content, true );
$is_global_styles_user_theme_json = isset( $raw_config['isGlobalStylesUserThemeJSON'] ) && true === $raw_config['isGlobalStylesUserThemeJSON'];
$config = array();
+ $theme_json = null;
if ( $is_global_styles_user_theme_json ) {
- $config = ( new WP_Theme_JSON( $raw_config, 'custom' ) )->get_raw_data();
+ $theme_json = new WP_Theme_JSON( $raw_config, 'custom' );
+ $config = $theme_json->get_raw_data();
}
// Base fields for every post.
@@ -405,9 +350,18 @@ class WP_REST_Global_Styles_Controller extends WP_REST_Controller {
if ( rest_is_field_included( '_links', $fields ) || rest_is_field_included( '_embedded', $fields ) ) {
$links = $this->prepare_links( $post->ID );
+
+ // Only return resolved URIs for get requests to user theme JSON.
+ if ( $theme_json ) {
+ $resolved_theme_uris = WP_Theme_JSON_Resolver::get_resolved_theme_uris( $theme_json );
+ if ( ! empty( $resolved_theme_uris ) ) {
+ $links['https://api.w.org/theme-file'] = $resolved_theme_uris;
+ }
+ }
+
$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 +385,12 @@ class WP_REST_Global_Styles_Controller extends WP_REST_Controller {
$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' ) ) {
@@ -454,13 +411,16 @@ class WP_REST_Global_Styles_Controller extends WP_REST_Controller {
*
* @since 5.9.0
* @since 6.2.0 Added 'edit-css' action.
+ * @since 6.6.0 Added $post and $request parameters.
*
+ * @param WP_Post $post Post object.
+ * @param WP_REST_Request $request Request object.
* @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';
}
@@ -473,21 +433,6 @@ class WP_REST_Global_Styles_Controller extends WP_REST_Controller {
}
/**
- * 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
@@ -588,6 +533,7 @@ class WP_REST_Global_Styles_Controller extends WP_REST_Controller {
* Returns the given theme global styles config.
*
* @since 5.9.0
+ * @since 6.6.0 Added custom relative theme file URIs to `_links`.
*
* @param WP_REST_Request $request The request instance.
* @return WP_REST_Response|WP_Error
@@ -622,11 +568,15 @@ class WP_REST_Global_Styles_Controller extends WP_REST_Controller {
$response = rest_ensure_response( $data );
if ( rest_is_field_included( '_links', $fields ) || rest_is_field_included( '_embedded', $fields ) ) {
- $links = array(
+ $links = array(
'self' => array(
'href' => rest_url( sprintf( '%s/%s/themes/%s', $this->namespace, $this->rest_base, $request['stylesheet'] ) ),
),
);
+ $resolved_theme_uris = WP_Theme_JSON_Resolver::get_resolved_theme_uris( $theme );
+ if ( ! empty( $resolved_theme_uris ) ) {
+ $links['https://api.w.org/theme-file'] = $resolved_theme_uris;
+ }
$response->add_links( $links );
}
@@ -664,6 +614,7 @@ class WP_REST_Global_Styles_Controller extends WP_REST_Controller {
*
* @since 6.0.0
* @since 6.2.0 Returns parent theme variations, if they exist.
+ * @since 6.6.0 Added custom relative theme file URIs to `_links` for each item.
*
* @param WP_REST_Request $request The request instance.
*
@@ -679,9 +630,28 @@ class WP_REST_Global_Styles_Controller extends WP_REST_Controller {
);
}
+ $response = array();
+
+ // Register theme-defined variations e.g. from block style variation partials under `/styles`.
+ $partials = WP_Theme_JSON_Resolver::get_style_variations( 'block' );
+ wp_register_block_style_variations_from_theme_json_partials( $partials );
+
$variations = WP_Theme_JSON_Resolver::get_style_variations();
+ foreach ( $variations as $variation ) {
+ $variation_theme_json = new WP_Theme_JSON( $variation );
+ $resolved_theme_uris = WP_Theme_JSON_Resolver::get_resolved_theme_uris( $variation_theme_json );
+ $data = rest_ensure_response( $variation );
+ if ( ! empty( $resolved_theme_uris ) ) {
+ $data->add_links(
+ array(
+ 'https://api.w.org/theme-file' => $resolved_theme_uris,
+ )
+ );
+ }
+ $response[] = $this->prepare_response_for_collection( $data );
+ }
- return rest_ensure_response( $variations );
+ return rest_ensure_response( $response );
}
/**