From d83214ed44b39f58013b3100c48509579bca3b6e Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Fri, 3 Apr 2020 14:17:32 +0100 Subject: [PATCH] Ensure `AbstractBlock::enqueue_data` is only run once in the editor request. (https://github.com/woocommerce/woocommerce-blocks/pull/2113) * Enqueue assets once * Updates classes extending AbstractBlock to use enqueue_scripts --- .../src/BlockTypes/AbstractBlock.php | 43 +++++++++++++++---- .../src/BlockTypes/ActiveFilters.php | 13 +++--- .../src/BlockTypes/AllReviews.php | 14 +++--- .../src/BlockTypes/AttributeFilter.php | 13 +++--- .../src/BlockTypes/Cart.php | 7 +-- .../src/BlockTypes/Checkout.php | 3 +- .../src/BlockTypes/PriceFilter.php | 13 +++--- .../src/BlockTypes/ReviewsByCategory.php | 14 +++--- .../src/BlockTypes/ReviewsByProduct.php | 14 +++--- 9 files changed, 72 insertions(+), 62 deletions(-) diff --git a/plugins/woocommerce-blocks/src/BlockTypes/AbstractBlock.php b/plugins/woocommerce-blocks/src/BlockTypes/AbstractBlock.php index afb9d704f95..f5a53a4c38f 100644 --- a/plugins/woocommerce-blocks/src/BlockTypes/AbstractBlock.php +++ b/plugins/woocommerce-blocks/src/BlockTypes/AbstractBlock.php @@ -28,6 +28,13 @@ abstract class AbstractBlock { */ protected $block_name = ''; + /** + * Tracks if assets have been enqueued. + * + * @var boolean + */ + protected $enqueued_assets = false; + /** * Constructor */ @@ -49,13 +56,6 @@ abstract class AbstractBlock { ); } - /** - * Will enqueue any editor assets a block needs to load - */ - public function enqueue_editor_assets() { - $this->enqueue_data(); - } - /** * Append frontend scripts when rendering the block. * @@ -63,10 +63,35 @@ abstract class AbstractBlock { * @param string $content Block content. Default empty string. * @return string Rendered block type output. */ - public function render( $attributes = array(), $content = '' ) { + public function render( $attributes = [], $content = '' ) { + $this->enqueue_assets( $attributes ); + return $content; + } + + /** + * Enqueue assets used for rendering the block. + * + * @param array $attributes Any attributes that currently are available from the block. + */ + public function enqueue_assets( array $attributes = [] ) { + if ( $this->enqueued_assets ) { + return; + } $this->enqueue_data( $attributes ); $this->enqueue_scripts( $attributes ); - return $content; + $this->enqueued_assets = true; + } + + /** + * Enqueue assets used for rendering the block in editor context. + * + * This is needed if a block is not yet within the post content--`render` and `enqueue_assets` may not have ran. + */ + public function enqueue_editor_assets() { + if ( $this->enqueued_assets ) { + return; + } + $this->enqueue_data(); } /** diff --git a/plugins/woocommerce-blocks/src/BlockTypes/ActiveFilters.php b/plugins/woocommerce-blocks/src/BlockTypes/ActiveFilters.php index 3dc19e8cf5b..6777c2600b4 100644 --- a/plugins/woocommerce-blocks/src/BlockTypes/ActiveFilters.php +++ b/plugins/woocommerce-blocks/src/BlockTypes/ActiveFilters.php @@ -9,6 +9,8 @@ namespace Automattic\WooCommerce\Blocks\BlockTypes; defined( 'ABSPATH' ) || exit; +use Automattic\WooCommerce\Blocks\Assets; + /** * ActiveFilters class. */ @@ -38,14 +40,11 @@ class ActiveFilters extends AbstractBlock { } /** - * Append frontend scripts when rendering the block. + * Register/enqueue scripts used for this block. * - * @param array $attributes Block attributes. Default empty array. - * @param string $content Block content. Default empty string. - * @return string Rendered block type output. + * @param array $attributes Any attributes that currently are available from the block. */ - public function render( $attributes = array(), $content = '' ) { - \Automattic\WooCommerce\Blocks\Assets::register_block_script( $this->block_name . '-frontend' ); - return $content; + protected function enqueue_scripts( array $attributes = [] ) { + Assets::register_block_script( $this->block_name . '-frontend' ); } } diff --git a/plugins/woocommerce-blocks/src/BlockTypes/AllReviews.php b/plugins/woocommerce-blocks/src/BlockTypes/AllReviews.php index 4067e4cf22d..f8b669ea288 100644 --- a/plugins/woocommerce-blocks/src/BlockTypes/AllReviews.php +++ b/plugins/woocommerce-blocks/src/BlockTypes/AllReviews.php @@ -9,6 +9,8 @@ namespace Automattic\WooCommerce\Blocks\BlockTypes; defined( 'ABSPATH' ) || exit; +use Automattic\WooCommerce\Blocks\Assets; + /** * AllReviews class. */ @@ -37,15 +39,11 @@ class AllReviews extends AbstractBlock { } /** - * Append frontend scripts when rendering the Product Categories List block. + * Register/enqueue scripts used for this block. * - * @param array $attributes Block attributes. Default empty array. - * @param string $content Block content. Default empty string. - * @return string Rendered block type output. + * @param array $attributes Any attributes that currently are available from the block. */ - public function render( $attributes = array(), $content = '' ) { - \Automattic\WooCommerce\Blocks\Assets::register_block_script( 'reviews-frontend' ); - - return $content; + protected function enqueue_scripts( array $attributes = [] ) { + Assets::register_block_script( 'reviews-frontend' ); } } diff --git a/plugins/woocommerce-blocks/src/BlockTypes/AttributeFilter.php b/plugins/woocommerce-blocks/src/BlockTypes/AttributeFilter.php index c379e726395..ecdee85d3b7 100644 --- a/plugins/woocommerce-blocks/src/BlockTypes/AttributeFilter.php +++ b/plugins/woocommerce-blocks/src/BlockTypes/AttributeFilter.php @@ -9,6 +9,8 @@ namespace Automattic\WooCommerce\Blocks\BlockTypes; defined( 'ABSPATH' ) || exit; +use Automattic\WooCommerce\Blocks\Assets; + /** * AttributeFilter class. */ @@ -38,14 +40,11 @@ class AttributeFilter extends AbstractBlock { } /** - * Append frontend scripts when rendering the block. + * Register/enqueue scripts used for this block. * - * @param array $attributes Block attributes. Default empty array. - * @param string $content Block content. Default empty string. - * @return string Rendered block type output. + * @param array $attributes Any attributes that currently are available from the block. */ - public function render( $attributes = array(), $content = '' ) { - \Automattic\WooCommerce\Blocks\Assets::register_block_script( $this->block_name . '-frontend' ); - return $content; + protected function enqueue_scripts( array $attributes = [] ) { + Assets::register_block_script( $this->block_name . '-frontend' ); } } diff --git a/plugins/woocommerce-blocks/src/BlockTypes/Cart.php b/plugins/woocommerce-blocks/src/BlockTypes/Cart.php index aca1812dad0..a82f821559c 100644 --- a/plugins/woocommerce-blocks/src/BlockTypes/Cart.php +++ b/plugins/woocommerce-blocks/src/BlockTypes/Cart.php @@ -50,8 +50,7 @@ class Cart extends AbstractBlock { */ public function render( $attributes = array(), $content = '' ) { do_action( 'woocommerce_blocks_enqueue_cart_block_scripts_before' ); - $this->enqueue_data( $attributes ); - $this->enqueue_scripts( $attributes ); + $this->enqueue_assets( $attributes ); do_action( 'woocommerce_blocks_enqueue_cart_block_scripts_after' ); return $content . $this->get_skeleton(); } @@ -64,10 +63,6 @@ class Cart extends AbstractBlock { * not in the post content on editor load. */ protected function enqueue_data( array $attributes = [] ) { - if ( did_action( 'woocommerce_blocks_cart_enqueue_data' ) ) { - return; - } - $data_registry = Package::container()->get( AssetDataRegistry::class ); diff --git a/plugins/woocommerce-blocks/src/BlockTypes/Checkout.php b/plugins/woocommerce-blocks/src/BlockTypes/Checkout.php index bf2f3901bd0..8c9adabbf83 100644 --- a/plugins/woocommerce-blocks/src/BlockTypes/Checkout.php +++ b/plugins/woocommerce-blocks/src/BlockTypes/Checkout.php @@ -51,8 +51,7 @@ class Checkout extends AbstractBlock { */ public function render( $attributes = array(), $content = '' ) { do_action( 'woocommerce_blocks_enqueue_checkout_block_scripts_before' ); - $this->enqueue_data( $attributes ); - $this->enqueue_scripts( $attributes ); + $this->enqueue_assets( $attributes ); do_action( 'woocommerce_blocks_enqueue_checkout_block_scripts_after' ); return $content . $this->get_skeleton(); } diff --git a/plugins/woocommerce-blocks/src/BlockTypes/PriceFilter.php b/plugins/woocommerce-blocks/src/BlockTypes/PriceFilter.php index a0656ba5f00..08f2dce83d3 100644 --- a/plugins/woocommerce-blocks/src/BlockTypes/PriceFilter.php +++ b/plugins/woocommerce-blocks/src/BlockTypes/PriceFilter.php @@ -9,6 +9,8 @@ namespace Automattic\WooCommerce\Blocks\BlockTypes; defined( 'ABSPATH' ) || exit; +use Automattic\WooCommerce\Blocks\Assets; + /** * PriceFilter class. */ @@ -38,14 +40,11 @@ class PriceFilter extends AbstractBlock { } /** - * Append frontend scripts when rendering the Product Categories List block. + * Register/enqueue scripts used for this block. * - * @param array $attributes Block attributes. Default empty array. - * @param string $content Block content. Default empty string. - * @return string Rendered block type output. + * @param array $attributes Any attributes that currently are available from the block. */ - public function render( $attributes = array(), $content = '' ) { - \Automattic\WooCommerce\Blocks\Assets::register_block_script( $this->block_name . '-frontend' ); - return $content; + protected function enqueue_scripts( array $attributes = [] ) { + Assets::register_block_script( $this->block_name . '-frontend' ); } } diff --git a/plugins/woocommerce-blocks/src/BlockTypes/ReviewsByCategory.php b/plugins/woocommerce-blocks/src/BlockTypes/ReviewsByCategory.php index 35a28143622..34a38beddd6 100644 --- a/plugins/woocommerce-blocks/src/BlockTypes/ReviewsByCategory.php +++ b/plugins/woocommerce-blocks/src/BlockTypes/ReviewsByCategory.php @@ -9,6 +9,8 @@ namespace Automattic\WooCommerce\Blocks\BlockTypes; defined( 'ABSPATH' ) || exit; +use Automattic\WooCommerce\Blocks\Assets; + /** * ReviewsByCategory class. */ @@ -37,15 +39,11 @@ class ReviewsByCategory extends AbstractBlock { } /** - * Append frontend scripts when rendering the Product Categories List block. + * Register/enqueue scripts used for this block. * - * @param array $attributes Block attributes. Default empty array. - * @param string $content Block content. Default empty string. - * @return string Rendered block type output. + * @param array $attributes Any attributes that currently are available from the block. */ - public function render( $attributes = array(), $content = '' ) { - \Automattic\WooCommerce\Blocks\Assets::register_block_script( 'reviews-frontend' ); - - return $content; + protected function enqueue_scripts( array $attributes = [] ) { + Assets::register_block_script( 'reviews-frontend' ); } } diff --git a/plugins/woocommerce-blocks/src/BlockTypes/ReviewsByProduct.php b/plugins/woocommerce-blocks/src/BlockTypes/ReviewsByProduct.php index 1601b5a9a55..b024a8c45c1 100644 --- a/plugins/woocommerce-blocks/src/BlockTypes/ReviewsByProduct.php +++ b/plugins/woocommerce-blocks/src/BlockTypes/ReviewsByProduct.php @@ -9,6 +9,8 @@ namespace Automattic\WooCommerce\Blocks\BlockTypes; defined( 'ABSPATH' ) || exit; +use Automattic\WooCommerce\Blocks\Assets; + /** * ReviewsByProduct class. */ @@ -37,15 +39,11 @@ class ReviewsByProduct extends AbstractBlock { } /** - * Append frontend scripts when rendering the Product Categories List block. + * Register/enqueue scripts used for this block. * - * @param array $attributes Block attributes. Default empty array. - * @param string $content Block content. Default empty string. - * @return string Rendered block type output. + * @param array $attributes Any attributes that currently are available from the block. */ - public function render( $attributes = array(), $content = '' ) { - \Automattic\WooCommerce\Blocks\Assets::register_block_script( 'reviews-frontend' ); - - return $content; + protected function enqueue_scripts( array $attributes = [] ) { + Assets::register_block_script( 'reviews-frontend' ); } }