Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Section Styles: Resolve ref values in variations data #63172

Merged
merged 7 commits into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions lib/block-supports/block-style-variations.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,38 @@
return $parsed_block;
}

// Recursively resolve any ref values with the appropriate value within the
// theme_json data.
$replace_refs = function ( &$variation_data ) use( &$replace_refs, $theme_json ) {

Check failure on line 84 in lib/block-supports/block-style-variations.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Expected 1 space after USE keyword; found 0
aaronrobertshaw marked this conversation as resolved.
Show resolved Hide resolved
foreach ( $variation_data as $key => &$value ) {
// Only need to potentially process arrays.
if ( is_array( $value ) ) {
// If ref value is set, attempt to find its matching value and update it.
if ( isset( $value['ref'] ) ) {
// Clean up any invalid ref value.
if ( empty( $value['ref'] ) || ! is_string( $value['ref'] ) ) {
unset( $variation_data[ $key ] );
}

$value_path = explode( '.', $value['ref'] ?? '' );
$ref_value = _wp_array_get( $theme_json, $value_path );

// Only update this value if the referenced path matched a value.
if ( $ref_value ) {
$value = $ref_value;
} else {
// Otherwise, remove the ref node.
unset( $variation_data[ $key ] );
}
} else {
// Recursively look for ref instances.
$replace_refs( $value, $theme_json );
}
}
}
};
$replace_refs( $variation_data );

$variation_instance = gutenberg_create_block_style_variation_instance_name( $parsed_block, $variation );
$class_name = "is-style-$variation_instance";
$updated_class_name = $parsed_block['attrs']['className'] . " $class_name";
Expand Down
73 changes: 70 additions & 3 deletions packages/block-editor/src/hooks/block-style-variation.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
getBlockSelectors,
} from '../components/global-styles';
import { useStyleOverride } from './utils';
import { getValueFromObjectPath } from '../utils/object';
import { store as blockEditorStore } from '../store';
import { globalStylesDataKey } from '../store/private-keys';
import { unlock } from '../lock-unlock';
Expand Down Expand Up @@ -180,6 +181,67 @@ export function __unstableBlockStyleVariationOverridesWithConfig( { config } ) {
);
}

/**
* Retrieves any variation styles data and resolves any referenced values.
*
* @param {Object} globalStyles A complete global styles object, containing settings and styles.
* @param {string} name The name of the desired block type.
* @param {variation} variation The of the block style variation to retrieve data for.
*
* @return {Object|undefined} The global styles data for the specified variation.
*/
function getVariationStylesWithRefValues( globalStyles, name, variation ) {
if ( ! globalStyles?.styles?.blocks?.[ name ]?.variations?.[ variation ] ) {
return;
}

// Helper to recursively look for `ref` values to resolve.
const replaceRefs = ( variationStyles ) => {
Object.keys( variationStyles ).forEach( ( key ) => {
const value = variationStyles[ key ];

// Only process objects.
if ( typeof value === 'object' && value !== null ) {
// Process `ref` value if present.
if ( value.ref !== undefined ) {
if (
typeof value.ref !== 'string' ||
value.ref.trim() === ''
) {
// Remove invalid ref.
delete variationStyles[ key ];
} else {
// Resolve `ref` value.
const refValue = getValueFromObjectPath(
globalStyles,
value.ref
);

if ( refValue ) {
variationStyles[ key ] = refValue;
} else {
delete variationStyles[ key ];
}
}
} else {
// Recursively resolve `ref` values in nested objects.
replaceRefs( value );
}
}
} );
};

// Deep clone variation node to avoid mutating it within global styles and losing refs.
const styles = JSON.parse(
JSON.stringify(
globalStyles.styles.blocks[ name ].variations[ variation ]
)
);
replaceRefs( styles );

return styles;
}

function useBlockStyleVariation( name, variation, clientId ) {
// Prefer global styles data in GlobalStylesContext, which are available
// if in the site editor. Otherwise fall back to whatever is in the
Expand All @@ -194,9 +256,14 @@ function useBlockStyleVariation( name, variation, clientId ) {
}, [] );

return useMemo( () => {
const styles = mergedConfig?.styles ?? globalStyles;
const variationStyles =
styles?.blocks?.[ name ]?.variations?.[ variation ];
const variationStyles = getVariationStylesWithRefValues(
{
settings: mergedConfig?.settings ?? globalSettings,
styles: mergedConfig?.styles ?? globalStyles,
},
Comment on lines +270 to +273
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Creating a "complete" theme.json object seemed the easiest approach to reason about when ref values could target any value within settings or styles etc.

name,
variation
);

return {
settings: mergedConfig?.settings ?? globalSettings,
Expand Down
Loading