Skip to content

Commit

Permalink
Backend resolution of theme file URIs for global styles revisions
Browse files Browse the repository at this point in the history
  • Loading branch information
ramonjd committed May 9, 2024
1 parent 181c271 commit 2133e80
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 20 deletions.
2 changes: 1 addition & 1 deletion .wp-env.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"core": "WordPress/WordPress",
"plugins": [ "." ],
"themes": [ "./test/emptytheme" ],
"themes": [ "./test/emptytheme", "./test/emptytheme-child" ],
"env": {
"tests": {
"mappings": {
Expand Down
6 changes: 4 additions & 2 deletions lib/class-wp-rest-global-styles-controller-gutenberg.php
Original file line number Diff line number Diff line change
Expand Up @@ -366,8 +366,10 @@ public function prepare_item_for_response( $post, $request ) { // phpcs:ignore V
$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 = array();
if ( $is_global_styles_user_theme_json ) {
$config = ( new WP_Theme_JSON_Gutenberg( $raw_config, 'custom' ) )->get_raw_data();
$theme_json = new WP_Theme_JSON_Gutenberg( $raw_config, 'custom' );
$config = $theme_json->get_raw_data();
}

// Base fields for every post.
Expand Down Expand Up @@ -411,7 +413,7 @@ public function prepare_item_for_response( $post, $request ) { // phpcs:ignore V
$links = $this->prepare_links( $post->ID );
// Only return resolved URIs for get requests to user theme JSON.
if ( $request->get_method() === WP_REST_Server::READABLE && $is_global_styles_user_theme_json ) {
$resolved_theme_uris = WP_Theme_JSON_Resolver_Gutenberg::get_resolved_theme_uris( $config );
$resolved_theme_uris = WP_Theme_JSON_Resolver_Gutenberg::get_resolved_theme_uris( $theme_json );
if ( ! empty( $resolved_theme_uris ) ) {
$links['theme_file_uris'] = $resolved_theme_uris;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class Gutenberg_REST_Global_Styles_Revisions_Controller_6_6 extends Gutenberg_RE
* Prepares the revision for the REST response.
*
* @since 6.3.0
* @since 6.6.0 Added resolved URI links to the response.
*
* @param WP_Post $post Post revision object.
* @param WP_REST_Request $request Request object.
Expand Down Expand Up @@ -80,22 +81,18 @@ public function prepare_item_for_response( $post, $request ) {
$data = $this->add_additional_fields_to_object( $data, $request );
$data = $this->filter_response_by_context( $data, $context );

//$response = rest_ensure_response( $data );
$response = rest_ensure_response( $data );

// Add resolved URIs to the response.
//if ( rest_is_field_included( '_links', $fields ) && $request->get_method() === WP_REST_Server::READABLE ) {
$links = array(
'self' => array(
'href' => rest_url( sprintf( '%s/%s/%s/%s/%s', $this->namespace, $this->parent_base, $parent->ID, $this->rest_base, $post->ID ) ),
),
);
$resolved_theme_uris = WP_Theme_JSON_Resolver_Gutenberg::get_resolved_theme_uris( $theme_json );
if ( ! empty( $resolved_theme_uris ) ) {
$links['theme_file_uris'] = $resolved_theme_uris;
if ( $request->get_method() === WP_REST_Server::READABLE ) {
$links = array();
$resolved_theme_uris = WP_Theme_JSON_Resolver_Gutenberg::get_resolved_theme_uris( $theme_json );
if ( ! empty( $resolved_theme_uris ) ) {
$links['theme_file_uris'] = $resolved_theme_uris;
}
$response->add_links( $links );
}
$data['_links'] = $links;
//}

return rest_ensure_response( $data );
return $response;
}
}
8 changes: 5 additions & 3 deletions lib/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ function gutenberg_is_experiment_enabled( $name ) {
require_once __DIR__ . '/compat/wordpress-6.5/class-gutenberg-rest-global-styles-revisions-controller-6-5.php';
require_once __DIR__ . '/compat/wordpress-6.5/rest-api.php';

// WordPress 6.6 compat.
require __DIR__ . '/compat/wordpress-6.6/class-gutenberg-rest-global-styles-revisions-controller-6-6.php';
require __DIR__ . '/compat/wordpress-6.6/class-gutenberg-rest-templates-controller-6-6.php';
require __DIR__ . '/compat/wordpress-6.6/rest-api.php';

// Plugin specific code.
require_once __DIR__ . '/class-wp-rest-global-styles-controller-gutenberg.php';
require_once __DIR__ . '/rest-api.php';
Expand Down Expand Up @@ -130,9 +135,6 @@ function gutenberg_is_experiment_enabled( $name ) {
require __DIR__ . '/compat/wordpress-6.6/block-bindings/pattern-overrides.php';
require __DIR__ . '/compat/wordpress-6.6/block-template-utils.php';
require __DIR__ . '/compat/wordpress-6.6/option.php';
require __DIR__ . '/compat/wordpress-6.6/class-gutenberg-rest-templates-controller-6-6.php';
require __DIR__ . '/compat/wordpress-6.6/class-gutenberg-rest-global-styles-revisions-controller-6-6.php';
require __DIR__ . '/compat/wordpress-6.6/rest-api.php';
require __DIR__ . '/compat/wordpress-6.6/post.php';

// Experimental features.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const { GlobalStylesContext, cleanEmptyObject } = unlock(
);

export function mergeBaseAndUserConfigs( base, user ) {
console.log( 'mergeBaseAndUserConfigs', base, user );
return deepmerge( base, user, {
// We only pass as arrays the presets,
// in which case we want the new array of values
Expand All @@ -31,7 +32,7 @@ export function mergeBaseAndUserConfigs( base, user ) {
}

function useGlobalStylesUserConfig() {
const { globalStylesId, isReady, settings, styles } = useSelect(
const { globalStylesId, isReady, settings, styles, _links } = useSelect(
( select ) => {
const { getEditedEntityRecord, hasFinishedResolution } =
select( coreStore );
Expand Down Expand Up @@ -65,6 +66,7 @@ function useGlobalStylesUserConfig() {
isReady: hasResolved,
settings: record?.settings,
styles: record?.styles,
_links: record?._links,
};
},
[]
Expand All @@ -76,6 +78,7 @@ function useGlobalStylesUserConfig() {
return {
settings: settings ?? {},
styles: styles ?? {},
_links: _links ?? {},
};
}, [ settings, styles ] );

Expand All @@ -98,6 +101,7 @@ function useGlobalStylesUserConfig() {
{
styles: cleanEmptyObject( updatedConfig.styles ) || {},
settings: cleanEmptyObject( updatedConfig.settings ) || {},
_links: cleanEmptyObject( updatedConfig._links ) || {},
},
options
);
Expand Down Expand Up @@ -128,6 +132,7 @@ function useGlobalStylesContext() {
}
return mergeBaseAndUserConfigs( baseConfig, userConfig );
}, [ userConfig, baseConfig ] );

const context = useMemo( () => {
return {
isReady: isUserConfigReady && isBaseConfigReady,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,21 @@ function ScreenRevisions() {
setUserConfig( () => ( {
styles: revision?.styles,
settings: revision?.settings,
_links: revision?._links,
} ) );
setIsLoadingRevisionWithUnsavedChanges( false );
onCloseRevisions();
};

const selectRevision = ( revision ) => {
setCurrentlySelectedRevision( {
/*
* The default must be an empty object so that
* `mergeBaseAndUserConfigs()` can merge them correctly.
*/
styles: revision?.styles || {},
settings: revision?.settings || {},
_links: revision?._links || {},
id: revision?.id,
} );
};
Expand Down

0 comments on commit 2133e80

Please sign in to comment.