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 / Http / Controllers / IntegrationController.php

IntegrationController.php in FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler 1.6.4, at app/Http/Controllers/IntegrationController.php

315 lines 10.1 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\Http\Controllers;
4
5 use FluentCart\App\Models\Meta;
6 use FluentCart\App\Modules\Integrations\AddOnModule;
7 use FluentCart\App\Modules\Integrations\GlobalIntegrationSettings;
8 use FluentCart\App\Modules\Integrations\IntegrationHelper;
9 use FluentCart\Framework\Http\Request\Request;
10 use FluentCart\Framework\Support\Arr;
11
12 class IntegrationController extends Controller
13 {
14 public function index()
15 {
16 return (new AddOnModule())->updateAddOnsStatus($this->request->all());
17 }
18
19
20 public function updateStatus()
21 {
22 return (new GlobalIntegrationSettings())->updateNotificationStatus($this->request->all());
23 }
24
25 public function saveSettings(Request $request)
26 {
27 $requestData = $request->all();
28 $integrationId = Arr::get($requestData, 'integration_id');
29 $integrationMeta = null;
30 $provider = Arr::get($requestData, 'integration_name');
31
32 if ($integrationId) {
33 $integrationMeta = Meta::where('id', $integrationId)
34 ->where('object_type', 'order_integration')
35 ->where('meta_key', $provider)
36 ->first();
37
38 if (!$integrationMeta) {
39 return $this->sendError([
40 'message' => __('Integration not found', 'fluent-cart')
41 ]);
42 }
43 }
44
45 $integrationFeed = json_decode(Arr::get($requestData, 'integration'), true);
46
47 $integrationFeedData = IntegrationHelper::validateAndFormatIntegrationFeedSettings($integrationFeed, [
48 'provider' => $provider,
49 'scope' => 'global',
50 'integration_id' => $integrationId
51 ]);
52
53 if (is_wp_error($integrationFeedData)) {
54 return $this->sendError([
55 'message' => $integrationFeedData->get_error_message(),
56 'errors' => $integrationFeedData->get_error_data()
57 ]);
58 }
59
60 if ($integrationMeta) {
61 $integrationMeta->meta_value = $integrationFeedData;
62 $integrationMeta->save();
63 } else {
64 $integrationMeta = Meta::create([
65 'object_type' => 'order_integration',
66 //phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key
67 'meta_key' => $provider,
68 //phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value
69 'meta_value' => $integrationFeedData
70 ]);
71 }
72
73 do_action('fluent_cart/reindex_integration_feeds', []);
74
75 return [
76 'message' => __('Integration has been successfully saved', 'fluent-cart'),
77 'integration_id' => $integrationId,
78 'integration_name' => $provider,
79 'created' => $integrationMeta->wasRecentlyCreated,
80 'feedData' => $integrationFeedData
81 ];
82 }
83
84 public function lists()
85 {
86 return (new GlobalIntegrationSettings())->getIntegrationList($this->request->all());
87 }
88
89 /**
90 * @return array
91 * @throws \Exception
92 * Get all Global Integrations
93 */
94 public function getFeeds(Request $request)
95 {
96 $availableIntegrations = apply_filters('fluent_cart/integration/order_integrations', [], []);
97 $availableIntegrations = array_filter($availableIntegrations, function ($integration) {
98 return in_array('global', Arr::get($integration, 'scopes', [])) && $integration['enabled'];
99 });
100
101 $formattedFeeds = [];
102 if ($availableIntegrations) {
103 $feeds = Meta::whereIn('meta_key', array_keys($availableIntegrations))
104 ->where('object_type', 'order_integration')
105 ->orderBy('id', 'ASC')
106 ->get();
107
108 foreach ($feeds as $feed) {
109 $data = $feed->meta_value;
110 $formattedFeeds[] = [
111 'id' => $feed->id,
112 'name' => Arr::get($data, 'name'),
113 'enabled' => $data['enabled'],
114 'provider' => $feed->meta_key,
115 'feed' => $data,
116 'scope' => 'global',
117 ];
118 }
119 }
120
121 return [
122 'feeds' => array_values($formattedFeeds),
123 'available_integrations' => $availableIntegrations,
124 'all_module_config_url' => admin_url('admin.php?page=fluent-cart#/integrations')
125 ];
126 }
127
128 public function changeStatus(Request $request, $integrationId)
129 {
130 $integration = Meta::query()
131 ->where('object_type', 'order_integration')
132 ->findOrFail($integrationId);
133
134 $meta = $integration->meta_value;
135 $status = $request->get('status', '');
136
137 $meta['enabled'] = $status === 'yes' ? 'yes' : 'no';
138 $integration->meta_value = $meta;
139 $integration->save();
140
141 return [
142 'message' => __('Integration status updated successfully.', 'fluent-cart'),
143 'meta' => $meta
144 ];
145 }
146
147 public function getSettings(Request $request)
148 {
149 $integration_name = $request->get('integration_name', false);
150 $allIntegrations = apply_filters('fluent_cart/integration/order_integrations', []);
151
152 if (!isset($allIntegrations[$integration_name])) {
153 return $this->sendError([
154 'message' => __('Integration not found', 'fluent-cart')
155 ]);
156 }
157
158 $baseAddon = $allIntegrations[$integration_name];
159 if (!in_array('global', Arr::get($baseAddon, 'scopes', [])) || !$baseAddon['enabled']) {
160 return $this->sendError([
161 'message' => __('This integration is not available for global scope or not enabled', 'fluent-cart')
162 ]);
163 }
164
165 // This is just for validation, we will use the integration_id if provided
166 $integrationId = $request->get('integration_id', 0);
167 if ($integrationId) {
168 $integrationMeta = Meta::where('id', $integrationId)
169 ->where('object_type', 'order_integration')
170 ->where('meta_key', $integration_name)
171 ->first();
172
173 if (!$integrationMeta) {
174 return $this->sendError([
175 'message' => __('Integration not found', 'fluent-cart')
176 ]);
177 }
178 }
179
180 $integrationManager = new GlobalIntegrationSettings();
181 $settings = $integrationManager->getIntegrationSettings(
182 [
183 'integration_name' => $integration_name,
184 'integration_id' => $this->request->get('integration_id', 0)
185 ],
186 'global'
187 );
188
189 return $settings;
190 }
191
192 public function deleteSettings(Request $request, $integrationId)
193 {
194 $integration = Meta::query()
195 ->where('object_type', 'order_integration')
196 ->findOrFail($integrationId);
197
198 $integration->delete();
199
200 return [
201 'message' => __('Integration has been deleted successfully.', 'fluent-cart'),
202 'id' => $integrationId
203 ];
204 }
205
206 /**
207 * @return array
208 * @throws \Exception
209 * Get global settings of an integration
210 */
211 public function getGlobalSettings()
212 {
213 return (new GlobalIntegrationSettings())->getGlobalSettingsData($this->request->all());
214 }
215
216 public function setGlobalSettings()
217 {
218 return (new GlobalIntegrationSettings())->saveGlobalSettingsData($this->request->all());
219 }
220
221 public function authenticateCredentials()
222 {
223 return (new GlobalIntegrationSettings())->authenticateCredentials($this->request->all());
224 }
225
226 public function chained()
227 {
228 return (new GlobalIntegrationSettings())->chainedData($this->request->all());
229 }
230
231 public function installPlugin()
232 {
233 return (new GlobalIntegrationSettings())->installPlugin($this->request->get('addon_key'));
234 }
235
236 public function getDynamicOptions(Request $request)
237 {
238 $optionKey = (string)$request->get('option_key', false);
239 $search = (string)$request->get('search', '');
240
241 if ($optionKey == 'post_type') {
242
243 $postType = $request->get('sub_option_key', '');
244 if (!$postType) {
245 return [
246 'options' => []
247 ];
248 }
249
250 $args = [
251 'post_type' => $postType,
252 'posts_per_page' => 20
253 ];
254
255 if ($search) {
256 $args['s'] = $search;
257 }
258
259 $posts = get_posts($args);
260
261 $formattedPosts = [];
262 if (!is_wp_error($posts)) {
263 foreach ($posts as $post) {
264 $formattedPosts[$post->ID] = [
265 'id' => strval($post->ID),
266 'title' => $post->post_title
267 ];
268 }
269 }
270
271 $includedIds = (array)$request->get('values', []);
272
273 if (!$includedIds) {
274 return [
275 'options' => array_values($formattedPosts)
276 ];
277 }
278
279 $includedIds = (array)$includedIds;
280
281 $includedIds = array_diff($includedIds, array_keys($formattedPosts));
282 if ($includedIds) {
283 $posts = get_posts([
284 'post_type' => $postType,
285 'post__in' => $includedIds
286 ]);
287 foreach ($posts as $post) {
288 $formattedPosts[$post->ID] = [
289 'id' => strval($post->ID),
290 'title' => $post->post_title
291 ];
292 }
293 }
294
295 return [
296 'options' => array_values($formattedPosts)
297 ];
298
299 }
300
301 if ($optionKey) {
302 $options = apply_filters('fluent_cart/integration/integration_options_' . $optionKey, [], [
303 'search' => (string)$request->get('search', ''),
304 'values' => (array)$request->get('values', []),
305 'option_key' => $optionKey
306 ]);
307 }
308
309 return [
310 'options' => isset($options) ? $options : []
311 ];
312 }
313
314 }
315