From e5e99868736d225e02fe0774baf2e4f8deb7b5fc Mon Sep 17 00:00:00 2001 From: Ramon Date: Tue, 4 Oct 2022 08:40:29 +1100 Subject: [PATCH] Script loader: remove 6.1 wp actions (#44519) * Now that 6.1-beta is running in development, we should remove the backported hook callbacks for wp_enqueue_stored_styles. This prevents the callback running twice. * Move `gutenberg_enqueue_stored_styles` to client-assets.php because it should not be versioned It is envisaged that Gutenberg will continue to use the Style Engine's `gutenberg_*` functions and `_Gutenberg` classes to aid continuou.s development. * Update lib/client-assets.php Co-authored-by: Ari Stathopoulos Co-authored-by: Ari Stathopoulos --- lib/client-assets.php | 93 ++++++++++++++++++++++ lib/compat/wordpress-6.1/script-loader.php | 69 ---------------- 2 files changed, 93 insertions(+), 69 deletions(-) diff --git a/lib/client-assets.php b/lib/client-assets.php index 9dad2f597fa3b4..905aef70cb4d29 100644 --- a/lib/client-assets.php +++ b/lib/client-assets.php @@ -445,3 +445,96 @@ function gutenberg_register_packages_styles( $styles ) { $styles->add_data( 'wp-widgets', 'rtl', 'replace' ); } add_action( 'wp_default_styles', 'gutenberg_register_packages_styles' ); + +/** + * Fetches, processes and compiles stored core styles, then combines and renders them to the page. + * Styles are stored via the Style Engine API. + * + * This hook also exists, and should be backported to Core in future versions. + * However, it is envisaged that Gutenberg will continue to use the Style Engine's `gutenberg_*` functions and `_Gutenberg` classes to aid continuous development. + * + * 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. + * } + * + * @since 6.1 + * + * @return void + */ +function gutenberg_enqueue_stored_styles( $options = array() ) { + $is_block_theme = wp_is_block_theme(); + $is_classic_theme = ! $is_block_theme; + + /* + * For block themes, print stored styles in the header. + * For classic themes, in the footer. + */ + if ( + ( $is_block_theme && doing_action( 'wp_footer' ) ) || + ( $is_classic_theme && doing_action( 'wp_enqueue_scripts' ) ) + ) { + return; + } + + $core_styles_keys = array( 'block-supports' ); + $compiled_core_stylesheet = ''; + $style_tag_id = 'core'; + foreach ( $core_styles_keys as $style_key ) { + // Adds comment if code is prettified to identify core styles sections in debugging. + $should_prettify = isset( $options['prettify'] ) ? true === $options['prettify'] : defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG; + if ( $should_prettify ) { + $compiled_core_stylesheet .= "/**\n * Core styles: $style_key\n */\n"; + } + // 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, $options ); + } + + // 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 ); + wp_enqueue_style( $style_tag_id ); + } + + // If there are any other stores registered by themes etc., print them out. + $additional_stores = WP_Style_Engine_CSS_Rules_Store_Gutenberg::get_stores(); + + /* + * Since the corresponding action hook in Core is removed below, + * this function should still honour any styles stored using the Core Style Engine store. + */ + if ( class_exists( 'WP_Style_Engine_CSS_Rules_Store' ) ) { + $additional_stores = array_merge( $additional_stores, WP_Style_Engine_CSS_Rules_Store::get_stores() ); + } + + foreach ( array_keys( $additional_stores ) as $store_name ) { + if ( in_array( $store_name, $core_styles_keys, true ) ) { + continue; + } + $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 ); + wp_add_inline_style( $key, $styles ); + wp_enqueue_style( $key ); + } + } +} + +/* + * Always remove the Core action hook while gutenberg_enqueue_stored_styles() exists to avoid styles being printed twice. + * This is also because gutenberg_enqueue_stored_styles uses the Style Engine's `gutenberg_*` functions and `_Gutenberg` classes, + * which are in continuous development and generally ahead of Core. + */ +remove_action( 'wp_enqueue_scripts', 'wp_enqueue_stored_styles' ); +remove_action( 'wp_footer', 'wp_enqueue_stored_styles', 1 ); + +// Enqueue stored styles. +add_action( 'wp_enqueue_scripts', 'gutenberg_enqueue_stored_styles' ); +add_action( 'wp_footer', 'gutenberg_enqueue_stored_styles', 1 ); diff --git a/lib/compat/wordpress-6.1/script-loader.php b/lib/compat/wordpress-6.1/script-loader.php index 8a93455b90f897..dc0d7e7dfe0f32 100644 --- a/lib/compat/wordpress-6.1/script-loader.php +++ b/lib/compat/wordpress-6.1/script-loader.php @@ -36,73 +36,6 @@ 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: 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( $options = array() ) { - $is_block_theme = wp_is_block_theme(); - $is_classic_theme = ! $is_block_theme; - - /* - * For block themes, print stored styles in the header. - * For classic themes, in the footer. - */ - if ( - ( $is_block_theme && doing_action( 'wp_footer' ) ) || - ( $is_classic_theme && doing_action( 'wp_enqueue_scripts' ) ) - ) { - return; - } - - $core_styles_keys = array( 'block-supports' ); - $compiled_core_stylesheet = ''; - $style_tag_id = 'core'; - foreach ( $core_styles_keys as $style_key ) { - // Adds comment if code is prettified to identify core styles sections in debugging. - $should_prettify = isset( $options['prettify'] ) ? true === $options['prettify'] : defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG; - if ( $should_prettify ) { - $compiled_core_stylesheet .= "/**\n * Core styles: $style_key\n */\n"; - } - // 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, $options ); - } - - // 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 ); - wp_enqueue_style( $style_tag_id ); - } - - // If there are any other stores registered by themes etc, print them out. - $additional_stores = WP_Style_Engine_CSS_Rules_Store_Gutenberg::get_stores(); - foreach ( array_keys( $additional_stores ) as $store_name ) { - if ( in_array( $store_name, $core_styles_keys, true ) ) { - continue; - } - $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 ); - wp_add_inline_style( $key, $styles ); - wp_enqueue_style( $key ); - } - } -} - /** * This applies a filter to the list of style nodes that comes from `get_style_nodes` in WP_Theme_JSON. * This particular filter removes all of the blocks from the array. @@ -179,8 +112,6 @@ function gutenberg_enqueue_global_styles() { // Enqueue global styles, and then block supports styles. add_action( 'wp_enqueue_scripts', 'gutenberg_enqueue_global_styles' ); add_action( 'wp_footer', 'gutenberg_enqueue_global_styles', 1 ); -add_action( 'wp_enqueue_scripts', 'gutenberg_enqueue_stored_styles' ); -add_action( 'wp_footer', 'gutenberg_enqueue_stored_styles', 1 ); /** * Loads classic theme styles on classic themes.