From 9b79fb189ea610ff635f21d32c290f9a22edec38 Mon Sep 17 00:00:00 2001 From: Ramon Date: Mon, 19 Sep 2022 14:51:26 +1000 Subject: [PATCH] Allowing gutenberg_enqueue_stored_styles() to pass down formatting options to gutenberg_style_engine_get_stylesheet_from_context(). (#44248) This is so tests and other usages of gutenberg_enqueue_stored_styles() can bypass, or at least don't have to rely on, the global constant `SCRIPT_DEBUG` to determine whether the output is prettified. --- lib/compat/wordpress-6.1/script-loader.php | 27 +++++++++++++++------- phpunit/script-loader.php | 19 ++++++++------- 2 files changed, 30 insertions(+), 16 deletions(-) diff --git a/lib/compat/wordpress-6.1/script-loader.php b/lib/compat/wordpress-6.1/script-loader.php index e32b72f0ce664..9c138ca88f4db 100644 --- a/lib/compat/wordpress-6.1/script-loader.php +++ b/lib/compat/wordpress-6.1/script-loader.php @@ -38,9 +38,20 @@ static function () use ( $style ) { /** * Fetches, processes and compiles stored core styles, then combines and renders them to the page. - * Styles are stored via the style engine API. See: packages/style-engine/README.md + * Styles are stored via the style engine API. + * + * See: https://developer.wordpress.org/block-editor/reference-guides/packages/packages-style-engine/ + * + * @param array $options { + * Optional. An array of options to pass to gutenberg_style_engine_get_stylesheet_from_context(). Default empty array. + * + * @type bool $optimize Whether to optimize the CSS output, e.g., combine rules. Default is `false`. + * @type bool $prettify Whether to add new lines and indents to output. Default is the test of whether the global constant `SCRIPT_DEBUG` is defined. + * } + * + * @return void */ -function gutenberg_enqueue_stored_styles() { +function gutenberg_enqueue_stored_styles( $options = array() ) { $is_block_theme = wp_is_block_theme(); $is_classic_theme = ! $is_block_theme; @@ -59,16 +70,16 @@ function gutenberg_enqueue_stored_styles() { $compiled_core_stylesheet = ''; $style_tag_id = 'core'; foreach ( $core_styles_keys as $style_key ) { - // Add comment to identify core styles sections in debugging. - if ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) { + // Adds comment to identify core styles sections in debugging. + if ( ( isset( $options['prettify'] ) && true === $options['prettify'] ) || ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ) { $compiled_core_stylesheet .= "/**\n * Core styles: $style_key\n */\n"; } - // Chain core store ids to signify what the styles contain. + // Chains core store ids to signify what the styles contain. $style_tag_id .= '-' . $style_key; - $compiled_core_stylesheet .= gutenberg_style_engine_get_stylesheet_from_context( $style_key ); + $compiled_core_stylesheet .= gutenberg_style_engine_get_stylesheet_from_context( $style_key, $options ); } - // Combine Core styles. + // Combines Core styles. if ( ! empty( $compiled_core_stylesheet ) ) { wp_register_style( $style_tag_id, false, array(), true, true ); wp_add_inline_style( $style_tag_id, $compiled_core_stylesheet ); @@ -81,7 +92,7 @@ function gutenberg_enqueue_stored_styles() { if ( in_array( $store_name, $core_styles_keys, true ) ) { continue; } - $styles = gutenberg_style_engine_get_stylesheet_from_context( $store_name ); + $styles = gutenberg_style_engine_get_stylesheet_from_context( $store_name, $options ); if ( ! empty( $styles ) ) { $key = "wp-style-engine-$store_name"; wp_register_style( $key, false, array(), true, true ); diff --git a/phpunit/script-loader.php b/phpunit/script-loader.php index bb11f6cd60db3..7f4f80bec575f 100644 --- a/phpunit/script-loader.php +++ b/phpunit/script-loader.php @@ -7,9 +7,8 @@ class WP_Script_Loader_Test extends WP_UnitTestCase { /** - * Clean up global scope. + * Cleans up global scope. * - * @global WP_Scripts $wp_scripts * @global WP_Styles $wp_styles */ public function clean_up_global_scope() { @@ -19,8 +18,10 @@ public function clean_up_global_scope() { } /** * Tests that stored CSS is enqueued. + * + * @covers ::wp_enqueue_stored_styles */ - public function test_enqueue_stored_styles() { + public function test_should_enqueue_stored_styles() { $core_styles_to_enqueue = array( array( 'selector' => '.saruman', @@ -32,7 +33,7 @@ public function test_enqueue_stored_styles() { ), ); - // Enqueue a block supports (core styles). + // Enqueues a block supports (core styles). gutenberg_style_engine_get_stylesheet_from_css_rules( $core_styles_to_enqueue, array( @@ -51,7 +52,7 @@ public function test_enqueue_stored_styles() { ), ); - // Enqueue some other styles. + // Enqueues some other styles. gutenberg_style_engine_get_stylesheet_from_css_rules( $my_styles_to_enqueue, array( @@ -59,15 +60,17 @@ public function test_enqueue_stored_styles() { ) ); - gutenberg_enqueue_stored_styles(); + gutenberg_enqueue_stored_styles( + array( 'prettify' => false ) + ); - $this->assertEquals( + $this->assertSame( array( '.saruman{color:white;height:100px;border-style:solid;}' ), wp_styles()->registered['core-block-supports']->extra['after'], 'Registered styles with handle of "core-block-supports" do not match expected value from Style Engine store.' ); - $this->assertEquals( + $this->assertSame( array( '.gandalf{color:grey;height:90px;border-style:dotted;}' ), wp_styles()->registered['wp-style-engine-my-styles']->extra['after'], 'Registered styles with handle of "wp-style-engine-my-styles" do not match expected value from the Style Engine store.'