From 887bfd351ab7c696676565f85ee2da82d0f41369 Mon Sep 17 00:00:00 2001 From: Narek Zakarian Date: Thu, 6 Feb 2025 12:29:17 +0400 Subject: [PATCH 01/12] Fix the wording according to implementation. --- modules/ppcp-compat/src/SettingsMap.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/modules/ppcp-compat/src/SettingsMap.php b/modules/ppcp-compat/src/SettingsMap.php index 7c97646a0..d045aa608 100644 --- a/modules/ppcp-compat/src/SettingsMap.php +++ b/modules/ppcp-compat/src/SettingsMap.php @@ -1,6 +1,6 @@ + * @var array */ private array $map; @@ -36,7 +36,7 @@ class SettingsMap { * The constructor. * * @param AbstractDataModel $model The new settings model. - * @param array $map The map of the new setting key to the old setting keys. + * @param array $map The map of the old setting key to the new setting keys. */ public function __construct( AbstractDataModel $model, array $map ) { $this->model = $model; @@ -53,9 +53,9 @@ public function get_model(): AbstractDataModel { } /** - * The map of the new setting key to the old setting keys. + * The map of the old setting key to the new setting keys. * - * @return array + * @return array */ public function get_map(): array { return $this->map; From 5fab7209907196f845aa54f585775b3e6e27c7a3 Mon Sep 17 00:00:00 2001 From: Narek Zakarian Date: Thu, 6 Feb 2025 12:58:58 +0400 Subject: [PATCH 02/12] Create a helper for mapping the styling settings. --- .../src/StylingSettingsMapHelper.php | 131 ++++++++++++++++++ 1 file changed, 131 insertions(+) create mode 100644 modules/ppcp-compat/src/StylingSettingsMapHelper.php diff --git a/modules/ppcp-compat/src/StylingSettingsMapHelper.php b/modules/ppcp-compat/src/StylingSettingsMapHelper.php new file mode 100644 index 000000000..b1d8ab8f0 --- /dev/null +++ b/modules/ppcp-compat/src/StylingSettingsMapHelper.php @@ -0,0 +1,131 @@ +model = $model; + } + + /** + * Maps old setting keys to new setting style names. + * + * The `StylingSettings` class stores settings as `LocationStylingDTO` objects. + * This method creates a mapping from old setting keys to the corresponding style names. + * + * Example: + * 'button_product_layout' => 'layout' + * + * This mapping will allow to retrieve the correct style value + * from a `LocationStylingDTO` object by dynamically accessing its properties. + * + * @psalm-return array + */ + public function map(): array { + + $mapped_settings = array(); + + foreach ( $this->locations_map() as $old_location_name => $new_location_name ) { + foreach ( $this->styles() as $style ) { + $old_styling_key = $this->get_old_styling_setting_key( $old_location_name, $style ); + $mapped_settings[ $old_styling_key ] = $style; + } + } + + return $mapped_settings; + } + + /** + * Retrieves the value of a mapped key from the new settings. + * + * @param string $old_key The key from the legacy settings. + * + * @return mixed|null The value of the mapped setting, or null if not found. + */ + public function mapped_value( string $old_key ) { + foreach ( $this->locations_map() as $old_location_name => $new_location_name ) { + foreach ( $this->styles() as $style ) { + if ( $old_key !== $this->get_old_styling_setting_key( $old_location_name, $style ) ) { + continue; + } + + $method = "get_{$new_location_name}"; + + if ( ! method_exists( $this->model, $method ) ) { + return null; + } + + $location_settings = $this->model->$method(); + + return $location_settings->$style ?? null; + } + } + + return null; + } + + /** + * Returns a mapping of old button location names to new settings location names. + * + * @return string[] The mapping of old location names to new location names. + */ + protected function locations_map(): array { + return array( + 'product' => 'product', + 'cart' => 'cart', + 'checkout' => 'classic_checkout', + 'mini-cart' => 'mini_cart', + 'checkout-block-express' => 'express_checkout', + ); + } + + /** + * Returns the available style names. + * + * @return string[] The list of available style names. + */ + protected function styles(): array { + return array( + 'enabled', + 'methods', + 'shape', + 'label', + 'color', + 'layout', + 'tagline', + ); + } + + /** + * Returns the old styling setting key name based on provided location and style names. + * + * @param string $location The location name. + * @param string $style The style name. + * @return string The old styling setting key name. + */ + protected function get_old_styling_setting_key( string $location, string $style ): string { + $location_setting_name_part = $location === 'checkout' ? '' : "_{$location}"; + return "button{$location_setting_name_part}_{$style}"; + } +} From e2f8aa81bfde65c8c61d22bf775fdf517b7948da Mon Sep 17 00:00:00 2001 From: Narek Zakarian Date: Thu, 6 Feb 2025 15:17:07 +0400 Subject: [PATCH 03/12] Move the mapping to new settings module --- modules/ppcp-compat/services.php | 42 -------------------------------- 1 file changed, 42 deletions(-) diff --git a/modules/ppcp-compat/services.php b/modules/ppcp-compat/services.php index 65ee1e3d8..b6abcea56 100644 --- a/modules/ppcp-compat/services.php +++ b/modules/ppcp-compat/services.php @@ -115,46 +115,4 @@ $container->get( 'api.bearer' ) ); }, - - /** - * Configuration for the new/old settings map. - * - * @returns SettingsMap[] - */ - 'compat.setting.new-to-old-map' => static function( ContainerInterface $container ) : array { - $are_new_settings_enabled = $container->get( 'wcgateway.settings.admin-settings-enabled' ); - if ( ! $are_new_settings_enabled ) { - return array(); - } - - return array( - new SettingsMap( - $container->get( 'settings.data.general' ), - /** - * The new GeneralSettings class stores the current connection - * details, without adding an environment-suffix (no `_sandbox` - * or `_production` in the field name) - * Only the `sandbox_merchant` flag indicates, which environment - * the credentials are used for. - */ - array( - 'merchant_id' => 'merchant_id', - 'client_id' => 'client_id', - 'client_secret' => 'client_secret', - 'sandbox_on' => 'sandbox_merchant', - 'live_client_id' => 'client_id', - 'live_client_secret' => 'client_secret', - 'live_merchant_id' => 'merchant_id', - 'live_merchant_email' => 'merchant_email', - 'sandbox_client_id' => 'client_id', - 'sandbox_client_secret' => 'client_secret', - 'sandbox_merchant_id' => 'merchant_id', - 'sandbox_merchant_email' => 'merchant_email', - ) - ), - ); - }, - 'compat.settings.settings_map_helper' => static function( ContainerInterface $container ) : SettingsMapHelper { - return new SettingsMapHelper( $container->get( 'compat.setting.new-to-old-map' ) ); - }, ); From 57b7ec6f94017ae20c8b1052d288a970fe008147 Mon Sep 17 00:00:00 2001 From: Narek Zakarian Date: Thu, 6 Feb 2025 16:23:14 +0400 Subject: [PATCH 04/12] Revert Moving the mapping to new settings module --- modules/ppcp-compat/src/SettingsMapHelper.php | 38 +++- .../src/StylingSettingsMapHelper.php | 27 +-- .../ppcp-settings/src/Compat/SettingsMap.php | 63 +++++++ .../src/Compat/SettingsMapHelper.php | 175 ++++++++++++++++++ .../src/Compat/StylingSettingsMapHelper.php | 131 +++++++++++++ 5 files changed, 406 insertions(+), 28 deletions(-) create mode 100644 modules/ppcp-settings/src/Compat/SettingsMap.php create mode 100644 modules/ppcp-settings/src/Compat/SettingsMapHelper.php create mode 100644 modules/ppcp-settings/src/Compat/StylingSettingsMapHelper.php diff --git a/modules/ppcp-compat/src/SettingsMapHelper.php b/modules/ppcp-compat/src/SettingsMapHelper.php index 8cedc104c..cdd022f54 100644 --- a/modules/ppcp-compat/src/SettingsMapHelper.php +++ b/modules/ppcp-compat/src/SettingsMapHelper.php @@ -10,6 +10,8 @@ namespace WooCommerce\PayPalCommerce\Compat; use RuntimeException; +use WooCommerce\PayPalCommerce\Settings\Data\StylingSettings; +use WooCommerce\PayPalCommerce\Settings\DTO\LocationStylingDTO; /** * A helper class to manage the transition between legacy and new settings. @@ -41,15 +43,24 @@ class SettingsMapHelper { */ protected array $model_cache = array(); + /** + * A helper for mapping the old/new styling settings. + * + * @var StylingSettingsMapHelper + */ + protected StylingSettingsMapHelper $styling_settings_map_helper; + /** * Constructor. * - * @param SettingsMap[] $settings_map A list of settings maps containing key definitions. + * @param SettingsMap[] $settings_map A list of settings maps containing key definitions. + * @param StylingSettingsMapHelper $styling_settings_map_helper A helper for mapping the old/new styling settings. * @throws RuntimeException When an old key has multiple mappings. */ - public function __construct( array $settings_map ) { + public function __construct( array $settings_map, StylingSettingsMapHelper $styling_settings_map_helper ) { $this->validate_settings_map( $settings_map ); - $this->settings_map = $settings_map; + $this->settings_map = $settings_map; + $this->styling_settings_map_helper = $styling_settings_map_helper; } /** @@ -80,15 +91,18 @@ protected function validate_settings_map( array $settings_map ) : void { */ public function mapped_value( string $old_key ) { $this->ensure_map_initialized(); - if ( ! isset( $this->key_to_model[ $old_key ] ) ) { return null; } - $mapping = $this->key_to_model[ $old_key ]; - $model_id = spl_object_id( $mapping['model'] ); + $mapping = $this->key_to_model[ $old_key ]; + $model = $mapping['model'] ?? false; - return $this->get_cached_model_value( $model_id, $mapping['new_key'], $mapping['model'] ); + if ( ! $model ) { + return null; + } + + return $this->get_cached_model_value( spl_object_id( $model ), $old_key, $mapping['new_key'], $mapping['model'] ); } /** @@ -108,17 +122,23 @@ public function has_mapped_key( string $old_key ) : bool { * Retrieves a cached model value or caches it if not already cached. * * @param int $model_id The unique identifier for the model object. + * @param string $old_key The key in the old settings structure. * @param string $new_key The key in the new settings structure. * @param object $model The model object. * * @return mixed|null The value of the key in the model, or null if not found. */ - protected function get_cached_model_value( int $model_id, string $new_key, object $model ) { + protected function get_cached_model_value( int $model_id, string $old_key, string $new_key, object $model ) { if ( ! isset( $this->model_cache[ $model_id ] ) ) { $this->model_cache[ $model_id ] = $model->to_array(); } - return $this->model_cache[ $model_id ][ $new_key ] ?? null; + switch ( true ) { + case $model instanceof StylingSettings: + return $this->styling_settings_map_helper->mapped_value( $old_key, $this->model_cache[ $model_id ] ); + default: + return $this->model_cache[ $model_id ][ $new_key ] ?? null; + } } /** diff --git a/modules/ppcp-compat/src/StylingSettingsMapHelper.php b/modules/ppcp-compat/src/StylingSettingsMapHelper.php index b1d8ab8f0..2a1b04c14 100644 --- a/modules/ppcp-compat/src/StylingSettingsMapHelper.php +++ b/modules/ppcp-compat/src/StylingSettingsMapHelper.php @@ -9,7 +9,7 @@ namespace WooCommerce\PayPalCommerce\Compat; -use WooCommerce\PayPalCommerce\Settings\Data\StylingSettings; +use WooCommerce\PayPalCommerce\Settings\DTO\LocationStylingDTO; /** * A map of old to new styling settings. @@ -18,16 +18,6 @@ * @psalm-import-type oldSettingsKey from SettingsMap */ class StylingSettingsMapHelper { - - /** - * The constructor. - * - * @param StylingSettings $model The styling settings model. - */ - public function __construct( StylingSettings $model ) { - $this->model = $model; - } - /** * Maps old setting keys to new setting style names. * @@ -59,25 +49,24 @@ public function map(): array { /** * Retrieves the value of a mapped key from the new settings. * - * @param string $old_key The key from the legacy settings. + * @param string $old_key The key from the legacy settings. + * @param LocationStylingDTO[] $model The list of location styling models. * * @return mixed|null The value of the mapped setting, or null if not found. */ - public function mapped_value( string $old_key ) { + public function mapped_value( string $old_key, array $model ) { foreach ( $this->locations_map() as $old_location_name => $new_location_name ) { foreach ( $this->styles() as $style ) { if ( $old_key !== $this->get_old_styling_setting_key( $old_location_name, $style ) ) { continue; } - $method = "get_{$new_location_name}"; + $location_settings = $model[ $new_location_name ] ?? false; - if ( ! method_exists( $this->model, $method ) ) { - return null; + if ( ! $location_settings instanceof LocationStylingDTO ) { + continue; } - $location_settings = $this->model->$method(); - return $location_settings->$style ?? null; } } @@ -124,7 +113,7 @@ protected function styles(): array { * @param string $style The style name. * @return string The old styling setting key name. */ - protected function get_old_styling_setting_key( string $location, string $style ): string { + public function get_old_styling_setting_key( string $location, string $style ): string { $location_setting_name_part = $location === 'checkout' ? '' : "_{$location}"; return "button{$location_setting_name_part}_{$style}"; } diff --git a/modules/ppcp-settings/src/Compat/SettingsMap.php b/modules/ppcp-settings/src/Compat/SettingsMap.php new file mode 100644 index 000000000..73a266e7b --- /dev/null +++ b/modules/ppcp-settings/src/Compat/SettingsMap.php @@ -0,0 +1,63 @@ + + */ + private array $map; + + /** + * The constructor. + * + * @param AbstractDataModel $model The new settings model. + * @param array $map The map of the old setting key to the new setting keys. + */ + public function __construct( AbstractDataModel $model, array $map ) { + $this->model = $model; + $this->map = $map; + } + + /** + * The model. + * + * @return AbstractDataModel + */ + public function get_model(): AbstractDataModel { + return $this->model; + } + + /** + * The map of the old setting key to the new setting keys. + * + * @return array + */ + public function get_map(): array { + return $this->map; + } +} diff --git a/modules/ppcp-settings/src/Compat/SettingsMapHelper.php b/modules/ppcp-settings/src/Compat/SettingsMapHelper.php new file mode 100644 index 000000000..611193eeb --- /dev/null +++ b/modules/ppcp-settings/src/Compat/SettingsMapHelper.php @@ -0,0 +1,175 @@ +validate_settings_map( $settings_map ); + $this->settings_map = $settings_map; + $this->styling_settings_map_helper = $styling_settings_map_helper; + } + + /** + * Validates the settings map for duplicate keys. + * + * @param SettingsMap[] $settings_map The settings map to validate. + * @throws RuntimeException When an old key has multiple mappings. + */ + protected function validate_settings_map( array $settings_map ) : void { + $seen_keys = array(); + + foreach ( $settings_map as $settings_map_instance ) { + foreach ( $settings_map_instance->get_map() as $old_key => $new_key ) { + if ( isset( $seen_keys[ $old_key ] ) ) { + throw new RuntimeException( "Duplicate mapping for legacy key '$old_key'." ); + } + $seen_keys[ $old_key ] = true; + } + } + } + + /** + * Retrieves the value of a mapped key from the new settings. + * + * @param string $old_key The key from the legacy settings. + * + * @return mixed|null The value of the mapped setting, or null if not found. + */ + public function mapped_value( string $old_key ) { + $this->ensure_map_initialized(); + if ( ! isset( $this->key_to_model[ $old_key ] ) ) { + return null; + } + + $mapping = $this->key_to_model[ $old_key ]; + $model = $mapping['model'] ?? false; + + if ( ! $model ) { + return null; + } + + switch ( true ) { + case $model instanceof StylingSettings: + return $this->styling_settings_map_helper->mapped_value( $old_key ); + default: + return $this->get_cached_model_value( spl_object_id( $model ), $mapping['new_key'], $model ); + } + } + + /** + * Determines if a given legacy key exists in the new settings. + * + * @param string $old_key The key from the legacy settings. + * + * @return bool True if the key exists in the new settings, false otherwise. + */ + public function has_mapped_key( string $old_key ) : bool { + $this->ensure_map_initialized(); + + return isset( $this->key_to_model[ $old_key ] ); + } + + /** + * Retrieves a cached model value or caches it if not already cached. + * + * @param int $model_id The unique identifier for the model object. + * @param string $new_key The key in the new settings structure. + * @param object $model The model object. + * + * @return mixed|null The value of the key in the model, or null if not found. + */ + protected function get_cached_model_value( int $model_id, string $new_key, object $model ) { + if ( ! isset( $this->model_cache[ $model_id ] ) ) { + $this->model_cache[ $model_id ] = $model->to_array(); + } + + return $this->model_cache[ $model_id ][ $new_key ] ?? null; + } + + /** + * Ensures the map of old-to-new settings is initialized. + * + * This method initializes the `key_to_model` array lazily to improve performance. + * + * @return void + */ + protected function ensure_map_initialized() : void { + if ( $this->key_to_model === null ) { + $this->initialize_key_map(); + } + } + + /** + * Initializes the indexed map of old-to-new settings keys. + * + * This method processes the provided settings maps and indexes the legacy + * keys to their corresponding metadata for efficient lookup. + * + * @return void + */ + protected function initialize_key_map() : void { + $this->key_to_model = array(); + + foreach ( $this->settings_map as $settings_map_instance ) { + foreach ( $settings_map_instance->get_map() as $old_key => $new_key ) { + $this->key_to_model[ $old_key ] = array( + 'new_key' => $new_key, + 'model' => $settings_map_instance->get_model(), + ); + } + } + } +} diff --git a/modules/ppcp-settings/src/Compat/StylingSettingsMapHelper.php b/modules/ppcp-settings/src/Compat/StylingSettingsMapHelper.php new file mode 100644 index 000000000..fc5a87fd9 --- /dev/null +++ b/modules/ppcp-settings/src/Compat/StylingSettingsMapHelper.php @@ -0,0 +1,131 @@ +model = $model; + } + + /** + * Maps old setting keys to new setting style names. + * + * The `StylingSettings` class stores settings as `LocationStylingDTO` objects. + * This method creates a mapping from old setting keys to the corresponding style names. + * + * Example: + * 'button_product_layout' => 'layout' + * + * This mapping will allow to retrieve the correct style value + * from a `LocationStylingDTO` object by dynamically accessing its properties. + * + * @psalm-return array + */ + public function map(): array { + + $mapped_settings = array(); + + foreach ( $this->locations_map() as $old_location_name => $new_location_name ) { + foreach ( $this->styles() as $style ) { + $old_styling_key = $this->get_old_styling_setting_key( $old_location_name, $style ); + $mapped_settings[ $old_styling_key ] = $style; + } + } + + return $mapped_settings; + } + + /** + * Retrieves the value of a mapped key from the new settings. + * + * @param string $old_key The key from the legacy settings. + * + * @return mixed|null The value of the mapped setting, or null if not found. + */ + public function mapped_value( string $old_key ) { + foreach ( $this->locations_map() as $old_location_name => $new_location_name ) { + foreach ( $this->styles() as $style ) { + if ( $old_key !== $this->get_old_styling_setting_key( $old_location_name, $style ) ) { + continue; + } + + $method = "get_{$new_location_name}"; + + if ( ! method_exists( $this->model, $method ) ) { + return null; + } + + $location_settings = $this->model->$method(); + + return $location_settings->$style ?? null; + } + } + + return null; + } + + /** + * Returns a mapping of old button location names to new settings location names. + * + * @return string[] The mapping of old location names to new location names. + */ + protected function locations_map(): array { + return array( + 'product' => 'product', + 'cart' => 'cart', + 'checkout' => 'classic_checkout', + 'mini-cart' => 'mini_cart', + 'checkout-block-express' => 'express_checkout', + ); + } + + /** + * Returns the available style names. + * + * @return string[] The list of available style names. + */ + protected function styles(): array { + return array( + 'enabled', + 'methods', + 'shape', + 'label', + 'color', + 'layout', + 'tagline', + ); + } + + /** + * Returns the old styling setting key name based on provided location and style names. + * + * @param string $location The location name. + * @param string $style The style name. + * @return string The old styling setting key name. + */ + protected function get_old_styling_setting_key( string $location, string $style ): string { + $location_setting_name_part = $location === 'checkout' ? '' : "_{$location}"; + return "button{$location_setting_name_part}_{$style}"; + } +} From e20fd52e58b84216a0daf8abbbede98c174160f5 Mon Sep 17 00:00:00 2001 From: Narek Zakarian Date: Thu, 6 Feb 2025 16:23:50 +0400 Subject: [PATCH 05/12] Add necessary services --- modules/ppcp-compat/services.php | 65 ++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/modules/ppcp-compat/services.php b/modules/ppcp-compat/services.php index b6abcea56..417221769 100644 --- a/modules/ppcp-compat/services.php +++ b/modules/ppcp-compat/services.php @@ -115,4 +115,69 @@ $container->get( 'api.bearer' ) ); }, + + /** + * Configuration for the new/old settings map. + * + * @returns SettingsMap[] + */ + 'compat.setting.new-to-old-map' => static function( ContainerInterface $container ) : array { + $are_new_settings_enabled = $container->get( 'wcgateway.settings.admin-settings-enabled' ); + if ( ! $are_new_settings_enabled ) { + return array(); + } + + $styling_settings_map_helper = $container->get( 'compat.settings.styling_map_helper' ); + assert( $styling_settings_map_helper instanceof StylingSettingsMapHelper ); + + return array( + new SettingsMap( + $container->get( 'settings.data.general' ), + /** + * The new GeneralSettings class stores the current connection + * details, without adding an environment-suffix (no `_sandbox` + * or `_production` in the field name) + * Only the `sandbox_merchant` flag indicates, which environment + * the credentials are used for. + */ + array( + 'merchant_id' => 'merchant_id', + 'client_id' => 'client_id', + 'client_secret' => 'client_secret', + 'sandbox_on' => 'sandbox_merchant', + 'live_client_id' => 'client_id', + 'live_client_secret' => 'client_secret', + 'live_merchant_id' => 'merchant_id', + 'live_merchant_email' => 'merchant_email', + 'sandbox_client_id' => 'client_id', + 'sandbox_client_secret' => 'client_secret', + 'sandbox_merchant_id' => 'merchant_id', + 'sandbox_merchant_email' => 'merchant_email', + ) + ), + new SettingsMap( + $container->get( 'settings.data.styling' ), + /** + * The `StylingSettings` class stores settings as `LocationStylingDTO` objects. + * This method creates a mapping from old setting keys to the corresponding style names. + * + * Example: + * 'button_product_layout' => 'layout' + * + * This mapping will allow to retrieve the correct style value + * from a `LocationStylingDTO` object by dynamically accessing its properties. + */ + $styling_settings_map_helper->map() + ), + ); + }, + 'compat.settings.settings_map_helper' => static function( ContainerInterface $container ) : SettingsMapHelper { + return new SettingsMapHelper( + $container->get( 'compat.setting.new-to-old-map' ), + $container->get( 'compat.settings.styling_map_helper' ) + ); + }, + 'compat.settings.styling_map_helper' => static function() : StylingSettingsMapHelper { + return new StylingSettingsMapHelper(); + }, ); From d1d5aac5044586f2ee4f9186bf8603169b8c5bce Mon Sep 17 00:00:00 2001 From: Narek Zakarian Date: Thu, 6 Feb 2025 16:24:57 +0400 Subject: [PATCH 06/12] Make the method private --- modules/ppcp-compat/src/StylingSettingsMapHelper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/ppcp-compat/src/StylingSettingsMapHelper.php b/modules/ppcp-compat/src/StylingSettingsMapHelper.php index 2a1b04c14..771519ea7 100644 --- a/modules/ppcp-compat/src/StylingSettingsMapHelper.php +++ b/modules/ppcp-compat/src/StylingSettingsMapHelper.php @@ -113,7 +113,7 @@ protected function styles(): array { * @param string $style The style name. * @return string The old styling setting key name. */ - public function get_old_styling_setting_key( string $location, string $style ): string { + protected function get_old_styling_setting_key( string $location, string $style ): string { $location_setting_name_part = $location === 'checkout' ? '' : "_{$location}"; return "button{$location_setting_name_part}_{$style}"; } From fd8346b487d7946f02f6d0ebf55f1de4188d9d20 Mon Sep 17 00:00:00 2001 From: Narek Zakarian Date: Thu, 6 Feb 2025 16:37:24 +0400 Subject: [PATCH 07/12] Move settings compat into separate namespace --- modules/ppcp-compat/services.php | 5 ++++- modules/ppcp-compat/src/{ => Settings}/SettingsMap.php | 4 ++-- .../ppcp-compat/src/{ => Settings}/SettingsMapHelper.php | 5 ++--- .../src/{ => Settings}/StylingSettingsMapHelper.php | 4 ++-- modules/ppcp-wc-gateway/src/Settings/Settings.php | 4 ++-- tests/e2e/PHPUnit/SettingsTest.php | 8 ++++---- 6 files changed, 16 insertions(+), 14 deletions(-) rename modules/ppcp-compat/src/{ => Settings}/SettingsMap.php (91%) rename modules/ppcp-compat/src/{ => Settings}/SettingsMapHelper.php (97%) rename modules/ppcp-compat/src/{ => Settings}/StylingSettingsMapHelper.php (96%) diff --git a/modules/ppcp-compat/services.php b/modules/ppcp-compat/services.php index 417221769..3ceedea9f 100644 --- a/modules/ppcp-compat/services.php +++ b/modules/ppcp-compat/services.php @@ -9,8 +9,11 @@ namespace WooCommerce\PayPalCommerce\Compat; -use WooCommerce\PayPalCommerce\Vendor\Psr\Container\ContainerInterface; use WooCommerce\PayPalCommerce\Compat\Assets\CompatAssets; +use WooCommerce\PayPalCommerce\Compat\Settings\SettingsMap; +use WooCommerce\PayPalCommerce\Compat\Settings\SettingsMapHelper; +use WooCommerce\PayPalCommerce\Compat\Settings\StylingSettingsMapHelper; +use WooCommerce\PayPalCommerce\Vendor\Psr\Container\ContainerInterface; return array( diff --git a/modules/ppcp-compat/src/SettingsMap.php b/modules/ppcp-compat/src/Settings/SettingsMap.php similarity index 91% rename from modules/ppcp-compat/src/SettingsMap.php rename to modules/ppcp-compat/src/Settings/SettingsMap.php index d045aa608..661481b27 100644 --- a/modules/ppcp-compat/src/SettingsMap.php +++ b/modules/ppcp-compat/src/Settings/SettingsMap.php @@ -2,12 +2,12 @@ /** * A map of old to new settings. * - * @package WooCommerce\PayPalCommerce\Compat + * @package WooCommerce\PayPalCommerce\Compat\Settings */ declare(strict_types=1); -namespace WooCommerce\PayPalCommerce\Compat; +namespace WooCommerce\PayPalCommerce\Compat\Settings; use WooCommerce\PayPalCommerce\Settings\Data\AbstractDataModel; diff --git a/modules/ppcp-compat/src/SettingsMapHelper.php b/modules/ppcp-compat/src/Settings/SettingsMapHelper.php similarity index 97% rename from modules/ppcp-compat/src/SettingsMapHelper.php rename to modules/ppcp-compat/src/Settings/SettingsMapHelper.php index cdd022f54..a18f2ceda 100644 --- a/modules/ppcp-compat/src/SettingsMapHelper.php +++ b/modules/ppcp-compat/src/Settings/SettingsMapHelper.php @@ -2,16 +2,15 @@ /** * A helper for mapping the new/old settings. * - * @package WooCommerce\PayPalCommerce\Compat + * @package WooCommerce\PayPalCommerce\Compat\Settings */ declare( strict_types = 1 ); -namespace WooCommerce\PayPalCommerce\Compat; +namespace WooCommerce\PayPalCommerce\Compat\Settings; use RuntimeException; use WooCommerce\PayPalCommerce\Settings\Data\StylingSettings; -use WooCommerce\PayPalCommerce\Settings\DTO\LocationStylingDTO; /** * A helper class to manage the transition between legacy and new settings. diff --git a/modules/ppcp-compat/src/StylingSettingsMapHelper.php b/modules/ppcp-compat/src/Settings/StylingSettingsMapHelper.php similarity index 96% rename from modules/ppcp-compat/src/StylingSettingsMapHelper.php rename to modules/ppcp-compat/src/Settings/StylingSettingsMapHelper.php index 771519ea7..fcec294e8 100644 --- a/modules/ppcp-compat/src/StylingSettingsMapHelper.php +++ b/modules/ppcp-compat/src/Settings/StylingSettingsMapHelper.php @@ -2,12 +2,12 @@ /** * A helper for mapping the old/new styling settings. * - * @package WooCommerce\PayPalCommerce\Compat + * @package WooCommerce\PayPalCommerce\Compat\Settings */ declare(strict_types=1); -namespace WooCommerce\PayPalCommerce\Compat; +namespace WooCommerce\PayPalCommerce\Compat\Settings; use WooCommerce\PayPalCommerce\Settings\DTO\LocationStylingDTO; diff --git a/modules/ppcp-wc-gateway/src/Settings/Settings.php b/modules/ppcp-wc-gateway/src/Settings/Settings.php index 182fba281..9340357a6 100644 --- a/modules/ppcp-wc-gateway/src/Settings/Settings.php +++ b/modules/ppcp-wc-gateway/src/Settings/Settings.php @@ -9,9 +9,9 @@ namespace WooCommerce\PayPalCommerce\WcGateway\Settings; -use WooCommerce\PayPalCommerce\Compat\SettingsMapHelper; -use WooCommerce\PayPalCommerce\WcGateway\Exception\NotFoundException; +use WooCommerce\PayPalCommerce\Compat\Settings\SettingsMapHelper; use WooCommerce\PayPalCommerce\Vendor\Psr\Container\ContainerInterface; +use WooCommerce\PayPalCommerce\WcGateway\Exception\NotFoundException; /** * Class Settings diff --git a/tests/e2e/PHPUnit/SettingsTest.php b/tests/e2e/PHPUnit/SettingsTest.php index b776f2e0f..cee650e01 100644 --- a/tests/e2e/PHPUnit/SettingsTest.php +++ b/tests/e2e/PHPUnit/SettingsTest.php @@ -2,11 +2,11 @@ namespace WooCommerce\PayPalCommerce\Tests\E2e; -use WooCommerce\PayPalCommerce\Compat\SettingsMap; -use WooCommerce\PayPalCommerce\Compat\SettingsMapHelper; -use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings; -use WooCommerce\PayPalCommerce\WcGateway\Exception\NotFoundException; +use WooCommerce\PayPalCommerce\Compat\Settings\SettingsMap; +use WooCommerce\PayPalCommerce\Compat\Settings\SettingsMapHelper; use WooCommerce\PayPalCommerce\Settings\Data\AbstractDataModel; +use WooCommerce\PayPalCommerce\WcGateway\Exception\NotFoundException; +use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings; class SettingsTest extends TestCase { private Settings $settings; From 72554570d3ef20cf7dc7e7c7cd13f3bd8733c84d Mon Sep 17 00:00:00 2001 From: Narek Zakarian Date: Thu, 6 Feb 2025 19:52:30 +0400 Subject: [PATCH 08/12] Remove the unnecessary files --- .../ppcp-settings/src/Compat/SettingsMap.php | 63 ------- .../src/Compat/SettingsMapHelper.php | 175 ------------------ .../src/Compat/StylingSettingsMapHelper.php | 131 ------------- 3 files changed, 369 deletions(-) delete mode 100644 modules/ppcp-settings/src/Compat/SettingsMap.php delete mode 100644 modules/ppcp-settings/src/Compat/SettingsMapHelper.php delete mode 100644 modules/ppcp-settings/src/Compat/StylingSettingsMapHelper.php diff --git a/modules/ppcp-settings/src/Compat/SettingsMap.php b/modules/ppcp-settings/src/Compat/SettingsMap.php deleted file mode 100644 index 73a266e7b..000000000 --- a/modules/ppcp-settings/src/Compat/SettingsMap.php +++ /dev/null @@ -1,63 +0,0 @@ - - */ - private array $map; - - /** - * The constructor. - * - * @param AbstractDataModel $model The new settings model. - * @param array $map The map of the old setting key to the new setting keys. - */ - public function __construct( AbstractDataModel $model, array $map ) { - $this->model = $model; - $this->map = $map; - } - - /** - * The model. - * - * @return AbstractDataModel - */ - public function get_model(): AbstractDataModel { - return $this->model; - } - - /** - * The map of the old setting key to the new setting keys. - * - * @return array - */ - public function get_map(): array { - return $this->map; - } -} diff --git a/modules/ppcp-settings/src/Compat/SettingsMapHelper.php b/modules/ppcp-settings/src/Compat/SettingsMapHelper.php deleted file mode 100644 index 611193eeb..000000000 --- a/modules/ppcp-settings/src/Compat/SettingsMapHelper.php +++ /dev/null @@ -1,175 +0,0 @@ -validate_settings_map( $settings_map ); - $this->settings_map = $settings_map; - $this->styling_settings_map_helper = $styling_settings_map_helper; - } - - /** - * Validates the settings map for duplicate keys. - * - * @param SettingsMap[] $settings_map The settings map to validate. - * @throws RuntimeException When an old key has multiple mappings. - */ - protected function validate_settings_map( array $settings_map ) : void { - $seen_keys = array(); - - foreach ( $settings_map as $settings_map_instance ) { - foreach ( $settings_map_instance->get_map() as $old_key => $new_key ) { - if ( isset( $seen_keys[ $old_key ] ) ) { - throw new RuntimeException( "Duplicate mapping for legacy key '$old_key'." ); - } - $seen_keys[ $old_key ] = true; - } - } - } - - /** - * Retrieves the value of a mapped key from the new settings. - * - * @param string $old_key The key from the legacy settings. - * - * @return mixed|null The value of the mapped setting, or null if not found. - */ - public function mapped_value( string $old_key ) { - $this->ensure_map_initialized(); - if ( ! isset( $this->key_to_model[ $old_key ] ) ) { - return null; - } - - $mapping = $this->key_to_model[ $old_key ]; - $model = $mapping['model'] ?? false; - - if ( ! $model ) { - return null; - } - - switch ( true ) { - case $model instanceof StylingSettings: - return $this->styling_settings_map_helper->mapped_value( $old_key ); - default: - return $this->get_cached_model_value( spl_object_id( $model ), $mapping['new_key'], $model ); - } - } - - /** - * Determines if a given legacy key exists in the new settings. - * - * @param string $old_key The key from the legacy settings. - * - * @return bool True if the key exists in the new settings, false otherwise. - */ - public function has_mapped_key( string $old_key ) : bool { - $this->ensure_map_initialized(); - - return isset( $this->key_to_model[ $old_key ] ); - } - - /** - * Retrieves a cached model value or caches it if not already cached. - * - * @param int $model_id The unique identifier for the model object. - * @param string $new_key The key in the new settings structure. - * @param object $model The model object. - * - * @return mixed|null The value of the key in the model, or null if not found. - */ - protected function get_cached_model_value( int $model_id, string $new_key, object $model ) { - if ( ! isset( $this->model_cache[ $model_id ] ) ) { - $this->model_cache[ $model_id ] = $model->to_array(); - } - - return $this->model_cache[ $model_id ][ $new_key ] ?? null; - } - - /** - * Ensures the map of old-to-new settings is initialized. - * - * This method initializes the `key_to_model` array lazily to improve performance. - * - * @return void - */ - protected function ensure_map_initialized() : void { - if ( $this->key_to_model === null ) { - $this->initialize_key_map(); - } - } - - /** - * Initializes the indexed map of old-to-new settings keys. - * - * This method processes the provided settings maps and indexes the legacy - * keys to their corresponding metadata for efficient lookup. - * - * @return void - */ - protected function initialize_key_map() : void { - $this->key_to_model = array(); - - foreach ( $this->settings_map as $settings_map_instance ) { - foreach ( $settings_map_instance->get_map() as $old_key => $new_key ) { - $this->key_to_model[ $old_key ] = array( - 'new_key' => $new_key, - 'model' => $settings_map_instance->get_model(), - ); - } - } - } -} diff --git a/modules/ppcp-settings/src/Compat/StylingSettingsMapHelper.php b/modules/ppcp-settings/src/Compat/StylingSettingsMapHelper.php deleted file mode 100644 index fc5a87fd9..000000000 --- a/modules/ppcp-settings/src/Compat/StylingSettingsMapHelper.php +++ /dev/null @@ -1,131 +0,0 @@ -model = $model; - } - - /** - * Maps old setting keys to new setting style names. - * - * The `StylingSettings` class stores settings as `LocationStylingDTO` objects. - * This method creates a mapping from old setting keys to the corresponding style names. - * - * Example: - * 'button_product_layout' => 'layout' - * - * This mapping will allow to retrieve the correct style value - * from a `LocationStylingDTO` object by dynamically accessing its properties. - * - * @psalm-return array - */ - public function map(): array { - - $mapped_settings = array(); - - foreach ( $this->locations_map() as $old_location_name => $new_location_name ) { - foreach ( $this->styles() as $style ) { - $old_styling_key = $this->get_old_styling_setting_key( $old_location_name, $style ); - $mapped_settings[ $old_styling_key ] = $style; - } - } - - return $mapped_settings; - } - - /** - * Retrieves the value of a mapped key from the new settings. - * - * @param string $old_key The key from the legacy settings. - * - * @return mixed|null The value of the mapped setting, or null if not found. - */ - public function mapped_value( string $old_key ) { - foreach ( $this->locations_map() as $old_location_name => $new_location_name ) { - foreach ( $this->styles() as $style ) { - if ( $old_key !== $this->get_old_styling_setting_key( $old_location_name, $style ) ) { - continue; - } - - $method = "get_{$new_location_name}"; - - if ( ! method_exists( $this->model, $method ) ) { - return null; - } - - $location_settings = $this->model->$method(); - - return $location_settings->$style ?? null; - } - } - - return null; - } - - /** - * Returns a mapping of old button location names to new settings location names. - * - * @return string[] The mapping of old location names to new location names. - */ - protected function locations_map(): array { - return array( - 'product' => 'product', - 'cart' => 'cart', - 'checkout' => 'classic_checkout', - 'mini-cart' => 'mini_cart', - 'checkout-block-express' => 'express_checkout', - ); - } - - /** - * Returns the available style names. - * - * @return string[] The list of available style names. - */ - protected function styles(): array { - return array( - 'enabled', - 'methods', - 'shape', - 'label', - 'color', - 'layout', - 'tagline', - ); - } - - /** - * Returns the old styling setting key name based on provided location and style names. - * - * @param string $location The location name. - * @param string $style The style name. - * @return string The old styling setting key name. - */ - protected function get_old_styling_setting_key( string $location, string $style ): string { - $location_setting_name_part = $location === 'checkout' ? '' : "_{$location}"; - return "button{$location_setting_name_part}_{$style}"; - } -} From 8549a7d515ca8d06355425f5a6bcf8ff3038245c Mon Sep 17 00:00:00 2001 From: Narek Zakarian Date: Thu, 6 Feb 2025 19:52:50 +0400 Subject: [PATCH 09/12] Support the other button locations mapping --- .../src/Settings/StylingSettingsMapHelper.php | 171 ++++++++++++++++-- 1 file changed, 154 insertions(+), 17 deletions(-) diff --git a/modules/ppcp-compat/src/Settings/StylingSettingsMapHelper.php b/modules/ppcp-compat/src/Settings/StylingSettingsMapHelper.php index fcec294e8..8266a7474 100644 --- a/modules/ppcp-compat/src/Settings/StylingSettingsMapHelper.php +++ b/modules/ppcp-compat/src/Settings/StylingSettingsMapHelper.php @@ -9,6 +9,8 @@ namespace WooCommerce\PayPalCommerce\Compat\Settings; +use RuntimeException; +use WooCommerce\PayPalCommerce\Button\Helper\ContextTrait; use WooCommerce\PayPalCommerce\Settings\DTO\LocationStylingDTO; /** @@ -18,6 +20,9 @@ * @psalm-import-type oldSettingsKey from SettingsMap */ class StylingSettingsMapHelper { + + use ContextTrait; + /** * Maps old setting keys to new setting style names. * @@ -34,7 +39,12 @@ class StylingSettingsMapHelper { */ public function map(): array { - $mapped_settings = array(); + $mapped_settings = array( + 'smart_button_locations' => '', + 'pay_later_button_locations' => '', + 'disable_funding' => '', + 'googlepay_button_enabled' => '', + ); foreach ( $this->locations_map() as $old_location_name => $new_location_name ) { foreach ( $this->styles() as $style ) { @@ -50,28 +60,45 @@ public function map(): array { * Retrieves the value of a mapped key from the new settings. * * @param string $old_key The key from the legacy settings. - * @param LocationStylingDTO[] $model The list of location styling models. + * @param LocationStylingDTO[] $styling_models The list of location styling models. * - * @return mixed|null The value of the mapped setting, or null if not found. + * @return mixed The value of the mapped setting, (null if not found). */ - public function mapped_value( string $old_key, array $model ) { - foreach ( $this->locations_map() as $old_location_name => $new_location_name ) { - foreach ( $this->styles() as $style ) { - if ( $old_key !== $this->get_old_styling_setting_key( $old_location_name, $style ) ) { - continue; - } + public function mapped_value(string $old_key, array $styling_models) { + switch ($old_key) { + case 'smart_button_locations': + return $this->mapped_smart_button_locations_value($styling_models); - $location_settings = $model[ $new_location_name ] ?? false; + case 'pay_later_button_locations': + return $this->mapped_pay_later_button_locations_value($styling_models); - if ( ! $location_settings instanceof LocationStylingDTO ) { - continue; - } + case 'disable_funding': + return $this->mapped_disabled_funding_value($styling_models); - return $location_settings->$style ?? null; - } - } + case 'googlepay_button_enabled': + return $this->mapped_google_pay_or_apple_pay_enabled_value($styling_models, 'googleplay'); - return null; + case 'applepay_button_enabled': + return $this->mapped_google_pay_or_apple_pay_enabled_value($styling_models, 'applepay'); + + default: + foreach ($this->locations_map() as $old_location_name => $new_location_name) { + foreach ($this->styles() as $style) { + if ($old_key !== $this->get_old_styling_setting_key($old_location_name, $style)) { + continue; + } + + $location_settings = $styling_models[$new_location_name] ?? false; + + if (!$location_settings instanceof LocationStylingDTO) { + continue; + } + + return $location_settings->$style ?? null; + } + } + return null; + } } /** @@ -89,6 +116,23 @@ protected function locations_map(): array { ); } + /** + * Returns a mapping of current context to new button location names. + * + * @return string[] The mapping of current context to new button location names. + */ + protected function current_context_to_new_button_location_map(): array { + return array( + 'product' => 'product', + 'cart' => 'cart', + 'cart-block' => 'cart', + 'checkout' => 'classic_checkout', + 'pay-now' => 'classic_checkout', + 'mini-cart' => 'mini_cart', + 'checkout-block' => 'express_checkout', + ); + } + /** * Returns the available style names. * @@ -117,4 +161,97 @@ protected function get_old_styling_setting_key( string $location, string $style $location_setting_name_part = $location === 'checkout' ? '' : "_{$location}"; return "button{$location_setting_name_part}_{$style}"; } + + /** + * Retrieves the mapped smart button locations from the new settings. + * + * @param LocationStylingDTO[] $styling_models The list of location styling models. + * @return string[] The list of enabled smart button locations. + */ + protected function mapped_smart_button_locations_value( array $styling_models ): array { + $enabled_locations = array(); + $locations = array_flip( $this->locations_map() ); + + foreach ( $styling_models as $model ) { + if ( ! $model->enabled ) { + continue; + } + + $enabled_locations[] = $locations[$model->location] ?? ''; + } + + return $enabled_locations; + } + + /** + * Retrieves the mapped pay later button locations from the new settings. + * + * @param LocationStylingDTO[] $styling_models The list of location styling models. + * @return string[] The list of locations where the Pay Later button is enabled. + */ + protected function mapped_pay_later_button_locations_value( array $styling_models ): array { + $enabled_locations = array(); + $locations = array_flip( $this->locations_map() ); + foreach ( $styling_models as $model ) { + if ( ! $model->enabled || ! in_array( 'paylater', $model->methods, true ) ) { + continue; + } + + $enabled_locations[] = $locations[$model->location] ?? ''; + } + + return $enabled_locations; + } + + /** + * Retrieves the mapped disabled funding value from the new settings. + * + * @param LocationStylingDTO[] $styling_models The list of location styling models. + * @return array|null The list of disabled funding, or null if none are disabled. + */ + protected function mapped_disabled_funding_value( array $styling_models ): ?array { + $disabled_funding = array(); + $locations_to_context_map = $this->current_context_to_new_button_location_map(); + + foreach ( $styling_models as $model ) { + if ( $model->location !== $locations_to_context_map[$this->context()] || in_array( 'venmo', $model->methods, true ) ) { + continue; + } + + $disabled_funding[] = 'venmo'; + + return $disabled_funding; + } + + return null; + } + + /** + * Retrieves the mapped enabled/disabled Google Pay or Apple Pay value from the new settings. + * + * @param LocationStylingDTO[] $styling_models The list of location styling models. + * @param 'googlepay'|'applepay' $button_name The button name ('googlepay' or 'applepay'). + * @return int The enabled (1) or disabled (0) state. + * @throws RuntimeException If an invalid button name is provided. + */ + protected function mapped_google_pay_or_apple_pay_enabled_value( array $styling_models, string $button_name ): int { + if ( $button_name !== 'googlepay' && $button_name !== 'applepay' ) { + throw new RuntimeException( 'Wrong button name is provided. Either "googlepay" or "applepay" can be used' ); + } + + $locations_to_context_map = $this->current_context_to_new_button_location_map(); + + foreach ( $styling_models as $model ) { + if ( ! $model->enabled + || $model->location !== $locations_to_context_map[$this->context()] + || ! in_array( $button_name, $model->methods, true ) + ) { + continue; + } + + return 1; + } + + return 0; + } } From d32b10801dd4090e87b0537649a8cf655a42b696 Mon Sep 17 00:00:00 2001 From: Narek Zakarian Date: Thu, 6 Feb 2025 19:57:15 +0400 Subject: [PATCH 10/12] Fix the cs --- .../src/Settings/StylingSettingsMapHelper.php | 48 +++++++++---------- 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/modules/ppcp-compat/src/Settings/StylingSettingsMapHelper.php b/modules/ppcp-compat/src/Settings/StylingSettingsMapHelper.php index 8266a7474..21c1f50e8 100644 --- a/modules/ppcp-compat/src/Settings/StylingSettingsMapHelper.php +++ b/modules/ppcp-compat/src/Settings/StylingSettingsMapHelper.php @@ -64,33 +64,33 @@ public function map(): array { * * @return mixed The value of the mapped setting, (null if not found). */ - public function mapped_value(string $old_key, array $styling_models) { - switch ($old_key) { + public function mapped_value( string $old_key, array $styling_models ) { + switch ( $old_key ) { case 'smart_button_locations': - return $this->mapped_smart_button_locations_value($styling_models); + return $this->mapped_smart_button_locations_value( $styling_models ); case 'pay_later_button_locations': - return $this->mapped_pay_later_button_locations_value($styling_models); + return $this->mapped_pay_later_button_locations_value( $styling_models ); case 'disable_funding': - return $this->mapped_disabled_funding_value($styling_models); + return $this->mapped_disabled_funding_value( $styling_models ); case 'googlepay_button_enabled': - return $this->mapped_google_pay_or_apple_pay_enabled_value($styling_models, 'googleplay'); + return $this->mapped_google_pay_or_apple_pay_enabled_value( $styling_models, 'googlepay' ); case 'applepay_button_enabled': - return $this->mapped_google_pay_or_apple_pay_enabled_value($styling_models, 'applepay'); + return $this->mapped_google_pay_or_apple_pay_enabled_value( $styling_models, 'applepay' ); default: - foreach ($this->locations_map() as $old_location_name => $new_location_name) { - foreach ($this->styles() as $style) { - if ($old_key !== $this->get_old_styling_setting_key($old_location_name, $style)) { + foreach ( $this->locations_map() as $old_location_name => $new_location_name ) { + foreach ( $this->styles() as $style ) { + if ( $old_key !== $this->get_old_styling_setting_key( $old_location_name, $style ) ) { continue; } - $location_settings = $styling_models[$new_location_name] ?? false; + $location_settings = $styling_models[ $new_location_name ] ?? false; - if (!$location_settings instanceof LocationStylingDTO) { + if ( ! $location_settings instanceof LocationStylingDTO ) { continue; } @@ -123,13 +123,13 @@ protected function locations_map(): array { */ protected function current_context_to_new_button_location_map(): array { return array( - 'product' => 'product', - 'cart' => 'cart', - 'cart-block' => 'cart', - 'checkout' => 'classic_checkout', - 'pay-now' => 'classic_checkout', - 'mini-cart' => 'mini_cart', - 'checkout-block' => 'express_checkout', + 'product' => 'product', + 'cart' => 'cart', + 'cart-block' => 'cart', + 'checkout' => 'classic_checkout', + 'pay-now' => 'classic_checkout', + 'mini-cart' => 'mini_cart', + 'checkout-block' => 'express_checkout', ); } @@ -177,7 +177,7 @@ protected function mapped_smart_button_locations_value( array $styling_models ): continue; } - $enabled_locations[] = $locations[$model->location] ?? ''; + $enabled_locations[] = $locations[ $model->location ] ?? ''; } return $enabled_locations; @@ -197,7 +197,7 @@ protected function mapped_pay_later_button_locations_value( array $styling_model continue; } - $enabled_locations[] = $locations[$model->location] ?? ''; + $enabled_locations[] = $locations[ $model->location ] ?? ''; } return $enabled_locations; @@ -214,7 +214,7 @@ protected function mapped_disabled_funding_value( array $styling_models ): ?arra $locations_to_context_map = $this->current_context_to_new_button_location_map(); foreach ( $styling_models as $model ) { - if ( $model->location !== $locations_to_context_map[$this->context()] || in_array( 'venmo', $model->methods, true ) ) { + if ( $model->location !== $locations_to_context_map[ $this->context() ] || in_array( 'venmo', $model->methods, true ) ) { continue; } @@ -229,7 +229,7 @@ protected function mapped_disabled_funding_value( array $styling_models ): ?arra /** * Retrieves the mapped enabled/disabled Google Pay or Apple Pay value from the new settings. * - * @param LocationStylingDTO[] $styling_models The list of location styling models. + * @param LocationStylingDTO[] $styling_models The list of location styling models. * @param 'googlepay'|'applepay' $button_name The button name ('googlepay' or 'applepay'). * @return int The enabled (1) or disabled (0) state. * @throws RuntimeException If an invalid button name is provided. @@ -243,7 +243,7 @@ protected function mapped_google_pay_or_apple_pay_enabled_value( array $styling_ foreach ( $styling_models as $model ) { if ( ! $model->enabled - || $model->location !== $locations_to_context_map[$this->context()] + || $model->location !== $locations_to_context_map[ $this->context() ] || ! in_array( $button_name, $model->methods, true ) ) { continue; From a6cb60687106cd8da73bfacea8ffe790ce4f16c4 Mon Sep 17 00:00:00 2001 From: Narek Zakarian Date: Fri, 7 Feb 2025 12:59:00 +0400 Subject: [PATCH 11/12] Support the cart block location for Google Pay --- .../src/Settings/StylingSettingsMapHelper.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/modules/ppcp-compat/src/Settings/StylingSettingsMapHelper.php b/modules/ppcp-compat/src/Settings/StylingSettingsMapHelper.php index 21c1f50e8..99ad3833f 100644 --- a/modules/ppcp-compat/src/Settings/StylingSettingsMapHelper.php +++ b/modules/ppcp-compat/src/Settings/StylingSettingsMapHelper.php @@ -110,6 +110,7 @@ protected function locations_map(): array { return array( 'product' => 'product', 'cart' => 'cart', + 'cart-block' => 'cart', 'checkout' => 'classic_checkout', 'mini-cart' => 'mini_cart', 'checkout-block-express' => 'express_checkout', @@ -178,6 +179,9 @@ protected function mapped_smart_button_locations_value( array $styling_models ): } $enabled_locations[] = $locations[ $model->location ] ?? ''; + if ( $model->location === 'cart' ) { + $enabled_locations[] = 'cart'; + } } return $enabled_locations; @@ -198,6 +202,9 @@ protected function mapped_pay_later_button_locations_value( array $styling_model } $enabled_locations[] = $locations[ $model->location ] ?? ''; + if ( $model->location === 'cart' ) { + $enabled_locations[] = 'cart'; + } } return $enabled_locations; @@ -234,7 +241,7 @@ protected function mapped_disabled_funding_value( array $styling_models ): ?arra * @return int The enabled (1) or disabled (0) state. * @throws RuntimeException If an invalid button name is provided. */ - protected function mapped_google_pay_or_apple_pay_enabled_value( array $styling_models, string $button_name ): int { + protected function mapped_google_pay_or_apple_pay_enabled_value( array $styling_models, string $button_name ): ?int { if ( $button_name !== 'googlepay' && $button_name !== 'applepay' ) { throw new RuntimeException( 'Wrong button name is provided. Either "googlepay" or "applepay" can be used' ); } From b00ac8f6413d351d5f8326f15fedd8f28102e454 Mon Sep 17 00:00:00 2001 From: Narek Zakarian Date: Fri, 7 Feb 2025 16:04:06 +0400 Subject: [PATCH 12/12] Add the `applepay_button_enabled` support --- modules/ppcp-compat/src/Settings/StylingSettingsMapHelper.php | 1 + 1 file changed, 1 insertion(+) diff --git a/modules/ppcp-compat/src/Settings/StylingSettingsMapHelper.php b/modules/ppcp-compat/src/Settings/StylingSettingsMapHelper.php index 99ad3833f..5755f9d01 100644 --- a/modules/ppcp-compat/src/Settings/StylingSettingsMapHelper.php +++ b/modules/ppcp-compat/src/Settings/StylingSettingsMapHelper.php @@ -44,6 +44,7 @@ public function map(): array { 'pay_later_button_locations' => '', 'disable_funding' => '', 'googlepay_button_enabled' => '', + 'applepay_button_enabled' => '', ); foreach ( $this->locations_map() as $old_location_name => $new_location_name ) {