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
This commit is contained in:
parent
8a78371703
commit
d83214ed44
|
@ -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();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -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' );
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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' );
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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' );
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
);
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
|
|
@ -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' );
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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' );
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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' );
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue