Merge pull request #1076 from woocommerce/PCP-121-remove-plugin-data-after-uninstalling

Remove plugin data options.
This commit is contained in:
Emili Castells 2022-12-19 14:56:28 +01:00 committed by GitHub
commit f74adcd7b5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
20 changed files with 3365 additions and 22 deletions

View file

@ -0,0 +1,11 @@
{
"presets": [
[
"@babel/preset-env",
{
"useBuiltIns": "usage",
"corejs": "3.25.0"
}
]
]
}

2
modules/ppcp-uninstall/.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
node_modules
/assets

View file

@ -0,0 +1,17 @@
{
"name": "woocommerce/ppcp-uninstall",
"type": "dhii-mod",
"description": "Uninstall module for PPCP",
"license": "GPL-2.0",
"require": {
"php": "^7.2 | ^8.0",
"dhii/module-interface": "^0.3.0-alpha1"
},
"autoload": {
"psr-4": {
"WooCommerce\\PayPalCommerce\\Uninstall\\": "src"
}
},
"minimum-stability": "dev",
"prefer-stable": true
}

View file

@ -0,0 +1,56 @@
<?php
/**
* The uninstall module extensions.
*
* @package WooCommerce\PayPalCommerce\Uninstall
*/
declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\Uninstall;
use WooCommerce\PayPalCommerce\Onboarding\State;
use WooCommerce\PayPalCommerce\Vendor\Psr\Container\ContainerInterface;
use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings;
return array(
'wcgateway.settings.fields' => static function ( ContainerInterface $container, array $fields ): array {
$uninstall_fields = array(
'uninstall_heading' => array(
'heading' => __( 'Uninstall/Clear Database', 'woocommerce-paypal-payments' ),
'type' => 'ppcp-heading',
'screens' => array(
State::STATE_ONBOARDED,
),
'requirements' => array(),
'gateway' => Settings::CONNECTION_TAB_ID,
'description' => __( 'Manage plugin data and scheduled actions stored in database.', 'woocommerce-paypal-payments' ),
),
'uninstall_clear_db_on_uninstall' => array(
'title' => __( 'Remove PayPal Payments data from Database on uninstall', 'woocommerce-paypal-payments' ),
'type' => 'checkbox',
'label' => __( 'Remove options and scheduled actions from database when uninstalling the plugin.', 'woocommerce-paypal-payments' ),
'default' => false,
'screens' => array(
State::STATE_START,
State::STATE_ONBOARDED,
),
'requirements' => array(),
'gateway' => Settings::CONNECTION_TAB_ID,
),
'uninstall_clear_db_now' => array(
'title' => __( 'Remove PayPal Payments data from Database.', 'woocommerce-paypal-payments' ),
'type' => 'ppcp-text',
'text' => '<button type="button" class="button ppcp-clear_db_now">' . esc_html__( 'Clear now', 'woocommerce-paypal-payments' ) . '</button>',
'screens' => array(
State::STATE_ONBOARDED,
),
'requirements' => array(),
'gateway' => Settings::CONNECTION_TAB_ID,
'description' => __( 'Click to remove options and scheduled actions from database now.', 'woocommerce-paypal-payments' ),
),
);
return array_merge( $fields, $uninstall_fields );
},
);

View file

@ -0,0 +1,16 @@
<?php
/**
* The uninstall module.
*
* @package WooCommerce\PayPalCommerce\Uninstall
*/
declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\Uninstall;
use WooCommerce\PayPalCommerce\Vendor\Dhii\Modular\Module\ModuleInterface;
return function (): ModuleInterface {
return new UninstallModule();
};

View file

@ -0,0 +1,32 @@
{
"name": "ppcp-uninstall",
"version": "1.0.0",
"license": "GPL-3.0-or-later",
"main": "resources/js/ppcp-clear-db.js",
"browserslist": [
"> 0.5%",
"Safari >= 8",
"Chrome >= 41",
"Firefox >= 43",
"Edge >= 14"
],
"dependencies": {
"core-js": "^3.25.0"
},
"devDependencies": {
"@babel/core": "^7.19",
"@babel/preset-env": "^7.19",
"babel-loader": "^8.2",
"cross-env": "^7.0.3",
"file-loader": "^6.2.0",
"sass": "^1.42.1",
"sass-loader": "^12.1.0",
"webpack": "^5.74",
"webpack-cli": "^4.10"
},
"scripts": {
"build": "cross-env BABEL_ENV=default NODE_ENV=production webpack",
"watch": "cross-env BABEL_ENV=default NODE_ENV=production webpack --watch",
"dev": "cross-env BABEL_ENV=default webpack --watch"
}
}

View file

@ -0,0 +1,43 @@
document.addEventListener(
'DOMContentLoaded',
() => {
const config = PayPalCommerceGatewayClearDb;
if (!typeof (config)) {
return;
}
const clearDbConfig = config.clearDb;
document.querySelector(clearDbConfig.button)?.addEventListener('click', function () {
const isConfirmed = confirm(clearDbConfig.confirmationMessage);
if (!isConfirmed) {
return;
}
const clearButton = document.querySelector(clearDbConfig.button);
clearButton.setAttribute('disabled', 'disabled');
fetch(clearDbConfig.endpoint, {
method: 'POST',
credentials: 'same-origin',
body: JSON.stringify({
nonce: clearDbConfig.nonce,
})
}).then((res)=>{
return res.json();
}).then((data)=>{
if (!data.success) {
jQuery(clearDbConfig.failureMessage).insertAfter(clearButton);
setTimeout(()=> jQuery(clearDbConfig.messageSelector).remove(),3000);
clearButton.removeAttribute('disabled');
throw Error(data.data.message);
}
jQuery(clearDbConfig.successMessage).insertAfter(clearButton);
setTimeout(()=> jQuery(clearDbConfig.messageSelector).remove(),3000);
clearButton.removeAttribute('disabled');
window.location.replace(clearDbConfig.redirectUrl);
});
})
},
);

View file

@ -0,0 +1,96 @@
<?php
/**
* The uninstall module services.
*
* @package WooCommerce\PayPalCommerce\Uninstall
*/
declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\Uninstall;
use WooCommerce\PayPalCommerce\ApiClient\Repository\PayPalRequestIdRepository;
use WooCommerce\PayPalCommerce\Uninstall\Assets\ClearDatabaseAssets;
use WooCommerce\PayPalCommerce\Vendor\Psr\Container\ContainerInterface;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\CardButtonGateway;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\CreditCardGateway;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayPalGateway;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayUponInvoice\PayUponInvoiceGateway;
use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings;
use WooCommerce\PayPalCommerce\Webhooks\Status\WebhookSimulation;
use WooCommerce\PayPalCommerce\Webhooks\WebhookRegistrar;
return array(
'uninstall.ppcp-all-option-names' => function( ContainerInterface $container ) : array {
return array(
$container->get( 'webhook.last-webhook-storage.key' ),
PayPalRequestIdRepository::KEY,
'woocommerce_ppcp-is_pay_later_settings_migrated',
'woocommerce_' . PayPalGateway::ID . '_settings',
'woocommerce_' . CreditCardGateway::ID . '_settings',
'woocommerce_' . PayUponInvoiceGateway::ID . '_settings',
'woocommerce_' . CardButtonGateway::ID . '_settings',
Settings::KEY,
'woocommerce-ppcp-version',
WebhookSimulation::OPTION_ID,
WebhookRegistrar::KEY,
);
},
'uninstall.ppcp-all-scheduled-action-names' => function( ContainerInterface $container ) : array {
return array(
'woocommerce_paypal_payments_check_pui_payment_captured',
'woocommerce_paypal_payments_check_saved_payment',
);
},
'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(
'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',
'messageSelector' => '.clear-db-info-message',
'confirmationMessage' => __( 'Are you sure? the operation will remove all plugin data.', 'woocommerce-paypal-payments' ),
'successMessage' => sprintf(
'<div class="updated clear-db-info-message"><p><strong>%1$s</strong></p></div>',
esc_html__( 'The plugin data is successfully cleared.', 'woocommerce-paypal-payments' )
),
'failureMessage' => sprintf(
'<div class="error clear-db-info-message"><p><strong>%1$s</strong></p></div>',
esc_html__( 'Operation failed. Check WooCommerce logs for more details.', 'woocommerce-paypal-payments' )
),
'redirectUrl' => admin_url( 'admin.php?page=wc-settings&tab=checkout&section=ppcp-gateway' ),
),
);
},
'uninstall.module-url' => static function ( ContainerInterface $container ): string {
/**
* The path cannot be false.
*
* @psalm-suppress PossiblyFalseArgument
*/
return plugins_url(
'/modules/ppcp-uninstall/',
dirname( realpath( __FILE__ ), 3 ) . '/woocommerce-paypal-payments.php'
);
},
'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

@ -0,0 +1,96 @@
<?php
/**
* Register and configure assets for uninstall module.
*
* @package WooCommerce\PayPalCommerce\Uninstall\Assets
*/
declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\Uninstall\Assets;
/**
* Class ClearDatabaseAssets
*/
class ClearDatabaseAssets {
/**
* The URL to the module.
*
* @var string
*/
private $module_url;
/**
* The assets version.
*
* @var string
*/
private $version;
/**
* The script name.
*
* @var string
*/
protected $script_name;
/**
* A map of script data.
*
* @var array
*/
protected $script_data;
/**
* ClearDatabaseAssets constructor.
*
* @param string $module_url The URL to the module.
* @param string $version The assets version.
* @param string $script_name The script name.
* @param array $script_data A map of script data.
*/
public function __construct(
string $module_url,
string $version,
string $script_name,
array $script_data
) {
$this->module_url = $module_url;
$this->version = $version;
$this->script_data = $script_data;
$this->script_name = $script_name;
}
/**
* Registers the scripts and styles.
*
* @return void
*/
public function register(): void {
$module_url = untrailingslashit( $this->module_url );
wp_register_script(
$this->script_name,
"{$module_url}/assets/js/{$this->script_name}.js",
array( 'jquery' ),
$this->version,
true
);
wp_localize_script(
$this->script_name,
'PayPalCommerceGatewayClearDb',
$this->script_data
);
}
/**
* Enqueues the necessary scripts.
*
* @return void
*/
public function enqueue(): void {
wp_enqueue_script( $this->script_name );
}
}

View file

@ -0,0 +1,34 @@
<?php
/**
* Clears the plugin related data from DB.
*
* @package WooCommerce\PayPalCommerce\Uninstall
*/
declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\Uninstall;
/**
* Class ClearDatabase
*/
class ClearDatabase implements ClearDatabaseInterface {
/**
* {@inheritDoc}
*/
public function delete_options( array $option_names ):void {
foreach ( $option_names as $option_name ) {
delete_option( $option_name );
}
}
/**
* {@inheritDoc}
*/
public function clear_scheduled_actions( array $action_names ):void {
foreach ( $action_names as $action_name ) {
as_unschedule_action( $action_name );
}
}
}

View file

@ -0,0 +1,32 @@
<?php
/**
* Can delete the options and clear scheduled actions from database.
*
* @package WooCommerce\PayPalCommerce\Uninstall
*/
declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\Uninstall;
use RuntimeException;
interface ClearDatabaseInterface {
/**
* Deletes the given options from database.
*
* @param string[] $option_names The list of option names.
* @throws RuntimeException If problem deleting.
*/
public function delete_options( array $option_names ): void;
/**
* Clears the given scheduled actions.
*
* @param string[] $action_names The list of scheduled action names.
* @throws RuntimeException If problem clearing.
*/
public function clear_scheduled_actions( array $action_names ): void;
}

View file

@ -0,0 +1,98 @@
<?php
/**
* The uninstall module.
*
* @package WooCommerce\PayPalCommerce\Uninstall
*/
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;
use WooCommerce\PayPalCommerce\Vendor\Interop\Container\ServiceProviderInterface;
use WooCommerce\PayPalCommerce\Vendor\Psr\Container\ContainerInterface;
use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings;
/**
* Class UninstallModule
*/
class UninstallModule implements ModuleInterface {
/**
* {@inheritDoc}
*/
public function setup(): ServiceProviderInterface {
return new ServiceProvider(
require __DIR__ . '/../services.php',
require __DIR__ . '/../extensions.php'
);
}
/**
* {@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' ) );
}
$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' ) );
}
/**
* 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;
}
}
);
}
}

View file

@ -0,0 +1,35 @@
const path = require('path');
const isProduction = process.env.NODE_ENV === 'production';
module.exports = {
devtool: isProduction ? 'source-map' : 'eval-source-map',
mode: isProduction ? 'production' : 'development',
target: 'web',
entry: {
'ppcp-clear-db': path.resolve('./resources/js/ppcp-clear-db.js'),
},
output: {
path: path.resolve(__dirname, 'assets/'),
filename: 'js/[name].js',
},
module: {
rules: [{
test: /\.js?$/,
exclude: /node_modules/,
loader: 'babel-loader',
},
{
test: /\.scss$/,
exclude: /node_modules/,
use: [
{
loader: 'file-loader',
options: {
name: 'css/[name].css',
}
},
{loader:'sass-loader'}
]
}]
}
};

File diff suppressed because it is too large Load diff

View file

@ -23,7 +23,7 @@ class WebhookSimulation {
public const STATE_WAITING = 'waiting';
public const STATE_RECEIVED = 'received';
private const OPTION_ID = 'ppcp-webhook-simulation';
public const OPTION_ID = 'ppcp-webhook-simulation';
/**
* The webhooks endpoint.