Skip to content

Commit

Permalink
Allowing gutenberg_enqueue_stored_styles() to pass down formatting op…
Browse files Browse the repository at this point in the history
…tions 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.
  • Loading branch information
ramonjd authored and ockham committed Sep 19, 2022
1 parent 49077b7 commit 964827e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 16 deletions.
27 changes: 19 additions & 8 deletions lib/compat/wordpress-6.1/script-loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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 );
Expand All @@ -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 );
Expand Down
19 changes: 11 additions & 8 deletions phpunit/script-loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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',
Expand All @@ -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(
Expand All @@ -51,23 +52,25 @@ 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(
'context' => 'my-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.'
Expand Down

0 comments on commit 964827e

Please sign in to comment.