-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Background images: add support for theme.json ref value resolution #64128
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
Changes from all commits
7f7ea9a
e359a75
550098a
bc8dcc3
fb65340
cb34d72
87bb56b
aa7be71
70ee622
2099c31
f21a3e9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| https://github.com/WordPress/wordpress-develop/pull/7137 | ||
|
|
||
| * https://github.com/WordPress/gutenberg/pull/64128 | ||
| * https://github.com/WordPress/gutenberg/pull/64192 | ||
| * https://github.com/WordPress/gutenberg/pull/64328 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2329,7 +2329,7 @@ protected static function flatten_tree( $tree, $prefix = '', $token = '--' ) { | |
| * ```php | ||
| * array( | ||
| * 'name' => 'property_name', | ||
| * 'value' => 'property_value, | ||
| * 'value' => 'property_value', | ||
| * ) | ||
| * ``` | ||
| * | ||
|
|
@@ -2338,6 +2338,7 @@ protected static function flatten_tree( $tree, $prefix = '', $token = '--' ) { | |
| * @since 6.1.0 Added `$theme_json`, `$selector`, and `$use_root_padding` parameters. | ||
| * @since 6.5.0 Output a `min-height: unset` rule when `aspect-ratio` is set. | ||
| * @since 6.6.0 Passing current theme JSON settings to wp_get_typography_font_size_value(). Using style engine to correctly fetch background CSS values. | ||
| * @since 6.7.0 Allow ref resolution of background properties. | ||
| * | ||
| * @param array $styles Styles to process. | ||
| * @param array $settings Theme settings. | ||
|
|
@@ -2381,21 +2382,28 @@ protected static function compute_style_properties( $styles, $settings = array() | |
| $root_variable_duplicates[] = substr( $css_property, $root_style_length ); | ||
| } | ||
|
|
||
| // Processes background styles. | ||
| if ( 'background' === $value_path[0] && isset( $styles['background'] ) ) { | ||
| /* | ||
| * For user-uploaded images at the block level, assign defaults. | ||
| * Matches defaults applied in the editor and in block supports: background.php. | ||
| */ | ||
| if ( static::ROOT_BLOCK_SELECTOR !== $selector && ! empty( $styles['background']['backgroundImage']['id'] ) ) { | ||
| $styles['background']['backgroundSize'] = $styles['background']['backgroundSize'] ?? 'cover'; | ||
| // If the background size is set to `contain` and no position is set, set the position to `center`. | ||
| if ( 'contain' === $styles['background']['backgroundSize'] && empty( $styles['background']['backgroundPosition'] ) ) { | ||
| $styles['background']['backgroundPosition'] = '50% 50%'; | ||
| } | ||
| /* | ||
| * Processes background image styles. | ||
| * If the value is a URL, it will be converted to a CSS `url()` value. | ||
| * For an uploaded image (images with a database ID), apply size and position | ||
| * defaults equal to those applied in block supports in lib/background.php. | ||
| */ | ||
| if ( 'background-image' === $css_property && ! empty( $value ) ) { | ||
ramonjd marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| $background_styles = gutenberg_style_engine_get_styles( | ||
| array( 'background' => array( 'backgroundImage' => $value ) ) | ||
| ); | ||
|
|
||
| $value = $background_styles['declarations'][ $css_property ]; | ||
| } | ||
| if ( empty( $value ) && static::ROOT_BLOCK_SELECTOR !== $selector && ! empty( $styles['background']['backgroundImage']['id'] ) ) { | ||
| if ( 'background-size' === $css_property ) { | ||
| $value = 'cover'; | ||
| } | ||
| // If the background size is set to `contain` and no position is set, set the position to `center`. | ||
| if ( 'background-position' === $css_property ) { | ||
| $background_size = $styles['background']['backgroundSize'] ?? null; | ||
| $value = 'contain' === $background_size ? '50% 50%' : null; | ||
| } | ||
| $background_styles = gutenberg_style_engine_get_styles( array( 'background' => $styles['background'] ) ); | ||
| $value = $background_styles['declarations'][ $css_property ] ?? $value; | ||
| } | ||
|
|
||
| // Skip if empty and not "0" or value represents array of longhand values. | ||
|
|
@@ -2463,6 +2471,7 @@ protected static function compute_style_properties( $styles, $settings = array() | |
| * @since 5.8.0 | ||
| * @since 5.9.0 Added support for values of array type, which are returned as is. | ||
| * @since 6.1.0 Added the `$theme_json` parameter. | ||
| * @since 6.7.0 Added support for background image refs | ||
| * | ||
| * @param array $styles Styles subtree. | ||
| * @param array $path Which property to process. | ||
|
|
@@ -2479,15 +2488,17 @@ protected static function get_property_value( $styles, $path, $theme_json = null | |
| } | ||
|
|
||
| /* | ||
| * This converts references to a path to the value at that path | ||
| * where the values is an array with a "ref" key, pointing to a path. | ||
| * Where the current value is an array with a 'ref' key pointing | ||
| * to a path, this converts that path into the value at that path. | ||
| * For example: { "ref": "style.color.background" } => "#fff". | ||
| */ | ||
| if ( is_array( $value ) && isset( $value['ref'] ) ) { | ||
| $value_path = explode( '.', $value['ref'] ); | ||
| $ref_value = _wp_array_get( $theme_json, $value_path ); | ||
| $ref_value = _wp_array_get( $theme_json, $value_path, null ); | ||
| // Background Image refs can refer to a string or an array containing a URL string. | ||
| $ref_value_url = $ref_value['url'] ?? null; | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is specifically for background images. It's part of the reason why I think this could be a style engine util, used both internally, and externally by The style engine could subsume many style-related utils and potentially reduce the site of
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Anything that can chip away at the size of |
||
| // Only use the ref value if we find anything. | ||
| if ( ! empty( $ref_value ) && is_string( $ref_value ) ) { | ||
| if ( ! empty( $ref_value ) && ( is_string( $ref_value ) || is_string( $ref_value_url ) ) ) { | ||
| $value = $ref_value; | ||
| } | ||
|
|
||
|
|
@@ -3247,6 +3258,25 @@ public function merge( $incoming ) { | |
| } | ||
| } | ||
| } | ||
|
|
||
| /* | ||
| * Style values are merged at the leaf level, however | ||
| * some values provide exceptions, namely style values that are | ||
| * objects and represent unique definitions for the style. | ||
| */ | ||
| $style_nodes = static::get_styles_block_nodes(); | ||
| foreach ( $style_nodes as $style_node ) { | ||
| $path = $style_node['path']; | ||
| /* | ||
| * Background image styles should be replaced, not merged, | ||
| * as they themselves are specific object definitions for the style. | ||
| */ | ||
| $background_image_path = array_merge( $path, static::PROPERTIES_METADATA['background-image'] ); | ||
aaronrobertshaw marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| $content = _wp_array_get( $incoming_data, $background_image_path, null ); | ||
| if ( isset( $content ) ) { | ||
| _wp_array_set( $this->theme_json, $background_image_path, $content ); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
|
|
||
This file was deleted.
Uh oh!
There was an error while loading. Please reload this page.