woocommerce-paypal-payments/modules/ppcp-button/src/class-buttonmodule.php

163 lines
3.8 KiB
PHP
Raw Normal View History

2020-04-02 08:38:00 +03:00
<?php
2020-08-31 11:12:46 +03:00
/**
* The button module.
*
2020-09-11 14:11:10 +03:00
* @package WooCommerce\PayPalCommerce\Button
2020-08-31 11:12:46 +03:00
*/
2020-04-28 12:31:12 +03:00
2020-04-02 08:38:00 +03:00
declare(strict_types=1);
2020-09-11 14:11:10 +03:00
namespace WooCommerce\PayPalCommerce\Button;
2020-04-02 08:38:00 +03:00
use Dhii\Container\ServiceProvider;
use Dhii\Modular\Module\ModuleInterface;
2020-09-11 14:11:10 +03:00
use WooCommerce\PayPalCommerce\Button\Assets\SmartButtonInterface;
use WooCommerce\PayPalCommerce\Button\Endpoint\ApproveOrderEndpoint;
use WooCommerce\PayPalCommerce\Button\Endpoint\ChangeCartEndpoint;
use WooCommerce\PayPalCommerce\Button\Endpoint\CreateOrderEndpoint;
use WooCommerce\PayPalCommerce\Button\Endpoint\DataClientIdEndpoint;
use WooCommerce\PayPalCommerce\Button\Helper\EarlyOrderHandler;
2020-04-02 08:38:00 +03:00
use Interop\Container\ServiceProviderInterface;
use Psr\Container\ContainerInterface;
2020-08-31 11:12:46 +03:00
/**
* Class ButtonModule
*/
2020-08-27 11:08:36 +03:00
class ButtonModule implements ModuleInterface {
2020-04-02 08:38:00 +03:00
2020-08-31 11:12:46 +03:00
/**
* Sets up the 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 11:08:36 +03:00
/**
2020-08-31 11:12:46 +03:00
* Runs the module.
*
2020-09-16 10:18:45 +03:00
* @param ContainerInterface|null $container The Container.
2020-08-27 11:08:36 +03:00
*/
public function run( ContainerInterface $container ): void {
2020-08-31 11:12:46 +03:00
2020-08-27 11:08:36 +03:00
add_action(
'wp',
static function () use ( $container ) {
if ( is_admin() ) {
return;
}
2020-08-31 11:12:46 +03:00
$smart_button = $container->get( 'button.smart-button' );
/**
* The Smart Button.
*
* @var SmartButtonInterface $smart_button
*/
$smart_button->render_wrapper();
2020-08-27 11:08:36 +03:00
}
);
add_action(
'wp_enqueue_scripts',
static function () use ( $container ) {
2020-04-02 08:38:00 +03:00
2020-08-31 11:12:46 +03:00
$smart_button = $container->get( 'button.smart-button' );
/**
* The Smart Button.
*
* @var SmartButtonInterface $smart_button
*/
$smart_button->enqueue();
2020-08-27 11:08:36 +03:00
}
);
2020-08-25 11:56:29 +03:00
2020-08-27 11:08:36 +03:00
add_filter(
'woocommerce_create_order',
static function ( $value ) use ( $container ) {
2020-08-31 11:12:46 +03:00
$early_order_handler = $container->get( 'button.helper.early-order-handler' );
2020-08-27 11:08:36 +03:00
if ( ! is_null( $value ) ) {
$value = (int) $value;
}
/**
2020-08-31 11:12:46 +03:00
* The Early Order Handler
*
* @var EarlyOrderHandler $early_order_handler
2020-08-27 11:08:36 +03:00
*/
2020-08-31 11:12:46 +03:00
return $early_order_handler->determine_wc_order_id( $value );
2020-08-27 11:08:36 +03:00
}
);
2020-08-25 11:56:29 +03:00
2020-08-31 11:12:46 +03:00
$this->register_ajax_endpoints( $container );
2020-08-27 11:08:36 +03:00
}
2020-08-31 11:12:46 +03:00
/**
* Registers the Ajax Endpoints.
*
* @param ContainerInterface $container The Container.
*/
private function register_ajax_endpoints( ContainerInterface $container ) {
2020-08-27 11:08:36 +03:00
add_action(
'wc_ajax_' . DataClientIdEndpoint::ENDPOINT,
static function () use ( $container ) {
$endpoint = $container->get( 'button.endpoint.data-client-id' );
/**
2020-08-31 11:12:46 +03:00
* The Data Client ID Endpoint.
*
2020-08-27 11:08:36 +03:00
* @var DataClientIdEndpoint $endpoint
*/
2020-08-31 11:12:46 +03:00
$endpoint->handle_request();
2020-08-27 11:08:36 +03:00
}
);
2020-04-02 08:38:00 +03:00
2020-08-27 11:08:36 +03:00
add_action(
'wc_ajax_' . ChangeCartEndpoint::ENDPOINT,
static function () use ( $container ) {
$endpoint = $container->get( 'button.endpoint.change-cart' );
/**
2020-08-31 11:12:46 +03:00
* The Change Cart Endpoint.
*
2020-08-27 11:08:36 +03:00
* @var ChangeCartEndpoint $endpoint
*/
2020-08-31 11:12:46 +03:00
$endpoint->handle_request();
2020-08-27 11:08:36 +03:00
}
);
2020-08-27 11:08:36 +03:00
add_action(
'wc_ajax_' . ApproveOrderEndpoint::ENDPOINT,
static function () use ( $container ) {
$endpoint = $container->get( 'button.endpoint.approve-order' );
/**
2020-08-31 11:12:46 +03:00
* The Approve Order Endpoint.
*
2020-08-27 11:08:36 +03:00
* @var ApproveOrderEndpoint $endpoint
*/
2020-08-31 11:12:46 +03:00
$endpoint->handle_request();
2020-08-27 11:08:36 +03:00
}
);
add_action(
'wc_ajax_' . CreateOrderEndpoint::ENDPOINT,
static function () use ( $container ) {
$endpoint = $container->get( 'button.endpoint.create-order' );
/**
2020-08-31 11:12:46 +03:00
* The Create Order Endpoint.
*
2020-08-27 11:08:36 +03:00
* @var CreateOrderEndpoint $endpoint
*/
2020-08-31 11:12:46 +03:00
$endpoint->handle_request();
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-04-06 11:16:18 +03:00
}