Initial commit.
This commit is contained in:
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@@ -0,0 +1,17 @@
|
||||
<?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 (array $fields, ContainerInterface $container): 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);
|
||||
});
|
||||
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* The uninstall module.
|
||||
*
|
||||
* @package WooCommerce\PayPalCommerce\Uninstall
|
||||
*/
|
||||
declare (strict_types=1);
|
||||
namespace WooCommerce\PayPalCommerce\Uninstall;
|
||||
|
||||
return function (): \WooCommerce\PayPalCommerce\Uninstall\UninstallModule {
|
||||
return new \WooCommerce\PayPalCommerce\Uninstall\UninstallModule();
|
||||
};
|
||||
@@ -0,0 +1,39 @@
|
||||
<?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\Settings\Ajax\SwitchSettingsUiEndpoint;
|
||||
use WooCommerce\PayPalCommerce\Settings\Service\Migration\PaymentSettingsMigration;
|
||||
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'), '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, 'ppcp_payment_tokens_migration_initialized', SwitchSettingsUiEndpoint::OPTION_NAME_SHOULD_USE_OLD_UI, SwitchSettingsUiEndpoint::OPTION_NAME_MIGRATION_IS_DONE, PaymentSettingsMigration::OPTION_NAME_BCDC_MIGRATION_OVERRIDE);
|
||||
}, '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', 'woocommerce_paypal_payments_payment_tokens_migration');
|
||||
}, 'uninstall.ppcp-all-action-names' => function (ContainerInterface $container): array {
|
||||
return array('woocommerce_paypal_payments_uninstall');
|
||||
}, '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§ion=ppcp-gateway')));
|
||||
}, 'uninstall.module-url' => static function (ContainerInterface $container): string {
|
||||
return plugins_url('/modules/ppcp-uninstall/', $container->get('ppcp.path-to-plugin-main-file'));
|
||||
}, '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): \WooCommerce\PayPalCommerce\Uninstall\ClearDatabaseInterface {
|
||||
return new \WooCommerce\PayPalCommerce\Uninstall\ClearDatabase();
|
||||
});
|
||||
@@ -0,0 +1,75 @@
|
||||
<?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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
<?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 \WooCommerce\PayPalCommerce\Uninstall\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);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function clear_actions(array $action_names): void
|
||||
{
|
||||
foreach ($action_names as $action_name) {
|
||||
do_action($action_name);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
<?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;
|
||||
/**
|
||||
* Clears the given actions.
|
||||
*
|
||||
* @param string[] $action_names The list of action names.
|
||||
* @throws RuntimeException If problem clearing.
|
||||
*/
|
||||
public function clear_actions(array $action_names): void;
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
<?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\Inpsyde\Modularity\Module\ExecutableModule;
|
||||
use WooCommerce\PayPalCommerce\Vendor\Inpsyde\Modularity\Module\ExtendingModule;
|
||||
use WooCommerce\PayPalCommerce\Vendor\Inpsyde\Modularity\Module\ModuleClassNameIdTrait;
|
||||
use WooCommerce\PayPalCommerce\Vendor\Inpsyde\Modularity\Module\ServiceModule;
|
||||
use WooCommerce\PayPalCommerce\Vendor\Psr\Container\ContainerInterface;
|
||||
use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings;
|
||||
/**
|
||||
* Class UninstallModule
|
||||
*/
|
||||
class UninstallModule implements ServiceModule, ExtendingModule, ExecutableModule
|
||||
{
|
||||
use ModuleClassNameIdTrait;
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function services(): array
|
||||
{
|
||||
return require __DIR__ . '/../services.php';
|
||||
}
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function extensions(): array
|
||||
{
|
||||
return require __DIR__ . '/../extensions.php';
|
||||
}
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function run(ContainerInterface $container): bool
|
||||
{
|
||||
add_action('init', static function () use ($container) {
|
||||
$page_id = $container->get('wcgateway.current-ppcp-settings-page-id');
|
||||
if (Settings::CONNECTION_TAB_ID === $page_id) {
|
||||
$container->get('uninstall.clear-db-assets')->register();
|
||||
add_action('admin_enqueue_scripts', array($container->get('uninstall.clear-db-assets'), 'enqueue'));
|
||||
}
|
||||
$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');
|
||||
$action_names = $container->get('uninstall.ppcp-all-action-names');
|
||||
self::handleClearDbAjaxRequest($request_data, $clear_db, $clear_db_endpoint, $option_names, $scheduled_action_names, $action_names);
|
||||
});
|
||||
return \true;
|
||||
}
|
||||
/**
|
||||
* 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.
|
||||
* @param string[] $action_names The list of action names.
|
||||
*/
|
||||
protected static function handleClearDbAjaxRequest(RequestData $request_data, \WooCommerce\PayPalCommerce\Uninstall\ClearDatabaseInterface $clear_db, string $nonce, array $option_names, array $scheduled_action_names, array $action_names): void
|
||||
{
|
||||
add_action("wc_ajax_{$nonce}", static function () use ($request_data, $clear_db, $nonce, $option_names, $scheduled_action_names, $action_names) {
|
||||
try {
|
||||
if (!current_user_can('manage_woocommerce')) {
|
||||
wp_send_json_error('Not admin.', 403);
|
||||
return \false;
|
||||
}
|
||||
// Validate nonce.
|
||||
$request_data->read_request($nonce);
|
||||
$clear_db->delete_options($option_names);
|
||||
$clear_db->clear_scheduled_actions($scheduled_action_names);
|
||||
$clear_db->clear_actions($action_names);
|
||||
update_option('woocommerce-ppcp-is-new-merchant', '1');
|
||||
wp_send_json_success();
|
||||
return \true;
|
||||
} catch (Exception $error) {
|
||||
wp_send_json_error($error->getMessage(), 403);
|
||||
return \false;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user