Skip to content

Commit

Permalink
Merge pull request #3100 from woocommerce/PCP-4209-create-a-method-to…
Browse files Browse the repository at this point in the history
…-reset-db-settings

Create a method to reset DB settings (4209)
  • Loading branch information
Dinamiko authored Feb 12, 2025
2 parents 8258c1c + cb218b6 commit 6eb5e9f
Show file tree
Hide file tree
Showing 11 changed files with 184 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,17 @@ $width_gap: 24px;
color: var(--color-text-teriary);
margin: 0;
}

.ppcp--card-actions {
opacity: 0.5;
transition: opacity 0.3s;

&:hover {
opacity: 1;
}

.components-button.is-tertiary:first-child {
padding-left: 0;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* Modal for disconnecting the merchant from the current PayPal account.
*/
.ppcp--modal-disconnect {
.ppcp--toggle-danger {
--wp-components-color-accent: #cc1818
}

.ppcp--action-buttons {
text-align: right;
margin-top: 32px;
}
}
1 change: 1 addition & 0 deletions modules/ppcp-settings/resources/css/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@

@import './components/reusable-components/payment-method-modal';
@import './components/screens/fullscreen';
@import './components/screens/modals';
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ const ConnectionDescription = () => {
'Your PayPal account connection details.',
'woocommerce-paypal-payments'
) }
<DisconnectButton />
<div className="ppcp--card-actions">
<DisconnectButton />
</div>
</>
);
};
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { __ } from '@wordpress/i18n';
import { Button, Modal } from '@wordpress/components';
import { Button, Modal, ToggleControl } from '@wordpress/components';
import { useCallback, useState } from '@wordpress/element';

import { CommonHooks } from '../../../../../../data';
import { HStack } from '../../../../../ReusableComponents/Stack';

const DisconnectButton = () => {
const [ isOpen, setIsOpen ] = useState( false );
const [ resetFlag, setResetFlag ] = useState( false );
const { disconnectMerchant } = CommonHooks.useDisconnectMerchant();

const handleOpen = useCallback( () => {
Expand All @@ -18,9 +19,9 @@ const DisconnectButton = () => {
}, [] );

const handleConfirm = useCallback( async () => {
await disconnectMerchant();
await disconnectMerchant( resetFlag );
window.location.reload();
}, [ disconnectMerchant ] );
}, [ disconnectMerchant, resetFlag ] );

const confirmationTitle = __(
'Disconnect from PayPal?',
Expand All @@ -39,6 +40,7 @@ const DisconnectButton = () => {

{ isOpen && (
<Modal
className="ppcp--modal-disconnect"
size="small"
title={ confirmationTitle }
onRequestClose={ handleCancel }
Expand All @@ -49,7 +51,28 @@ const DisconnectButton = () => {
'woocommerce-paypal-payments'
) }
</p>
<HStack>
<ToggleControl
__nextHasNoMarginBottom
className="ppcp--toggle-danger"
checked={ resetFlag }
onChange={ setResetFlag }
label={ __(
'Start over',
'woocommerce-paypal-payments'
) }
help={
resetFlag
? __(
'Attention: The plugin is reset to its initial state!',
'woocommerce-paypal-payments'
)
: __(
'Disconnect, but preserve all settings',
'woocommerce-paypal-payments'
)
}
/>
<HStack className="ppcp--action-buttons">
<Button variant="tertiary" onClick={ handleCancel }>
{ __( 'Cancel', 'woocommerce-paypal-payments' ) }
</Button>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,13 +153,17 @@ export function authenticateWithOAuth( sharedId, authCode, useSandbox ) {
/**
* Side effect. Checks webhook simulation.
*
* @param {boolean} fullReset When true, all plugin settings are reset to initial values.
* @return {Function} The thunk function.
*/
export function disconnectMerchant() {
export function disconnectMerchant( fullReset = false ) {
return async () => {
return await apiFetch( {
path: REST_DISCONNECT_MERCHANT_PATH,
method: 'POST',
data: {
reset: fullReset,
},
} );
};
}
Expand Down
47 changes: 31 additions & 16 deletions modules/ppcp-settings/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
use WooCommerce\PayPalCommerce\Settings\Service\TodosEligibilityService;
use WooCommerce\PayPalCommerce\Vendor\Psr\Container\ContainerInterface;
use WooCommerce\PayPalCommerce\Settings\Service\DataSanitizer;
use WooCommerce\PayPalCommerce\Settings\Service\SettingsDataManager;

return array(
'settings.url' => static function ( ContainerInterface $container ) : string {
Expand Down Expand Up @@ -122,9 +123,10 @@
$container->get( 'woocommerce.logger.woocommerce' )
);
},
'settings.rest.connect_manual' => static function ( ContainerInterface $container ) : AuthenticationRestEndpoint {
'settings.rest.authentication' => static function ( ContainerInterface $container ) : AuthenticationRestEndpoint {
return new AuthenticationRestEndpoint(
$container->get( 'settings.service.authentication_manager' ),
$container->get( 'settings.service.data-manager' )
);
},
'settings.rest.login_link' => static function ( ContainerInterface $container ) : LoginLinkRestEndpoint {
Expand Down Expand Up @@ -246,6 +248,19 @@
'settings.service.sanitizer' => static function ( ContainerInterface $container ) : DataSanitizer {
return new DataSanitizer();
},
'settings.service.data-manager' => static function ( ContainerInterface $container ) : SettingsDataManager {
$models = array(
$container->get( 'settings.data.onboarding' ),
$container->get( 'settings.data.general' ),
$container->get( 'settings.data.styling' ),
$container->get( 'settings.data.payment' ),
$container->get( 'settings.data.settings' ),
$container->get( 'settings.data.todos' ),
$container->get( 'settings.data.definition.todos' ),
);

return new SettingsDataManager( $models );
},
'settings.ajax.switch_ui' => static function ( ContainerInterface $container ) : SwitchSettingsUiEndpoint {
return new SwitchSettingsUiEndpoint(
$container->get( 'woocommerce.logger.woocommerce' ),
Expand All @@ -270,14 +285,14 @@
$container->get( 'settings.data.general' )
);
},
'settings.service.todos_eligibilities' => static function( ContainerInterface $container ): TodosEligibilityService {
'settings.service.todos_eligibilities' => static function ( ContainerInterface $container ) : TodosEligibilityService {
$features = apply_filters(
'woocommerce_paypal_payments_rest_common_merchant_features',
array()
);

$payment_endpoint = $container->get( 'settings.rest.payment' );
$settings = $payment_endpoint->get_details()->get_data();
$settings = $payment_endpoint->get_details()->get_data();

$pay_later_endpoint = $container->get( 'settings.rest.pay_later_messaging' );
$pay_later_settings = $pay_later_endpoint->get_details()->get_data();
Expand Down Expand Up @@ -312,20 +327,20 @@
$is_pay_later_messaging_enabled_for_any_location = ! array_filter( $pay_later_statuses );

return new TodosEligibilityService(
$capabilities['acdc'] && ! $gateways['axo'], // Enable Fastlane.
$capabilities['acdc'] && ! $gateways['card-button'], // Enable Credit and Debit Cards on your checkout.
$is_pay_later_messaging_enabled_for_any_location, // Enable Pay Later messaging.
! $is_pay_later_messaging_enabled_for_any_location && ! $pay_later_statuses['product'], // Add Pay Later messaging (Product page).
! $is_pay_later_messaging_enabled_for_any_location && ! $pay_later_statuses['cart'], // Add Pay Later messaging (Cart).
! $is_pay_later_messaging_enabled_for_any_location && ! $pay_later_statuses['checkout'], // Add Pay Later messaging (Checkout).
true, // Configure a PayPal Subscription.
true, // Add PayPal buttons.
true, // Register Domain for Apple Pay.
$capabilities['acdc'] && ! $gateways['axo'], // Enable Fastlane.
$capabilities['acdc'] && ! $gateways['card-button'], // Enable Credit and Debit Cards on your checkout.
$is_pay_later_messaging_enabled_for_any_location, // Enable Pay Later messaging.
! $is_pay_later_messaging_enabled_for_any_location && ! $pay_later_statuses['product'], // Add Pay Later messaging (Product page).
! $is_pay_later_messaging_enabled_for_any_location && ! $pay_later_statuses['cart'], // Add Pay Later messaging (Cart).
! $is_pay_later_messaging_enabled_for_any_location && ! $pay_later_statuses['checkout'], // Add Pay Later messaging (Checkout).
true, // Configure a PayPal Subscription.
true, // Add PayPal buttons.
true, // Register Domain for Apple Pay.
$capabilities['acdc'] && ! ( $capabilities['apple_pay'] && $capabilities['google_pay'] ), // Add digital wallets to your account.
$capabilities['acdc'] && ! $capabilities['apple_pay'], // Add Apple Pay to your account.
$capabilities['acdc'] && ! $capabilities['google_pay'], // Add Google Pay to your account.
true, // Configure a PayPal Subscription.
$capabilities['apple_pay'] && ! $gateways['apple_pay'], // Enable Apple Pay.
$capabilities['acdc'] && ! $capabilities['apple_pay'], // Add Apple Pay to your account.
$capabilities['acdc'] && ! $capabilities['google_pay'], // Add Google Pay to your account.
true, // Configure a PayPal Subscription.
$capabilities['apple_pay'] && ! $gateways['apple_pay'], // Enable Apple Pay.
$capabilities['google_pay'] && ! $gateways['google_pay'], // Enable Google Pay.
);
},
Expand Down
7 changes: 7 additions & 0 deletions modules/ppcp-settings/src/Data/AbstractDataModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@ public function save() : void {
update_option( static::OPTION_KEY, $this->data );
}

/**
* Deletes the settings entry from the WordPress database.
*/
public function purge() : void {
delete_option( static::OPTION_KEY );
}

/**
* Gets all model data as an array.
*
Expand Down
23 changes: 21 additions & 2 deletions modules/ppcp-settings/src/Endpoint/AuthenticationRestEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use WP_REST_Response;
use WP_REST_Server;
use WooCommerce\PayPalCommerce\Settings\Service\AuthenticationManager;
use WooCommerce\PayPalCommerce\Settings\Service\SettingsDataManager;

/**
* REST controller for authenticating and connecting to a PayPal merchant account.
Expand All @@ -40,6 +41,13 @@ class AuthenticationRestEndpoint extends RestEndpoint {
*/
private AuthenticationManager $authentication_manager;

/**
* Settings data manager service.
*
* @var SettingsDataManager
*/
private SettingsDataManager $data_manager;

/**
* Defines the JSON response format (when connection was successful).
*
Expand All @@ -58,9 +66,12 @@ class AuthenticationRestEndpoint extends RestEndpoint {
* Constructor.
*
* @param AuthenticationManager $authentication_manager The authentication manager.
* @param SettingsDataManager $data_manager Settings data manager, to reset
* settings.
*/
public function __construct( AuthenticationManager $authentication_manager ) {
public function __construct( AuthenticationManager $authentication_manager, SettingsDataManager $data_manager ) {
$this->authentication_manager = $authentication_manager;
$this->data_manager = $data_manager;
}

/**
Expand Down Expand Up @@ -209,11 +220,19 @@ public function connect_oauth( WP_REST_Request $request ) : WP_REST_Response {
/**
* Disconnect the merchant and clear the authentication details.
*
* @param WP_REST_Request $request Full data about the request.
*
* @return WP_REST_Response
*/
public function disconnect() : WP_REST_Response {
public function disconnect( WP_REST_Request $request ) : WP_REST_Response {
$reset_settings = $request->get_param( 'reset' );

$this->authentication_manager->disconnect();

if ( $reset_settings ) {
$this->data_manager->reset_all_settings();
}

return $this->return_success( 'OK' );
}
}
62 changes: 62 additions & 0 deletions modules/ppcp-settings/src/Service/SettingsDataManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php
/**
* Provides functionality for general settings-data management.
*
* @package WooCommerce\PayPalCommerce\Settings\Service
*/

declare( strict_types = 1 );

namespace WooCommerce\PayPalCommerce\Settings\Service;

use WooCommerce\PayPalCommerce\Settings\Data\AbstractDataModel;

/**
* Class SettingsDataManager
*
* Manages operations related to plugin settings, primarily focusing on reset functionality.
* This service can be expanded in the future to include other settings management operations.
*/
class SettingsDataManager {

/**
* Stores a list of all AbstractDataModel instances that are managed by
* this service.
*
* @var AbstractDataModel[]
*/
private array $models = array();

/**
* Constructor.
*
* @param array $data_models List of AbstractDataModel instances.
*/
public function __construct( array $data_models ) {
foreach ( $data_models as $data_model ) {
if ( $data_model instanceof AbstractDataModel ) {
$this->models[] = $data_model;
}
}
}

/**
* Completely purges all settings from the DB.
*
* @return void
*/
public function reset_all_settings() : void {
/**
* Broadcast the settings-reset event to allow other modules to perform
* cleanup tasks, if needed.
*/
do_action( 'woocommerce_paypal_payments_reset_settings' );

foreach ( $this->models as $model ) {
$model->purge();
}

// Clear any caches.
wp_cache_flush();
}
}
2 changes: 1 addition & 1 deletion modules/ppcp-settings/src/SettingsModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ static function () use ( $container ) : void {
$endpoints = array(
'onboarding' => $container->get( 'settings.rest.onboarding' ),
'common' => $container->get( 'settings.rest.common' ),
'connect_manual' => $container->get( 'settings.rest.connect_manual' ),
'connect_manual' => $container->get( 'settings.rest.authentication' ),
'login_link' => $container->get( 'settings.rest.login_link' ),
'webhooks' => $container->get( 'settings.rest.webhooks' ),
'refresh_feature_status' => $container->get( 'settings.rest.refresh_feature_status' ),
Expand Down

0 comments on commit 6eb5e9f

Please sign in to comment.