From 419f2c82a7609bd36331de011fd0017432fecad9 Mon Sep 17 00:00:00 2001 From: Luigi Date: Tue, 29 Mar 2022 17:44:15 +0200 Subject: [PATCH 1/4] Mini Cart block - fix translations handling #6153 Mini Cart block - fix translations handling --- src/BlockTypes/MiniCart.php | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/src/BlockTypes/MiniCart.php b/src/BlockTypes/MiniCart.php index 8212927ff7e..437dfbcd522 100644 --- a/src/BlockTypes/MiniCart.php +++ b/src/BlockTypes/MiniCart.php @@ -260,11 +260,10 @@ protected function append_script_and_deps_src( $script ) { return; } $this->scripts_to_lazy_load[ $script->handle ] = array( - 'src' => $script->src, - 'version' => $script->ver, - 'before' => $wp_scripts->print_inline_script( $script->handle, 'before', false ), - 'after' => $wp_scripts->print_inline_script( $script->handle, 'after', false ), - 'translations' => $wp_scripts->print_translations( $script->handle, false ), + 'src' => $script->src, + 'version' => $script->ver, + 'before' => $wp_scripts->print_inline_script( $script->handle, 'before', false ), + 'after' => $wp_scripts->print_inline_script( $script->handle, 'after', false ), ); } @@ -442,4 +441,26 @@ protected function get_tax_label() { protected function get_cart_payload() { return WC()->api->get_endpoint_data( '/wc/store/cart' ); } + + /** + * Register script and style assets for the block type before it is registered. + * + * This registers the scripts; it does not enqueue them. + * The children blocks are register here because this block handles the loading of the frontend scripts. + */ + protected function register_block_type_assets() { + parent::register_block_type_assets(); + $blocks = [ + 'mini-cart-contents-block/filled-cart', + 'mini-cart-contents-block/empty-cart', + 'mini-cart-contents-block/title', + 'mini-cart-contents-block/items', + 'mini-cart-contents-block/products-table', + 'mini-cart-contents-block/footer', + 'mini-cart-contents-block/shopping-button', + ]; + $chunks = preg_filter( '/$/', '-frontend', $blocks ); + + $this->register_chunk_translations( $chunks ); + } } From a559c8fb4af9937530c7e7d1a56efa7b8e91ae02 Mon Sep 17 00:00:00 2001 From: Tung Du Date: Wed, 30 Mar 2022 16:35:04 +0700 Subject: [PATCH 2/4] Mini Cart block - fix translations handling (#6164) --- src/BlockTypes/MiniCart.php | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/src/BlockTypes/MiniCart.php b/src/BlockTypes/MiniCart.php index 437dfbcd522..fc3ade6da69 100644 --- a/src/BlockTypes/MiniCart.php +++ b/src/BlockTypes/MiniCart.php @@ -161,8 +161,9 @@ protected function enqueue_data( array $attributes = [] ) { } $this->scripts_to_lazy_load['wc-block-mini-cart-component-frontend'] = array( - 'src' => $script_data['src'], - 'version' => $script_data['version'], + 'src' => $script_data['src'], + 'version' => $script_data['version'], + 'translations' => $this->get_dependencies_translations(), ); $this->asset_data_registry->add( @@ -443,13 +444,12 @@ protected function get_cart_payload() { } /** - * Register script and style assets for the block type before it is registered. - * - * This registers the scripts; it does not enqueue them. - * The children blocks are register here because this block handles the loading of the frontend scripts. + * Prepare translations for inner blocks and dependencies. */ - protected function register_block_type_assets() { - parent::register_block_type_assets(); + protected function get_dependencies_translations() { + $wp_scripts = wp_scripts(); + $translations = array(); + $blocks = [ 'mini-cart-contents-block/filled-cart', 'mini-cart-contents-block/empty-cart', @@ -461,6 +461,15 @@ protected function register_block_type_assets() { ]; $chunks = preg_filter( '/$/', '-frontend', $blocks ); - $this->register_chunk_translations( $chunks ); + foreach ( $chunks as $chunk ) { + $handle = 'wc-blocks-' . $chunk . '-chunk'; + $this->asset_api->register_script( $handle, $this->asset_api->get_block_asset_build_path( $chunk ), [], true ); + $translations[] = $wp_scripts->print_translations( $handle, false ); + wp_deregister_script( $handle ); + } + + $translations = array_filter( $translations ); + + return implode( '', $translations ); } } From 4765958d836c2e3c55084501c89f104ec9889bc3 Mon Sep 17 00:00:00 2001 From: Luigi Teschio Date: Wed, 30 Mar 2022 14:36:56 +0200 Subject: [PATCH 3/4] loads translations for deps Co-authored-by: Tung Du --- src/BlockTypes/MiniCart.php | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/BlockTypes/MiniCart.php b/src/BlockTypes/MiniCart.php index fc3ade6da69..cf8445645e4 100644 --- a/src/BlockTypes/MiniCart.php +++ b/src/BlockTypes/MiniCart.php @@ -447,6 +447,15 @@ protected function get_cart_payload() { * Prepare translations for inner blocks and dependencies. */ protected function get_dependencies_translations() { + /** + * Temporary remove the this filter so $wp_scripts->print_translations + * calls won't accident print the translations scripts for the block. + * + * $wp_scripts->print_translations() calls load_script_textdomain() + * which calls load_script_translations() containing the below filter. + */ + remove_filter( 'pre_load_script_translations', 'woocommerce_blocks_get_i18n_data_json', 10, 4 ); + $wp_scripts = wp_scripts(); $translations = array(); @@ -468,8 +477,15 @@ protected function get_dependencies_translations() { wp_deregister_script( $handle ); } + foreach ( array_keys( $this->scripts_to_lazy_load ) as $script ) { + $translations[] = $wp_scripts->print_translations( $script, false ); + } + $translations = array_filter( $translations ); + // Re-add the filter. + add_filter( 'pre_load_script_translations', 'woocommerce_blocks_get_i18n_data_json', 10, 4 ); + return implode( '', $translations ); } } From cbadbfe357b071c90893518e9e066a0460722484 Mon Sep 17 00:00:00 2001 From: Tung Du Date: Thu, 31 Mar 2022 22:35:15 +0700 Subject: [PATCH 4/4] address code review. load translations close to associated scripts --- src/BlockTypes/MiniCart.php | 42 ++++++++++++++++++------------------- 1 file changed, 20 insertions(+), 22 deletions(-) diff --git a/src/BlockTypes/MiniCart.php b/src/BlockTypes/MiniCart.php index cf8445645e4..24b57a20881 100644 --- a/src/BlockTypes/MiniCart.php +++ b/src/BlockTypes/MiniCart.php @@ -133,6 +133,16 @@ protected function enqueue_data( array $attributes = [] ) { ); } + /** + * Temporary remove the this filter so $wp_scripts->print_translations + * calls won't accident print the translations scripts for the block + * when inserted as a widget. + * + * $wp_scripts->print_translations() calls load_script_textdomain() + * which calls load_script_translations() containing the below filter. + */ + remove_filter( 'pre_load_script_translations', 'woocommerce_blocks_get_i18n_data_json', 10, 4 ); + $script_data = $this->asset_api->get_script_data( 'build/mini-cart-component-frontend.js' ); $num_dependencies = count( $script_data['dependencies'] ); @@ -163,9 +173,12 @@ protected function enqueue_data( array $attributes = [] ) { $this->scripts_to_lazy_load['wc-block-mini-cart-component-frontend'] = array( 'src' => $script_data['src'], 'version' => $script_data['version'], - 'translations' => $this->get_dependencies_translations(), + 'translations' => $this->get_inner_blocks_translations(), ); + // Re-add the filter. + add_filter( 'pre_load_script_translations', 'woocommerce_blocks_get_i18n_data_json', 10, 4 ); + $this->asset_data_registry->add( 'mini_cart_block_frontend_dependencies', $this->scripts_to_lazy_load, @@ -261,10 +274,11 @@ protected function append_script_and_deps_src( $script ) { return; } $this->scripts_to_lazy_load[ $script->handle ] = array( - 'src' => $script->src, - 'version' => $script->ver, - 'before' => $wp_scripts->print_inline_script( $script->handle, 'before', false ), - 'after' => $wp_scripts->print_inline_script( $script->handle, 'after', false ), + 'src' => $script->src, + 'version' => $script->ver, + 'before' => $wp_scripts->print_inline_script( $script->handle, 'before', false ), + 'after' => $wp_scripts->print_inline_script( $script->handle, 'after', false ), + 'translations' => $wp_scripts->print_translations( $script->handle, false ), ); } @@ -446,16 +460,7 @@ protected function get_cart_payload() { /** * Prepare translations for inner blocks and dependencies. */ - protected function get_dependencies_translations() { - /** - * Temporary remove the this filter so $wp_scripts->print_translations - * calls won't accident print the translations scripts for the block. - * - * $wp_scripts->print_translations() calls load_script_textdomain() - * which calls load_script_translations() containing the below filter. - */ - remove_filter( 'pre_load_script_translations', 'woocommerce_blocks_get_i18n_data_json', 10, 4 ); - + protected function get_inner_blocks_translations() { $wp_scripts = wp_scripts(); $translations = array(); @@ -477,15 +482,8 @@ protected function get_dependencies_translations() { wp_deregister_script( $handle ); } - foreach ( array_keys( $this->scripts_to_lazy_load ) as $script ) { - $translations[] = $wp_scripts->print_translations( $script, false ); - } - $translations = array_filter( $translations ); - // Re-add the filter. - add_filter( 'pre_load_script_translations', 'woocommerce_blocks_get_i18n_data_json', 10, 4 ); - return implode( '', $translations ); } }