From 61756c5fa4906e9482632bfe38c084a0ce07071f Mon Sep 17 00:00:00 2001 From: ramon Date: Mon, 20 May 2024 14:03:02 -0700 Subject: [PATCH] First commit - sync global styles controller changes from https://github.com/WordPress/gutenberg/pull/61271 --- ...class-wp-rest-global-styles-controller.php | 40 +++++++++++++++++-- 1 file changed, 37 insertions(+), 3 deletions(-) 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..f2070632eef69 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 @@ -353,6 +353,8 @@ protected function prepare_item_for_database( $request ) { * Prepare a global styles config output for response. * * @since 5.9.0 + * @since 6.2.0 Handling of style.css was added to WP_Theme_JSON. + * @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 +364,10 @@ public function prepare_item_for_response( $post, $request ) { $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,6 +409,15 @@ public function prepare_item_for_response( $post, $request ) { 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(); @@ -588,6 +601,7 @@ public function get_theme_item_permissions_check( $request ) { * 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 +636,15 @@ public function get_theme_item( $request ) { $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 +682,7 @@ public function get_theme_items_permissions_check( $request ) { * * @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 +698,24 @@ public function get_theme_items( $request ) { ); } + $response = array(); $variations = WP_Theme_JSON_Resolver::get_style_variations(); - return rest_ensure_response( $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( $response ); } /**