Implement mechanism for reliably passing through server data in editor context. (https://github.com/woocommerce/woocommerce-blocks/pull/2076)

* add method for enqueueing editor assets for the block editor

* configure Checkout to ensure editor assets are loaded in the block editor.

* move common functions to abstract

* ensure cart has server data passed through to editor

* ensure `enqueue_data` is only ever called once.

* make sure we enqueue data for cart

* add missing attributes arg

* remove logic that doesn’t do anything
This commit is contained in:
Darren Ethier 2020-03-31 09:20:49 -04:00 committed by GitHub
parent 0046b73bbf
commit 355c1269d8
3 changed files with 61 additions and 12 deletions

View File

@ -28,6 +28,13 @@ abstract class AbstractBlock {
*/ */
protected $block_name = ''; protected $block_name = '';
/**
* Constructor
*/
public function __construct() {
add_action( 'enqueue_block_editor_assets', [ $this, 'enqueue_editor_assets' ] );
}
/** /**
* Registers the block type with WordPress. * Registers the block type with WordPress.
*/ */
@ -41,4 +48,23 @@ abstract class AbstractBlock {
) )
); );
} }
/**
* Will enqueue any editor assets a block needs to load
*/
public function enqueue_editor_assets() {
$this->enqueue_data();
}
/**
* Extra data passed through from server to client for block.
*
* @param array $attributes Any attributes that currently are available from the block.
* Note, this will be empty in the editor context when the block is
* not in the post content on editor load.
*/
protected function enqueue_data( array $attributes = [] ) {
// noop. Child classes should override this if needed.
}
} }

View File

@ -38,6 +38,7 @@ class Cart extends AbstractBlock {
); );
} }
/** /**
* Append frontend scripts when rendering the Cart block. * Append frontend scripts when rendering the Cart block.
* *
@ -46,6 +47,24 @@ class Cart extends AbstractBlock {
* @return string Rendered block type output. * @return string Rendered block type output.
*/ */
public function render( $attributes = array(), $content = '' ) { public function render( $attributes = array(), $content = '' ) {
$this->enqueue_data( $attributes );
do_action( 'woocommerce_blocks_enqueue_cart_block_scripts_before' );
\Automattic\WooCommerce\Blocks\Assets::register_block_script(
$this->block_name . '-frontend',
$this->block_name . '-block-frontend'
);
do_action( 'woocommerce_blocks_enqueue_cart_block_scripts_after' );
return $content . $this->get_skeleton();
}
/**
* Extra data passed through from server to client for block.
*
* @param array $attributes Any attributes that currently are available from the block.
* Note, this will be empty in the editor context when the block is
* not in the post content on editor load.
*/
protected function enqueue_data( array $attributes = [] ) {
$data_registry = Package::container()->get( $data_registry = Package::container()->get(
\Automattic\WooCommerce\Blocks\Assets\AssetDataRegistry::class \Automattic\WooCommerce\Blocks\Assets\AssetDataRegistry::class
); );
@ -74,13 +93,6 @@ class Cart extends AbstractBlock {
$max_quantity_limit = apply_filters( 'woocommerce_maximum_quantity_selected_cart', 99 ); $max_quantity_limit = apply_filters( 'woocommerce_maximum_quantity_selected_cart', 99 );
$data_registry->add( 'quantitySelectLimit', $max_quantity_limit ); $data_registry->add( 'quantitySelectLimit', $max_quantity_limit );
} }
do_action( 'woocommerce_blocks_enqueue_cart_block_scripts_before' );
\Automattic\WooCommerce\Blocks\Assets::register_block_script(
$this->block_name . '-frontend',
$this->block_name . '-block-frontend'
);
do_action( 'woocommerce_blocks_enqueue_cart_block_scripts_after' );
return $content . $this->get_skeleton();
} }
/** /**

View File

@ -41,6 +41,7 @@ class Checkout extends AbstractBlock {
); );
} }
/** /**
* Append frontend scripts when rendering the block. * Append frontend scripts when rendering the block.
* *
@ -49,6 +50,21 @@ class Checkout extends AbstractBlock {
* @return string Rendered block type output. * @return string Rendered block type output.
*/ */
public function render( $attributes = array(), $content = '' ) { public function render( $attributes = array(), $content = '' ) {
$this->enqueue_data( $attributes );
do_action( 'woocommerce_blocks_enqueue_checkout_block_scripts_before' );
Assets::register_block_script( $this->block_name . '-frontend', $this->block_name . '-block-frontend' );
do_action( 'woocommerce_blocks_enqueue_checkout_block_scripts_after' );
return $content . $this->get_skeleton();
}
/**
* Extra data passed through from server to client for block.
*
* @param array $attributes Any attributes that currently are available from the block.
* Note, this will be empty in the editor context when the block is
* not in the post content on editor load.
*/
protected function enqueue_data( array $attributes = [] ) {
$data_registry = Package::container()->get( $data_registry = Package::container()->get(
AssetDataRegistry::class AssetDataRegistry::class
); );
@ -84,11 +100,6 @@ class Checkout extends AbstractBlock {
$this->hydrate_from_api( $data_registry ); $this->hydrate_from_api( $data_registry );
$this->hydrate_customer_payment_methods( $data_registry ); $this->hydrate_customer_payment_methods( $data_registry );
} }
do_action( 'woocommerce_blocks_enqueue_checkout_block_scripts_before' );
Assets::register_block_script( $this->block_name . '-frontend', $this->block_name . '-block-frontend' );
do_action( 'woocommerce_blocks_enqueue_checkout_block_scripts_after' );
return $content . $this->get_skeleton();
} }
/** /**