PluginProbe
MultiSafepay plugin for WooCommerce / trunk
MultiSafepay plugin for WooCommerce vtrunk
6.11.1 6.12.0 6.13.0 6.2.0 6.2.1 6.3.0 6.3.1 6.4.0 6.4.1 6.4.2 6.4.3 6.5.0 6.5.1 6.6.0 6.6.1 6.6.2 6.7.0 6.7.1 6.7.2 6.7.3 6.8.0 6.8.1 6.8.2 6.8.3 6.9.0 All 84 releases
multisafepay / src / Blocks / BlocksController.php

BlocksController.php in MultiSafepay plugin for WooCommerce trunk, at src/Blocks/BlocksController.php

141 lines 4.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php declare(strict_types=1);
2
3 namespace MultiSafepay\WooCommerce\Blocks;
4
5 use Automattic\WooCommerce\StoreApi\Payments\PaymentContext;
6 use Automattic\WooCommerce\StoreApi\Payments\PaymentResult;
7 use Automattic\WooCommerce\Blocks\Payments\PaymentMethodRegistry;
8 use MultiSafepay\Exception\InvalidDataInitializationException;
9 use MultiSafepay\WooCommerce\PaymentMethods\Base\BasePaymentMethodBlocks;
10 use MultiSafepay\WooCommerce\Services\BlocksPaymentDataService;
11 use MultiSafepay\WooCommerce\Utils\Logger;
12 use WP_Post;
13
14 /**
15 * Defines all the methods needed to register the MultiSafepay payment methods in WooCommerce checkout block.
16 */
17 class BlocksController {
18
19 /**
20 * Map MultiSafepay Blocks payment component data into order meta.
21 *
22 * @param PaymentContext $context Holds context for the payment.
23 * @param PaymentResult $result Result of the payment.
24 * @return void
25 */
26 public function map_blocks_payment_data_to_order_meta( PaymentContext $context, PaymentResult $result ): void {
27 ( new BlocksPaymentDataService() )->save_blocks_payment_data_to_order_meta( $context, $result );
28 }
29
30 /**
31 * Add MultiSafepay payment method script dependency for blocks.
32 *
33 * @param array $dependencies Script dependencies.
34 * @param string $handle Script handle.
35 *
36 * @return array
37 */
38 public function add_multisafepay_block_dependencies( array $dependencies, string $handle ): array {
39 $supported_handles = array( 'wc-checkout-block', 'wc-checkout-block-frontend', 'wc-cart-block', 'wc-cart-block-frontend' );
40
41 if ( ! in_array( $handle, $supported_handles, true ) ) {
42 return $dependencies;
43 }
44
45 static $script_handles = null;
46
47 if ( null === $script_handles ) {
48 try {
49 $payment_method_blocks = new BasePaymentMethodBlocks();
50 $script_handles = is_admin()
51 ? $payment_method_blocks->get_payment_method_script_handles_for_admin()
52 : $payment_method_blocks->get_payment_method_script_handles();
53 } catch ( InvalidDataInitializationException $invalid_data_initialization_exception ) {
54 ( new Logger() )->log_warning(
55 'Failed to load MultiSafepay Blocks script handles: ' . $invalid_data_initialization_exception->getMessage()
56 );
57
58 return $dependencies;
59 }
60 }
61 $dependencies = array_merge( $dependencies, $script_handles );
62
63 return array_values( array_unique( $dependencies ) );
64 }
65
66 /**
67 * Enqueue MultiSafepay assets in the Checkout block editor.
68 *
69 * @return void
70 * @throws InvalidDataInitializationException
71 */
72 public function enqueue_checkout_block_editor_assets(): void {
73 if ( ! is_admin() || ! function_exists( 'get_current_screen' ) ) {
74 return;
75 }
76
77 $screen = get_current_screen();
78
79 if ( ! $screen || ! $screen->is_block_editor() ) {
80 return;
81 }
82
83 global $post;
84 $post_id = ( $post instanceof WP_Post ) ? (int) $post->ID : 0;
85 $checkout_id = (int) get_option( 'woocommerce_checkout_page_id', 0 );
86
87 if ( ! $post_id || $post_id !== $checkout_id ) {
88 return;
89 }
90
91 try {
92 $payment_method_blocks = new BasePaymentMethodBlocks();
93 $handles = $payment_method_blocks->get_payment_method_script_handles_for_admin();
94
95 foreach ( $handles as $handle ) {
96 wp_enqueue_script( $handle );
97 }
98 } catch ( InvalidDataInitializationException $invalid_data_initialization_exception ) {
99 ( new Logger() )->log_warning(
100 'Failed to enqueue MultiSafepay Blocks assets in editor: ' . $invalid_data_initialization_exception->getMessage()
101 );
102 }
103 }
104
105 /**
106 * Register the MultiSafepay payment methods in WooCommerce Blocks.
107 *
108 * @return void
109 */
110 public function register_multisafepay_payment_methods_blocks(): void {
111 static $registered = false;
112
113 if ( $registered ) {
114 return;
115 }
116
117 if ( class_exists( PaymentMethodRegistry::class ) ) {
118 add_action(
119 'woocommerce_blocks_payment_method_type_registration',
120 function ( PaymentMethodRegistry $payment_method_registry ) {
121 $payment_method_registry->register( new BasePaymentMethodBlocks() );
122 }
123 );
124
125 add_filter(
126 'woocommerce_blocks_register_script_dependencies',
127 array( $this, 'add_multisafepay_block_dependencies' ),
128 10,
129 2
130 );
131
132 add_action(
133 'enqueue_block_editor_assets',
134 array( $this, 'enqueue_checkout_block_editor_assets' )
135 );
136
137 $registered = true;
138 }
139 }
140 }
141