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 / BaseIntegrationManager.php

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

181 lines 5.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\App\Helpers\Status;
6 use FluentCart\App\Models\Order;
7 use FluentCart\App\Services\ShortCodeParser\ShortcodeTemplateBuilder;
8 use FluentCart\Framework\Support\Arr;
9
10 abstract class BaseIntegrationManager
11 {
12 protected $title = '';
13
14 protected $description = '';
15
16 protected $integrationKey = '';
17
18 protected $priority = 10;
19
20 protected $runOnBackgroundForProduct = true;
21
22 protected $runOnBackgroundForGlobal = true;
23
24 public $logo = '';
25
26 public $hasGlobalMenu = false;
27
28 public $category = 'crm';
29
30 public $disableGlobalSettings = false;
31
32 public $installable = '';
33
34 public $scopes = ['global', 'product'];
35
36 public $integrationId = null;
37
38 public function __construct($title, $integrationKey, $priority = 10)
39 {
40 $this->title = $title;
41 $this->integrationKey = $integrationKey;
42 $this->priority = $priority;
43 }
44
45 final public function register()
46 {
47 add_filter('fluent_cart/integration/order_integrations', function ($addons) {
48 $addons[$this->integrationKey] = $this->getInfo();
49 return $addons;
50 }, $this->priority, 1);
51
52 $isConfigured = $this->isConfigured();
53
54 if ($isConfigured) {
55 add_filter('fluent_cart/integration/get_integration_defaults_' . $this->integrationKey, array($this, 'getIntegrationDefaults'), 10, 2);
56 add_filter('fluent_cart/integration/get_integration_settings_fields_' . $this->integrationKey, array($this, 'getSettingsFields'), 10, 2);
57 add_filter('fluent_cart/integration/integration_saving_data_' . $this->integrationKey, [$this, 'validateFeedData'], 10, 2);
58 add_action('fluent_cart/integration/run/' . $this->integrationKey, function ($eventData) {
59 $order = Arr::get($eventData, 'order', null);
60 if (!$order) {
61 return;
62 }
63 $this->processAction($order, $eventData);
64 }, $this->priority, 1);
65 }
66
67 }
68
69 public function getInfo()
70 {
71 return [
72 'priority' => $this->priority,
73 'title' => $this->title,
74 'description' => $this->description,
75 'category' => $this->category,
76 'disable_global_settings' => $this->disableGlobalSettings,
77 'config_url' => $this->disableGlobalSettings ? '' : admin_url('admin.php?page=fluent-cart#/integrations/' . $this->integrationKey),
78 'logo' => $this->logo,
79 'enabled' => $this->isConfigured(),
80 'scopes' => $this->scopes,
81 'installable' => $this->installable,
82 'delay_on_product_action' => $this->runOnBackgroundForProduct,
83 'delay_on_global_action' => $this->runOnBackgroundForGlobal,
84 ];
85 }
86
87 abstract function processAction($order, $eventData);
88
89 public function actionFields(): array
90 {
91 return Status::eventTriggers();
92 }
93
94 public function notify($feed, $order, $customer)
95 {
96 // Each integration have to implement this notify method
97 return;
98 }
99
100 abstract public function getIntegrationDefaults($settings);
101
102 abstract public function getSettingsFields($settings, $args = []);
103
104 public function validateFeedData($data, $args)
105 {
106 return $data;
107 }
108
109 public function isConfigured()
110 {
111 $globalStatus = $this->getApiSettings();
112 return $globalStatus && !!Arr::get($globalStatus, 'status');
113 }
114
115 public function getApiSettings()
116 {
117 $optionValue = fluent_cart_get_option('_integration_api_' . $this->integrationKey);
118
119 $settings = is_array($optionValue) ? $optionValue : json_decode((string)$optionValue, true);
120
121 if (!$settings || empty($settings['status'])) {
122 $settings = [
123 'apiKey' => '',
124 'status' => false
125 ];
126 }
127
128 return $settings;
129 }
130
131 protected function getSelectedTagIds($data, $inputData, $simpleKey = 'tag_ids', $routingId = 'tag_ids_selection_type', $routersKey = 'tag_routers')
132 {
133 $routing = Arr::get($data, $routingId, 'simple');
134 if (!$routing || $routing == 'simple') {
135 return Arr::get($data, $simpleKey, []);
136 }
137
138 $routers = Arr::get($data, $routersKey);
139 if (empty($routers)) {
140 return [];
141 }
142
143 return $this->evaluateRoutings($routers, $inputData);
144 }
145
146 protected function evaluateRoutings($routings, $inputData)
147 {
148 $validInputs = [];
149 foreach ($routings as $routing) {
150 $inputValue = Arr::get($routing, 'input_value');
151 if (!$inputValue) {
152 continue;
153 }
154 $condition = [
155 'conditionals' => [
156 'status' => true,
157 'is_test' => true,
158 'type' => 'any',
159 'conditions' => [
160 $routing
161 ]
162 ]
163 ];
164
165 if (\FluentCart\App\Services\ConditionAssesor::evaluate($condition, $inputData)) {
166 $validInputs[] = $inputValue;
167 }
168 }
169
170 return $validInputs;
171 }
172
173 public function parseSmartCode($text, Order $order)
174 {
175 return (string)ShortcodeTemplateBuilder::make($text, [
176 'order' => $order,
177 'customer' => $order->customer ? $order->customer : []
178 ]);
179 }
180 }
181