From 7ce3a91bcfda5b4f813d847fb9d475279e669778 Mon Sep 17 00:00:00 2001 From: Anton Vlasenko Date: Mon, 12 Sep 2022 21:34:19 +0200 Subject: [PATCH 1/4] Rename filter_out_block_nodes to gutenberg_filter_out_block_nodes. Compatibility code should be prefixed with gutenberg_. --- lib/compat/wordpress-6.1/script-loader.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/compat/wordpress-6.1/script-loader.php b/lib/compat/wordpress-6.1/script-loader.php index e6a42faf932f8b..ba3d2c11c876ef 100644 --- a/lib/compat/wordpress-6.1/script-loader.php +++ b/lib/compat/wordpress-6.1/script-loader.php @@ -104,7 +104,7 @@ function gutenberg_enqueue_stored_styles() { * @param array $nodes The nodes to filter. * @return array A filtered array of style nodes. */ -function filter_out_block_nodes( $nodes ) { +function gutenberg_filter_out_block_nodes( $nodes ) { return array_filter( $nodes, function( $node ) { @@ -144,7 +144,7 @@ function gutenberg_enqueue_global_styles() { * This removes the CSS from the global-styles stylesheet and adds it to the inline CSS for each block. * This filter has to be registered before we call gutenberg_get_global_stylesheet(); */ - add_filter( 'gutenberg_get_style_nodes', 'filter_out_block_nodes', 10, 1 ); + add_filter( 'gutenberg_get_style_nodes', 'gutenberg_filter_out_block_nodes', 10, 1 ); $stylesheet = gutenberg_get_global_stylesheet(); if ( empty( $stylesheet ) ) { From d3739ffc86d31967a78e06fa5a9b7766ff6b75a8 Mon Sep 17 00:00:00 2001 From: Anton Vlasenko Date: Mon, 12 Sep 2022 22:25:37 +0200 Subject: [PATCH 2/4] Deprecate wp_enqueue_block_view_script in Gutenberg as Gutenberg functions should use the gutenberg_ prefix. --- lib/compat/wordpress-6.1/blocks.php | 129 ++++++++++++++++------------ 1 file changed, 74 insertions(+), 55 deletions(-) diff --git a/lib/compat/wordpress-6.1/blocks.php b/lib/compat/wordpress-6.1/blocks.php index a33515632099b3..e7f29addffc8e3 100644 --- a/lib/compat/wordpress-6.1/blocks.php +++ b/lib/compat/wordpress-6.1/blocks.php @@ -102,6 +102,77 @@ function gutenberg_block_type_metadata_view_script( $settings, $metadata ) { } add_filter( 'block_type_metadata_settings', 'gutenberg_block_type_metadata_view_script', 10, 2 ); +/** + * Enqueues a frontend script for a specific block. + * + * Scripts enqueued using this function will only get printed + * when the block gets rendered on the frontend. + * + * @since 6.1.0 + * + * @param string $block_name The block name, including namespace. + * @param array $args An array of arguments [handle,src,deps,ver,media,textdomain]. + * + * @return void + */ +function gutenberg_enqueue_block_view_script( $block_name, $args ) { + $args = wp_parse_args( + $args, + array( + 'handle' => '', + 'src' => '', + 'deps' => array(), + 'ver' => false, + 'in_footer' => false, + + // Additional args to allow translations for the script's textdomain. + 'textdomain' => '', + ) + ); + + /** + * Callback function to register and enqueue scripts. + * + * @param string $content When the callback is used for the render_block filter, + * the content needs to be returned so the function parameter + * is to ensure the content exists. + * @return string Block content. + */ + $callback = static function( $content, $block ) use ( $args, $block_name ) { + + // Sanity check. + if ( empty( $block['blockName'] ) || $block_name !== $block['blockName'] ) { + return $content; + } + + // Register the stylesheet. + if ( ! empty( $args['src'] ) ) { + wp_register_script( $args['handle'], $args['src'], $args['deps'], $args['ver'], $args['in_footer'] ); + } + + // Enqueue the stylesheet. + wp_enqueue_script( $args['handle'] ); + + // If a textdomain is defined, use it to set the script translations. + if ( ! empty( $args['textdomain'] ) && in_array( 'wp-i18n', $args['deps'], true ) ) { + wp_set_script_translations( $args['handle'], $args['textdomain'], $args['domainpath'] ); + } + + return $content; + }; + + /* + * The filter's callback here is an anonymous function because + * using a named function in this case is not possible. + * + * The function cannot be unhooked, however, users are still able + * to dequeue the script registered/enqueued by the callback + * which is why in this case, using an anonymous function + * was deemed acceptable. + */ + add_filter( 'render_block', $callback, 10, 2 ); +} + if ( ! function_exists( 'wp_enqueue_block_view_script' ) ) { /** * Enqueues a frontend script for a specific block. @@ -114,64 +185,12 @@ function gutenberg_block_type_metadata_view_script( $settings, $metadata ) { * @param string $block_name The block name, including namespace. * @param array $args An array of arguments [handle,src,deps,ver,media,textdomain]. * + * @deprecated 6.1.0 Please use gutenberg_enqueue_block_view_script instead. + * * @return void */ function wp_enqueue_block_view_script( $block_name, $args ) { - $args = wp_parse_args( - $args, - array( - 'handle' => '', - 'src' => '', - 'deps' => array(), - 'ver' => false, - 'in_footer' => false, - - // Additional args to allow translations for the script's textdomain. - 'textdomain' => '', - ) - ); - - /** - * Callback function to register and enqueue scripts. - * - * @param string $content When the callback is used for the render_block filter, - * the content needs to be returned so the function parameter - * is to ensure the content exists. - * @return string Block content. - */ - $callback = static function( $content, $block ) use ( $args, $block_name ) { - - // Sanity check. - if ( empty( $block['blockName'] ) || $block_name !== $block['blockName'] ) { - return $content; - } - - // Register the stylesheet. - if ( ! empty( $args['src'] ) ) { - wp_register_script( $args['handle'], $args['src'], $args['deps'], $args['ver'], $args['in_footer'] ); - } - - // Enqueue the stylesheet. - wp_enqueue_script( $args['handle'] ); - - // If a textdomain is defined, use it to set the script translations. - if ( ! empty( $args['textdomain'] ) && in_array( 'wp-i18n', $args['deps'], true ) ) { - wp_set_script_translations( $args['handle'], $args['textdomain'], $args['domainpath'] ); - } - - return $content; - }; - - /* - * The filter's callback here is an anonymous function because - * using a named function in this case is not possible. - * - * The function cannot be unhooked, however, users are still able - * to dequeue the script registered/enqueued by the callback - * which is why in this case, using an anonymous function - * was deemed acceptable. - */ - add_filter( 'render_block', $callback, 10, 2 ); + return gutenberg_enqueue_block_view_script( $block_name, $args ); } } From 275297038c087456d8c58e131f2bee158d5479ca Mon Sep 17 00:00:00 2001 From: Anton Vlasenko Date: Mon, 12 Sep 2022 22:42:10 +0200 Subject: [PATCH 3/4] Fix CS. --- lib/compat/wordpress-6.1/blocks.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/compat/wordpress-6.1/blocks.php b/lib/compat/wordpress-6.1/blocks.php index e7f29addffc8e3..994d9608b83873 100644 --- a/lib/compat/wordpress-6.1/blocks.php +++ b/lib/compat/wordpress-6.1/blocks.php @@ -190,7 +190,7 @@ function gutenberg_enqueue_block_view_script( $block_name, $args ) { * @return void */ function wp_enqueue_block_view_script( $block_name, $args ) { - return gutenberg_enqueue_block_view_script( $block_name, $args ); + gutenberg_enqueue_block_view_script( $block_name, $args ); } } From 54da4412d486f1e7b4e3aa45cf9418a1f15fac94 Mon Sep 17 00:00:00 2001 From: Anton Vlasenko Date: Tue, 13 Sep 2022 07:26:51 +0200 Subject: [PATCH 4/4] Revert the changes back. Why? Please see https://github.com/WordPress/gutenberg/blob/trunk/lib/README.md#prefer-the-wp-prefix --- lib/compat/wordpress-6.1/blocks.php | 129 ++++++++++++---------------- 1 file changed, 55 insertions(+), 74 deletions(-) diff --git a/lib/compat/wordpress-6.1/blocks.php b/lib/compat/wordpress-6.1/blocks.php index 994d9608b83873..a33515632099b3 100644 --- a/lib/compat/wordpress-6.1/blocks.php +++ b/lib/compat/wordpress-6.1/blocks.php @@ -102,77 +102,6 @@ function gutenberg_block_type_metadata_view_script( $settings, $metadata ) { } add_filter( 'block_type_metadata_settings', 'gutenberg_block_type_metadata_view_script', 10, 2 ); -/** - * Enqueues a frontend script for a specific block. - * - * Scripts enqueued using this function will only get printed - * when the block gets rendered on the frontend. - * - * @since 6.1.0 - * - * @param string $block_name The block name, including namespace. - * @param array $args An array of arguments [handle,src,deps,ver,media,textdomain]. - * - * @return void - */ -function gutenberg_enqueue_block_view_script( $block_name, $args ) { - $args = wp_parse_args( - $args, - array( - 'handle' => '', - 'src' => '', - 'deps' => array(), - 'ver' => false, - 'in_footer' => false, - - // Additional args to allow translations for the script's textdomain. - 'textdomain' => '', - ) - ); - - /** - * Callback function to register and enqueue scripts. - * - * @param string $content When the callback is used for the render_block filter, - * the content needs to be returned so the function parameter - * is to ensure the content exists. - * @return string Block content. - */ - $callback = static function( $content, $block ) use ( $args, $block_name ) { - - // Sanity check. - if ( empty( $block['blockName'] ) || $block_name !== $block['blockName'] ) { - return $content; - } - - // Register the stylesheet. - if ( ! empty( $args['src'] ) ) { - wp_register_script( $args['handle'], $args['src'], $args['deps'], $args['ver'], $args['in_footer'] ); - } - - // Enqueue the stylesheet. - wp_enqueue_script( $args['handle'] ); - - // If a textdomain is defined, use it to set the script translations. - if ( ! empty( $args['textdomain'] ) && in_array( 'wp-i18n', $args['deps'], true ) ) { - wp_set_script_translations( $args['handle'], $args['textdomain'], $args['domainpath'] ); - } - - return $content; - }; - - /* - * The filter's callback here is an anonymous function because - * using a named function in this case is not possible. - * - * The function cannot be unhooked, however, users are still able - * to dequeue the script registered/enqueued by the callback - * which is why in this case, using an anonymous function - * was deemed acceptable. - */ - add_filter( 'render_block', $callback, 10, 2 ); -} - if ( ! function_exists( 'wp_enqueue_block_view_script' ) ) { /** * Enqueues a frontend script for a specific block. @@ -185,12 +114,64 @@ function gutenberg_enqueue_block_view_script( $block_name, $args ) { * @param string $block_name The block name, including namespace. * @param array $args An array of arguments [handle,src,deps,ver,media,textdomain]. * - * @deprecated 6.1.0 Please use gutenberg_enqueue_block_view_script instead. - * * @return void */ function wp_enqueue_block_view_script( $block_name, $args ) { - gutenberg_enqueue_block_view_script( $block_name, $args ); + $args = wp_parse_args( + $args, + array( + 'handle' => '', + 'src' => '', + 'deps' => array(), + 'ver' => false, + 'in_footer' => false, + + // Additional args to allow translations for the script's textdomain. + 'textdomain' => '', + ) + ); + + /** + * Callback function to register and enqueue scripts. + * + * @param string $content When the callback is used for the render_block filter, + * the content needs to be returned so the function parameter + * is to ensure the content exists. + * @return string Block content. + */ + $callback = static function( $content, $block ) use ( $args, $block_name ) { + + // Sanity check. + if ( empty( $block['blockName'] ) || $block_name !== $block['blockName'] ) { + return $content; + } + + // Register the stylesheet. + if ( ! empty( $args['src'] ) ) { + wp_register_script( $args['handle'], $args['src'], $args['deps'], $args['ver'], $args['in_footer'] ); + } + + // Enqueue the stylesheet. + wp_enqueue_script( $args['handle'] ); + + // If a textdomain is defined, use it to set the script translations. + if ( ! empty( $args['textdomain'] ) && in_array( 'wp-i18n', $args['deps'], true ) ) { + wp_set_script_translations( $args['handle'], $args['textdomain'], $args['domainpath'] ); + } + + return $content; + }; + + /* + * The filter's callback here is an anonymous function because + * using a named function in this case is not possible. + * + * The function cannot be unhooked, however, users are still able + * to dequeue the script registered/enqueued by the callback + * which is why in this case, using an anonymous function + * was deemed acceptable. + */ + add_filter( 'render_block', $callback, 10, 2 ); } }