File "UninstallModule.php"

Full Path: /home/siazco/grocery.siazco.se/wp-content/plugins/woocommerce-paypal-payments/modules/ppcp-uninstall/src/UninstallModule.php
File size: 3.88 KB
MIME-type: text/x-php
Charset: utf-8

<?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 {
		$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' );
		$action_names           = $container->get( 'uninstall.ppcp-all-action-names' );

		$this->handleClearDbAjaxRequest( $request_data, $clear_db, $clear_db_endpoint, $option_names, $scheduled_action_names, $action_names );

		return true;
	}

	/**
	 * 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.
	 * @param string[]               $action_names The list of action names.
	 */
	protected function handleClearDbAjaxRequest(
		RequestData $request_data,
		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 );

					wp_send_json_success();
					return true;
				} catch ( Exception $error ) {
					wp_send_json_error( $error->getMessage(), 403 );
					return false;
				}
			}
		);
	}
}