From 9debaf8daf50fbde684d9608bfd583122ee44c67 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Albert=20Juh=C3=A9=20Lluveras?= Date: Thu, 20 Feb 2020 16:16:32 +0100 Subject: [PATCH] Cart & Checkout: conditionally register country and state settings (https://github.com/woocommerce/woocommerce-blocks/pull/1782) * Conditionally register wcSettings in the Cart and Checkout blocks * Add tests --- .../src/Assets/AssetDataRegistry.php | 10 ++++++++++ .../woocommerce-blocks/src/BlockTypes/Cart.php | 8 ++++++-- .../src/BlockTypes/Checkout.php | 16 ++++++++++++---- .../tests/php/Assets/AssetDataRegistry.php | 6 ++++++ 4 files changed, 34 insertions(+), 6 deletions(-) diff --git a/plugins/woocommerce-blocks/src/Assets/AssetDataRegistry.php b/plugins/woocommerce-blocks/src/Assets/AssetDataRegistry.php index 5132a526a68..38c20f15a65 100644 --- a/plugins/woocommerce-blocks/src/Assets/AssetDataRegistry.php +++ b/plugins/woocommerce-blocks/src/Assets/AssetDataRegistry.php @@ -165,6 +165,16 @@ class AssetDataRegistry { return $this->data; } + /** + * Allows checking whether a key exists. + * + * @param string $key The key to check if exists. + * @return bool Whether the key exists in the current data registry. + */ + public function exists( $key ) { + return array_key_exists( $key, $this->data ); + } + /** * Interface for adding data to the registry. * diff --git a/plugins/woocommerce-blocks/src/BlockTypes/Cart.php b/plugins/woocommerce-blocks/src/BlockTypes/Cart.php index 8f7f9db19fc..784f239be88 100644 --- a/plugins/woocommerce-blocks/src/BlockTypes/Cart.php +++ b/plugins/woocommerce-blocks/src/BlockTypes/Cart.php @@ -49,8 +49,12 @@ class Cart extends AbstractBlock { $data_registry = Package::container()->get( \Automattic\WooCommerce\Blocks\Assets\AssetDataRegistry::class ); - $data_registry->add( 'shippingCountries', WC()->countries->get_shipping_countries() ); - $data_registry->add( 'shippingStates', WC()->countries->get_shipping_country_states() ); + if ( ! $data_registry->exists( 'shippingCountries' ) ) { + $data_registry->add( 'shippingCountries', WC()->countries->get_shipping_countries() ); + } + if ( ! $data_registry->exists( 'shippingStates' ) ) { + $data_registry->add( 'shippingStates', WC()->countries->get_shipping_country_states() ); + } \Automattic\WooCommerce\Blocks\Assets::register_block_script( $this->block_name . '-frontend', $this->block_name . '-block-frontend' diff --git a/plugins/woocommerce-blocks/src/BlockTypes/Checkout.php b/plugins/woocommerce-blocks/src/BlockTypes/Checkout.php index f4e725e54b0..3bbe140cec5 100644 --- a/plugins/woocommerce-blocks/src/BlockTypes/Checkout.php +++ b/plugins/woocommerce-blocks/src/BlockTypes/Checkout.php @@ -50,10 +50,18 @@ class Checkout extends AbstractBlock { $data_registry = Package::container()->get( \Automattic\WooCommerce\Blocks\Assets\AssetDataRegistry::class ); - $data_registry->add( 'allowedCountries', WC()->countries->get_allowed_countries() ); - $data_registry->add( 'shippingCountries', WC()->countries->get_shipping_countries() ); - $data_registry->add( 'allowedStates', WC()->countries->get_allowed_country_states() ); - $data_registry->add( 'shippingStates', WC()->countries->get_shipping_country_states() ); + if ( ! $data_registry->exists( 'allowedCountries' ) ) { + $data_registry->add( 'allowedCountries', WC()->countries->get_allowed_countries() ); + } + if ( ! $data_registry->exists( 'shippingCountries' ) ) { + $data_registry->add( 'shippingCountries', WC()->countries->get_shipping_countries() ); + } + if ( ! $data_registry->exists( 'allowedStates' ) ) { + $data_registry->add( 'allowedStates', WC()->countries->get_allowed_country_states() ); + } + if ( ! $data_registry->exists( 'shippingStates' ) ) { + $data_registry->add( 'shippingStates', WC()->countries->get_shipping_country_states() ); + } \Automattic\WooCommerce\Blocks\Assets::register_block_script( $this->block_name . '-frontend', $this->block_name . '-block-frontend' ); return $content; } diff --git a/plugins/woocommerce-blocks/tests/php/Assets/AssetDataRegistry.php b/plugins/woocommerce-blocks/tests/php/Assets/AssetDataRegistry.php index 942a949bdf0..c824fee38cc 100644 --- a/plugins/woocommerce-blocks/tests/php/Assets/AssetDataRegistry.php +++ b/plugins/woocommerce-blocks/tests/php/Assets/AssetDataRegistry.php @@ -31,6 +31,12 @@ class AssetDataRegistry extends WP_UnitTestCase { $this->assertEquals( [ 'test' => 'foo' ], $this->registry->get() ); } + public function test_data_exists() { + $this->registry->add( 'foo', 'lorem-ipsum' ); + $this->assertEquals( true, $this->registry->exists( 'foo' ) ); + $this->assertEquals( false, $this->registry->exists( 'bar' ) ); + } + public function test_add_lazy_data() { $lazy = function () { return 'bar';