PluginProbe
seQura / 3.0.6
seQura v3.0.6
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.6, at src/Controllers/Hooks/Asset/class-assets-controller.php

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