Endpoint
1 year ago
AdvancedCardPaymentMethod.php
7 months ago
BlocksModule.php
1 year ago
PayPalPaymentMethod.php
9 months ago
BlocksModule.php
97 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * The blocks module. |
| 5 | * |
| 6 | * @package WooCommerce\PayPalCommerce\Blocks |
| 7 | */ |
| 8 | declare (strict_types=1); |
| 9 | namespace WooCommerce\PayPalCommerce\Blocks; |
| 10 | |
| 11 | use Automattic\WooCommerce\Blocks\Payments\PaymentMethodRegistry; |
| 12 | use WooCommerce\PayPalCommerce\Blocks\Endpoint\UpdateShippingEndpoint; |
| 13 | use WooCommerce\PayPalCommerce\Button\Assets\SmartButtonInterface; |
| 14 | use WooCommerce\PayPalCommerce\Vendor\Inpsyde\Modularity\Module\ExecutableModule; |
| 15 | use WooCommerce\PayPalCommerce\Vendor\Inpsyde\Modularity\Module\ExtendingModule; |
| 16 | use WooCommerce\PayPalCommerce\Vendor\Inpsyde\Modularity\Module\ModuleClassNameIdTrait; |
| 17 | use WooCommerce\PayPalCommerce\Vendor\Inpsyde\Modularity\Module\ServiceModule; |
| 18 | use WooCommerce\PayPalCommerce\Vendor\Psr\Container\ContainerInterface; |
| 19 | use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings; |
| 20 | use WooCommerce\PayPalCommerce\WcSubscriptions\Helper\SubscriptionHelper; |
| 21 | /** |
| 22 | * Class BlocksModule |
| 23 | */ |
| 24 | class BlocksModule implements ServiceModule, ExtendingModule, ExecutableModule |
| 25 | { |
| 26 | use ModuleClassNameIdTrait; |
| 27 | /** |
| 28 | * {@inheritDoc} |
| 29 | */ |
| 30 | public function services(): array |
| 31 | { |
| 32 | return require __DIR__ . '/../services.php'; |
| 33 | } |
| 34 | /** |
| 35 | * {@inheritDoc} |
| 36 | */ |
| 37 | public function extensions(): array |
| 38 | { |
| 39 | return require __DIR__ . '/../extensions.php'; |
| 40 | } |
| 41 | /** |
| 42 | * {@inheritDoc} |
| 43 | */ |
| 44 | public function run(ContainerInterface $c): bool |
| 45 | { |
| 46 | if (!class_exists('Automattic\WooCommerce\Blocks\Payments\Integrations\AbstractPaymentMethodType') || !function_exists('woocommerce_store_api_register_payment_requirements')) { |
| 47 | add_action('admin_notices', function () { |
| 48 | printf('<div class="notice notice-error"><p>%1$s</p></div>', wp_kses_post(__('PayPal checkout block initialization failed, possibly old WooCommerce version or disabled WooCommerce Blocks plugin.', 'woocommerce-paypal-payments'))); |
| 49 | }); |
| 50 | return \true; |
| 51 | } |
| 52 | add_action('woocommerce_blocks_payment_method_type_registration', function (PaymentMethodRegistry $payment_method_registry) use ($c): void { |
| 53 | $payment_method_registry->register($c->get('blocks.method')); |
| 54 | $settings = $c->get('wcgateway.settings'); |
| 55 | assert($settings instanceof Settings); |
| 56 | $payment_method_registry->register($c->get('blocks.advanced-card-method')); |
| 57 | }); |
| 58 | woocommerce_store_api_register_payment_requirements(array('data_callback' => function () use ($c): array { |
| 59 | $smart_button = $c->get('button.smart-button'); |
| 60 | assert($smart_button instanceof SmartButtonInterface); |
| 61 | if (isset($smart_button->script_data()['continuation'])) { |
| 62 | return array('ppcp_continuation'); |
| 63 | } |
| 64 | return array(); |
| 65 | })); |
| 66 | add_action('wc_ajax_' . UpdateShippingEndpoint::ENDPOINT, static function () use ($c) { |
| 67 | $endpoint = $c->get('blocks.endpoint.update-shipping'); |
| 68 | assert($endpoint instanceof UpdateShippingEndpoint); |
| 69 | $endpoint->handle_request(); |
| 70 | }); |
| 71 | // Enqueue frontend scripts. |
| 72 | add_action('wp_enqueue_scripts', static function () use ($c) { |
| 73 | if (!has_block('woocommerce/checkout') && !has_block('woocommerce/cart')) { |
| 74 | return; |
| 75 | } |
| 76 | $module_url = $c->get('blocks.url'); |
| 77 | $asset_version = $c->get('ppcp.asset-version'); |
| 78 | wp_register_style('wc-ppcp-blocks', untrailingslashit($module_url) . '/assets/css/gateway.css', array(), $asset_version); |
| 79 | wp_enqueue_style('wc-ppcp-blocks'); |
| 80 | }); |
| 81 | // Enqueue editor styles. |
| 82 | add_action('enqueue_block_editor_assets', static function () use ($c) { |
| 83 | $module_url = $c->get('blocks.url'); |
| 84 | $asset_version = $c->get('ppcp.asset-version'); |
| 85 | wp_register_style('wc-ppcp-blocks-editor', untrailingslashit($module_url) . '/assets/css/gateway-editor.css', array(), $asset_version); |
| 86 | wp_enqueue_style('wc-ppcp-blocks-editor'); |
| 87 | }); |
| 88 | add_filter('woocommerce_paypal_payments_sdk_components_hook', function (array $components, string $context) { |
| 89 | if (str_ends_with($context, '-block')) { |
| 90 | $components[] = 'buttons'; |
| 91 | } |
| 92 | return $components; |
| 93 | }, 10, 2); |
| 94 | return \true; |
| 95 | } |
| 96 | } |
| 97 |