PluginProbe
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler / 1.3.21
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler v1.3.21
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 / Http / Controllers / ModuleSettingsController.php

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

277 lines 10.3 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\Api\ModuleSettings;
6 use FluentCart\Api\Sanitizer\Sanitizer;
7 use FluentCart\App\Services\PluginInstaller\AddonManager;
8 use FluentCart\App\Vite;
9 use FluentCart\Framework\Http\Request\Request;
10 use FluentCart\Framework\Support\Arr;
11
12 class ModuleSettingsController extends Controller
13 {
14 public function getSettings(): \WP_REST_Response
15 {
16 $fields = ModuleSettings::fileds();
17 $values = ModuleSettings::getAllSettings();
18
19 return $this->sendSuccess([
20 'fields' => [
21 'modules_settings' => [
22 'title' => __('Features & addon', 'fluent-cart'),
23 'type' => 'section',
24 'class' => 'no-padding',
25 'disable_nesting' => true,
26 'columns' => [
27 'default' => 1,
28 'md' => 1
29 ],
30 'schema' => $fields
31 ]
32 ],
33 'settings' => $values
34 ]);
35 }
36
37 public function saveSettings(Request $request)
38 {
39 $prevSettings = ModuleSettings::getAllSettings(false);
40
41 $data = $request->only(
42 ModuleSettings::validKeys()
43 );
44
45 $data = Sanitizer::sanitize($data);
46
47 ModuleSettings::saveSettings($data);
48
49 foreach ($data as $moduleKey => $moduleData) {
50 $prevStatus = Arr::get($prevSettings, $moduleKey . '.active', 'no');
51 $newStatus = Arr::get($moduleData, 'active', 'no');
52 if ($newStatus === $prevStatus) {
53 continue;
54 }
55
56 if ($prevStatus === 'yes' && $newStatus === 'no') {
57 // Module deactivated
58 do_action('fluent_cart/module/deactivated/' . $moduleKey, $moduleData, $prevSettings[$moduleKey]);
59 } elseif ($prevStatus === 'no' && $newStatus === 'yes') {
60 // Module activated
61 do_action('fluent_cart/module/activated/' . $moduleKey, $moduleData, $prevSettings[$moduleKey]);
62 }
63 }
64
65 return $this->sendSuccess([
66 'message' => __('Settings saved successfully', 'fluent-cart')
67 ]);
68 }
69
70 public function getPluginAddons(): \WP_REST_Response
71 {
72 $addons = $this->getRegisteredPluginAddons();
73
74 // Add installation status for each addon
75 foreach ($addons as $key => &$addon) {
76 $isUpcoming = Arr::get($addon, 'upcoming', false);
77 if($isUpcoming){
78 $addon['is_installed'] = false;
79 $addon['is_active'] = false;
80 $addon['plugin_file'] = '';
81 $addon['plugin_file'] = '';
82 $addon['source_link'] = '';
83 $addon['source_type'] = '';
84 continue;
85 }
86 if (!empty($addon['plugin_file']) && !empty($addon['plugin_slug'])) {
87 $status = (new AddonManager())->getAddonStatus($addon['plugin_slug'], $addon['plugin_file']);
88 $addon['is_installed'] = $status['is_installed'];
89 $addon['is_active'] = $status['is_active'];
90 } else {
91 $addon['is_installed'] = false;
92 $addon['is_active'] = false;
93 }
94 }
95
96 return $this->sendSuccess([
97 'addons' => $addons,
98 ]);
99 }
100
101 public function installPluginAddon(Request $request): \WP_REST_Response
102 {
103 $pluginSlug = $request->getSafe('plugin_slug', 'sanitize_text_field');
104 $sourceType = $request->getSafe('source_type', 'sanitize_text_field', 'wordpress');
105 $sourceLink = $request->getSafe('source_link', 'sanitize_url', '');
106 $assetPath = $request->getSafe('asset_path', 'sanitize_text_field', '');
107 $assetPath = trim($assetPath);
108 if(empty($assetPath)){
109 $assetPath = 'zipball_url';
110 }
111
112
113 if (!$pluginSlug) {
114 return $this->sendError([
115 'message' => __('Plugin slug is required.', 'fluent-cart')
116 ]);
117 }
118
119 // Validate the addon is in the allowed list
120 $registeredAddons = $this->getRegisteredPluginAddons();
121 $allowedAddon = null;
122
123 foreach ($registeredAddons as $addon) {
124 if ($addon['plugin_slug'] === $pluginSlug) {
125 $allowedAddon = $addon;
126 break;
127 }
128 }
129
130 if (!$allowedAddon) {
131 return $this->sendError([
132 'message' => __('This addon cannot be installed.', 'fluent-cart')
133 ]);
134 }
135
136 // Use source from registered addon if not provided
137 if (empty($sourceType) && !empty($allowedAddon['source_type'])) {
138 $sourceType = $allowedAddon['source_type'];
139 }
140 if (empty($sourceLink) && !empty($allowedAddon['source_link'])) {
141 $sourceLink = $allowedAddon['source_link'];
142 }
143
144 $addonManager = new AddonManager();
145 $result = $addonManager->installAddon($sourceType, $sourceLink, $pluginSlug, $assetPath);
146
147 if (is_wp_error($result)) {
148 return $this->sendError([
149 'message' => $result->get_error_message()
150 ]);
151 }
152
153 return $this->sendSuccess($result);
154 }
155
156 public function activatePluginAddon(Request $request): \WP_REST_Response
157 {
158 $pluginFile = $request->getSafe('plugin_file', 'sanitize_text_field');
159
160 if (!$pluginFile) {
161 return $this->sendError([
162 'message' => __('Plugin file is required.', 'fluent-cart')
163 ]);
164 }
165
166
167 $addonManager = new AddonManager();
168 $result = $addonManager->activateAddon($pluginFile);
169
170 if (is_wp_error($result)) {
171 return $this->sendError([
172 'message' => $result->get_error_message()
173 ]);
174 }
175
176 return $this->sendSuccess([
177 'message' => __('Addon activated successfully.', 'fluent-cart')
178 ]);
179 }
180
181 public function verifyTurnstileKeys(Request $request): \WP_REST_Response
182 {
183 $siteKey = $request->getSafe('site_key', 'sanitize_text_field');
184 $secretKey = $request->getSafe('secret_key', 'sanitize_text_field');
185
186 $token = $request->getSafe('token', 'sanitize_text_field');
187
188 if (empty($siteKey) || empty($secretKey)) {
189 return $this->sendError([
190 'message' => __('Both Site Key and Secret Key are required.', 'fluent-cart')
191 ]);
192 }
193
194 if (empty($token)) {
195 return $this->sendError([
196 'message' => __('Could not get a Turnstile token. Please check your Site Key and ensure this domain is allowed in Cloudflare.', 'fluent-cart')
197 ]);
198 }
199
200 $response = wp_remote_post('https://challenges.cloudflare.com/turnstile/v0/siteverify', [
201 'body' => [
202 'secret' => $secretKey,
203 'response' => $token,
204 ],
205 'timeout' => 10
206 ]);
207
208 if (is_wp_error($response)) {
209 return $this->sendError([
210 'message' => __('Could not connect to Cloudflare. Please try again.', 'fluent-cart')
211 ]);
212 }
213
214 $body = wp_remote_retrieve_body($response);
215 $result = json_decode($body, true);
216
217 if (!empty($result['success'])) {
218 return $this->sendSuccess([
219 'message' => __('Turnstile keys are valid and working.', 'fluent-cart')
220 ]);
221 }
222
223 $errorCodes = Arr::get($result, 'error-codes', []);
224 $errorMessage = __('Invalid Turnstile keys. Please check your Site Key and Secret Key.', 'fluent-cart');
225
226 if (in_array('invalid-input-secret', $errorCodes)) {
227 $errorMessage = __('The Secret Key is invalid. Please check it in your Cloudflare Dashboard.', 'fluent-cart');
228 }
229
230 return $this->sendError([
231 'message' => $errorMessage
232 ]);
233 }
234
235 private function getRegisteredPluginAddons(): array
236 {
237 $addons = [
238 'elementor-block' => [
239 'title' => __('Elementor Blocks', 'fluent-cart'),
240 'description' => __('Enable to get Elementor Blocks for FluentCart. Minimum Requirement: Elementor V3.34', 'fluent-cart'),
241 'logo' => Vite::getAssetUrl('images/elementor/black.svg'),
242 'dark_logo' => Vite::getAssetUrl('images/elementor/white.svg'),
243 'plugin_slug' => 'fluent-cart-elementor-blocks',
244 'plugin_file' => 'fluent-cart-elementor-blocks/fluent-cart-elementor-blocks.php',
245 'source_type' => 'cdn',
246 'source_link' => 'https://addons-cdn.fluentcart.com/fluent-cart-elementor-blocks.zip',
247 'upcoming' => false,
248 'repo_link' => 'https://github.com/WPManageNinja/fluent-cart-elementor-blocks'
249 ],
250 'fluent-pdf' => [
251 'title' => __('Fluent PDF', 'fluent-cart'),
252 'description' => __('Generate PDF receipts and attach them to email notifications.', 'fluent-cart'),
253 'logo' => Vite::getAssetUrl('images/fluent-pdf/black.svg'),
254 'dark_logo' => Vite::getAssetUrl('images/fluent-pdf/white.svg'),
255 'plugin_slug' => 'fluentforms-pdf',
256 'plugin_file' => 'fluentforms-pdf/fluentforms-pdf.php',
257 'source_type' => 'wordpress',
258 'upcoming' => false,
259 ],
260 'fluent-cart-migrator' => [
261 'title' => __('FluentCart Migrator', 'fluent-cart'),
262 'description' => __('Migrate your store data to FluentCart from other eCommerce platforms.', 'fluent-cart'),
263 'logo' => Vite::getAssetUrl('images/logo.svg'),
264 'plugin_slug' => 'fluent-cart-migrator',
265 'plugin_file' => 'fluent-cart-migrator/fluent-cart-migrator.php',
266 'source_type' => 'cdn',
267 'source_link' => 'https://addons-cdn.fluentcart.com/fluent-cart-migrator.zip',
268 'upcoming' => false,
269 'repo_link' => 'https://fluentcart.com/fluentcart-addons/?3181_search=Migrator'
270 ],
271 ];
272
273 // Allow other modules/plugins to register their addons
274 return apply_filters('fluent_cart/module_settings/plugin_addons', $addons);
275 }
276 }
277