woocommerce-paypal-payments/modules/ppcp-webhooks/src/class-webhookmodule.php

140 lines
3.4 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;
2021-09-16 13:48:20 +03:00
use WooCommerce\PayPalCommerce\WcGateway\Assets\WebhooksStatusPageAssets;
2021-09-16 17:08:43 +03:00
use WooCommerce\PayPalCommerce\Webhooks\Endpoint\ResubscribeEndpoint;
2021-09-16 13:48:20 +03:00
use WooCommerce\PayPalCommerce\Webhooks\Status\WebhooksStatusPage;
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
/**
* Setup the Webhook module.
*
* @return ServiceProviderInterface
*/
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
/**
* Run the Webhook module.
*
2020-09-16 10:18:45 +03:00
* @param ContainerInterface|null $container The Container.
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-16 12:44:29 +03:00
$page_id = $container->get( 'wcgateway.current-ppcp-settings-page-id' );
if ( WebhooksStatusPage::ID === $page_id ) {
$GLOBALS['hide_save_button'] = true;
$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
}
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
}