diff --git a/modules/ppcp-applepay/src/Assets/ApplePayButton.php b/modules/ppcp-applepay/src/Assets/ApplePayButton.php index cee3660a0..98a9e86ed 100644 --- a/modules/ppcp-applepay/src/Assets/ApplePayButton.php +++ b/modules/ppcp-applepay/src/Assets/ApplePayButton.php @@ -147,6 +147,19 @@ class ApplePayButton implements ButtonInterface { */ public function initialize(): void { add_filter( 'ppcp_onboarding_options', array( $this, 'add_apple_onboarding_option' ), 10, 1 ); + add_filter( + 'ppcp_partner_referrals_option', + function ( array $option ): array { + if ( $option['valid'] ) { + return $option; + } + if ( $option['field'] === 'ppcp-onboarding-apple' ) { + $option['valid'] = true; + $option['value'] = ( $option['value'] ? '1' : '' ); + } + return $option; + } + ); add_filter( 'ppcp_partner_referrals_data', function ( array $data ): array { @@ -206,7 +219,7 @@ class ApplePayButton implements ButtonInterface { $checked = ''; } - return $options . '
  • '; diff --git a/modules/ppcp-googlepay/src/Assets/Button.php b/modules/ppcp-googlepay/src/Assets/Button.php index fadb7b89e..f8edf694f 100644 --- a/modules/ppcp-googlepay/src/Assets/Button.php +++ b/modules/ppcp-googlepay/src/Assets/Button.php @@ -127,6 +127,7 @@ class Button implements ButtonInterface { */ public function initialize(): void { add_filter( 'ppcp_onboarding_options', array( $this, 'add_onboarding_options' ), 10, 1 ); + add_filter( 'ppcp_partner_referrals_option', array( $this, 'filter_partner_referrals_option' ), 10, 1 ); add_filter( 'ppcp_partner_referrals_data', array( $this, 'add_partner_referrals_data' ), 10, 1 ); } @@ -150,11 +151,28 @@ class Button implements ButtonInterface { } return $options - . '
  • '; } + /** + * Filters a partner referrals option. + * + * @param array $option The option data. + * @return array + */ + public function filter_partner_referrals_option( array $option ): array { + if ( $option['valid'] ) { + return $option; + } + if ( $option['field'] === 'ppcp-onboarding-google' ) { + $option['valid'] = true; + $option['value'] = ( $option['value'] ? '1' : '' ); + } + return $option; + } + /** * Adds to partner referrals data. * diff --git a/modules/ppcp-onboarding/resources/js/onboarding.js b/modules/ppcp-onboarding/resources/js/onboarding.js index 46c962e81..1e32cd753 100644 --- a/modules/ppcp-onboarding/resources/js/onboarding.js +++ b/modules/ppcp-onboarding/resources/js/onboarding.js @@ -13,69 +13,87 @@ const ppcp_onboarding = { reload: function() { const buttons = document.querySelectorAll(ppcp_onboarding.BUTTON_SELECTOR); - if (0 === buttons.length) { - return; + if (buttons.length > 0) { + // Add event listeners to buttons preventing link clicking if PayPal init failed. + buttons.forEach( + (element) => { + if (element.hasAttribute('data-ppcp-button-initialized')) { + return; + } + + element.addEventListener( + 'click', + (e) => { + if (!element.hasAttribute('data-ppcp-button-initialized') || 'undefined' === typeof window.PAYPAL) { + e.preventDefault(); + } + } + ); + } + ); + + // Clear any previous PayPal scripts. + [ppcp_onboarding.PAYPAL_JS_ID, 'signup-js', 'biz-js'].forEach( + (scriptID) => { + const scriptTag = document.getElementById(scriptID); + + if (scriptTag) { + scriptTag.parentNode.removeChild(scriptTag); + } + + if ('undefined' !== typeof window.PAYPAL) { + delete window.PAYPAL; + } + } + ); + + // Load PayPal scripts. + const paypalScriptTag = document.createElement('script'); + paypalScriptTag.id = ppcp_onboarding.PAYPAL_JS_ID; + paypalScriptTag.src = PayPalCommerceGatewayOnboarding.paypal_js_url; + document.body.appendChild(paypalScriptTag); + + if (ppcp_onboarding._timeout) { + clearTimeout(ppcp_onboarding._timeout); + } + + ppcp_onboarding._timeout = setTimeout( + () => { + buttons.forEach((element) => { element.setAttribute('data-ppcp-button-initialized', 'true'); }); + + if ('undefined' !== window.PAYPAL.apps.Signup) { + window.PAYPAL.apps.Signup.render(); + } + }, + 1000 + ); } - // Add event listeners to buttons preventing link clicking if PayPal init failed. - buttons.forEach( - (element) => { - if (element.hasAttribute('data-ppcp-button-initialized')) { - return; - } + const $onboarding_inputs = function () { + return jQuery('*[data-onboarding-option]'); + }; + const onboarding_options = function () { + let options = {}; + $onboarding_inputs().each((index, el) => { + const opt = jQuery(el).data('onboardingOption'); + options[opt] = el.checked; + }); + return options; + } + const disable_onboarding_options = function () { + $onboarding_inputs().each((index, el) => { + el.setAttribute('disabled', 'disabled'); + }); + } + const enable_onboarding_options = function () { + $onboarding_inputs().each((index, el) => { + el.removeAttribute('disabled'); + }); + } + const update_onboarding_options = function () { + const spinner = ''; - element.addEventListener( - 'click', - (e) => { - if (!element.hasAttribute('data-ppcp-button-initialized') || 'undefined' === typeof window.PAYPAL) { - e.preventDefault(); - } - } - ); - } - ); - - // Clear any previous PayPal scripts. - [ppcp_onboarding.PAYPAL_JS_ID, 'signup-js', 'biz-js'].forEach( - (scriptID) => { - const scriptTag = document.getElementById(scriptID); - - if (scriptTag) { - scriptTag.parentNode.removeChild(scriptTag); - } - - if ('undefined' !== typeof window.PAYPAL) { - delete window.PAYPAL; - } - } - ); - - // Load PayPal scripts. - const paypalScriptTag = document.createElement('script'); - paypalScriptTag.id = ppcp_onboarding.PAYPAL_JS_ID; - paypalScriptTag.src = PayPalCommerceGatewayOnboarding.paypal_js_url; - document.body.appendChild(paypalScriptTag); - - if (ppcp_onboarding._timeout) { - clearTimeout(ppcp_onboarding._timeout); - } - - ppcp_onboarding._timeout = setTimeout( - () => { - buttons.forEach((element) => { element.setAttribute('data-ppcp-button-initialized', 'true'); }); - - if ('undefined' !== window.PAYPAL.apps.Signup) { - window.PAYPAL.apps.Signup.render(); - } - }, - 1000 - ); - - const onboard_pui = document.querySelector('#ppcp-onboarding-pui'); - const spinner = ''; - onboard_pui?.addEventListener('click', (event) => { - event.preventDefault(); - onboard_pui.setAttribute('disabled', 'disabled'); + disable_onboarding_options(); buttons.forEach((element) => { element.removeAttribute('href'); element.setAttribute('disabled', 'disabled'); @@ -90,7 +108,7 @@ const ppcp_onboarding = { credentials: 'same-origin', body: JSON.stringify({ nonce: PayPalCommerceGatewayOnboarding.pui_nonce, - checked: onboard_pui.checked + settings: onboarding_options() }) }).then((res)=>{ return res.json(); @@ -99,7 +117,6 @@ const ppcp_onboarding = { alert('Could not update signup buttons: ' + JSON.stringify(data)); return; } - buttons.forEach((element) => { for (let [key, value] of Object.entries(data.data.signup_links)) { key = 'connect-to' + key.replace(/-/g, ''); @@ -110,9 +127,13 @@ const ppcp_onboarding = { } } }); - onboard_pui.removeAttribute('disabled'); + enable_onboarding_options(); }); - }) + } + $onboarding_inputs().on('click', (event) => { + event.preventDefault(); + update_onboarding_options(); + }); }, loginSeller: function(env, authCode, sharedId) { diff --git a/modules/ppcp-onboarding/services.php b/modules/ppcp-onboarding/services.php index 06f2d27a7..27ab1ad25 100644 --- a/modules/ppcp-onboarding/services.php +++ b/modules/ppcp-onboarding/services.php @@ -18,7 +18,7 @@ use WooCommerce\PayPalCommerce\ApiClient\Endpoint\PartnerReferrals; use WooCommerce\PayPalCommerce\ApiClient\Helper\Cache; use WooCommerce\PayPalCommerce\Onboarding\Assets\OnboardingAssets; use WooCommerce\PayPalCommerce\Onboarding\Endpoint\LoginSellerEndpoint; -use WooCommerce\PayPalCommerce\Onboarding\Endpoint\PayUponInvoiceEndpoint; +use WooCommerce\PayPalCommerce\Onboarding\Endpoint\UpdateSignupLinksEndpoint; use WooCommerce\PayPalCommerce\Onboarding\Render\OnboardingOptionsRenderer; use WooCommerce\PayPalCommerce\Onboarding\Render\OnboardingRenderer; use WooCommerce\PayPalCommerce\Onboarding\OnboardingRESTController; @@ -187,8 +187,8 @@ return array( $logger ); }, - 'onboarding.endpoint.pui' => static function( ContainerInterface $container ) : PayUponInvoiceEndpoint { - return new PayUponInvoiceEndpoint( + 'onboarding.endpoint.pui' => static function( ContainerInterface $container ) : UpdateSignupLinksEndpoint { + return new UpdateSignupLinksEndpoint( $container->get( 'wcgateway.settings' ), $container->get( 'button.request-data' ), $container->get( 'onboarding.signup-link-cache' ), diff --git a/modules/ppcp-onboarding/src/Assets/OnboardingAssets.php b/modules/ppcp-onboarding/src/Assets/OnboardingAssets.php index 5cac972dc..422830ecb 100644 --- a/modules/ppcp-onboarding/src/Assets/OnboardingAssets.php +++ b/modules/ppcp-onboarding/src/Assets/OnboardingAssets.php @@ -10,6 +10,7 @@ declare(strict_types=1); namespace WooCommerce\PayPalCommerce\Onboarding\Assets; use WooCommerce\PayPalCommerce\Onboarding\Endpoint\LoginSellerEndpoint; +use WooCommerce\PayPalCommerce\Onboarding\Endpoint\UpdateSignupLinksEndpoint; use WooCommerce\PayPalCommerce\Onboarding\Environment; use WooCommerce\PayPalCommerce\Onboarding\State; use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings; @@ -155,8 +156,8 @@ class OnboardingAssets { 'error_messages' => array( 'no_credentials' => __( 'API credentials must be entered to save the settings.', 'woocommerce-paypal-payments' ), ), - 'pui_endpoint' => \WC_AJAX::get_endpoint( 'ppc-pui' ), - 'pui_nonce' => wp_create_nonce( 'ppc-pui' ), + 'pui_endpoint' => \WC_AJAX::get_endpoint( UpdateSignupLinksEndpoint::ENDPOINT ), + 'pui_nonce' => wp_create_nonce( UpdateSignupLinksEndpoint::ENDPOINT ), ); } diff --git a/modules/ppcp-onboarding/src/Endpoint/PayUponInvoiceEndpoint.php b/modules/ppcp-onboarding/src/Endpoint/UpdateSignupLinksEndpoint.php similarity index 82% rename from modules/ppcp-onboarding/src/Endpoint/PayUponInvoiceEndpoint.php rename to modules/ppcp-onboarding/src/Endpoint/UpdateSignupLinksEndpoint.php index bc1607aa4..d7487424a 100644 --- a/modules/ppcp-onboarding/src/Endpoint/PayUponInvoiceEndpoint.php +++ b/modules/ppcp-onboarding/src/Endpoint/UpdateSignupLinksEndpoint.php @@ -14,14 +14,17 @@ use Psr\Log\LoggerInterface; use WooCommerce\PayPalCommerce\ApiClient\Helper\Cache; use WooCommerce\PayPalCommerce\Button\Endpoint\EndpointInterface; use WooCommerce\PayPalCommerce\Button\Endpoint\RequestData; +use WooCommerce\PayPalCommerce\Onboarding\Helper\OnboardingUrl; use WooCommerce\PayPalCommerce\Onboarding\Render\OnboardingRenderer; use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings; use WooCommerce\PayPalCommerce\WcGateway\Exception\NotFoundException; /** - * Class PayUponInvoiceEndpoint + * Class UpdateSignupLinksEndpoint */ -class PayUponInvoiceEndpoint implements EndpointInterface { +class UpdateSignupLinksEndpoint implements EndpointInterface { + + const ENDPOINT = 'ppc-update-signup-links'; /** * The settings. @@ -66,7 +69,7 @@ class PayUponInvoiceEndpoint implements EndpointInterface { protected $logger; /** - * PayUponInvoiceEndpoint constructor. + * UpdateSignupLinksEndpoint constructor. * * @param Settings $settings The settings. * @param RequestData $request_data The request data. @@ -97,7 +100,7 @@ class PayUponInvoiceEndpoint implements EndpointInterface { * @return string */ public static function nonce(): string { - return 'ppc-pui'; + return self::ENDPOINT; } /** @@ -116,13 +119,23 @@ class PayUponInvoiceEndpoint implements EndpointInterface { try { $data = $this->request_data->read_request( $this->nonce() ); - $this->settings->set( 'ppcp-onboarding-pui', $data['checked'] ); + + foreach ( $data['settings'] ?? array() as $field => $value ) { + $option = apply_filters( 'ppcp_partner_referrals_option', array( + 'field' => $field, + 'value' => $value, + 'valid' => false, + ) ); + + if ( $option['valid'] ) { + $this->settings->set( $field, $value ); + } + } + $this->settings->persist(); foreach ( $this->signup_link_ids as $key ) { - if ( $this->signup_link_cache->has( $key ) ) { - $this->signup_link_cache->delete( $key ); - } + ( new OnboardingUrl( $this->signup_link_cache, $key, get_current_user_id() ) )->delete(); } foreach ( $this->signup_link_ids as $key ) { diff --git a/modules/ppcp-onboarding/src/OnboardingModule.php b/modules/ppcp-onboarding/src/OnboardingModule.php index 1f94f0fee..578cda42c 100644 --- a/modules/ppcp-onboarding/src/OnboardingModule.php +++ b/modules/ppcp-onboarding/src/OnboardingModule.php @@ -9,6 +9,7 @@ declare(strict_types=1); namespace WooCommerce\PayPalCommerce\Onboarding; +use WooCommerce\PayPalCommerce\Onboarding\Endpoint\UpdateSignupLinksEndpoint; use WooCommerce\PayPalCommerce\Vendor\Dhii\Container\ServiceProvider; use WooCommerce\PayPalCommerce\Vendor\Dhii\Modular\Module\ModuleInterface; use WooCommerce\PayPalCommerce\Onboarding\Assets\OnboardingAssets; @@ -96,7 +97,7 @@ class OnboardingModule implements ModuleInterface { ); add_action( - 'wc_ajax_ppc-pui', + 'wc_ajax_' . UpdateSignupLinksEndpoint::ENDPOINT, static function () use ( $c ) { $endpoint = $c->get( 'onboarding.endpoint.pui' ); $endpoint->handle_request(); diff --git a/modules/ppcp-onboarding/src/Render/OnboardingOptionsRenderer.php b/modules/ppcp-onboarding/src/Render/OnboardingOptionsRenderer.php index f78c817c6..a5d7628ec 100644 --- a/modules/ppcp-onboarding/src/Render/OnboardingOptionsRenderer.php +++ b/modules/ppcp-onboarding/src/Render/OnboardingOptionsRenderer.php @@ -95,7 +95,7 @@ class OnboardingOptionsRenderer { $checked = ''; } - return '
  • '; } diff --git a/modules/ppcp-wc-gateway/services.php b/modules/ppcp-wc-gateway/services.php index 3805230a0..8266d70e7 100644 --- a/modules/ppcp-wc-gateway/services.php +++ b/modules/ppcp-wc-gateway/services.php @@ -200,10 +200,12 @@ return array( return $ppcp_tab ? $ppcp_tab : $section; }, - 'wcgateway.settings' => static function ( ContainerInterface $container ): Settings { - $default_button_locations = $container->get( 'wcgateway.button.default-locations' ); - return new Settings( $default_button_locations ); - }, + 'wcgateway.settings' => SingletonDecorator::make( + static function ( ContainerInterface $container ): Settings { + $default_button_locations = $container->get( 'wcgateway.button.default-locations' ); + return new Settings( $default_button_locations ); + } + ), 'wcgateway.notice.connect' => static function ( ContainerInterface $container ): ConnectAdminNotice { $state = $container->get( 'onboarding.state' ); $settings = $container->get( 'wcgateway.settings' ); diff --git a/modules/ppcp-wc-gateway/src/Gateway/PayUponInvoice/PayUponInvoice.php b/modules/ppcp-wc-gateway/src/Gateway/PayUponInvoice/PayUponInvoice.php index 0d0807659..9da44d069 100644 --- a/modules/ppcp-wc-gateway/src/Gateway/PayUponInvoice/PayUponInvoice.php +++ b/modules/ppcp-wc-gateway/src/Gateway/PayUponInvoice/PayUponInvoice.php @@ -142,6 +142,20 @@ class PayUponInvoice { $this->settings->persist(); } + add_filter( + 'ppcp_partner_referrals_option', + function ( array $option ): array { + if ( $option['valid'] ) { + return $option; + } + if ( $option['field'] === 'ppcp-onboarding-pui' ) { + $option['valid'] = true; + $option['value'] = ( $option['value'] ? '1' : '' ); + } + return $option; + } + ); + add_filter( 'ppcp_partner_referrals_data', function ( array $data ): array {