PluginProbe
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler / 1.3.20
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler v1.3.20
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 / StorageDrivers / S3 / S3.php

S3.php in FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler 1.3.20, at app/Modules/StorageDrivers/S3/S3.php

332 lines 9.8 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\StorageDrivers\S3;
4
5 use FluentCart\App\Services\FileSystem\Drivers\S3\S3ConnectionVerify;
6 use FluentCart\App\Services\FileSystem\Drivers\S3\S3Driver;
7 use FluentCart\App\Vite;
8 use FluentCart\App\Modules\StorageDrivers\BaseStorageDriver;
9 use FluentCart\Framework\Support\Arr;
10
11 class S3 extends BaseStorageDriver
12 {
13 /**
14 * title, slug, brandColor
15 */
16 public function __construct()
17 {
18 parent::__construct(
19 __('S3', 'fluent-cart'),
20 's3',
21 '#4f94d4'
22 );
23 }
24
25 public function registerHooks()
26 {
27 add_filter('fluent_cart/verify_driver_connect_info_' . $this->slug, [$this, 'verifyConnectInfo'], 10, 2);
28 add_filter('fluent_cart/get_dynamic_search_s3_bucket_list', [$this, 'getBucketList'], 10, 2);
29
30 }
31
32 public function getBucketList(): array
33 {
34 $bucketList = (new S3Driver())->buckets();
35 $buckets = [];
36 foreach ($bucketList as $bucket) {
37 $buckets[] = array(
38 "label" => $bucket,
39 "value" => $bucket,
40 );
41 }
42 return $buckets;
43 }
44
45 public function getLogo(): string
46 {
47 return Vite::getAssetUrl("images/storage-drivers/s3.svg");
48 }
49
50 public function hasBucket(): bool
51 {
52 return true;
53 }
54
55
56 public function getDescription()
57 {
58 return esc_html__('S3 bucket allows to configure storage options and others for efficient and secure cloud-based file storage', 'fluent-cart');
59 }
60
61 public function isEnabled(): bool
62 {
63 $settings = $this->getSettings();
64 $isActive = Arr::get($settings, 'is_active') === 'yes';
65 $hasSelectedBuckets = !empty(Arr::get($settings, 'buckets')) && is_array(Arr::get($settings, 'buckets')) && count(Arr::get($settings, 'buckets')) > 0;
66 return $isActive && $hasSelectedBuckets;
67 }
68
69 public function getSettings()
70 {
71 return (new S3Settings())->get();
72 }
73
74 public function updateSettings($data)
75 {
76 $settings = $this->getSettings();
77 foreach ($this->hiddenSettingKeys() as $key) {
78 if (empty($data[$key]) && isset($settings[$key])) {
79 $data[$key] = $settings[$key];
80 }
81 }
82
83 $isVarified = true;
84 if (Arr::get($data, 'is_active') === 'yes') {
85 $isVarified = $this->verifyConnectInfo($data);
86
87 if (!is_wp_error($isVarified)) {
88 $data['is_active'] = 'yes';
89 }
90 $data['show_buckets'] = 'yes';
91 } else {
92 $data['is_active'] = 'no';
93 $data['access_key'] = '';
94 $data['secret_key'] = '';
95 $data['buckets'] = [];
96 $data['show_buckets'] = 'no';
97 $cacheKey = 'fct_s3_region';
98 \FluentCart\App\Models\Meta::query()->where('meta_key', $cacheKey)->delete();
99 $message = __('S3 is deactivated successfully', 'fluent-cart');
100 }
101
102
103 // if (empty($data['buckets'])) {
104 // $data['is_active'] = 'no';
105 // $data['buckets'] = [];
106 // } else {
107 // if ($isVarified) {
108 // //$data['is_active'] = 'yes';
109 // }
110 // }
111
112 if (is_wp_error($isVarified)) {
113 return $isVarified;
114 }
115
116
117 parent::updateSettings($data);
118
119 $shouldReload = true;
120
121 if (empty($message)) {
122 if (Arr::get($data, 'is_active') === 'yes') {
123 $message = __('Your s3 storage is activated successfully', 'fluent-cart');
124 } else {
125 $message = __('S3 is configured successfully but not activated.', 'fluent-cart');
126 $shouldReload = true;
127 }
128 }
129
130
131 return [
132 'data' => Arr::except($data, $this->hiddenSettingKeys()),
133 'message' => $message,
134 'shouldReload' => $shouldReload
135 ];
136 }
137
138 /**
139 * Verify Connect configuration
140 */
141 public function verifyConnectInfo(array $data, $args = [])
142 {
143 $settings = $this->getSettings();
144 $isUsingDefineMode = Arr::get($settings, 'is_using_define_mode');
145
146 $accessKey = Arr::get($data, 'access_key', null);
147 $secretKey = Arr::get($data, 'secret_key', null);
148
149 if (empty($secretKey)) {
150 $secretKey = Arr::get($settings, 'secret_key');
151 }
152
153
154 if ($isUsingDefineMode) {
155 $secretKey = FCT_S3_SECRET_KEY;
156 $accessKey = FCT_S3_ACCESS_KEY;
157 }
158
159 if (empty($secretKey) || empty($accessKey)) {
160 return new \WP_Error('invalid_credentials', __('Invalid credentials', 'fluent-cart'));
161 }
162 return S3ConnectionVerify::verify(
163 $secretKey,
164 $accessKey
165 );
166 }
167
168 public function fields(): array
169 {
170 $settings = $this->getSettings();
171 $showBucket = Arr::get($settings, 'show_buckets') === 'yes';
172 $usingDefineMode = Arr::get($settings, 'is_using_define_mode');
173
174 $schema = [
175 'is_active' => [
176 'value' => '',
177 'label' => __('Enable s3 driver', 'fluent-cart'),
178 'type' => 'checkbox',
179 'attributes' => [
180 'disabled' => !$showBucket
181 ],
182 ],
183 'access_key' => [
184 'conditions' => [
185 [
186 'key' => 'is_active',
187 'operator' => '==',
188 'value' => 'yes'
189 ],
190 ],
191 'value' => '',
192 'label' => __('Access Key', 'fluent-cart'),
193 'type' => 'text',
194 'placeholder' => __('Enter access key', 'fluent-cart')
195 ],
196 'secret_key' => [
197 'conditions' => [
198 [
199 'key' => 'is_active',
200 'operator' => '==',
201 'value' => 'yes'
202 ],
203 ],
204 'value' => '',
205 'label' => __('Secret Key', 'fluent-cart'),
206 'type' => 'text',
207 'attributes' => [
208 'type' => 'password'
209 ],
210 'placeholder' => __('Enter secret key', 'fluent-cart')
211 ],
212 ];
213
214
215 if ($showBucket) {
216 $bucketList = Arr::get($settings, 'buckets', []);
217 $buckets = [];
218
219 foreach ($bucketList as $bucket) {
220 $buckets[] = array(
221 "label" => $bucket,
222 "value" => $bucket,
223 );
224 }
225
226 $schema['buckets'] = [
227 'conditions' => [
228 [
229 'key' => 'is_active',
230 'operator' => '==',
231 'value' => 'yes'
232 ],
233 ],
234 'value' => '',
235 'label' => __('Buckets', 'fluent-cart'),
236 'type' => 'remote_select',
237 'remote_key' => 's3_bucket_list',
238 'multiple' => true,
239 'options' => [],
240 'placeholder' => __('Select bucket', 'fluent-cart'),
241 'search_only_on_type' => false,
242 ];
243 }
244
245 if ($usingDefineMode) {
246 unset($schema['access_key'], $schema['secret_key']);
247 $schema['is_using_define_mode'] = [
248 'value' => __('Using Define Mode', 'fluent-cart'),
249 'type' => 'html'
250 ];
251 }
252
253 return [
254 'view' => [
255 'title' => __('S3 Settings', 'fluent-cart'),
256 'type' => 'section',
257 'disable_nesting' => true,
258 'columns' => [
259 'default' => 1,
260 'md' => 1
261 ],
262 'schema' => $schema
263 ]
264 ];
265 }
266
267 public function getDriverClass(): string
268 {
269 return S3Driver::class;
270 }
271
272 public function hiddenSettingKeys(): array
273 {
274 return [
275 //'secret_key',
276 'region',
277 'show_buckets'
278 ];
279 }
280
281 public static function getBucketRegion($bucket)
282 {
283 $cacheKey = 'fct_s3_region';
284 $existingMeta = \FluentCart\App\Models\Meta::query()->where('meta_key', $cacheKey)->first();
285
286
287 if ($existingMeta) {
288 $region = Arr::get($existingMeta->meta_value, $bucket);
289 if ($region) {
290 return $region;
291 }
292 }
293
294 //$url = "https://{$bucket}.s3.amazonaws.com"; // The global endpoint
295 if (strpos($bucket, '.') !== false) {
296 // If bucket contains dot, use path-style (required for SSL)
297 $url = "https://s3.amazonaws.com/{$bucket}";
298 } else {
299 // Normal bucket: use virtual-hosted style
300 $url = "https://{$bucket}.s3.amazonaws.com";
301 }
302
303
304 $response = wp_remote_head($url);
305
306 if (is_wp_error($response)) {
307 return null;
308 }
309
310 $headers = wp_remote_retrieve_headers($response);
311
312 $region = Arr::get($headers, 'x-amz-bucket-region') ?? 'us-east-1';
313
314 // 5. Save or update the Meta record
315 if ($existingMeta) {
316 $values = $existingMeta->meta_value;
317 $values[$bucket] = $region;
318 $existingMeta->meta_value = $values;
319 $existingMeta->save();
320 } else {
321 \FluentCart\App\Models\Meta::query()->create([
322 'meta_key' => $cacheKey,
323 'meta_value' => [
324 $bucket => $region
325 ],
326 ]);
327 }
328
329 return $region;
330 }
331 }
332