Skip to content

Commit

Permalink
Deprecate the $check_key_exists parameter from AssetDataRegistry and …
Browse files Browse the repository at this point in the history
…disallow duplicate data for all cases (#46139)

* Ensures data is added to the registry just once.

* ditch unnecessary param.

* Deprecate the  param for AssetDataRegistry

* Remove the  param for additional calls to AssetDataRegistry::add()

* Remove the  param for additional call to AssetDataRegistry::add() on WCAdminSharedSettings

* Trigger a PHP warning instead of throwing a fatal whenever the key being registered is not a string and whenever attempting to override existing data.

* Add changefile(s) from automation for the following project(s): woocommerce

* Replace trigger_error with error_log to clear out lint errors.

* Address lint for trigger_error

* Update the test_already_existing_key_on_adding_data test to expect a warning instead of an exception

* Update tests

* Update method from add_data to add.

* Remove the throw InvalidArgumentException for the add and add_data methods.

* Remove the now unnecessary test_already_existing_key_on_adding_data test, considering we now return early whenever the key is duplicated, the code never reaches this stage via AssetDataRegistry:add

* Move the call to the exists method to within the add_data method.

---------

Co-authored-by: github-actions <github-actions@github.com>
  • Loading branch information
nefeline and github-actions committed Apr 8, 2024
1 parent 3aea83f commit 854ddf5
Show file tree
Hide file tree
Showing 27 changed files with 98 additions and 123 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: major
Type: fix

Deprecate the $check_key_exists parameter from AssetDataRegistry and disallow duplicate data for all cases.
40 changes: 13 additions & 27 deletions plugins/woocommerce/src/Blocks/Assets/AssetDataRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -297,24 +297,15 @@ public function exists( $key ) {
* @param string $key The key used to reference the data being registered. This should use camelCase.
* @param mixed $data If not a function, registered to the registry as is. If a function, then the
* callback is invoked right before output to the screen.
* @param boolean $check_key_exists If set to true, duplicate data will be ignored if the key exists.
* @param boolean $check_key_exists Deprecated. If set to true, duplicate data will be ignored if the key exists.
* If false, duplicate data will cause an exception.
*
* @throws InvalidArgumentException Only throws when site is in debug mode. Always logs the error.
*/
public function add( $key, $data, $check_key_exists = false ) {
if ( $check_key_exists && $this->exists( $key ) ) {
return;
}
try {
$this->add_data( $key, $data );
} catch ( Exception $e ) {
if ( $this->debug() ) {
// bubble up.
throw $e;
}
wc_caught_exception( $e, __METHOD__, [ $key, $data ] );
if ( $check_key_exists ) {
wc_deprecated_argument( 'Automattic\WooCommerce\Blocks\Assets\AssetDataRegistry::add()', '8.9', 'The $check_key_exists parameter is no longer used: all duplicate data will be ignored if the key exists by default' );
}

$this->add_data( $key, $data );
}

/**
Expand Down Expand Up @@ -414,24 +405,19 @@ public function enqueue_asset_data() {
*
* @param string $key Key for the data.
* @param mixed $data Value for the data.
*
* @throws InvalidArgumentException If key is not a string or already
* exists in internal data cache.
*/
protected function add_data( $key, $data ) {
if ( ! is_string( $key ) ) {
if ( $this->debug() ) {
throw new InvalidArgumentException(
'Key for the data being registered must be a string'
);
}
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_trigger_error
trigger_error( esc_html__( 'Key for the data being registered must be a string', 'woocommerce' ), E_USER_WARNING );
return;
}
if ( $this->exists( $key ) ) {
return;
}
if ( isset( $this->data[ $key ] ) ) {
if ( $this->debug() ) {
throw new InvalidArgumentException(
'Overriding existing data with an already registered key is not allowed'
);
}
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_trigger_error
trigger_error( esc_html__( 'Overriding existing data with an already registered key is not allowed', 'woocommerce' ), E_USER_WARNING );
return;
}
if ( \is_callable( $data ) ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -695,12 +695,12 @@ protected function get_add_to_cart( $product ) {
*/
protected function enqueue_data( array $attributes = [] ) {
parent::enqueue_data( $attributes );
$this->asset_data_registry->add( 'minColumns', wc_get_theme_support( 'product_blocks::min_columns', 1 ), true );
$this->asset_data_registry->add( 'maxColumns', wc_get_theme_support( 'product_blocks::max_columns', 6 ), true );
$this->asset_data_registry->add( 'defaultColumns', wc_get_theme_support( 'product_blocks::default_columns', 3 ), true );
$this->asset_data_registry->add( 'minRows', wc_get_theme_support( 'product_blocks::min_rows', 1 ), true );
$this->asset_data_registry->add( 'maxRows', wc_get_theme_support( 'product_blocks::max_rows', 6 ), true );
$this->asset_data_registry->add( 'defaultRows', wc_get_theme_support( 'product_blocks::default_rows', 3 ), true );
$this->asset_data_registry->add( 'minColumns', wc_get_theme_support( 'product_blocks::min_columns', 1 ) );
$this->asset_data_registry->add( 'maxColumns', wc_get_theme_support( 'product_blocks::max_columns', 6 ) );
$this->asset_data_registry->add( 'defaultColumns', wc_get_theme_support( 'product_blocks::default_columns', 3 ) );
$this->asset_data_registry->add( 'minRows', wc_get_theme_support( 'product_blocks::min_rows', 1 ) );
$this->asset_data_registry->add( 'maxRows', wc_get_theme_support( 'product_blocks::max_rows', 6 ) );
$this->asset_data_registry->add( 'defaultRows', wc_get_theme_support( 'product_blocks::default_rows', 3 ) );
}

/**
Expand Down
14 changes: 7 additions & 7 deletions plugins/woocommerce/src/Blocks/BlockTypes/AllProducts.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ class AllProducts extends AbstractBlock {
protected function enqueue_data( array $attributes = [] ) {
parent::enqueue_data( $attributes );
// Set this so filter blocks being used as widgets know when to render.
$this->asset_data_registry->add( 'hasFilterableProducts', true, true );
$this->asset_data_registry->add( 'minColumns', wc_get_theme_support( 'product_blocks::min_columns', 1 ), true );
$this->asset_data_registry->add( 'maxColumns', wc_get_theme_support( 'product_blocks::max_columns', 6 ), true );
$this->asset_data_registry->add( 'defaultColumns', wc_get_theme_support( 'product_blocks::default_columns', 3 ), true );
$this->asset_data_registry->add( 'minRows', wc_get_theme_support( 'product_blocks::min_rows', 1 ), true );
$this->asset_data_registry->add( 'maxRows', wc_get_theme_support( 'product_blocks::max_rows', 6 ), true );
$this->asset_data_registry->add( 'defaultRows', wc_get_theme_support( 'product_blocks::default_rows', 3 ), true );
$this->asset_data_registry->add( 'hasFilterableProducts', true );
$this->asset_data_registry->add( 'minColumns', wc_get_theme_support( 'product_blocks::min_columns', 1 ) );
$this->asset_data_registry->add( 'maxColumns', wc_get_theme_support( 'product_blocks::max_columns', 6 ) );
$this->asset_data_registry->add( 'defaultColumns', wc_get_theme_support( 'product_blocks::default_columns', 3 ) );
$this->asset_data_registry->add( 'minRows', wc_get_theme_support( 'product_blocks::min_rows', 1 ) );
$this->asset_data_registry->add( 'maxRows', wc_get_theme_support( 'product_blocks::max_rows', 6 ) );
$this->asset_data_registry->add( 'defaultRows', wc_get_theme_support( 'product_blocks::default_rows', 3 ) );

// Hydrate the All Product block with data from the API. This is for the add to cart buttons which show current quantity in cart, and events.
if ( ! is_admin() && ! WC()->is_rest_api_request() ) {
Expand Down
4 changes: 2 additions & 2 deletions plugins/woocommerce/src/Blocks/BlockTypes/AllReviews.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ protected function get_block_type_script( $key = null ) {
*/
protected function enqueue_data( array $attributes = [] ) {
parent::enqueue_data( $attributes );
$this->asset_data_registry->add( 'reviewRatingsEnabled', wc_review_ratings_enabled(), true );
$this->asset_data_registry->add( 'showAvatars', '1' === get_option( 'show_avatars' ), true );
$this->asset_data_registry->add( 'reviewRatingsEnabled', wc_review_ratings_enabled() );
$this->asset_data_registry->add( 'showAvatars', '1' === get_option( 'show_avatars' ) );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class AttributeFilter extends AbstractBlock {
*/
protected function enqueue_data( array $attributes = [] ) {
parent::enqueue_data( $attributes );
$this->asset_data_registry->add( 'attributes', array_values( wc_get_attribute_taxonomies() ), true );
$this->asset_data_registry->add( 'attributes', array_values( wc_get_attribute_taxonomies() ) );
}

/**
Expand Down
24 changes: 12 additions & 12 deletions plugins/woocommerce/src/Blocks/BlockTypes/Cart.php
Original file line number Diff line number Diff line change
Expand Up @@ -233,21 +233,21 @@ protected function render( $attributes, $content, $block ) {
protected function enqueue_data( array $attributes = [] ) {
parent::enqueue_data( $attributes );

$this->asset_data_registry->add( 'countryData', CartCheckoutUtils::get_country_data(), true );
$this->asset_data_registry->add( 'baseLocation', wc_get_base_location(), true );
$this->asset_data_registry->add( 'isShippingCalculatorEnabled', filter_var( get_option( 'woocommerce_enable_shipping_calc' ), FILTER_VALIDATE_BOOLEAN ), true );
$this->asset_data_registry->add( 'displayItemizedTaxes', 'itemized' === get_option( 'woocommerce_tax_total_display' ), true );
$this->asset_data_registry->add( 'displayCartPricesIncludingTax', 'incl' === get_option( 'woocommerce_tax_display_cart' ), true );
$this->asset_data_registry->add( 'taxesEnabled', wc_tax_enabled(), true );
$this->asset_data_registry->add( 'couponsEnabled', wc_coupons_enabled(), true );
$this->asset_data_registry->add( 'shippingEnabled', wc_shipping_enabled(), true );
$this->asset_data_registry->add( 'hasDarkEditorStyleSupport', current_theme_supports( 'dark-editor-style' ), true );
$this->asset_data_registry->add( 'countryData', CartCheckoutUtils::get_country_data() );
$this->asset_data_registry->add( 'baseLocation', wc_get_base_location() );
$this->asset_data_registry->add( 'isShippingCalculatorEnabled', filter_var( get_option( 'woocommerce_enable_shipping_calc' ), FILTER_VALIDATE_BOOLEAN ) );
$this->asset_data_registry->add( 'displayItemizedTaxes', 'itemized' === get_option( 'woocommerce_tax_total_display' ) );
$this->asset_data_registry->add( 'displayCartPricesIncludingTax', 'incl' === get_option( 'woocommerce_tax_display_cart' ) );
$this->asset_data_registry->add( 'taxesEnabled', wc_tax_enabled() );
$this->asset_data_registry->add( 'couponsEnabled', wc_coupons_enabled() );
$this->asset_data_registry->add( 'shippingEnabled', wc_shipping_enabled() );
$this->asset_data_registry->add( 'hasDarkEditorStyleSupport', current_theme_supports( 'dark-editor-style' ) );
$this->asset_data_registry->register_page_id( isset( $attributes['checkoutPageId'] ) ? $attributes['checkoutPageId'] : 0 );
$this->asset_data_registry->add( 'isBlockTheme', wc_current_theme_is_fse_theme(), true );
$this->asset_data_registry->add( 'activeShippingZones', CartCheckoutUtils::get_shipping_zones(), true );
$this->asset_data_registry->add( 'isBlockTheme', wc_current_theme_is_fse_theme() );
$this->asset_data_registry->add( 'activeShippingZones', CartCheckoutUtils::get_shipping_zones() );

$pickup_location_settings = get_option( 'woocommerce_pickup_location_settings', [] );
$this->asset_data_registry->add( 'localPickupEnabled', wc_string_to_bool( $pickup_location_settings['enabled'] ?? 'no' ), true );
$this->asset_data_registry->add( 'localPickupEnabled', wc_string_to_bool( $pickup_location_settings['enabled'] ?? 'no' ) );

// Hydrate the following data depending on admin or frontend context.
if ( ! is_admin() && ! WC()->is_rest_api_request() ) {
Expand Down
30 changes: 14 additions & 16 deletions plugins/woocommerce/src/Blocks/BlockTypes/Checkout.php
Original file line number Diff line number Diff line change
Expand Up @@ -252,37 +252,35 @@ protected function is_checkout_endpoint() {
protected function enqueue_data( array $attributes = [] ) {
parent::enqueue_data( $attributes );

$this->asset_data_registry->add( 'countryData', CartCheckoutUtils::get_country_data(), true );
$this->asset_data_registry->add( 'baseLocation', wc_get_base_location(), true );
$this->asset_data_registry->add( 'countryData', CartCheckoutUtils::get_country_data() );
$this->asset_data_registry->add( 'baseLocation', wc_get_base_location() );
$this->asset_data_registry->add(
'checkoutAllowsGuest',
false === filter_var(
wc()->checkout()->is_registration_required(),
FILTER_VALIDATE_BOOLEAN
),
true
)
);
$this->asset_data_registry->add(
'checkoutAllowsSignup',
filter_var(
wc()->checkout()->is_registration_enabled(),
FILTER_VALIDATE_BOOLEAN
),
true
)
);
$this->asset_data_registry->add( 'checkoutShowLoginReminder', filter_var( get_option( 'woocommerce_enable_checkout_login_reminder' ), FILTER_VALIDATE_BOOLEAN ), true );
$this->asset_data_registry->add( 'displayCartPricesIncludingTax', 'incl' === get_option( 'woocommerce_tax_display_cart' ), true );
$this->asset_data_registry->add( 'displayItemizedTaxes', 'itemized' === get_option( 'woocommerce_tax_total_display' ), true );
$this->asset_data_registry->add( 'forcedBillingAddress', 'billing_only' === get_option( 'woocommerce_ship_to_destination' ), true );
$this->asset_data_registry->add( 'taxesEnabled', wc_tax_enabled(), true );
$this->asset_data_registry->add( 'couponsEnabled', wc_coupons_enabled(), true );
$this->asset_data_registry->add( 'shippingEnabled', wc_shipping_enabled(), true );
$this->asset_data_registry->add( 'hasDarkEditorStyleSupport', current_theme_supports( 'dark-editor-style' ), true );
$this->asset_data_registry->add( 'checkoutShowLoginReminder', filter_var( get_option( 'woocommerce_enable_checkout_login_reminder' ), FILTER_VALIDATE_BOOLEAN ) );
$this->asset_data_registry->add( 'displayCartPricesIncludingTax', 'incl' === get_option( 'woocommerce_tax_display_cart' ) );
$this->asset_data_registry->add( 'displayItemizedTaxes', 'itemized' === get_option( 'woocommerce_tax_total_display' ) );
$this->asset_data_registry->add( 'forcedBillingAddress', 'billing_only' === get_option( 'woocommerce_ship_to_destination' ) );
$this->asset_data_registry->add( 'taxesEnabled', wc_tax_enabled() );
$this->asset_data_registry->add( 'couponsEnabled', wc_coupons_enabled() );
$this->asset_data_registry->add( 'shippingEnabled', wc_shipping_enabled() );
$this->asset_data_registry->add( 'hasDarkEditorStyleSupport', current_theme_supports( 'dark-editor-style' ) );
$this->asset_data_registry->register_page_id( isset( $attributes['cartPageId'] ) ? $attributes['cartPageId'] : 0 );
$this->asset_data_registry->add( 'isBlockTheme', wc_current_theme_is_fse_theme(), true );
$this->asset_data_registry->add( 'isBlockTheme', wc_current_theme_is_fse_theme() );

$pickup_location_settings = get_option( 'woocommerce_pickup_location_settings', [] );
$this->asset_data_registry->add( 'localPickupEnabled', wc_string_to_bool( $pickup_location_settings['enabled'] ?? 'no' ), true );
$this->asset_data_registry->add( 'localPickupEnabled', wc_string_to_bool( $pickup_location_settings['enabled'] ?? 'no' ) );

$is_block_editor = $this->is_block_editor();

Expand Down
9 changes: 4 additions & 5 deletions plugins/woocommerce/src/Blocks/BlockTypes/ClassicTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ protected function enqueue_data( array $attributes = [] ) {

// Indicate to interactivity powered components that this block is on the page,
// and needs refresh to update data.
$this->asset_data_registry->add( 'needsRefreshForInteractivityAPI', true, true );
$this->asset_data_registry->add( 'needsRefreshForInteractivityAPI', true );
}

/**
Expand Down Expand Up @@ -133,15 +133,14 @@ protected function render( $attributes, $content, $block ) {

if ( $valid ) {
// Set this so that our product filters can detect if it's a PHP template.
$this->asset_data_registry->add( 'isRenderingPhpTemplate', true, true );
$this->asset_data_registry->add( 'isRenderingPhpTemplate', true );

// Set this so filter blocks being used as widgets know when to render.
$this->asset_data_registry->add( 'hasFilterableProducts', true, true );
$this->asset_data_registry->add( 'hasFilterableProducts', true );

$this->asset_data_registry->add(
'pageUrl',
html_entity_decode( get_pagenum_link() ),
''
html_entity_decode( get_pagenum_link() )
);

return $this->render_archive_product();
Expand Down
2 changes: 1 addition & 1 deletion plugins/woocommerce/src/Blocks/BlockTypes/FeaturedItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,6 @@ private function hasFocalPoint( $attributes ): bool {
*/
protected function enqueue_data( array $attributes = [] ) {
parent::enqueue_data( $attributes );
$this->asset_data_registry->add( 'defaultHeight', wc_get_theme_support( 'featured_block::default_height', 500 ), true );
$this->asset_data_registry->add( 'defaultHeight', wc_get_theme_support( 'featured_block::default_height', 500 ) );
}
}
9 changes: 3 additions & 6 deletions plugins/woocommerce/src/Blocks/BlockTypes/MiniCart.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,15 +159,13 @@ protected function enqueue_data( array $attributes = [] ) {

$this->asset_data_registry->add(
'taxLabel',
$this->tax_label,
''
$this->tax_label
);
}

$this->asset_data_registry->add(
'displayCartPricesIncludingTax',
$this->display_cart_prices_including_tax,
true
$this->display_cart_prices_including_tax
);

$template_part_edit_uri = '';
Expand Down Expand Up @@ -206,8 +204,7 @@ protected function enqueue_data( array $attributes = [] ) {

$this->asset_data_registry->add(
'templatePartEditUri',
$template_part_edit_uri,
''
$template_part_edit_uri
);

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,6 @@ protected function render_content( $order, $permission = false, $attributes = []
*/
protected function enqueue_data( array $attributes = [] ) {
parent::enqueue_data( $attributes );
$this->asset_data_registry->add( 'additionalAddressFields', Package::container()->get( CheckoutFields::class )->get_fields_for_location( 'address' ), true );
$this->asset_data_registry->add( 'additionalAddressFields', Package::container()->get( CheckoutFields::class )->get_fields_for_location( 'address' ) );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,6 @@ protected function render_content( $order, $permission = false, $attributes = []
*/
protected function enqueue_data( array $attributes = [] ) {
parent::enqueue_data( $attributes );
$this->asset_data_registry->add( 'additionalAddressFields', Package::container()->get( CheckoutFields::class )->get_fields_for_location( 'address' ), true );
$this->asset_data_registry->add( 'additionalAddressFields', Package::container()->get( CheckoutFields::class )->get_fields_for_location( 'address' ) );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ protected function enqueue_data( array $attributes = array() ) {

// The `loop_shop_per_page` filter can be found in WC_Query::product_query().
// phpcs:ignore WooCommerce.Commenting.CommentHooks.MissingHookComment
$this->asset_data_registry->add( 'loopShopPerPage', apply_filters( 'loop_shop_per_page', wc_get_default_products_per_row() * wc_get_default_product_rows_per_page() ), true );
$this->asset_data_registry->add( 'loopShopPerPage', apply_filters( 'loop_shop_per_page', wc_get_default_products_per_row() * wc_get_default_product_rows_per_page() ) );
}

/**
Expand Down Expand Up @@ -415,12 +415,12 @@ public function add_support_for_filter_blocks( $pre_render, $parsed_block ) {
}

$this->parsed_block = $parsed_block;
$this->asset_data_registry->add( 'hasFilterableProducts', true, true );
$this->asset_data_registry->add( 'hasFilterableProducts', true );
/**
* It enables the page to refresh when a filter is applied, ensuring that the product collection block,
* which is a server-side rendered (SSR) block, retrieves the products that match the filters.
*/
$this->asset_data_registry->add( 'isRenderingPhpTemplate', true, true );
$this->asset_data_registry->add( 'isRenderingPhpTemplate', true );

return $pre_render;
}
Expand Down
8 changes: 4 additions & 4 deletions plugins/woocommerce/src/Blocks/BlockTypes/ProductFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ protected function enqueue_data( array $attributes = [] ) {
global $pagenow;
parent::enqueue_data( $attributes );

$this->asset_data_registry->add( 'isBlockTheme', wc_current_theme_is_fse_theme(), true );
$this->asset_data_registry->add( 'isProductArchive', is_shop() || is_product_taxonomy(), true );
$this->asset_data_registry->add( 'isSiteEditor', 'site-editor.php' === $pagenow, true );
$this->asset_data_registry->add( 'isWidgetEditor', 'widgets.php' === $pagenow || 'customize.php' === $pagenow, true );
$this->asset_data_registry->add( 'isBlockTheme', wc_current_theme_is_fse_theme() );
$this->asset_data_registry->add( 'isProductArchive', is_shop() || is_product_taxonomy() );
$this->asset_data_registry->add( 'isSiteEditor', 'site-editor.php' === $pagenow );
$this->asset_data_registry->add( 'isWidgetEditor', 'widgets.php' === $pagenow || 'customize.php' === $pagenow );
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ function( $status ) use ( $stock_status_options, $action_namespace ) {
*/
protected function enqueue_data( array $stock_statuses = [] ) {
parent::enqueue_data( $stock_statuses );
$this->asset_data_registry->add( 'stockStatusOptions', wc_get_product_stock_status_options(), true );
$this->asset_data_registry->add( 'hideOutOfStockItems', 'yes' === get_option( 'woocommerce_hide_out_of_stock_items' ), true );
$this->asset_data_registry->add( 'stockStatusOptions', wc_get_product_stock_status_options() );
$this->asset_data_registry->add( 'hideOutOfStockItems', 'yes' === get_option( 'woocommerce_hide_out_of_stock_items' ) );
}

/**
Expand Down

0 comments on commit 854ddf5

Please sign in to comment.