PluginProbe
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler / 1.5.1
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler v1.5.1
1.6.5 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 All 48 releases
fluent-cart / app / Modules / Integrations / IntegrationHelper.php

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

94 lines 3.0 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 $fields = Arr::get($settingsFields, 'fields');
30
31 $validKeys = ['enabled', 'conditional_variation_ids'];
32
33 $errors = [];
34 foreach ($fields as $field) {
35 $key = (string)Arr::get($field, 'key');
36 if (!$key) {
37 continue;
38 }
39
40 $validKeys[] = $key;
41 if (empty($field['required'])) {
42 continue;
43 }
44 if (!array_key_exists($key, $integration) || (is_array($integration[$key]) ? empty($integration[$key]) : trim((string)$integration[$key]) === '')) {
45 $label = $field['label'] ?? $key;
46 $errors[$key] = sprintf(
47 /* translators: 1: attribute name */
48 __('%s is required.', 'fluent-cart'), $label);
49 }
50 }
51
52 if ($errors && $integration['enabled']) {
53 return new \WP_Error(
54 'integration_validation_error',
55 __('Please fill up the required fields:', 'fluent-cart'),
56 $errors
57 );
58 }
59
60 $validatedData = Arr::only($integration, $validKeys);
61
62 return apply_filters('fluent_cart/integration/integration_saving_data_' . $args['provider'], $validatedData, [
63 'provider' => $args['provider'],
64 'scope' => $args['scope'],
65 'product_id' => $args['product_id'],
66 'integration_id' => $args['integration_id'],
67 'raw_data' => $integration
68 ]);
69 }
70
71 public static function formatFeedDataForEditing($meta, $args = [])
72 {
73 $data = $meta->meta_value;
74
75 $enabled = $data['enabled'];
76 $enabled = $enabled === true || $enabled === 'true';
77
78 $args['feed'] = $meta;
79
80 $data = apply_filters('fluent_cart/integration/editing_integration_' . $meta->meta_key, $data, $args);
81
82 $feedData = [
83 'id' => $meta->id,
84 'name' => Arr::get($data, 'name'),
85 'enabled' => $enabled,
86 'provider' => $meta->meta_key,
87 'feed' => $data,
88 'scope' => $args['scope'] ?? 'product',
89 ];
90
91 return $feedData;
92 }
93 }
94