PluginProbe
seQura / 3.2.2
seQura v3.2.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 / Core / Extension / Infrastructure / Configuration / class-configuration.php

class-configuration.php in seQura 3.2.2, at src/Core/Extension/Infrastructure/Configuration/class-configuration.php

753 lines 19.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Extends the Configuration class.
4 * Delegate to the ConfigurationManager instance to access the data in the database.
5 *
6 * @package SeQura\WC
7 */
8
9 namespace SeQura\WC\Core\Extension\Infrastructure\Configuration;
10
11 use SeQura\Core\BusinessLogic\AdminAPI\AdminAPI;
12 use SeQura\Core\BusinessLogic\Domain\OrderStatusSettings\Services\OrderStatusSettingsService;
13 use SeQura\Core\Infrastructure\Configuration\Configuration as CoreConfiguration;
14 use SeQura\Core\Infrastructure\ServiceRegister;
15 use SeQura\WC\Core\Extension\BusinessLogic\Domain\PromotionalWidgets\Models\Widget_Location;
16 use SeQura\Core\BusinessLogic\Domain\Order\Models\OrderRequest\Platform;
17 use SeQura\WC\Services\Interface_Constants;
18 use Throwable;
19 use WP_Site;
20
21 /**
22 * Extends the Configuration class. Wrapper to ease the read and write of configuration values.
23 */
24 class Configuration extends CoreConfiguration {
25
26 private const CONF_DB_VERSION = 'dbVersion';
27
28 /**
29 * Marketplace version.
30 *
31 * @var ?string
32 */
33 private $marketplace_version;
34
35 /**
36 * Retrieves the store ID.
37 */
38 public function get_store_id(): string {
39 return (string) get_current_blog_id();
40 }
41
42 /**
43 * Retrieves integration name.
44 *
45 * @return string Integration name.
46 */
47 public function getIntegrationName() {
48 return 'WooCommerce';
49 }
50
51 /**
52 * Gets the current version of the module/integration.
53 */
54 public function get_module_version(): string {
55 return strval( $this->getConfigurationManager()->getConfigValue( 'version', '' ) );
56 }
57
58 /**
59 * Gets the current version of the module/integration.
60 *
61 * @param string $version The version number.
62 */
63 public function set_module_version( $version ): void {
64 $this->getConfigurationManager()->saveConfigValue( 'version', $version );
65 }
66
67 /**
68 * Returns async process starter url, always in http.
69 *
70 * @param string $guid Process identifier.
71 *
72 * @return string Formatted URL of async process starter endpoint.
73 */
74 public function getAsyncProcessUrl( $guid ) {
75 return ''; // Not used in this implementation.
76 }
77
78 /**
79 * Check if the current page is the settings page.
80 */
81 public function is_settings_page(): bool {
82 return is_admin() && isset( $_GET['page'] ) && $this->get_page() === $_GET['page']; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
83 }
84
85 /**
86 * Get the configuration page slug.
87 */
88 public function get_page(): string {
89 return 'sequra';
90 }
91
92 /**
93 * Get the configuration page parent slug.
94 */
95 public function get_parent_page(): string {
96 return 'woocommerce';
97 }
98
99 /**
100 * Version published in the marketplace.
101 */
102 public function get_marketplace_version(): string {
103
104 if ( null !== $this->marketplace_version ) {
105 return $this->marketplace_version;
106 }
107
108 if ( ! function_exists( 'plugins_api' ) ) {
109 require_once ABSPATH . 'wp-admin/includes/plugin-install.php';
110 }
111 $response = \plugins_api(
112 'plugin_information',
113 array(
114 'slug' => 'sequra',
115 'fields' => array( 'version' => true ),
116 )
117 );
118 if ( \is_wp_error( $response ) || empty( $response->version ) ) {
119 return '';
120 }
121
122 $this->marketplace_version = $response->version;
123 return $this->marketplace_version;
124 }
125
126 /**
127 * Current store. Has keys storeId and storeName.
128 *
129 * @return array<string, mixed>
130 */
131 public function get_current_store(): array {
132 return array(
133 'storeId' => \get_current_blog_id(),
134 'storeName' => \get_bloginfo( 'name' ),
135 );
136 }
137
138 /**
139 * List of stores. Each store is an array with storeId and storeName.
140 *
141 * @return array<array<string, mixed>>
142 */
143 public function get_stores(): array {
144 $stores = array();
145 if ( function_exists( 'get_sites' ) ) {
146 /**
147 * Available sites
148 *
149 * @var WP_Site $site
150 */
151 foreach ( \get_sites() as $site ) {
152 $stores[] = array(
153 'storeId' => $site->blog_id,
154 'storeName' => $site->blogname,
155 );
156 }
157 } else {
158 $stores[] = $this->get_current_store();
159 }
160 return $stores;
161 }
162
163 /**
164 * Get password from connection settings.
165 */
166 public function get_password(): string {
167
168 try {
169 $config = AdminAPI::get()
170 ->connection( $this->get_store_id() )
171 ->getOnboardingData()
172 ->toArray();
173
174 return $config['password'] ?? '';
175 } catch ( Throwable $e ) {
176 return '';
177 }
178 }
179
180 /**
181 * Get order status mappings.
182 *
183 * @return OrderStatusMapping[]
184 */
185 public function get_order_statuses(): array {
186 try {
187 $order_status_service = ServiceRegister::getService( OrderStatusSettingsService::class );
188 return $order_status_service->getOrderStatusSettings(); // @phpstan-ignore-line
189 } catch ( \Throwable $e ) {
190 return array();
191 }
192 }
193
194 /**
195 * Get enabledForServices from general settings.
196 */
197 public function is_enabled_for_services(): bool {
198 try {
199 $config = $this->get_general_settings();
200 return ! empty( $config['enabledForServices'] );
201 } catch ( Throwable $e ) {
202 return false;
203 }
204 }
205
206 /**
207 * Check if current IP is allowed to use the payment gateway.
208 */
209 public function is_available_for_ip(): bool {
210 try {
211 $config = $this->get_general_settings();
212 // phpcs:ignore WordPressVIPMinimum.Variables.ServerVariables.UserControlledHeaders, WordPressVIPMinimum.Variables.RestrictedVariables.cache_constraints___SERVER__REMOTE_ADDR__
213 $remote_addr = isset( $_SERVER['REMOTE_ADDR'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REMOTE_ADDR'] ) ) : '';
214
215 $allowed_ip_addresses = array();
216 if ( isset( $config['allowedIPAddresses'] ) && is_array( $config['allowedIPAddresses'] ) ) {
217 foreach ( $config['allowedIPAddresses'] as $ip ) {
218 $ip = trim( (string) $ip );
219 if ( ! empty( $ip ) ) {
220 $allowed_ip_addresses[] = $ip;
221 }
222 }
223 }
224
225 return empty( $allowed_ip_addresses ) || in_array( $remote_addr, $allowed_ip_addresses, true );
226 } catch ( Throwable $e ) {
227 return true;
228 }
229 }
230 /**
231 * Get allowFirstServicePaymentDelay from general settings.
232 */
233 public function allow_first_service_payment_delay(): bool {
234 try {
235 $config = $this->get_general_settings();
236 return ! empty( $config['allowFirstServicePaymentDelay'] );
237 } catch ( Throwable $e ) {
238 return false;
239 }
240 }
241
242 /**
243 * Get if registration items are allowed
244 */
245 public function allow_service_reg_items(): bool {
246 try {
247 $config = $this->get_general_settings();
248 return ! empty( $config['allowServiceRegItems'] );
249 } catch ( Throwable $e ) {
250 return false;
251 }
252 }
253
254 /**
255 * Get defaultServicesEndDate from general settings.
256 */
257 public function get_default_services_end_date(): string {
258 try {
259 $config = $this->get_general_settings();
260 return $config['defaultServicesEndDate'] ?? 'PY1';
261 } catch ( Throwable $e ) {
262 return 'PY1';
263 }
264 }
265
266 /**
267 * Get excludedProducts from general settings.
268 *
269 * @return array<string>
270 */
271 public function get_excluded_products(): array {
272 try {
273 $config = $this->get_general_settings();
274 if ( ! empty( $config['excludedProducts'] ) && is_array( $config['excludedProducts'] ) ) {
275 return $config['excludedProducts'];
276 }
277 return array();
278 } catch ( Throwable $e ) {
279 return array();
280 }
281 }
282
283 /**
284 * Get excludedCategories from general settings.
285 *
286 * @return array<int>
287 */
288 public function get_excluded_categories(): array {
289 try {
290 $config = $this->get_general_settings();
291 if ( ! empty( $config['excludedCategories'] ) && is_array( $config['excludedCategories'] ) ) {
292 return array_map( 'absint', $config['excludedCategories'] );
293 }
294 return array();
295 } catch ( Throwable $e ) {
296 return array();
297 }
298 }
299
300 /**
301 * Get general settings as array
302 *
303 * @throws Throwable
304 *
305 * @return array<string, mixed>
306 */
307 protected function get_general_settings(): array {
308 return AdminAPI::get()
309 ->generalSettings( $this->get_store_id() )
310 ->getGeneralSettings()
311 ->toArray();
312 }
313
314 /**
315 * URL to the marketplace's plugin page.
316 */
317 public function get_marketplace_url(): string {
318 return 'https://wordpress.org/plugins/sequra/';
319 }
320
321 /**
322 * Saves dbVersion in integration database.
323 */
324 public function save_db_version( string $db_version ): void {
325 $this->saveConfigValue( self::CONF_DB_VERSION, $db_version );
326 }
327
328 /**
329 * Retrieves dbVersion from integration database.
330 */
331 public function get_db_version(): string {
332 return $this->getConfigValue( self::CONF_DB_VERSION, '' );
333 }
334
335 /**
336 * Get general settings as array
337 *
338 * @throws Throwable
339 *
340 * @return array<string, mixed>
341 */
342 protected function get_widget_settings(): array {
343 return AdminAPI::get()
344 ->widgetConfiguration( $this->get_store_id() )
345 ->getWidgetSettings()
346 ->toArray();
347 }
348
349 /**
350 * Check if the widget is enabled.
351 */
352 public function is_widget_enabled( ?string $payment_method, ?string $campaign, ?string $country ): bool {
353 try {
354 $config = $this->get_widget_settings();
355 return ! empty( $config['useWidgets'] )
356 && ! empty( $config['displayWidgetOnProductPage'] )
357 && $this->is_widget_enabled_in_custom_locations( $payment_method, $campaign, $country );
358 } catch ( Throwable $e ) {
359 return false;
360 }
361 }
362
363 /**
364 * Look for the mini widget configuration for a country
365 *
366 * @param array<string, string> $mini_widgets Mini widgets configuration
367 */
368 protected function get_mini_widget( string $country, array $mini_widgets ): ?array {
369 foreach ( $mini_widgets as $mini_widget ) {
370 if ( isset( $mini_widget['countryCode'] ) && $mini_widget['countryCode'] === $country ) {
371 return $mini_widget;
372 }
373 }
374 return null;
375 }
376
377 /**
378 * Check if the cart widget is enabled.
379 */
380 public function is_cart_widget_enabled( string $country ): bool {
381 try {
382 $config = $this->get_widget_settings();
383 $is_valid = ! empty( $config['useWidgets'] )
384 && ! empty( $config['showInstallmentAmountInCartPage'] )
385 && isset(
386 $config['selForCartPrice'],
387 $config['selForCartLocation']
388 );
389
390 if ( $is_valid && isset( $config['cartMiniWidgets'] ) ) {
391 $mini_widget = $this->get_mini_widget( $country, (array) $config['cartMiniWidgets'] );
392 $is_valid = isset( $mini_widget['message'], $mini_widget['product'] );
393 }
394 return $is_valid;
395 } catch ( Throwable $e ) { // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedCatch
396 return false;
397 }
398 }
399
400 /**
401 * Check if the product listing widget is enabled.
402 */
403 public function is_product_listing_widget_enabled( string $country ): bool {
404 try {
405 $config = $this->get_widget_settings();
406 $is_valid = ! empty( $config['useWidgets'] )
407 && ! empty( $config['showInstallmentAmountInProductListing'] )
408 && isset(
409 $config['selForListingPrice'],
410 $config['selForListingLocation']
411 );
412
413 if ( $is_valid && isset( $config['listingMiniWidgets'] ) ) {
414 $mini_widget = $this->get_mini_widget( $country, (array) $config['listingMiniWidgets'] );
415 $is_valid = isset( $mini_widget['message'], $mini_widget['product'] );
416 }
417 return $is_valid;
418 } catch ( Throwable $e ) { // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedCatch
419 return false;
420 }
421 }
422
423 /**
424 * Get the cart widget configuration for a country as an array
425 *
426 * @return null|array<string, mixed> Contains the following keys:
427 * - selForPrice: string
428 * - selForLocation: string
429 * - message: string
430 * - messageBelowLimit: string
431 * - product: string
432 * - campaign: ?string
433 */
434 public function get_cart_widget_config( string $country ): ?array {
435 try {
436 $config = $this->get_widget_settings();
437 $mini_widget = $this->get_mini_widget( $country, $config['cartMiniWidgets'] ?? array() );
438
439 return array(
440 'selForPrice' => empty( $mini_widget['selForPrice'] ) ? ( $config['selForCartPrice'] ?? '' ) : $mini_widget['selForPrice'],
441 'selForLocation' => empty( $mini_widget['selForLocation'] ) ? ( $config['selForCartLocation'] ?? '' ) : $mini_widget['selForLocation'],
442 'message' => $mini_widget['message'] ?? $this->get_mini_widget_default_message( $country ),
443 'messageBelowLimit' => $mini_widget['messageBelowLimit'] ?? $this->get_mini_widget_default_message_below_limit( $country ),
444 'product' => $mini_widget['product'] ?? 'pp3',
445 'campaign' => $mini_widget['campaign'] ?? null,
446 // 'title' => $mini_widget['title'] ?? null,
447 );
448 } catch ( Throwable $e ) {
449 return null;
450 }
451 }
452
453 /**
454 * Get the product listing widget configuration for a country as an array
455 *
456 * @return null|array<string, mixed> Contains the following keys:
457 * - selForPrice: string
458 * - selForLocation: string
459 * - message: string
460 * - messageBelowLimit: string
461 * - product: string
462 * - campaign: ?string
463 */
464 public function get_product_listing_widget_config( string $country ): ?array {
465 try {
466 $config = $this->get_widget_settings();
467 $mini_widget = $this->get_mini_widget( $country, $config['listingMiniWidgets'] ?? array() );
468
469 return array(
470 'selForPrice' => empty( $mini_widget['selForPrice'] ) ? ( $config['selForListingPrice'] ?? '' ) : $mini_widget['selForPrice'],
471 'selForLocation' => empty( $mini_widget['selForLocation'] ) ? ( $config['selForListingLocation'] ?? '' ) : $mini_widget['selForLocation'],
472 'message' => $mini_widget['message'] ?? $this->get_mini_widget_default_message( $country ),
473 'messageBelowLimit' => $mini_widget['messageBelowLimit'] ?? $this->get_mini_widget_default_message_below_limit( $country ),
474 'product' => $mini_widget['product'] ?? 'pp3',
475 'campaign' => $mini_widget['campaign'] ?? null,
476 // 'title' => $mini_widget['title'] ?? null,
477 );
478 } catch ( Throwable $e ) {
479 return null;
480 }
481 }
482
483 /**
484 * Get the mini widget message
485 */
486 public function get_mini_widget_default_message( string $country ): string {
487 return $this->get_mini_widget_default_messages()[ $country ] ?? '';
488 }
489
490 /**
491 * Get the mini widget message
492 */
493 public function get_mini_widget_default_message_below_limit( string $country ): string {
494 return $this->get_mini_widget_default_messages_below_limit()[ $country ] ?? '';
495 }
496 /**
497 * Get the mini widget message
498 */
499 public function get_mini_widget_default_messages(): array {
500 /**
501 * Filter the default message below limit for the mini widget.
502 *
503 * @since 3.0.0
504 * @return string The default message below limit for the mini widget.
505 */
506 return \apply_filters(
507 'sequra_mini_widget_default_message',
508 array(
509 'ES' => 'Desde %s/mes con seQura',
510 'FR' => 'À partir de %s/mois avec seQura',
511 'IT' => 'Da %s/mese con seQura',
512 'PT' => 'De %s/mês com seQura',
513 )
514 );
515 }
516
517 /**
518 * Get the mini widget message
519 */
520 public function get_mini_widget_default_messages_below_limit(): array {
521 /**
522 * Filter the default message below limit for the mini widget.
523 *
524 * @since 3.0.0
525 * @return string The default message below limit for the mini widget.
526 */
527 return \apply_filters(
528 'sequra_mini_widget_default_message_below_limit',
529 array(
530 'ES' => 'Fracciona con seQura a partir de %s',
531 'FR' => 'Fraction avec seQura à partir de %s',
532 'IT' => 'Frazione con seQura da %s',
533 'PT' => 'Fração com seQura a partir de %s',
534 )
535 );
536 }
537
538 /**
539 * Check if the widget is enabled in custom locations.
540 */
541 private function is_widget_enabled_in_custom_locations( ?string $payment_method, ?string $campaign, ?string $country ): bool {
542 try {
543 $custom_location = $this->get_widget_custom_location( $payment_method, $campaign, $country );
544 return $custom_location ? $custom_location->get_display_widget() : true;
545 } catch ( Throwable $e ) {
546 return false;
547 }
548 }
549
550 /**
551 * Get the widget location selector
552 */
553 public function get_widget_dest_css_sel( ?string $payment_method, ?string $campaign, ?string $country ): string {
554 try {
555 $config = $this->get_widget_settings();
556 $sel = $config['selForDefaultLocation'];
557 $custom_location = $this->get_widget_custom_location( $payment_method, $campaign, $country );
558 return $custom_location ? $custom_location->get_sel_for_target() : $sel;
559 } catch ( Throwable $e ) {
560 return '';
561 }
562 }
563
564 /**
565 * Get the widget price selector
566 */
567 public function get_widget_price_css_sel(): string {
568 try {
569 $config = $this->get_widget_settings();
570 return $config['selForPrice'] ?? '';
571 } catch ( Throwable $e ) {
572 return '';
573 }
574 }
575
576 /**
577 * Get the widget alt price selector
578 */
579 public function get_widget_alt_price_css_sel(): string {
580 try {
581 $config = $this->get_widget_settings();
582 return $config['selForAltPrice'] ?? '';
583 } catch ( Throwable $e ) {
584 return '';
585 }
586 }
587
588 /**
589 * Get the selector used to check when the alt price should be displayed
590 */
591 public function get_widget_is_alt_price_css_sel(): string {
592 try {
593 $config = $this->get_widget_settings();
594 return $config['selForAltPriceTrigger'] ?? '';
595 } catch ( Throwable $e ) {
596 return '';
597 }
598 }
599
600 /**
601 * Get the widget custom location
602 */
603 private function get_widget_custom_location( ?string $payment_method, ?string $campaign, ?string $country ): ?Widget_Location {
604 $config = $this->get_widget_settings();
605 if ( ! empty( $payment_method ) && ! empty( $country ) && isset( $config['customLocations'] ) && is_array( $config['customLocations'] ) ) {
606 foreach ( $config['customLocations'] as $location ) {
607 $loc = Widget_Location::from_array( $location );
608 if ( null !== $loc
609 && $loc->get_product() === $payment_method
610 && $loc->get_country() === $country
611 && ( $loc->get_campaign() ?? null ) === $campaign ) {
612 return $loc;
613 }
614 }
615 }
616 return null;
617 }
618
619 /**
620 * Get widget theme
621 */
622 public function get_widget_theme( ?string $payment_method, ?string $campaign, ?string $country ): string {
623 try {
624 $config = $this->get_widget_settings();
625 $style = $config['widgetConfiguration'] ?? '';
626 $custom_location = $this->get_widget_custom_location( $payment_method, $campaign, $country );
627 return $custom_location ? $custom_location->get_widget_styles() : $style;
628 } catch ( Throwable $e ) {
629 return '';
630 }
631 }
632
633 /**
634 * Get asset key
635 */
636 public function get_assets_key(): ?string {
637 try {
638 $config = $this->get_widget_settings();
639 return $config['assetsKey'] ?? null;
640 } catch ( Throwable $e ) {
641 return null;
642 }
643 }
644
645 /**
646 * Get merchant ref
647 */
648 public function get_merchant_ref( $country ): ?string {
649 try {
650 $countries_conf = AdminAPI::get()
651 ->countryConfiguration( $this->get_store_id() )
652 ->getCountryConfigurations()
653 ->toArray();
654
655 foreach ( $countries_conf as $country_conf ) {
656 if ( $country_conf['countryCode'] === $country ) {
657 return $country_conf['merchantId'] ?? null;
658 }
659 }
660 return null;
661 } catch ( Throwable $e ) {
662 return null;
663 }
664 }
665
666 /**
667 * Get connection settings as array
668 *
669 * @throws Throwable
670 */
671 private function get_connection_settings(): array {
672 return AdminAPI::get()
673 ->connection( $this->get_store_id() )
674 ->getConnectionSettings()
675 ->toArray();
676 }
677
678 /**
679 * Get the environment
680 */
681 public function get_env(): ?string {
682 $conn = null;
683 try {
684 $conn = $this->get_connection_settings();
685 } catch ( Throwable $e ) {
686 return null;
687 }
688 return $conn['environment'] ?? null;
689 }
690
691 /**
692 * Get platform payload
693 */
694 public function get_platform(): Platform {
695 /**
696 * Constants service
697 *
698 * @var Interface_Constants
699 */
700 $constants = ServiceRegister::getService( Interface_Constants::class );
701 /**
702 * WooCommerce data
703 *
704 * @var array<string, string>
705 */
706 $woo = $constants->get_woocommerce_data();
707
708 /**
709 * Environment data
710 *
711 * @var array<string, string>
712 */
713 $env = $constants->get_environment_data();
714
715 /**
716 * Plugin data
717 *
718 * @var array<string, string>
719 */
720 $sq = $constants->get_plugin_data();
721
722 /**
723 * Filter the module version to be used in the platform options.
724 *
725 * @since 3.0.0
726 */
727 $version = \apply_filters(
728 'sequra_platform_options_version',
729 $sq['Version'] ?? ''
730 );
731
732 $platform_version = ( $woo['Version'] ?? '' ) . ( isset( $env['wp_version'] ) ? " + WordPress {$env['wp_version']}" : '' );
733
734 /**
735 * Filter the platform options.
736 *
737 * @since 3.0.0
738 */
739 return \apply_filters(
740 'sequra_platform_options',
741 new Platform(
742 $this->getIntegrationName(),
743 $platform_version,
744 $env['uname'],
745 $env['db_name'],
746 $env['db_version'],
747 $version,
748 $env['php_version']
749 )
750 );
751 }
752 }
753