Add the clear database functionality assets registration

This commit is contained in:
Narek Zakarian 2022-12-08 17:23:41 +04:00
parent 75ed64646b
commit 98e1c5cb9a
No known key found for this signature in database
GPG key ID: 07AFD7E7A9C164A7
4 changed files with 143 additions and 5 deletions

View file

@ -40,7 +40,7 @@ return array(
'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-uninstall-clear_now">' . esc_html__( 'Clear now', 'woocommerce-paypal-payments' ) . '</button>',
'text' => '<button type="button" class="button ppcp-clear_db_now">' . esc_html__( 'Clear now', 'woocommerce-paypal-payments' ) . '</button>',
'screens' => array(
State::STATE_ONBOARDED,
),

View file

@ -10,6 +10,7 @@ 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;
@ -42,4 +43,32 @@ return array(
'woocommerce_paypal_payments_check_saved_payment',
);
},
'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' ),
'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' ),
),
);
},
'uninstall.module-url' => static function ( ContainerInterface $container ): string {
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( 'webhook.module-url' ),
$container->get( 'ppcp.asset-version' ),
'ppcp-clear-db',
$container->get( 'uninstall.clear-database-script-data' )
);
},
);

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}",
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

@ -9,10 +9,12 @@ declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\Uninstall;
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
@ -31,10 +33,21 @@ class UninstallModule implements ModuleInterface {
/**
* {@inheritDoc}
*
* @param ContainerInterface $c A services container instance.
*/
public function run( ContainerInterface $c ): void {
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'));
}
}
/**
* 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'));
}
}