From 66ac06e9fc25d128278627ed22f94731fbb0ce00 Mon Sep 17 00:00:00 2001 From: Thomas Roberts <5656702+opr@users.noreply.github.com> Date: Fri, 5 Apr 2024 12:08:54 +0100 Subject: [PATCH] Add default keys to local pickup settings to prevent PHP warning (#46182) --- .../changelog/fix-local-pickup-title-sync | 4 +++ .../src/Blocks/BlockTypes/Cart.php | 5 +-- .../src/Blocks/BlockTypes/Checkout.php | 8 ++--- .../Blocks/Shipping/ShippingController.php | 2 +- .../StoreApi/Utilities/LocalPickupUtils.php | 33 +++++++++++++++++-- 5 files changed, 42 insertions(+), 10 deletions(-) create mode 100644 plugins/woocommerce/changelog/fix-local-pickup-title-sync diff --git a/plugins/woocommerce/changelog/fix-local-pickup-title-sync b/plugins/woocommerce/changelog/fix-local-pickup-title-sync new file mode 100644 index 00000000000..aa8699f8fb3 --- /dev/null +++ b/plugins/woocommerce/changelog/fix-local-pickup-title-sync @@ -0,0 +1,4 @@ +Significance: patch +Type: fix + +Prevent PHP warning if local pickup has not been set up in your store diff --git a/plugins/woocommerce/src/Blocks/BlockTypes/Cart.php b/plugins/woocommerce/src/Blocks/BlockTypes/Cart.php index 8af17041cfb..2c3e9205845 100644 --- a/plugins/woocommerce/src/Blocks/BlockTypes/Cart.php +++ b/plugins/woocommerce/src/Blocks/BlockTypes/Cart.php @@ -2,6 +2,7 @@ namespace Automattic\WooCommerce\Blocks\BlockTypes; use Automattic\WooCommerce\Blocks\Utils\CartCheckoutUtils; +use Automattic\WooCommerce\StoreApi\Utilities\LocalPickupUtils; /** * Cart class. @@ -246,8 +247,8 @@ class Cart extends AbstractBlock { $this->asset_data_registry->add( 'isBlockTheme', wc_current_theme_is_fse_theme(), true ); $this->asset_data_registry->add( 'activeShippingZones', CartCheckoutUtils::get_shipping_zones(), true ); - $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 ); + $pickup_location_settings = LocalPickupUtils::get_local_pickup_settings(); + $this->asset_data_registry->add( 'localPickupEnabled', $pickup_location_settings['enabled'], true ); // Hydrate the following data depending on admin or frontend context. if ( ! is_admin() && ! WC()->is_rest_api_request() ) { diff --git a/plugins/woocommerce/src/Blocks/BlockTypes/Checkout.php b/plugins/woocommerce/src/Blocks/BlockTypes/Checkout.php index 3c30c6be733..986e0a4b2ff 100644 --- a/plugins/woocommerce/src/Blocks/BlockTypes/Checkout.php +++ b/plugins/woocommerce/src/Blocks/BlockTypes/Checkout.php @@ -267,7 +267,7 @@ class Checkout extends AbstractBlock { if ( ( ! empty( $post->post_type ) && ! empty( $post->post_name ) && 'page-checkout' !== $post->post_name && 'wp_template' === $post->post_type ) || false === has_block( 'woocommerce/checkout', $post ) ) { return; } - $pickup_location_settings = get_option( 'woocommerce_pickup_location_settings', array() ); + $pickup_location_settings = LocalPickupUtils::get_local_pickup_settings(); if ( ! isset( $pickup_location_settings['title'] ) ) { return; @@ -350,9 +350,9 @@ class Checkout extends AbstractBlock { $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 ); - $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( 'localPickupText', wc_clean( $pickup_location_settings['title'] ) ?? __( 'Local Pickup', 'woocommerce' ), true ); + $pickup_location_settings = LocalPickupUtils::get_local_pickup_settings(); + $this->asset_data_registry->add( 'localPickupEnabled', $pickup_location_settings['enabled'], true ); + $this->asset_data_registry->add( 'localPickupText', $pickup_location_settings['title'], true ); $is_block_editor = $this->is_block_editor(); diff --git a/plugins/woocommerce/src/Blocks/Shipping/ShippingController.php b/plugins/woocommerce/src/Blocks/Shipping/ShippingController.php index 3e0aca3ed62..aa86fa0879b 100644 --- a/plugins/woocommerce/src/Blocks/Shipping/ShippingController.php +++ b/plugins/woocommerce/src/Blocks/Shipping/ShippingController.php @@ -293,7 +293,7 @@ class ShippingController { } $settings = array( - 'pickupLocationSettings' => get_option( 'woocommerce_pickup_location_settings', array() ), + 'pickupLocationSettings' => LocalPickupUtils::get_local_pickup_settings(), 'pickupLocations' => $formatted_pickup_locations, 'readonlySettings' => array( 'hasLegacyPickup' => $has_legacy_pickup, diff --git a/plugins/woocommerce/src/StoreApi/Utilities/LocalPickupUtils.php b/plugins/woocommerce/src/StoreApi/Utilities/LocalPickupUtils.php index 29cff3b2142..3f3e894e237 100644 --- a/plugins/woocommerce/src/StoreApi/Utilities/LocalPickupUtils.php +++ b/plugins/woocommerce/src/StoreApi/Utilities/LocalPickupUtils.php @@ -7,14 +7,41 @@ namespace Automattic\WooCommerce\StoreApi\Utilities; */ class LocalPickupUtils { + /** + * Gets the local pickup location settings. + */ + public static function get_local_pickup_settings() { + $pickup_location_settings = get_option( + 'woocommerce_pickup_location_settings', + [ + 'enabled' => 'no', + 'title' => __( 'Local Pickup', 'woocommerce' ), + ] + ); + + if ( empty( $pickup_location_settings['title'] ) ) { + $pickup_location_settings['title'] = __( 'Local Pickup', 'woocommerce' ); + } + + if ( empty( $pickup_location_settings['enabled'] ) ) { + $pickup_location_settings['enabled'] = 'no'; + } + + // All consumers of this turn it into a bool eventually. Doing it here removes the need for that. + $pickup_location_settings['enabled'] = wc_string_to_bool( $pickup_location_settings['enabled'] ); + $pickup_location_settings['title'] = wc_clean( $pickup_location_settings['title'] ); + + return $pickup_location_settings; + } + /** * Checks if WC Blocks local pickup is enabled. * * @return bool True if local pickup is enabled. */ public static function is_local_pickup_enabled() { - $pickup_location_settings = get_option( 'woocommerce_pickup_location_settings', [] ); - return wc_string_to_bool( $pickup_location_settings['enabled'] ?? 'no' ); + $pickup_location_settings = self::get_local_pickup_settings(); + return $pickup_location_settings['enabled']; } /** * Gets a list of payment method ids that support the 'local-pickup' feature. @@ -24,7 +51,7 @@ class LocalPickupUtils { public static function get_local_pickup_method_ids() { $all_methods_supporting_local_pickup = array_reduce( WC()->shipping()->get_shipping_methods(), - function( $methods, $method ) { + function ( $methods, $method ) { if ( $method->supports( 'local-pickup' ) ) { $methods[] = $method->id; }