woocommerce-paypal-payments/modules/ppcp-webhooks/src/WebhookModule.php

171 lines
4.2 KiB
PHP
Raw Normal View History

2020-07-06 11:04:06 +03:00
<?php
2020-08-27 13:10:16 +03:00
/**
* The webhook module.
*
2020-09-11 14:11:10 +03:00
* @package WooCommerce\PayPalCommerce\Webhooks
2020-08-27 13:10:16 +03:00
*/
2020-07-06 11:04:06 +03:00
declare(strict_types=1);
2020-09-11 14:11:10 +03:00
namespace WooCommerce\PayPalCommerce\Webhooks;
2020-07-06 11:04:06 +03:00
use Dhii\Container\ServiceProvider;
use Dhii\Modular\Module\ModuleInterface;
use Exception;
2020-07-06 11:04:06 +03:00
use Interop\Container\ServiceProviderInterface;
use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;
use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings;
2021-09-16 17:08:43 +03:00
use WooCommerce\PayPalCommerce\Webhooks\Endpoint\ResubscribeEndpoint;
2021-09-22 17:13:38 +03:00
use WooCommerce\PayPalCommerce\Webhooks\Endpoint\SimulateEndpoint;
use WooCommerce\PayPalCommerce\Webhooks\Endpoint\SimulationStateEndpoint;
2021-10-13 16:57:06 +03:00
use WooCommerce\PayPalCommerce\Webhooks\Status\Assets\WebhooksStatusPageAssets;
2020-07-06 11:04:06 +03:00
2020-08-27 13:10:16 +03:00
/**
* Class WebhookModule
*/
2020-08-27 11:08:36 +03:00
class WebhookModule implements ModuleInterface {
2020-08-27 13:10:16 +03:00
/**
2021-08-30 08:08:41 +02:00
* {@inheritDoc}
2020-08-27 13:10:16 +03:00
*/
2020-08-27 11:08:36 +03:00
public function setup(): ServiceProviderInterface {
return new ServiceProvider(
require __DIR__ . '/../services.php',
require __DIR__ . '/../extensions.php'
);
}
2020-08-27 13:10:16 +03:00
/**
2021-08-30 08:08:41 +02:00
* {@inheritDoc}
2020-08-27 13:10:16 +03:00
*/
public function run( ContainerInterface $container ): void {
$logger = $container->get( 'woocommerce.logger.woocommerce' );
assert( $logger instanceof LoggerInterface );
2020-08-27 11:08:36 +03:00
add_action(
'rest_api_init',
static function () use ( $container ) {
$endpoint = $container->get( 'webhook.endpoint.controller' );
/**
2020-08-27 13:10:16 +03:00
* The Incoming Webhook Endpoint.
*
2020-08-27 11:08:36 +03:00
* @var IncomingWebhookEndpoint $endpoint
*/
$endpoint->register();
}
);
add_action(
WebhookRegistrar::EVENT_HOOK,
static function () use ( $container ) {
$registrar = $container->get( 'webhook.registrar' );
2020-08-27 13:10:16 +03:00
/**
* The Webhook Registrar.
*
* @var WebhookRegistrar $endpoint
*/
2020-08-27 11:08:36 +03:00
$registrar->register();
}
);
add_action(
2020-10-09 19:55:28 -03:00
'woocommerce_paypal_payments_gateway_deactivate',
2020-08-27 11:08:36 +03:00
static function () use ( $container ) {
$registrar = $container->get( 'webhook.registrar' );
2020-08-27 13:10:16 +03:00
/**
* The Webhook Registrar.
*
* @var WebhookRegistrar $endpoint
*/
2020-08-27 11:08:36 +03:00
$registrar->unregister();
}
);
2021-09-16 12:44:29 +03:00
2021-09-16 17:08:43 +03:00
add_action(
'wc_ajax_' . ResubscribeEndpoint::ENDPOINT,
static function () use ( $container ) {
$endpoint = $container->get( 'webhook.endpoint.resubscribe' );
assert( $endpoint instanceof ResubscribeEndpoint );
$endpoint->handle_request();
}
);
2021-09-22 17:13:38 +03:00
add_action(
'wc_ajax_' . SimulateEndpoint::ENDPOINT,
static function () use ( $container ) {
$endpoint = $container->get( 'webhook.endpoint.simulate' );
assert( $endpoint instanceof SimulateEndpoint );
$endpoint->handle_request();
}
);
add_action(
'wc_ajax_' . SimulationStateEndpoint::ENDPOINT,
static function () use ( $container ) {
$endpoint = $container->get( 'webhook.endpoint.simulation-state' );
assert( $endpoint instanceof SimulationStateEndpoint );
$endpoint->handle_request();
}
);
2021-09-16 12:44:29 +03:00
$page_id = $container->get( 'wcgateway.current-ppcp-settings-page-id' );
if ( Settings::CONNECTION_TAB_ID === $page_id ) {
$asset_loader = $container->get( 'webhook.status.assets' );
2021-09-16 13:48:20 +03:00
assert( $asset_loader instanceof WebhooksStatusPageAssets );
add_action(
'init',
array( $asset_loader, 'register' )
);
add_action(
'admin_enqueue_scripts',
array( $asset_loader, 'enqueue' )
);
try {
$webhooks = $container->get( 'webhook.status.registered-webhooks' );
if ( empty( $webhooks ) ) {
$registrar = $container->get( 'webhook.registrar' );
assert( $registrar instanceof WebhookRegistrar );
// Looks like we cannot call rest_url too early.
add_action(
'init',
function () use ( $registrar ) {
$registrar->register();
}
);
}
} catch ( Exception $exception ) {
$logger->error( 'Failed to load webhooks list: ' . $exception->getMessage() );
}
2021-09-16 12:44:29 +03:00
}
add_action(
'woocommerce_paypal_payments_gateway_migrate',
static function () use ( $container ) {
$registrar = $container->get( 'webhook.registrar' );
assert( $registrar instanceof WebhookRegistrar );
add_action(
'init',
function () use ( $registrar ) {
$registrar->unregister();
$registrar->register();
}
);
}
);
2020-08-27 11:08:36 +03:00
}
2020-09-16 10:18:45 +03:00
/**
* Returns the key for the module.
*
* @return string|void
*/
public function getKey() {
}
2020-07-06 11:04:06 +03:00
}