From ed3f81cf5a035f3342f466a5eb64f666c52fbb81 Mon Sep 17 00:00:00 2001 From: Andrew Serong <14988353+andrewserong@users.noreply.github.com> Date: Thu, 14 Sep 2023 17:03:59 +1000 Subject: [PATCH 1/9] Backport background image block support from Gutenberg --- src/wp-includes/block-supports/background.php | 109 ++++++++++++++++++ src/wp-includes/class-wp-theme-json.php | 7 +- .../style-engine/class-wp-style-engine.php | 53 +++++++++ src/wp-settings.php | 1 + .../tests/style-engine/styleEngine.php | 19 +++ 5 files changed, 188 insertions(+), 1 deletion(-) create mode 100644 src/wp-includes/block-supports/background.php diff --git a/src/wp-includes/block-supports/background.php b/src/wp-includes/block-supports/background.php new file mode 100644 index 0000000000000..988ba04b8a97d --- /dev/null +++ b/src/wp-includes/block-supports/background.php @@ -0,0 +1,109 @@ +attributes ) { + $block_type->attributes = array(); + } + + // Check for existing style attribute definition e.g. from block.json. + if ( array_key_exists( 'style', $block_type->attributes ) ) { + return; + } + + $has_background_support = block_has_support( $block_type, array( 'background' ), false ); + + if ( $has_background_support ) { + $block_type->attributes['style'] = array( + 'type' => 'object', + ); + } +} + +/** + * Renders the background styles to the block wrapper. + * This block support uses the `render_block` hook to ensure that + * it is also applied to non-server-rendered blocks. + * + * @since 6.4.0 + * @access private + * + * @param string $block_content Rendered block content. + * @param array $block Block object. + * @return string Filtered block content. + */ +function wp_render_background_support( $block_content, $block ) { + $block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] ); + $block_attributes = $block['attrs']; + $has_background_image_support = block_has_support( $block_type, array( 'background', 'backgroundImage' ), false ); + + if ( + ! $has_background_image_support || + wp_should_skip_block_supports_serialization( $block_type, 'background', 'backgroundImage' ) + ) { + return $block_content; + } + + $background_image_source = _wp_array_get( $block_attributes, array( 'style', 'background', 'backgroundImage', 'source' ), null ); + $background_image_url = _wp_array_get( $block_attributes, array( 'style', 'background', 'backgroundImage', 'url' ), null ); + $background_size = _wp_array_get( $block_attributes, array( 'style', 'background', 'backgroundSize' ), 'cover' ); + + $background_block_styles = array(); + + if ( + 'file' === $background_image_source && + $background_image_url + ) { + // Set file based background URL. + $background_block_styles['backgroundImage']['url'] = $background_image_url; + // Only output the background size when an image url is set. + $background_block_styles['backgroundSize'] = $background_size; + } + + $styles = wp_style_engine_get_styles( array( 'background' => $background_block_styles ) ); + + if ( ! empty( $styles['css'] ) ) { + // Inject background styles to the first element, presuming it's the wrapper, if it exists. + $tags = new WP_HTML_Tag_Processor( $block_content ); + + if ( $tags->next_tag() ) { + $existing_style = $tags->get_attribute( 'style' ); + $updated_style = ''; + + if ( ! empty( $existing_style ) && ! str_ends_with( $existing_style, ';' ) ) { + $updated_style = $existing_style . '; '; + } + + $updated_style .= $styles['css']; + $tags->set_attribute( 'style', $updated_style ); + } + + return $tags->get_updated_html(); + } + + return $block_content; +} + +// Register the block support. +WP_Block_Supports::get_instance()->register( + 'background', + array( + 'register_attribute' => 'wp_register_background_support', + ) +); + +add_filter( 'render_block', 'wp_render_background_support', 10, 2 ); diff --git a/src/wp-includes/class-wp-theme-json.php b/src/wp-includes/class-wp-theme-json.php index 14a2d80408bf1..e01a4102e7716 100644 --- a/src/wp-includes/class-wp-theme-json.php +++ b/src/wp-includes/class-wp-theme-json.php @@ -342,13 +342,16 @@ class WP_Theme_JSON { * @since 6.2.0 Added `dimensions.minHeight`, 'shadow.presets', 'shadow.defaultPresets', * `position.fixed` and `position.sticky`. * @since 6.3.0 Added support for `typography.textColumns`, removed `layout.definitions`. - * @since 6.4.0 Added `layout.allowEditing` and `typography.writingMode`. + * @since 6.4.0 Added `layout.allowEditing`, `background.backgroundImage`, and `typography.writingMode`. * * @var array */ const VALID_SETTINGS = array( 'appearanceTools' => null, 'useRootPaddingAwareAlignments' => null, + 'background' => array( + 'backgroundImage' => null, + ), 'border' => array( 'color' => null, 'radius' => null, @@ -563,9 +566,11 @@ public static function get_element_class_name( $element ) { * * @since 6.0.0 * @since 6.2.0 Added `dimensions.minHeight` and `position.sticky`. + * @since 6.4.0 Added `background.backgroundImage`. * @var array */ const APPEARANCE_TOOLS_OPT_INS = array( + array( 'background', 'backgroundImage' ), array( 'border', 'color' ), array( 'border', 'radius' ), array( 'border', 'style' ), diff --git a/src/wp-includes/style-engine/class-wp-style-engine.php b/src/wp-includes/style-engine/class-wp-style-engine.php index 6bb03a707011a..bb7b4caba917f 100644 --- a/src/wp-includes/style-engine/class-wp-style-engine.php +++ b/src/wp-includes/style-engine/class-wp-style-engine.php @@ -22,6 +22,7 @@ * @access private * @since 6.1.0 * @since 6.3.0 Added support for text-columns. + * @since 6.4.0 Added support for background.backgroundImage. */ #[AllowDynamicProperties] final class WP_Style_Engine { @@ -51,6 +52,21 @@ final class WP_Style_Engine { * @var array */ const BLOCK_STYLE_DEFINITIONS_METADATA = array( + 'background' => array( + 'backgroundImage' => array( + 'property_keys' => array( + 'default' => 'background-image', + ), + 'value_func' => array( self::class, 'get_url_or_value_css_declaration' ), + 'path' => array( 'background', 'backgroundImage' ), + ), + 'backgroundSize' => array( + 'property_keys' => array( + 'default' => 'background-size', + ), + 'path' => array( 'background', 'backgroundSize' ), + ), + ), 'color' => array( 'text' => array( 'property_keys' => array( @@ -589,6 +605,43 @@ protected static function get_individual_property_css_declarations( $style_value return $css_declarations; } + /** + * Style value parser that constructs a CSS definition array comprising a single CSS property and value. + * If the provided value is an array containing a `url` property, the function will return a CSS definition array + * with a single property and value, with `url` escaped and injected into a CSS `url()` function, + * e.g., array( 'background-image' => "url( '...' )" ). + * + * @since 6.4.0 + * + * @param array $style_value A single raw style value from $block_styles array. + * @param array $style_definition A single style definition from BLOCK_STYLE_DEFINITIONS_METADATA. + * + * @return string[] An associative array of CSS definitions, e.g., array( "$property" => "$value", "$property" => "$value" ). + */ + protected static function get_url_or_value_css_declaration( $style_value, $style_definition ) { + if ( empty( $style_value ) ) { + return array(); + } + + $css_declarations = array(); + + if ( isset( $style_definition['property_keys']['default'] ) ) { + $value = null; + + if ( ! empty( $style_value['url'] ) ) { + $value = "url('" . $style_value['url'] . "')"; + } elseif ( is_string( $style_value ) ) { + $value = $style_value; + } + + if ( null !== $value ) { + $css_declarations[ $style_definition['property_keys']['default'] ] = $value; + } + } + + return $css_declarations; + } + /** * Returns compiled CSS from CSS declarations. * diff --git a/src/wp-settings.php b/src/wp-settings.php index 7b6aff30fec34..528f335cb7c2a 100644 --- a/src/wp-settings.php +++ b/src/wp-settings.php @@ -340,6 +340,7 @@ require ABSPATH . WPINC . '/class-wp-block-supports.php'; require ABSPATH . WPINC . '/block-supports/utils.php'; require ABSPATH . WPINC . '/block-supports/align.php'; +require ABSPATH . WPINC . '/block-supports/background.php'; require ABSPATH . WPINC . '/block-supports/border.php'; require ABSPATH . WPINC . '/block-supports/colors.php'; require ABSPATH . WPINC . '/block-supports/custom-classname.php'; diff --git a/tests/phpunit/tests/style-engine/styleEngine.php b/tests/phpunit/tests/style-engine/styleEngine.php index 299cf23fbc6cf..b1a01563c2097 100644 --- a/tests/phpunit/tests/style-engine/styleEngine.php +++ b/tests/phpunit/tests/style-engine/styleEngine.php @@ -509,6 +509,25 @@ public function data_wp_style_engine_get_styles() { ), ), ), + + 'inline_background_image_url_with_background_size' => array( + 'block_styles' => array( + 'background' => array( + 'backgroundImage' => array( + 'url' => 'https://example.com/image.jpg', + ), + 'backgroundSize' => 'cover', + ), + ), + 'options' => array(), + 'expected_output' => array( + 'css' => "background-image:url('https://example.com/image.jpg');background-size:cover;", + 'declarations' => array( + 'background-image' => "url('https://example.com/image.jpg')", + 'background-size' => 'cover', + ), + ), + ), ); } From c2638b5af35853334ee338277ca86056b3f2f2e6 Mon Sep 17 00:00:00 2001 From: Andrew Serong <14988353+andrewserong@users.noreply.github.com> Date: Fri, 15 Sep 2023 12:12:30 +1000 Subject: [PATCH 2/9] Update appearanceTools test --- tests/phpunit/tests/theme/wpThemeJson.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/phpunit/tests/theme/wpThemeJson.php b/tests/phpunit/tests/theme/wpThemeJson.php index cc531ef815895..592fd90cfda35 100644 --- a/tests/phpunit/tests/theme/wpThemeJson.php +++ b/tests/phpunit/tests/theme/wpThemeJson.php @@ -262,6 +262,9 @@ public function test_get_settings_appearance_true_opts_in() { $actual = $theme_json->get_settings(); $expected = array( + 'background' => array( + 'backgroundImage' => true, + ), 'border' => array( 'width' => true, 'style' => true, @@ -295,6 +298,9 @@ public function test_get_settings_appearance_true_opts_in() { ), ), 'core/group' => array( + 'background' => array( + 'backgroundImage' => true, + ), 'border' => array( 'width' => true, 'style' => true, From 639390dd303dd87fdb99db82354535c601d409dd Mon Sep 17 00:00:00 2001 From: Andrew Serong <14988353+andrewserong@users.noreply.github.com> Date: Fri, 15 Sep 2023 12:55:37 +1000 Subject: [PATCH 3/9] Add tests --- src/wp-includes/block-supports/background.php | 7 +- .../tests/block-supports/background.php | 187 ++++++++++++++++++ 2 files changed, 192 insertions(+), 2 deletions(-) create mode 100644 tests/phpunit/tests/block-supports/background.php diff --git a/src/wp-includes/block-supports/background.php b/src/wp-includes/block-supports/background.php index 988ba04b8a97d..079359018114a 100644 --- a/src/wp-includes/block-supports/background.php +++ b/src/wp-includes/block-supports/background.php @@ -84,8 +84,11 @@ function wp_render_background_support( $block_content, $block ) { $existing_style = $tags->get_attribute( 'style' ); $updated_style = ''; - if ( ! empty( $existing_style ) && ! str_ends_with( $existing_style, ';' ) ) { - $updated_style = $existing_style . '; '; + if ( ! empty( $existing_style ) ) { + $updated_style = $existing_style; + if ( ! str_ends_with( $existing_style, ';' ) ) { + $updated_style .= ';'; + } } $updated_style .= $styles['css']; diff --git a/tests/phpunit/tests/block-supports/background.php b/tests/phpunit/tests/block-supports/background.php new file mode 100644 index 0000000000000..208a3bec1e767 --- /dev/null +++ b/tests/phpunit/tests/block-supports/background.php @@ -0,0 +1,187 @@ +test_block_name = null; + $this->theme_root = realpath( DIR_TESTDATA . '/themedir1' ); + $this->orig_theme_dir = $GLOBALS['wp_theme_directories']; + + // /themes is necessary as theme.php functions assume /themes is the root if there is only one root. + $GLOBALS['wp_theme_directories'] = array( WP_CONTENT_DIR . '/themes', $this->theme_root ); + + add_filter( 'theme_root', array( $this, 'filter_set_theme_root' ) ); + add_filter( 'stylesheet_root', array( $this, 'filter_set_theme_root' ) ); + add_filter( 'template_root', array( $this, 'filter_set_theme_root' ) ); + + // Clear caches. + wp_clean_themes_cache(); + unset( $GLOBALS['wp_themes'] ); + WP_Style_Engine_CSS_Rules_Store::remove_all_stores(); + } + + public function tear_down() { + $GLOBALS['wp_theme_directories'] = $this->orig_theme_dir; + + // Clear up the filters to modify the theme root. + remove_filter( 'theme_root', array( $this, 'filter_set_theme_root' ) ); + remove_filter( 'stylesheet_root', array( $this, 'filter_set_theme_root' ) ); + remove_filter( 'template_root', array( $this, 'filter_set_theme_root' ) ); + + wp_clean_themes_cache(); + unset( $GLOBALS['wp_themes'] ); + WP_Style_Engine_CSS_Rules_Store::remove_all_stores(); + unregister_block_type( $this->test_block_name ); + $this->test_block_name = null; + parent::tear_down(); + } + + public function filter_set_theme_root() { + return $this->theme_root; + } + + /** + * Tests that background image block support works as expected. + * + * @covers ::wp_render_background_support + * + * @dataProvider data_background_block_support + * + * @param string $theme_name The theme to switch to. + * @param string $block_name The test block name to register. + * @param mixed $background_settings The background block support settings. + * @param mixed $background_style The background styles within the block attributes. + * @param string $expected_wrapper Expected markup for the block wrapper. + * @param string $wrapper Existing markup for the block wrapper. + */ + public function test_background_block_support( $theme_name, $block_name, $background_settings, $background_style, $expected_wrapper, $wrapper ) { + switch_theme( $theme_name ); + $this->test_block_name = $block_name; + + register_block_type( + $this->test_block_name, + array( + 'api_version' => 2, + 'attributes' => array( + 'style' => array( + 'type' => 'object', + ), + ), + 'supports' => array( + 'background' => $background_settings, + ), + ) + ); + + $block = array( + 'blockName' => $block_name, + 'attrs' => array( + 'style' => array( + 'background' => $background_style, + ), + ), + ); + + $actual = wp_render_background_support( $wrapper, $block ); + + $this->assertEquals( + $expected_wrapper, + $actual, + 'Background block wrapper markup should be correct' + ); + } + + /** + * Data provider. + * + * @return array + */ + public function data_background_block_support() { + return array( + 'background image style is applied' => array( + 'theme_name' => 'block-theme-child-with-fluid-typography', + 'block_name' => 'test/background-rules-are-output', + 'background_settings' => array( + 'backgroundImage' => true, + ), + 'background_style' => array( + 'backgroundImage' => array( + 'url' => 'https://example.com/image.jpg', + 'source' => 'file', + ), + ), + 'expected_wrapper' => '
Content
', + 'wrapper' => '
Content
', + ), + 'background image style is appended if a style attribute already exists' => array( + 'theme_name' => 'block-theme-child-with-fluid-typography', + 'block_name' => 'test/background-rules-are-output', + 'background_settings' => array( + 'backgroundImage' => true, + ), + 'background_style' => array( + 'backgroundImage' => array( + 'url' => 'https://example.com/image.jpg', + 'source' => 'file', + ), + ), + 'expected_wrapper' => '
Content
', + 'wrapper' => '
Content
', + ), + 'background image style is appended if a style attribute containing multiple styles already exists' => array( + 'theme_name' => 'block-theme-child-with-fluid-typography', + 'block_name' => 'test/background-rules-are-output', + 'background_settings' => array( + 'backgroundImage' => true, + ), + 'background_style' => array( + 'backgroundImage' => array( + 'url' => 'https://example.com/image.jpg', + 'source' => 'file', + ), + ), + 'expected_wrapper' => '
Content
', + 'wrapper' => '
Content
', + ), + 'background image style is not applied if the block does not support background image' => array( + 'theme_name' => 'block-theme-child-with-fluid-typography', + 'block_name' => 'test/background-rules-are-not-output', + 'background_settings' => array( + 'backgroundImage' => false, + ), + 'background_style' => array( + 'backgroundImage' => array( + 'url' => 'https://example.com/image.jpg', + 'source' => 'file', + ), + ), + 'expected_wrapper' => '
Content
', + 'wrapper' => '
Content
', + ), + ); + } +} From ae4a68cb6529d78b8ce7284eecfd505b8c6d7657 Mon Sep 17 00:00:00 2001 From: Andrew Serong <14988353+andrewserong@users.noreply.github.com> Date: Fri, 15 Sep 2023 12:58:24 +1000 Subject: [PATCH 4/9] Rename test file --- .../{background.php => wpRenderBackgroundSupport.php} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename tests/phpunit/tests/block-supports/{background.php => wpRenderBackgroundSupport.php} (100%) diff --git a/tests/phpunit/tests/block-supports/background.php b/tests/phpunit/tests/block-supports/wpRenderBackgroundSupport.php similarity index 100% rename from tests/phpunit/tests/block-supports/background.php rename to tests/phpunit/tests/block-supports/wpRenderBackgroundSupport.php From 6bf73f7c4783345a6d37c4daceab642a77568ccc Mon Sep 17 00:00:00 2001 From: Andrew Serong <14988353+andrewserong@users.noreply.github.com> Date: Fri, 15 Sep 2023 14:30:06 +1000 Subject: [PATCH 5/9] Add ticket to test --- .../phpunit/tests/block-supports/wpRenderBackgroundSupport.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/phpunit/tests/block-supports/wpRenderBackgroundSupport.php b/tests/phpunit/tests/block-supports/wpRenderBackgroundSupport.php index 208a3bec1e767..9644059d28a72 100644 --- a/tests/phpunit/tests/block-supports/wpRenderBackgroundSupport.php +++ b/tests/phpunit/tests/block-supports/wpRenderBackgroundSupport.php @@ -67,6 +67,8 @@ public function filter_set_theme_root() { /** * Tests that background image block support works as expected. * + * @ticket 59357 + * * @covers ::wp_render_background_support * * @dataProvider data_background_block_support From f2723a4a6a87ef4fcf7274d3d70108be644ff791 Mon Sep 17 00:00:00 2001 From: Andrew Serong <14988353+andrewserong@users.noreply.github.com> Date: Fri, 15 Sep 2023 16:01:02 +1000 Subject: [PATCH 6/9] Update src/wp-includes/block-supports/background.php Co-authored-by: Mukesh Panchal --- src/wp-includes/block-supports/background.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wp-includes/block-supports/background.php b/src/wp-includes/block-supports/background.php index 079359018114a..6db1e22587cc8 100644 --- a/src/wp-includes/block-supports/background.php +++ b/src/wp-includes/block-supports/background.php @@ -44,7 +44,7 @@ function wp_register_background_support( $block_type ) { * * @param string $block_content Rendered block content. * @param array $block Block object. - * @return string Filtered block content. + * @return string Filtered block content. */ function wp_render_background_support( $block_content, $block ) { $block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] ); From dd57bccfd04cf88b55b0840c48afa407a261b70e Mon Sep 17 00:00:00 2001 From: Andrew Serong <14988353+andrewserong@users.noreply.github.com> Date: Fri, 15 Sep 2023 16:02:25 +1000 Subject: [PATCH 7/9] Update src/wp-includes/class-wp-theme-json.php Co-authored-by: Mukesh Panchal --- src/wp-includes/class-wp-theme-json.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/class-wp-theme-json.php b/src/wp-includes/class-wp-theme-json.php index e01a4102e7716..dbc918c470d41 100644 --- a/src/wp-includes/class-wp-theme-json.php +++ b/src/wp-includes/class-wp-theme-json.php @@ -342,8 +342,8 @@ class WP_Theme_JSON { * @since 6.2.0 Added `dimensions.minHeight`, 'shadow.presets', 'shadow.defaultPresets', * `position.fixed` and `position.sticky`. * @since 6.3.0 Added support for `typography.textColumns`, removed `layout.definitions`. - * @since 6.4.0 Added `layout.allowEditing`, `background.backgroundImage`, and `typography.writingMode`. - * + * @since 6.4.0 Added support for `layout.allowEditing`, `background.backgroundImage`, + * and `typography.writingMode`. * @var array */ const VALID_SETTINGS = array( From 395186759308f3af0aca5f3f16661cc79516c67a Mon Sep 17 00:00:00 2001 From: Andrew Serong <14988353+andrewserong@users.noreply.github.com> Date: Fri, 15 Sep 2023 16:02:46 +1000 Subject: [PATCH 8/9] Update src/wp-includes/style-engine/class-wp-style-engine.php Co-authored-by: Mukesh Panchal --- src/wp-includes/style-engine/class-wp-style-engine.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/wp-includes/style-engine/class-wp-style-engine.php b/src/wp-includes/style-engine/class-wp-style-engine.php index bb7b4caba917f..3c40bfd574579 100644 --- a/src/wp-includes/style-engine/class-wp-style-engine.php +++ b/src/wp-includes/style-engine/class-wp-style-engine.php @@ -615,7 +615,6 @@ protected static function get_individual_property_css_declarations( $style_value * * @param array $style_value A single raw style value from $block_styles array. * @param array $style_definition A single style definition from BLOCK_STYLE_DEFINITIONS_METADATA. - * * @return string[] An associative array of CSS definitions, e.g., array( "$property" => "$value", "$property" => "$value" ). */ protected static function get_url_or_value_css_declaration( $style_value, $style_definition ) { From b5dfb019adbd0bc1e7ca560b6c5b590e70fd7f17 Mon Sep 17 00:00:00 2001 From: Andrew Serong <14988353+andrewserong@users.noreply.github.com> Date: Fri, 15 Sep 2023 16:02:54 +1000 Subject: [PATCH 9/9] Update tests/phpunit/tests/block-supports/wpRenderBackgroundSupport.php Co-authored-by: Mukesh Panchal --- tests/phpunit/tests/block-supports/wpRenderBackgroundSupport.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/phpunit/tests/block-supports/wpRenderBackgroundSupport.php b/tests/phpunit/tests/block-supports/wpRenderBackgroundSupport.php index 9644059d28a72..4f38db87ab317 100644 --- a/tests/phpunit/tests/block-supports/wpRenderBackgroundSupport.php +++ b/tests/phpunit/tests/block-supports/wpRenderBackgroundSupport.php @@ -1,5 +1,4 @@