From 894920bdf6fe9b136577d9d076cd0408a63303bf Mon Sep 17 00:00:00 2001 From: Narek Zakarian Date: Tue, 19 Mar 2024 17:10:56 +0400 Subject: [PATCH 01/11] Fix the custom placement --- .../resources/js/paylater-configurator.js | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/modules/ppcp-paylater-configurator/resources/js/paylater-configurator.js b/modules/ppcp-paylater-configurator/resources/js/paylater-configurator.js index 15f65f183..d03912743 100644 --- a/modules/ppcp-paylater-configurator/resources/js/paylater-configurator.js +++ b/modules/ppcp-paylater-configurator/resources/js/paylater-configurator.js @@ -40,10 +40,7 @@ document.addEventListener( 'DOMContentLoaded', () => { partnerClientId: PcpPayLaterConfigurator.partnerClientId, partnerName: 'WooCommerce', bnCode: 'Woo_PPCP', - placements: ['cart', 'checkout', 'product', 'shop', 'home'], - custom_placement:[{ - message_reference: 'woocommerceBlock', - }], + placements: ['cart', 'checkout', 'product', 'shop', 'home', 'woocommerceBlock'], styleOverrides: { button: publishButtonClassName, header: PcpPayLaterConfigurator.headerClassName, From 785404335f18796eb1c2760df7ab8636a6c9db36 Mon Sep 17 00:00:00 2001 From: Narek Zakarian Date: Tue, 19 Mar 2024 17:11:23 +0400 Subject: [PATCH 02/11] Save the text color --- modules/ppcp-paylater-configurator/src/Endpoint/SaveConfig.php | 1 + 1 file changed, 1 insertion(+) diff --git a/modules/ppcp-paylater-configurator/src/Endpoint/SaveConfig.php b/modules/ppcp-paylater-configurator/src/Endpoint/SaveConfig.php index 6584b7da3..41152b6d6 100644 --- a/modules/ppcp-paylater-configurator/src/Endpoint/SaveConfig.php +++ b/modules/ppcp-paylater-configurator/src/Endpoint/SaveConfig.php @@ -129,6 +129,7 @@ class SaveConfig { $this->set_value_if_present( $config, 'logo-type', "pay_later_{$location}_message_logo" ); $this->set_value_if_present( $config, 'logo-color', "pay_later_{$location}_message_color" ); $this->set_value_if_present( $config, 'text-size', "pay_later_{$location}_message_text_size" ); + $this->set_value_if_present( $config, 'text-color', "pay_later_{$location}_message_text_color" ); } /** From 7ca0940ef2e3e6fc0ba429ca13e38191eafb2aed Mon Sep 17 00:00:00 2001 From: Narek Zakarian Date: Tue, 19 Mar 2024 17:12:20 +0400 Subject: [PATCH 03/11] Adjust the configurator config to support custom placement --- .../src/Factory/ConfigFactory.php | 102 ++++++++++++++---- 1 file changed, 80 insertions(+), 22 deletions(-) diff --git a/modules/ppcp-paylater-configurator/src/Factory/ConfigFactory.php b/modules/ppcp-paylater-configurator/src/Factory/ConfigFactory.php index d36fe91c3..0fedbe44c 100644 --- a/modules/ppcp-paylater-configurator/src/Factory/ConfigFactory.php +++ b/modules/ppcp-paylater-configurator/src/Factory/ConfigFactory.php @@ -27,7 +27,7 @@ class ConfigFactory { 'product' => $this->for_location( $settings, 'product' ), 'shop' => $this->for_location( $settings, 'shop' ), 'home' => $this->for_location( $settings, 'home' ), - 'woocommerceBlock' => $this->for_location( $settings, 'woocommerceBlock' ), + 'custom_placement' => $this->for_location( $settings, 'woocommerceBlock' ), ); } @@ -40,29 +40,87 @@ class ConfigFactory { private function for_location( Settings $settings, string $location ): array { $selected_locations = $settings->has( 'pay_later_messaging_locations' ) ? $settings->get( 'pay_later_messaging_locations' ) : array(); - if ( in_array( $location, array( 'shop', 'home' ), true ) ) { - $config = array( - 'layout' => $this->get_or_default( $settings, "pay_later_{$location}_message_layout", 'flex' ), - 'color' => $this->get_or_default( $settings, "pay_later_{$location}_message_flex_color", 'black' ), - 'ratio' => $this->get_or_default( $settings, "pay_later_{$location}_message_flex_ratio", '8x1' ), - ); - } elseif ( $location !== 'woocommerceBlock' ) { - $config = array( - 'layout' => $this->get_or_default( $settings, "pay_later_{$location}_message_layout", 'text' ), - 'logo-position' => $this->get_or_default( $settings, "pay_later_{$location}_message_position", 'left' ), - 'logo-type' => $this->get_or_default( $settings, "pay_later_{$location}_message_logo", 'inline' ), - 'text-color' => $this->get_or_default( $settings, "pay_later_{$location}_message_color", 'black' ), - 'text-size' => $this->get_or_default( $settings, "pay_later_{$location}_message_text_size", '12' ), - - ); + switch ( $location ) { + case 'shop': + case 'home': + $config = $this->for_shop_or_home( $settings, $location, $selected_locations ); + break; + case 'woocommerceBlock': + $config = $this->for_woocommerce_block( $selected_locations ); + break; + default: + $config = $this->for_default_location( $settings, $location, $selected_locations ); + break; } - return array_merge( - array( - 'status' => in_array( $location, $selected_locations, true ) ? 'enabled' : 'disabled', - 'placement' => $location, - ), - $config ?? array() + return $config; + } + + /** + * Returns the configurator config for shop, home locations. + * + * @param Settings $settings The settings. + * @param string $location The location. + * @param string[] $selected_locations The list of selected locations. + * @return array{ + * layout: string, + * color: string, + * ratio: string, + * status: string, + * placement: string + * } The configurator config map. + */ + private function for_shop_or_home( Settings $settings, string $location, array $selected_locations ): array { + return array( + 'layout' => $this->get_or_default( $settings, "pay_later_{$location}_message_layout", 'flex' ), + 'color' => $this->get_or_default( $settings, "pay_later_{$location}_message_flex_color", 'black' ), + 'ratio' => $this->get_or_default( $settings, "pay_later_{$location}_message_flex_ratio", '8x1' ), + 'status' => in_array( $location, $selected_locations, true ) ? 'enabled' : 'disabled', + 'placement' => $location, + ); + } + + /** + * Returns the configurator config for woocommerceBlock location. + * + * @param array $selected_locations The list of selected locations. + * @return array{ + * status: string, + * message_reference: string + * } The configurator config map. + */ + private function for_woocommerce_block( array $selected_locations ): array { + return array( + 'status' => in_array( 'woocommerceBlock', $selected_locations, true ) ? 'enabled' : 'disabled', + 'message_reference' => 'woocommerceBlock', + ); + } + + /** + * Returns the configurator config for default locations. + * + * @param Settings $settings The settings. + * @param string $location The location. + * @param string[] $selected_locations The list of selected locations. + * @return array{ + * layout: string, + * logo-position: string, + * logo-type: string, + * text-color: string, + * text-size: string, + * status: string, + * placement: string + * } The configurator config map. + */ + private function for_default_location( Settings $settings, string $location, array $selected_locations ): array { + return array( + 'layout' => $this->get_or_default( $settings, "pay_later_{$location}_message_layout", 'text' ), + 'logo-position' => $this->get_or_default( $settings, "pay_later_{$location}_message_position", 'left' ), + 'logo-type' => $this->get_or_default( $settings, "pay_later_{$location}_message_logo", 'inline' ), + 'text-color' => $this->get_or_default( $settings, "pay_later_{$location}_message_text_color", 'black' ), + 'text-size' => $this->get_or_default( $settings, "pay_later_{$location}_message_text_size", '12' ), + 'status' => in_array( $location, $selected_locations, true ) ? 'enabled' : 'disabled', + 'placement' => $location, ); } From ded41850fffcbd707805a1dc3fa724f67986d927 Mon Sep 17 00:00:00 2001 From: Narek Zakarian Date: Tue, 19 Mar 2024 18:07:19 +0400 Subject: [PATCH 04/11] Change placment names for the block --- modules/ppcp-paylater-block/resources/js/edit.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/modules/ppcp-paylater-block/resources/js/edit.js b/modules/ppcp-paylater-block/resources/js/edit.js index c702d0ad9..06f801761 100644 --- a/modules/ppcp-paylater-block/resources/js/edit.js +++ b/modules/ppcp-paylater-block/resources/js/edit.js @@ -194,12 +194,11 @@ export default function Edit( { attributes, clientId, setAttributes } ) { help={ __( 'Used for the analytics dashboard in the merchant account.', 'woocommerce-paypal-payments' ) } options={ [ { label: __( 'Detect automatically', 'woocommerce-paypal-payments' ), value: 'auto' }, + { label: __( 'Product Page', 'woocommerce-paypal-payments' ), value: 'product-page' }, { label: __( 'Cart', 'woocommerce-paypal-payments' ), value: 'cart' }, - { label: __( 'Payment', 'woocommerce-paypal-payments' ), value: 'payment' }, - { label: __( 'Product', 'woocommerce-paypal-payments' ), value: 'product' }, - { label: __( 'Product list', 'woocommerce-paypal-payments' ), value: 'product-list' }, + { label: __( 'Checkout', 'woocommerce-paypal-payments' ), value: 'checkout' }, { label: __( 'Home', 'woocommerce-paypal-payments' ), value: 'home' }, - { label: __( 'Category', 'woocommerce-paypal-payments' ), value: 'category' }, + { label: __( 'Shop', 'woocommerce-paypal-payments' ), value: 'shop' }, ] } value={ placement } onChange={ ( value ) => setAttributes( { placement: value } ) } From 6c60ee1e86f19029a00f60d0272f59f895a5ac5c Mon Sep 17 00:00:00 2001 From: Narek Zakarian Date: Tue, 19 Mar 2024 18:35:49 +0400 Subject: [PATCH 05/11] Fix Psalm --- .../src/Factory/ConfigFactory.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/modules/ppcp-paylater-configurator/src/Factory/ConfigFactory.php b/modules/ppcp-paylater-configurator/src/Factory/ConfigFactory.php index 0fedbe44c..7c7b61e7c 100644 --- a/modules/ppcp-paylater-configurator/src/Factory/ConfigFactory.php +++ b/modules/ppcp-paylater-configurator/src/Factory/ConfigFactory.php @@ -66,7 +66,7 @@ class ConfigFactory { * layout: string, * color: string, * ratio: string, - * status: string, + * status: "disabled"|"enabled", * placement: string * } The configurator config map. */ @@ -85,7 +85,7 @@ class ConfigFactory { * * @param array $selected_locations The list of selected locations. * @return array{ - * status: string, + * status: "disabled"|"enabled", * message_reference: string * } The configurator config map. */ @@ -108,7 +108,7 @@ class ConfigFactory { * logo-type: string, * text-color: string, * text-size: string, - * status: string, + * status: "disabled"|"enabled", * placement: string * } The configurator config map. */ @@ -131,9 +131,9 @@ class ConfigFactory { * @param string $key The key. * @param mixed $default The default value. * @param array|null $allowed_values The list of allowed values, or null if all values are allowed. - * @return mixed + * @return string */ - private function get_or_default( Settings $settings, string $key, $default, ?array $allowed_values = null ) { + private function get_or_default( Settings $settings, string $key, $default, ?array $allowed_values = null ): string { if ( $settings->has( $key ) ) { $value = $settings->get( $key ); if ( ! $allowed_values || in_array( $value, $allowed_values, true ) ) { From 4cfa04b47396891eea9af390a639a02324395dcb Mon Sep 17 00:00:00 2001 From: Narek Zakarian Date: Wed, 20 Mar 2024 16:24:00 +0400 Subject: [PATCH 06/11] Fix the text color --- .../resources/js/paylater-configurator.js | 2 +- modules/ppcp-paylater-configurator/src/Endpoint/SaveConfig.php | 2 +- .../ppcp-paylater-configurator/src/Factory/ConfigFactory.php | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/modules/ppcp-paylater-configurator/resources/js/paylater-configurator.js b/modules/ppcp-paylater-configurator/resources/js/paylater-configurator.js index d03912743..440e2648b 100644 --- a/modules/ppcp-paylater-configurator/resources/js/paylater-configurator.js +++ b/modules/ppcp-paylater-configurator/resources/js/paylater-configurator.js @@ -29,7 +29,7 @@ document.addEventListener( 'DOMContentLoaded', () => { setTimeout(() => { saveChangesButton.click(); // Trigger click event on saveChangesButton isSaving = false; // Reset flag when saving is complete - }, 500); // Adjust the delay as needed + }, 1000); // Adjust the delay as needed } }); diff --git a/modules/ppcp-paylater-configurator/src/Endpoint/SaveConfig.php b/modules/ppcp-paylater-configurator/src/Endpoint/SaveConfig.php index 41152b6d6..b44ca77af 100644 --- a/modules/ppcp-paylater-configurator/src/Endpoint/SaveConfig.php +++ b/modules/ppcp-paylater-configurator/src/Endpoint/SaveConfig.php @@ -129,7 +129,7 @@ class SaveConfig { $this->set_value_if_present( $config, 'logo-type', "pay_later_{$location}_message_logo" ); $this->set_value_if_present( $config, 'logo-color', "pay_later_{$location}_message_color" ); $this->set_value_if_present( $config, 'text-size', "pay_later_{$location}_message_text_size" ); - $this->set_value_if_present( $config, 'text-color', "pay_later_{$location}_message_text_color" ); + $this->set_value_if_present( $config, 'text-color', "pay_later_{$location}_message_color" ); } /** diff --git a/modules/ppcp-paylater-configurator/src/Factory/ConfigFactory.php b/modules/ppcp-paylater-configurator/src/Factory/ConfigFactory.php index 7c7b61e7c..695fadf4c 100644 --- a/modules/ppcp-paylater-configurator/src/Factory/ConfigFactory.php +++ b/modules/ppcp-paylater-configurator/src/Factory/ConfigFactory.php @@ -117,7 +117,7 @@ class ConfigFactory { 'layout' => $this->get_or_default( $settings, "pay_later_{$location}_message_layout", 'text' ), 'logo-position' => $this->get_or_default( $settings, "pay_later_{$location}_message_position", 'left' ), 'logo-type' => $this->get_or_default( $settings, "pay_later_{$location}_message_logo", 'inline' ), - 'text-color' => $this->get_or_default( $settings, "pay_later_{$location}_message_text_color", 'black' ), + 'text-color' => $this->get_or_default( $settings, "pay_later_{$location}_message_color", 'black' ), 'text-size' => $this->get_or_default( $settings, "pay_later_{$location}_message_text_size", '12' ), 'status' => in_array( $location, $selected_locations, true ) ? 'enabled' : 'disabled', 'placement' => $location, From 2fedd36dbf329aeffbac9d88e5491adfc0559aea Mon Sep 17 00:00:00 2001 From: Narek Zakarian Date: Wed, 20 Mar 2024 16:26:28 +0400 Subject: [PATCH 07/11] Fix the placement label for block --- modules/ppcp-paylater-block/resources/js/edit.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/ppcp-paylater-block/resources/js/edit.js b/modules/ppcp-paylater-block/resources/js/edit.js index 06f801761..44b4f895d 100644 --- a/modules/ppcp-paylater-block/resources/js/edit.js +++ b/modules/ppcp-paylater-block/resources/js/edit.js @@ -194,7 +194,7 @@ export default function Edit( { attributes, clientId, setAttributes } ) { help={ __( 'Used for the analytics dashboard in the merchant account.', 'woocommerce-paypal-payments' ) } options={ [ { label: __( 'Detect automatically', 'woocommerce-paypal-payments' ), value: 'auto' }, - { label: __( 'Product Page', 'woocommerce-paypal-payments' ), value: 'product-page' }, + { label: __( 'Product Page', 'woocommerce-paypal-payments' ), value: 'product' }, { label: __( 'Cart', 'woocommerce-paypal-payments' ), value: 'cart' }, { label: __( 'Checkout', 'woocommerce-paypal-payments' ), value: 'checkout' }, { label: __( 'Home', 'woocommerce-paypal-payments' ), value: 'home' }, From 8309a4d54a2a5608887abde3372819e66cf6f144 Mon Sep 17 00:00:00 2001 From: Narek Zakarian Date: Tue, 2 Apr 2024 15:09:02 +0400 Subject: [PATCH 08/11] Fix the block placement name --- .../ppcp-paylater-configurator/src/Factory/ConfigFactory.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/ppcp-paylater-configurator/src/Factory/ConfigFactory.php b/modules/ppcp-paylater-configurator/src/Factory/ConfigFactory.php index 695fadf4c..c73bbd0ab 100644 --- a/modules/ppcp-paylater-configurator/src/Factory/ConfigFactory.php +++ b/modules/ppcp-paylater-configurator/src/Factory/ConfigFactory.php @@ -27,7 +27,7 @@ class ConfigFactory { 'product' => $this->for_location( $settings, 'product' ), 'shop' => $this->for_location( $settings, 'shop' ), 'home' => $this->for_location( $settings, 'home' ), - 'custom_placement' => $this->for_location( $settings, 'woocommerceBlock' ), + 'custom_placement' => array( $this->for_location( $settings, 'woocommerceBlock' ) ), ); } @@ -91,7 +91,7 @@ class ConfigFactory { */ private function for_woocommerce_block( array $selected_locations ): array { return array( - 'status' => in_array( 'woocommerceBlock', $selected_locations, true ) ? 'enabled' : 'disabled', + 'status' => in_array( 'custom_placement', $selected_locations, true ) ? 'enabled' : 'disabled', 'message_reference' => 'woocommerceBlock', ); } From 012c7281f73f8d6da5529459589a8e50cf0e0722 Mon Sep 17 00:00:00 2001 From: Narek Zakarian Date: Tue, 2 Apr 2024 15:09:21 +0400 Subject: [PATCH 09/11] Fix the block placement name --- .../resources/js/paylater-configurator.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/modules/ppcp-paylater-configurator/resources/js/paylater-configurator.js b/modules/ppcp-paylater-configurator/resources/js/paylater-configurator.js index 440e2648b..9549ef2af 100644 --- a/modules/ppcp-paylater-configurator/resources/js/paylater-configurator.js +++ b/modules/ppcp-paylater-configurator/resources/js/paylater-configurator.js @@ -33,14 +33,13 @@ document.addEventListener( 'DOMContentLoaded', () => { } }); - merchantConfigurators.Messaging({ config: PcpPayLaterConfigurator.config, merchantClientId: PcpPayLaterConfigurator.merchantClientId, partnerClientId: PcpPayLaterConfigurator.partnerClientId, partnerName: 'WooCommerce', bnCode: 'Woo_PPCP', - placements: ['cart', 'checkout', 'product', 'shop', 'home', 'woocommerceBlock'], + placements: ['cart', 'checkout', 'product', 'shop', 'home', 'custom_placement'], styleOverrides: { button: publishButtonClassName, header: PcpPayLaterConfigurator.headerClassName, From 155677934abbd831a9f95b7b0a66dbdc7e448c68 Mon Sep 17 00:00:00 2001 From: Narek Zakarian Date: Tue, 2 Apr 2024 15:09:52 +0400 Subject: [PATCH 10/11] Fix saving the block config --- .../ppcp-paylater-configurator/src/Endpoint/SaveConfig.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/modules/ppcp-paylater-configurator/src/Endpoint/SaveConfig.php b/modules/ppcp-paylater-configurator/src/Endpoint/SaveConfig.php index b44ca77af..df4e4affa 100644 --- a/modules/ppcp-paylater-configurator/src/Endpoint/SaveConfig.php +++ b/modules/ppcp-paylater-configurator/src/Endpoint/SaveConfig.php @@ -99,10 +99,13 @@ class SaveConfig { $this->settings->set( 'pay_later_messaging_enabled', true ); $enabled_locations = array(); - foreach ( $config as $placement => $data ) { $this->save_config_for_location( $data, $placement ); + if ( $placement === 'custom_placement' ) { + $data = $data[0] ?? array(); + } + if ( $data['status'] === 'enabled' ) { $enabled_locations[] = $placement; } From b4e1ce3eb8db4232313efad0d65baca0ba10c3ef Mon Sep 17 00:00:00 2001 From: Narek Zakarian Date: Tue, 2 Apr 2024 15:10:35 +0400 Subject: [PATCH 11/11] Fix checking if the block placement is active --- modules/ppcp-paylater-block/src/PayLaterBlockModule.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/ppcp-paylater-block/src/PayLaterBlockModule.php b/modules/ppcp-paylater-block/src/PayLaterBlockModule.php index d6b9c7404..223ccd972 100644 --- a/modules/ppcp-paylater-block/src/PayLaterBlockModule.php +++ b/modules/ppcp-paylater-block/src/PayLaterBlockModule.php @@ -40,7 +40,7 @@ class PayLaterBlockModule implements ModuleInterface { * @return bool true if the block is enabled, otherwise false. */ public static function is_block_enabled( SettingsStatus $settings_status ): bool { - return self::is_module_loading_required() && $settings_status->is_pay_later_messaging_enabled_for_location( 'woocommerceBlock' ); + return self::is_module_loading_required() && $settings_status->is_pay_later_messaging_enabled_for_location( 'custom_placement' ); } /**