PluginProbe
seQura / 3.0.2
seQura v3.0.2
4.3.4 4.3.3 4.3.2 4.3.1 trunk 2.0.0 2.0.10 2.0.11 2.0.12 2.0.5 2.0.6 2.0.7 2.0.8 2.0.9 3.0.0 3.0.2 3.0.5 3.0.6 3.0.7 3.1.0 3.1.1 3.2.0 3.2.1 3.2.2 4.0.0 All 30 releases
sequra / src / Controllers / Hooks / Asset / class-assets-controller.php

class-assets-controller.php in seQura 3.0.2, at src/Controllers/Hooks/Asset/class-assets-controller.php

426 lines 13.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Assets Controller
4 *
5 * @package SeQura/WC
6 * @subpackage SeQura/WC/Controllers
7 */
8
9 namespace SeQura\WC\Controllers\Hooks\Asset;
10
11 use SeQura\WC\Controllers\Controller;
12 use SeQura\WC\Core\Extension\Infrastructure\Configuration\Configuration;
13 use SeQura\WC\Services\Assets\Interface_Assets;
14 use SeQura\WC\Services\I18n\Interface_I18n;
15 use SeQura\WC\Services\Interface_Logger_Service;
16 use SeQura\WC\Services\Payment\Interface_Payment_Method_Service;
17 use SeQura\WC\Services\Regex\Interface_Regex;
18
19 /**
20 * Define the assets related functionality
21 */
22 class Assets_Controller extends Controller implements Interface_Assets_Controller {
23
24 private const HANDLE_SETTINGS_PAGE = 'sequra-settings';
25 private const HANDLE_CHECKOUT = 'sequra-checkout';
26 private const HANDLE_WIDGET = 'sequra-widget';
27 private const HANDLE_CORE = 'sequra-core';
28 private const INTEGRATION_CORE_VERSION = '1.0.0';
29 private const STRATEGY_DEFER = 'defer';
30
31 /**
32 * URL to the assets directory
33 *
34 * @var string
35 */
36 private $assets_dir_url;
37
38 /**
39 * Path to the assets directory
40 *
41 * @var string
42 */
43 private $assets_dir_path;
44
45 /**
46 * Version of the assets
47 *
48 * @var string
49 */
50 private $assets_version;
51
52 /**
53 * I18n service
54 *
55 * @var Interface_I18n
56 */
57 private $i18n;
58
59 /**
60 * Settings service
61 *
62 * @var Configuration
63 */
64 private $configuration;
65
66 /**
67 * Assets service
68 *
69 * @var Interface_Assets
70 */
71 private $assets;
72
73 /**
74 * Payment method service
75 *
76 * @var Interface_Payment_Method_Service
77 */
78 private $payment_method_service;
79
80 /**
81 * Regex service
82 *
83 * @var Interface_Regex
84 */
85 private $regex;
86
87 /**
88 * Constructor
89 */
90 public function __construct(
91 string $assets_dir_url,
92 string $assets_dir_path,
93 string $assets_version,
94 Interface_I18n $i18n,
95 Interface_Logger_Service $logger,
96 string $templates_path,
97 Configuration $configuration,
98 Interface_Assets $assets,
99 Interface_Payment_Method_Service $payment_method_service,
100 Interface_Regex $regex
101 ) {
102 parent::__construct( $logger, $templates_path );
103 $this->assets_dir_url = $assets_dir_url;
104 $this->assets_dir_path = $assets_dir_path;
105 $this->assets_version = $assets_version;
106 $this->i18n = $i18n;
107 $this->logger = $logger;
108 $this->configuration = $configuration;
109 $this->assets = $assets;
110 $this->payment_method_service = $payment_method_service;
111 $this->regex = $regex;
112 }
113
114 /**
115 * Enqueue styles and scripts in WP-Admin
116 */
117 public function enqueue_admin(): void {
118 $this->logger->log_info( 'Hook executed', __FUNCTION__, __CLASS__ );
119 if ( ! $this->configuration->is_settings_page() ) {
120 return;
121 }
122 // Styles.
123 wp_enqueue_style( self::HANDLE_CORE, "{$this->assets_dir_url}/css/sequra-core.css", array(), self::INTEGRATION_CORE_VERSION );
124
125 // Scripts.
126 wp_register_script( self::HANDLE_SETTINGS_PAGE, "{$this->assets_dir_url}/js/dist/page/settings.min.js", array(), $this->assets_version, true );
127 wp_localize_script( self::HANDLE_SETTINGS_PAGE, 'SequraFE', $this->get_sequra_fe_l10n() );
128 wp_enqueue_script( self::HANDLE_SETTINGS_PAGE );
129 }
130
131 /**
132 * Get the SequraFE object
133 *
134 * @return mixed[]
135 */
136 private function get_sequra_fe_l10n(): array {
137 $connection_config = array(
138 'getConnectionDataUrl' => get_rest_url( null, 'sequra/v1/onboarding/data/{storeId}' ),
139 );
140 $payment_page_config = array_merge(
141 $connection_config,
142 array(
143 'getPaymentMethodsUrl' => get_rest_url( null, 'sequra/v1/payment/methods/{storeId}/{merchantId}' ),
144 'getAllPaymentMethodsUrl' => get_rest_url( null, 'sequra/v1/payment/methods/{storeId}' ),
145 'getSellingCountriesUrl' => get_rest_url( null, 'sequra/v1/onboarding/countries/selling/{storeId}' ),
146 'getCountrySettingsUrl' => get_rest_url( null, 'sequra/v1/onboarding/countries/{storeId}' ),
147 'validateConnectionDataUrl' => get_rest_url( null, 'sequra/v1/onboarding/data/validate/{storeId}' ),
148 )
149 );
150 $onboarding_page_config = array_merge(
151 $payment_page_config,
152 array(
153 'saveConnectionDataUrl' => get_rest_url( null, 'sequra/v1/onboarding/data/{storeId}' ),
154 'saveCountrySettingsUrl' => get_rest_url( null, 'sequra/v1/onboarding/countries/{storeId}' ),
155 'getWidgetSettingsUrl' => get_rest_url( null, 'sequra/v1/onboarding/widgets/{storeId}' ),
156 'saveWidgetSettingsUrl' => get_rest_url( null, 'sequra/v1/onboarding/widgets/{storeId}' ),
157 )
158 );
159 $page_config = array(
160 'onboarding' => $onboarding_page_config,
161 'settings' => array_merge(
162 $onboarding_page_config,
163 array(
164 'getShopPaymentMethodsUrl' => '', // Not used in this implementation.
165 'getShopCategoriesUrl' => get_rest_url( null, 'sequra/v1/settings/shop-categories/{storeId}' ),
166 'getGeneralSettingsUrl' => get_rest_url( null, 'sequra/v1/settings/general/{storeId}' ),
167 'saveGeneralSettingsUrl' => get_rest_url( null, 'sequra/v1/settings/general/{storeId}' ),
168 'getShopOrderStatusesUrl' => get_rest_url( null, 'sequra/v1/settings/order-status/list/{storeId}' ),
169 'getOrderStatusMappingSettingsUrl' => get_rest_url( null, 'sequra/v1/settings/order-status/{storeId}' ),
170 'saveOrderStatusMappingSettingsUrl' => get_rest_url( null, 'sequra/v1/settings/order-status/{storeId}' ),
171 'disconnectUrl' => get_rest_url( null, 'sequra/v1/onboarding/data/disconnect/{storeId}' ),
172 )
173 ),
174 'payment' => $payment_page_config,
175 'transactions' => array_merge(
176 $connection_config,
177 array(
178 'getTransactionLogsUrl' => get_rest_url( null, 'sequra/v1/log/{storeId}' ),
179 )
180 ),
181 'advanced' => array_merge(
182 $connection_config,
183 array(
184 'getLogsUrl' => get_rest_url( null, 'sequra/v1/log/{storeId}' ),
185 'removeLogsUrl' => get_rest_url( null, 'sequra/v1/log/{storeId}' ),
186 'getLogsSettingsUrl' => get_rest_url( null, 'sequra/v1/log/settings/{storeId}' ),
187 'saveLogsSettingsUrl' => get_rest_url( null, 'sequra/v1/log/settings/{storeId}' ),
188 )
189 ),
190 );
191
192 $state_controller = array(
193 'storesUrl' => get_rest_url( null, 'sequra/v1/settings/stores/{storeId}' ),
194 'currentStoreUrl' => get_rest_url( null, 'sequra/v1/settings/current-store' ),
195 'stateUrl' => get_rest_url( null, 'sequra/v1/settings/state/{storeId}' ),
196 'versionUrl' => get_rest_url( null, 'sequra/v1/settings/version/{storeId}' ),
197 'shopNameUrl' => get_rest_url( null, 'sequra/v1/settings/shop-name/{storeId}' ),
198 'pageConfiguration' => $page_config,
199 );
200
201 $pages_payment = array( 'methods' );
202 $sequra_fe = array(
203 'translations' => array(
204 'default' => $this->load_translation(),
205 'current' => $this->load_translation( $this->i18n->get_lang() ),
206 ),
207 'pages' => array(
208 'onboarding' => array( 'connect', 'countries', 'widgets' ),
209 'settings' => array( 'general', 'connection', 'order_status', 'widget' ),
210 'payment' => $pages_payment,
211 'advanced' => array( 'debug' ),
212 ),
213 'integration' => array(
214 'authToken' => '', // Not used in this implementation.
215 'isMultistore' => count( $this->configuration->get_stores() ) > 1,
216 'hasVersion' => version_compare( $this->configuration->get_marketplace_version(), $this->configuration->get_module_version(), '>' ),
217 ),
218 'generalSettings' => array(
219 'useHostedPage' => false,
220 'useReplacementPaymentMethod' => false,
221 'useAllowedIPAddresses' => true,
222 'useOrderReporting' => false,
223 ),
224 'isPromotional' => false,
225 '_state_controller' => $state_controller,
226 'customHeader' => array( 'X-WP-Nonce' => wp_create_nonce( 'wp_rest' ) ),
227
228 'regex' => array(
229 'ip' => $this->regex->ip( false ),
230 'dateOrDuration' => $this->regex->date_or_duration( false ),
231 ),
232 'miniWidgetLabels' => array(
233 'messages' => $this->configuration->get_mini_widget_default_messages(),
234 'messagesBelowLimit' => $this->configuration->get_mini_widget_default_messages_below_limit(),
235 ),
236 );
237
238 return $sequra_fe;
239 }
240
241 /**
242 * Get the SequraWidgetFacade object
243 *
244 * @return array<string, mixed>
245 */
246 private function get_sequra_widget_facade_l10n(): array {
247 $merchant = $this->configuration->get_merchant_ref( $this->i18n->get_current_country() );
248 $methods = $this->payment_method_service->get_all_widget_compatible_payment_methods( $this->configuration->get_store_id(), $merchant );
249 return array(
250 'scriptUri' => $this->assets->get_cdn_resource_uri( $this->configuration->get_env(), 'sequra-checkout.min.js' ),
251 'thousandSeparator' => wc_get_price_thousand_separator(),
252 'decimalSeparator' => wc_get_price_decimal_separator(),
253 'locale' => $this->i18n->get_locale(),
254 'merchant' => $merchant,
255 'assetKey' => $this->configuration->get_assets_key(),
256 'products' => array_column( $methods, 'product' ),
257 'widgets' => array(),
258 'miniWidgets' => array(),
259 );
260 }
261
262 /**
263 * Get the script arguments to enqueue based on the WP version
264 *
265 * @return array|bool
266 */
267 private function get_script_args( string $strategy, bool $in_footer ) {
268 $wp_version = get_bloginfo( 'version' );
269 if ( version_compare( $wp_version, '6.3', '>=' ) ) {
270 return array(
271 'strategy' => $strategy,
272 'in_footer' => $in_footer,
273 );
274 }
275 return $in_footer;
276 }
277
278 /**
279 * Enqueue styles and scripts in Front-End for the checkout page
280 */
281 private function enqueue_front_checkout(): void {
282 wp_enqueue_style( self::HANDLE_CHECKOUT, "{$this->assets_dir_url}/css/checkout.css", array(), $this->assets_version );
283 wp_register_script(
284 self::HANDLE_CHECKOUT,
285 "{$this->assets_dir_url}/js/dist/page/checkout.min.js",
286 array(),
287 $this->assets_version,
288 $this->get_script_args( self::STRATEGY_DEFER, true )
289 );
290
291 /**
292 * Check if the checkout is a Gutenberg block based version
293 *
294 * @since 3.0.0
295 * TODO: Document the hook
296 */
297 $is_block = apply_filters(
298 'sequra_is_block_checkout',
299 ! $this->is_order_pay_page() // TODO: Remove this condition once the block checkout is implemented in the order pay page.
300 && class_exists( 'Automattic\WooCommerce\Blocks\Utils\CartCheckoutUtils' )
301 && method_exists( 'Automattic\WooCommerce\Blocks\Utils\CartCheckoutUtils', 'is_checkout_block_default' )
302 && \Automattic\WooCommerce\Blocks\Utils\CartCheckoutUtils::is_checkout_block_default()
303 );
304
305 wp_localize_script(
306 self::HANDLE_CHECKOUT,
307 'SeQuraCheckout',
308 array(
309 'isBlockCheckout' => $is_block,
310 )
311 );
312 wp_enqueue_script( self::HANDLE_CHECKOUT );
313 }
314
315 /**
316 * Enqueue styles and scripts in Front-End for the widgets
317 */
318 private function enqueue_front_widgets(): void {
319
320 wp_enqueue_style( self::HANDLE_WIDGET, "{$this->assets_dir_url}/css/widget.css", array(), $this->assets_version );
321 wp_register_script(
322 self::HANDLE_WIDGET,
323 "{$this->assets_dir_url}/js/dist/page/widget-facade.min.js",
324 array(),
325 $this->assets_version,
326 $this->get_script_args( self::STRATEGY_DEFER, false )
327 );
328 wp_localize_script( self::HANDLE_WIDGET, 'SequraWidgetFacade', $this->get_sequra_widget_facade_l10n() );
329 wp_enqueue_script( self::HANDLE_WIDGET );
330 }
331
332 /**
333 * Check if the current post has the widget shortcode in its content
334 */
335 private function has_widget_shortcode(): bool {
336 if ( is_checkout() || is_cart() || ! is_singular() ) {
337 return false;
338 }
339 global $post;
340 return has_shortcode( $post->post_content, 'sequra_widget' );
341 }
342
343 /**
344 * Check if the current post has the cart widget shortcode in its content
345 */
346 private function has_cart_widget_shortcode(): bool {
347 if ( is_checkout() || is_product() || ! is_page() ) {
348 return false;
349 }
350 global $post;
351 return has_shortcode( $post->post_content, 'sequra_cart_widget' );
352 }
353
354 /**
355 * Check if the current post has the cart widget shortcode in its content
356 */
357 private function has_product_listing_widget_shortcode(): bool {
358 if ( is_checkout() || is_product() || is_cart() || ! is_page() ) {
359 return false;
360 }
361 global $post;
362 return has_shortcode( $post->post_content, 'sequra_product_listing_widget' );
363 }
364
365 /**
366 * Check if the current page is the order pay page
367 */
368 private function is_order_pay_page(): bool {
369 return ( (int) strval( get_query_var( 'order-pay' ) ) ) > 0;
370 }
371
372 /**
373 * Enqueue styles and scripts in Front-End
374 */
375 public function enqueue_front(): void {
376 $this->logger->log_info( 'Hook executed', __FUNCTION__, __CLASS__ );
377 if ( is_checkout() || $this->is_order_pay_page() ) {
378 $this->enqueue_front_checkout();
379 }
380
381 if ( is_product()
382 || $this->has_widget_shortcode()
383 || is_cart()
384 || $this->has_cart_widget_shortcode()
385 || is_product_category()
386 || is_product_tag()
387 || is_shop()
388 || $this->has_product_listing_widget_shortcode() ) {
389 $this->enqueue_front_widgets();
390 }
391 }
392
393 /**
394 * Load translations from the .json file
395 *
396 * @param string $lang Language code.
397 * @return mixed[]
398 */
399 private function load_translation( $lang = 'en' ): array {
400 $path = "{$this->assets_dir_path}/lang/{$lang}.json";
401 $translations = array();
402
403 global $wp_filesystem;
404 require_once ABSPATH . '/wp-admin/includes/file.php';
405 if ( ! WP_Filesystem() ) {
406 return $translations;
407 }
408
409 if ( ! $wp_filesystem->exists( $path ) ) {
410 return $translations;
411 }
412
413 $content = $wp_filesystem->get_contents( $path );
414
415 if ( empty( $content ) ) {
416 return $translations;
417 }
418
419 $content = json_decode( $content, true );
420 if ( is_array( $content ) ) {
421 $translations = $content;
422 }
423 return $translations;
424 }
425 }
426