PluginProbe
PostNL for WooCommerce / 5.9.12
PostNL for WooCommerce v5.9.12
5.9.12 5.9.11 5.9.10 5.9.9 5.9.8 5.9.7 5.9.6 trunk 2.5.0 2.5.1 2.5.2 2.5.3 2.5.4 2.5.5 3.1.4 3.1.5 3.1.6 3.1.7 4.0.0 4.0.1 4.0.2 4.3.2 4.3.3 4.4.0 4.4.1 All 72 releases
woo-postnl / src / Checkout_Blocks / Blocks_Integration.php

Blocks_Integration.php in PostNL for WooCommerce 5.9.12, at src/Checkout_Blocks/Blocks_Integration.php

262 lines 7.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace PostNLWooCommerce\Checkout_Blocks;
4
5 use Automattic\WooCommerce\Blocks\Integrations\IntegrationInterface;
6 use PostNLWooCommerce\Shipping_Method\Fill_In_With_PostNL_Settings;
7 use PostNLWooCommerce\Utils;
8 use function PostNLWooCommerce\postnl;
9
10 if ( ! defined( 'ABSPATH' ) ) {
11 exit;
12 }
13
14 /**
15 * Class for integrating with WooCommerce Blocks
16 */
17 class Blocks_Integration implements IntegrationInterface {
18
19 /**
20 * Settings class instance.
21 *
22 * @var Fill_In_With_PostNL_Settings
23 */
24 protected $fill_in_with_settings;
25
26 /**
27 * The name of the integration.
28 *
29 * @return string
30 */
31 public function get_name() {
32 return 'postnl-for-woocommerce-blocks';
33 }
34
35 /**
36 * When called invokes any initialization/setup for the integration.
37 */
38 public function initialize() {
39 $this->fill_in_with_settings = new Fill_In_With_PostNL_Settings();
40 $this->register_scripts_and_styles();
41 }
42
43 /**
44 * Registers scripts and styles for both editor and frontend.
45 */
46 private function register_scripts_and_styles() {
47 // Register main integration script and style.
48 $this->register_main_integration();
49
50 // Register block editor scripts.
51 $this->register_block_script(
52 'postnl-container-editor',
53 '/build/postnl-container.js',
54 '/build/postnl-container.asset.php'
55 );
56 $this->register_block_script(
57 'postnl-fill-in-with-editor',
58 '/build/postnl-fill-in-with-editor.js',
59 '/build/postnl-fill-in-with-editor.asset.php'
60 );
61
62 // Register frontend scripts for blocks.
63 $this->register_frontend_script(
64 'postnl-container-frontend',
65 '/build/postnl-container-frontend.js',
66 '/build/postnl-container-frontend.asset.php'
67 );
68
69 $this->register_frontend_script(
70 'postnl-fill-in-with-frontend',
71 '/build/postnl-fill-in-with-frontend.js',
72 '/build/postnl-fill-in-with-frontend.asset.php'
73 );
74 }
75
76 /**
77 * Registers the main JS file required to add filters and Slot/Fills.
78 */
79 private function register_main_integration() {
80 $script_path = '/build/index.js';
81
82 $script_url = POSTNL_WC_PLUGIN_DIR_URL . $script_path;
83
84 $script_asset_path = POSTNL_WC_PLUGIN_DIR_PATH . '/build/index.asset.php';
85 $script_asset = file_exists( $script_asset_path )
86 ? require $script_asset_path
87 : array(
88 'dependencies' => array(),
89 'version' => $this->get_file_version( $script_path ),
90 );
91
92 wp_register_script(
93 'postnl-delivery-day-integration',
94 $script_url,
95 $script_asset['dependencies'],
96 $script_asset['version'],
97 true
98 );
99
100 wp_set_script_translations(
101 'postnl-delivery-day-integration',
102 'postnl-for-woocommerce',
103 POSTNL_WC_PLUGIN_DIR_PATH . '/languages'
104 );
105 }
106
107
108 /**
109 * Helper method to register block editor scripts.
110 *
111 * @param string $handle Script handle name.
112 * @param string $script_path Path to the JS file.
113 * @param string $asset_path Path to the asset file.
114 */
115 private function register_block_script( $handle, $script_path, $asset_path ) {
116 $script_url = POSTNL_WC_PLUGIN_DIR_URL . $script_path;
117 $asset_file = POSTNL_WC_PLUGIN_DIR_PATH . $asset_path;
118 $asset = file_exists( $asset_file ) ? require $asset_file : array(
119 'dependencies' => array(),
120 'version' => $this->get_file_version( $script_path ),
121 );
122
123 wp_register_script(
124 $handle,
125 $script_url,
126 $asset['dependencies'],
127 $asset['version'],
128 true
129 );
130
131 wp_set_script_translations(
132 $handle,
133 'postnl-for-woocommerce',
134 POSTNL_WC_PLUGIN_DIR_PATH . '/languages'
135 );
136 }
137
138 /**
139 * Helper method to register frontend scripts.
140 *
141 * @param string $handle Script handle name.
142 * @param string $script_path Path to the JS file.
143 * @param string $asset_path Path to the asset file.
144 */
145 private function register_frontend_script( $handle, $script_path, $asset_path ) {
146 $script_url = POSTNL_WC_PLUGIN_DIR_URL . $script_path;
147 $asset_file = POSTNL_WC_PLUGIN_DIR_PATH . $asset_path;
148 $asset = file_exists( $asset_file ) ? require $asset_file : array(
149 'dependencies' => array(),
150 'version' => $this->get_file_version( $script_path ),
151 );
152
153 wp_register_script(
154 $handle,
155 $script_url,
156 $asset['dependencies'],
157 $asset['version'],
158 true
159 );
160
161 if ( 'postnl-fill-in-with-frontend' === $handle ) {
162 $selected_location = $this->fill_in_with_settings->get_button_placement( 'checkout' );
163 if ( 'after_customer_details' === $selected_location ) {
164 $block_location = 'woocommerce/checkout-billing-address-block';
165 } else {
166 $block_location = 'woocommerce/checkout-contact-information-block';
167 }
168 wp_localize_script(
169 $handle,
170 'postnlSettings',
171 array(
172 'is_enabled_for_checkout' => $this->fill_in_with_settings->is_fill_in_with_postnl_enabled_for_checkout(),
173 'blockLocation' => $block_location,
174 'ajaxUrl' => esc_url_raw( admin_url( 'admin-ajax.php' ) ),
175 'ajaxNonce' => wp_create_nonce( 'postnl_user_info' ),
176 )
177 );
178 }
179
180 wp_set_script_translations(
181 $handle,
182 'postnl-for-woocommerce',
183 POSTNL_WC_PLUGIN_DIR_PATH . '/languages'
184 );
185 }
186
187
188
189 /**
190 * Get the file modified time as a cache buster if we're in dev mode.
191 *
192 * @param string $file Local path to the file.
193 *
194 * @return string The cache buster value to use for the given file.
195 */
196 protected function get_file_version( $file ) {
197 $file_path = POSTNL_WC_PLUGIN_DIR_PATH . $file;
198 if ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG && file_exists( $file_path ) ) {
199 return filemtime( $file_path );
200 }
201
202 return POSTNL_WC_VERSION;
203 }
204
205 /**
206 * Returns an array of script handles to enqueue in the frontend context.
207 *
208 * @return string[]
209 */
210 public function get_script_handles() {
211 return array(
212 'postnl-delivery-day-integration',
213 'postnl-container-frontend',
214 'postnl-fill-in-with-frontend',
215 );
216 }
217
218 /**
219 * Returns an array of script handles to enqueue in the editor context.
220 *
221 * @return string[]
222 */
223 public function get_editor_script_handles() {
224 return array(
225 'postnl-delivery-day-integration',
226 'postnl-container-editor',
227 'postnl-fill-in-with-editor',
228 );
229 }
230
231
232 /**
233 * An array of key, value pairs of data made available to the block on the client side.
234 *
235 * @return array
236 */
237 public function get_script_data() {
238 $letterbox = Utils::is_cart_eligible_auto_letterbox( WC()->cart );
239 $settings = postnl()->get_shipping_settings();
240
241 return array(
242 'ajax_url' => admin_url( 'admin-ajax.php' ),
243 'nonce' => wp_create_nonce( 'postnl_delivery_day_nonce' ),
244 'letterbox' => $letterbox,
245 'supported_countries' => Utils::get_available_country(),
246 'is_nl_address_enabled' => $settings->is_reorder_nl_address_enabled(),
247 'is_pickup_points_enabled' => $settings->is_pickup_points_enabled(),
248 'fill_in_with_postnl_settings' => array(
249 'rest_url' => rest_url( 'postnl/v1/get-redirect-uri' ),
250 'nonce' => wp_create_nonce( 'wp_rest' ),
251 'is_fill_in_with_postnl_enabled' => $this->fill_in_with_settings->is_fill_in_with_postnl_enabled(),
252 'postnl_logo_url' => POSTNL_WC_PLUGIN_DIR_URL . '/assets/images/postnl-logo.svg',
253 ),
254 'delivery_day_fee' => $settings->get_delivery_days_fee(),
255 'delivery_day_fee_formatted' => Utils::get_formatted_fee_total_price( $settings->get_delivery_days_fee() ),
256 'pickup_fee' => $settings->get_pickup_delivery_fee(),
257 'pickup_fee_formatted' => Utils::get_formatted_fee_total_price( $settings->get_pickup_delivery_fee() ),
258 'default_checkout_tab' => $settings->get_default_checkout_tab(),
259 );
260 }
261 }
262