PluginProbe
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler / 1.6.4
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler v1.6.4
1.6.4 1.6.3 1.6.2 1.6.1 1.6.0 1.5.4 1.5.5 1.5.3 1.5.2 1.5.1 1.5.0 1.4.2 1.4.1 1.4.0 1.3.28 1.3.27 1.3.26 1.3.25 1.3.23 1.3.22 1.3.21 1.3.20 1.3.19 trunk 1.2.0 All 47 releases
fluent-cart / app / Modules / Integrations / IntegrationHelper.php

IntegrationHelper.php in FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler 1.6.4, at app/Modules/Integrations/IntegrationHelper.php

105 lines 3.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentCart\App\Modules\Integrations;
4
5 use FluentCart\Framework\Support\Arr;
6
7 class IntegrationHelper
8 {
9 public static function validateAndFormatIntegrationFeedSettings(array $integration, $args = [])
10 {
11 $defaultSettings = [
12 'provider' => '',
13 'scope' => 'product',
14 'product_id' => '',
15 'integration_id' => ''
16 ];
17
18 $args = wp_parse_args($args, $defaultSettings);
19
20 if (empty($args['provider']) || empty($args['scope'])) {
21 return new \WP_Error(
22 'integration_validation_error',
23 __('Provider and scope are required for integration settings.', 'fluent-cart')
24 );
25 }
26
27 $settingsFields = apply_filters('fluent_cart/integration/get_integration_settings_fields_' . $args['provider'], [], $args);
28
29 // An unregistered provider leaves the settings-fields filter unanswered.
30 // Without this guard the foreach below iterated null (warning, silently
31 // skipped) and every submitted key passed through unvalidated — callers
32 // then persisted integration feeds keyed by arbitrary provider names.
33 if (empty($settingsFields)) {
34 return new \WP_Error(
35 'integration_validation_error',
36 __('Unknown integration provider.', 'fluent-cart')
37 );
38 }
39
40 $fields = Arr::get($settingsFields, 'fields');
41
42 $validKeys = ['enabled', 'conditional_variation_ids'];
43
44 $errors = [];
45 foreach ((array) $fields as $field) {
46 $key = (string)Arr::get($field, 'key');
47 if (!$key) {
48 continue;
49 }
50
51 $validKeys[] = $key;
52 if (empty($field['required'])) {
53 continue;
54 }
55 if (!array_key_exists($key, $integration) || (is_array($integration[$key]) ? empty($integration[$key]) : trim((string)$integration[$key]) === '')) {
56 $label = $field['label'] ?? $key;
57 $errors[$key] = sprintf(
58 /* translators: 1: attribute name */
59 __('%s is required.', 'fluent-cart'), $label);
60 }
61 }
62
63 if ($errors && $integration['enabled']) {
64 return new \WP_Error(
65 'integration_validation_error',
66 __('Please fill up the required fields:', 'fluent-cart'),
67 $errors
68 );
69 }
70
71 $validatedData = Arr::only($integration, $validKeys);
72
73 return apply_filters('fluent_cart/integration/integration_saving_data_' . $args['provider'], $validatedData, [
74 'provider' => $args['provider'],
75 'scope' => $args['scope'],
76 'product_id' => $args['product_id'],
77 'integration_id' => $args['integration_id'],
78 'raw_data' => $integration
79 ]);
80 }
81
82 public static function formatFeedDataForEditing($meta, $args = [])
83 {
84 $data = $meta->meta_value;
85
86 $enabled = $data['enabled'];
87 $enabled = $enabled === true || $enabled === 'true';
88
89 $args['feed'] = $meta;
90
91 $data = apply_filters('fluent_cart/integration/editing_integration_' . $meta->meta_key, $data, $args);
92
93 $feedData = [
94 'id' => $meta->id,
95 'name' => Arr::get($data, 'name'),
96 'enabled' => $enabled,
97 'provider' => $meta->meta_key,
98 'feed' => $data,
99 'scope' => $args['scope'] ?? 'product',
100 ];
101
102 return $feedData;
103 }
104 }
105