Handle the clear db AJAX request.

This commit is contained in:
Narek Zakarian 2022-12-08 19:25:19 +04:00
parent f0798d7e88
commit 2fd1a6f574
No known key found for this signature in database
GPG key ID: 07AFD7E7A9C164A7
2 changed files with 87 additions and 25 deletions

View file

@ -44,14 +44,26 @@ return array(
);
},
'uninstall.clear-db-endpoint' => function( ContainerInterface $container ) : string {
return 'ppcp-clear-db';
},
'uninstall.clear-database-script-data' => function( ContainerInterface $container ) : array {
return array(
'clearDb' => array(
'ajaxUrl' => WC()->ajax_url(),
'nonce' => wp_create_nonce( 'ppc-uninstall-clear-database' ),
'endpoint' => \WC_AJAX::get_endpoint( $container->get( 'uninstall.clear-db-endpoint' ) ),
'nonce' => wp_create_nonce( $container->get( 'uninstall.clear-db-endpoint' ) ),
'button' => '.ppcp-clear_db_now',
'failureMessage' => __( 'Operation failed. Check WooCommerce logs for more details.', 'woocommerce-paypal-payments' ),
'ConfirmationMessage' => __( 'Operation failed. Check WooCommerce logs for more details.', 'woocommerce-paypal-payments' ),
'messageSelector' => '.clear-db-info-message',
'ConfirmationMessage' => __( 'Are you sure? the operation will remove all plugin data.', 'woocommerce-paypal-payments' ),
'successMessage' => sprintf(
'<span class="success clear-db-info-message">%1$s</span>',
esc_html__( 'The plugin data is successfully cleared.', 'woocommerce-paypal-payments' )
),
'failureMessage' => sprintf(
'<span class="error clear-db-info-message">%1$s</span>',
esc_html__( 'Operation failed. Check WooCommerce logs for more details.', 'woocommerce-paypal-payments' )
),
),
);
},
@ -63,12 +75,16 @@ return array(
);
},
'uninstall.clear-db-assets' => function( ContainerInterface $container ) : ClearDatabaseAssets {
return new ClearDatabaseAssets(
$container->get( 'uninstall.module-url' ),
$container->get( 'ppcp.asset-version' ),
'ppcp-clear-db',
$container->get( 'uninstall.clear-database-script-data' )
);
},
'uninstall.clear-db-assets' => function( ContainerInterface $container ) : ClearDatabaseAssets {
return new ClearDatabaseAssets(
$container->get( 'uninstall.module-url' ),
$container->get( 'ppcp.asset-version' ),
'ppcp-clear-db',
$container->get( 'uninstall.clear-database-script-data' )
);
},
'uninstall.clear-db' => function( ContainerInterface $container ) : ClearDatabaseInterface {
return new ClearDatabase();
},
);

View file

@ -9,6 +9,8 @@ declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\Uninstall;
use Exception;
use WooCommerce\PayPalCommerce\Button\Endpoint\RequestData;
use WooCommerce\PayPalCommerce\Uninstall\Assets\ClearDatabaseAssets;
use WooCommerce\PayPalCommerce\Vendor\Dhii\Container\ServiceProvider;
use WooCommerce\PayPalCommerce\Vendor\Dhii\Modular\Module\ModuleInterface;
@ -35,19 +37,63 @@ class UninstallModule implements ModuleInterface {
* {@inheritDoc}
*/
public function run( ContainerInterface $container ): void {
$page_id = $container->get( 'wcgateway.current-ppcp-settings-page-id' );
if ( Settings::CONNECTION_TAB_ID === $page_id ) {
$this->registerClearDatabaseAssets($container->get('uninstall.clear-db-assets'));
}
$page_id = $container->get( 'wcgateway.current-ppcp-settings-page-id' );
if ( Settings::CONNECTION_TAB_ID === $page_id ) {
$this->registerClearDatabaseAssets( $container->get( 'uninstall.clear-db-assets' ) );
$request_data = $container->get( 'button.request-data' );
$clear_db = $container->get( 'uninstall.clear-db' );
$clear_db_endpoint = $container->get( 'uninstall.clear-db-endpoint' );
$option_names = $container->get( 'uninstall.ppcp-all-option-names' );
$scheduled_action_names = $container->get( 'uninstall.ppcp-all-scheduled-action-names' );
$this->handleClearDbAjaxRequest( $request_data, $clear_db, $clear_db_endpoint, $option_names, $scheduled_action_names );
}
}
/**
* Registers the assets for clear database functionality.
*
* @param ClearDatabaseAssets $asset_loader The clear database functionality asset loader.
*/
protected function registerClearDatabaseAssets(ClearDatabaseAssets $asset_loader): void{
add_action('init', array($asset_loader, 'register'));
add_action('admin_enqueue_scripts', array($asset_loader, 'enqueue'));
}
/**
* Registers the assets for clear database functionality.
*
* @param ClearDatabaseAssets $asset_loader The clear database functionality asset loader.
*/
protected function registerClearDatabaseAssets( ClearDatabaseAssets $asset_loader ): void {
add_action( 'init', array( $asset_loader, 'register' ) );
add_action( 'admin_enqueue_scripts', array( $asset_loader, 'enqueue' ) );
}
/**
* Handles the AJAX request to clear the database.
*
* @param RequestData $request_data The request data helper.
* @param ClearDatabaseInterface $clear_db Can delete the options and clear scheduled actions from database.
* @param string $nonce The nonce.
* @param string[] $option_names The list of option names.
* @param string[] $scheduled_action_names The list of scheduled action names.
*/
protected function handleClearDbAjaxRequest(
RequestData $request_data,
ClearDatabaseInterface $clear_db,
string $nonce,
array $option_names,
array $scheduled_action_names
): void {
add_action(
"wc_ajax_{$nonce}",
static function () use ( $request_data, $clear_db, $nonce, $option_names, $scheduled_action_names ) {
try {
// Validate nonce.
$request_data->read_request( $nonce );
$clear_db->delete_options( $option_names );
$clear_db->clear_scheduled_actions( $scheduled_action_names );
wp_send_json_success();
return true;
} catch ( Exception $error ) {
wp_send_json_error( $error->getMessage(), 403 );
return false;
}
}
);
}
}