| @@ -32,10 +32,11 @@ | ||
| 32 | 32 | $message = Arr::get($data, 'message', __('Settings saved successfully', 'fluent-cart')); |
| 33 | 33 | |
| 34 | 34 | if (is_wp_error($data)) { |
| 35 | 35 | return $this->sendError([ |
| 36 | - 'message' => $data->get_error_message() | |
| 37 | - ], 401); | |
| 36 | + 'message' => $data->get_error_message(), | |
| 37 | + 'error_data' => $data->get_error_data() | |
| 38 | + ], 400); | |
| 38 | 39 | } else { |
| 39 | 40 | return $this->sendSuccess([ |
| 40 | 41 | 'message' => $message, |
| 41 | 42 | 'data' => $data |
| @@ -59,25 +60,196 @@ | ||
| 59 | 60 | } |
| 60 | 61 | |
| 61 | 62 | public function verifyConnectInfo(Request $request, GlobalStorageHandler $globalHandler) |
| 62 | 63 | { |
| 63 | - | |
| 64 | 64 | $data = $request->settings; |
| 65 | 65 | $driver = sanitize_text_field($request->get('driver')); |
| 66 | - $driver = (new FileManager($driver))->getDriver(); | |
| 67 | - if (empty($driver)) { | |
| 66 | + $storageDriver = $this->resolveStorageDriver($driver); | |
| 67 | + if (is_wp_error($storageDriver)) { | |
| 68 | 68 | return $this->sendError([ |
| 69 | - 'message' => __('Invalid driver', 'fluent-cart') | |
| 69 | + 'message' => $storageDriver->get_error_message(), | |
| 70 | + 'error_data' => $storageDriver->get_error_data() | |
| 70 | 71 | ], 404); |
| 71 | 72 | } |
| 72 | - $isVerified = $driver->getStorageDriver()->verifyConnectInfo($data); | |
| 73 | + | |
| 74 | + $isVerified = $storageDriver->verifyConnectInfo($data); | |
| 73 | 75 | if (is_wp_error($isVerified)) { |
| 74 | 76 | return $this->sendError([ |
| 75 | - 'message' => $isVerified->get_error_message() | |
| 76 | - ], 401); | |
| 77 | + 'message' => $isVerified->get_error_message(), | |
| 78 | + 'error_data' => $isVerified->get_error_data() | |
| 79 | + ], 400); | |
| 77 | 80 | } else { |
| 78 | 81 | return $this->sendSuccess([ |
| 79 | 82 | 'message' => Arr::get($isVerified, 'message', __('Connection verified successfully', 'fluent-cart')) |
| 80 | 83 | ]); |
| 84 | + } | |
| 85 | + } | |
| 86 | + | |
| 87 | + public function createBucket(Request $request) | |
| 88 | + { | |
| 89 | + $data = (array) $request->settings; | |
| 90 | + $driver = sanitize_text_field($request->driver); | |
| 91 | + | |
| 92 | + $storageDriver = $this->resolveBucketActionDriver($driver, 'createBucket'); | |
| 93 | + if (is_wp_error($storageDriver)) { | |
| 94 | + return $this->sendError([ | |
| 95 | + 'message' => $storageDriver->get_error_message(), | |
| 96 | + 'error_data' => $storageDriver->get_error_data() | |
| 97 | + ], 400); | |
| 98 | + } | |
| 99 | + | |
| 100 | + $result = $storageDriver->createBucket($data); | |
| 101 | + | |
| 102 | + if (is_wp_error($result)) { | |
| 103 | + return $this->sendError([ | |
| 104 | + 'message' => $result->get_error_message(), | |
| 105 | + 'error_data' => $result->get_error_data() | |
| 106 | + ], 400); | |
| 107 | + } | |
| 108 | + | |
| 109 | + return $this->sendSuccess([ | |
| 110 | + 'message' => Arr::get($result, 'message', __('Bucket created successfully', 'fluent-cart')), | |
| 111 | + 'data' => $result | |
| 112 | + ]); | |
| 113 | + } | |
| 114 | + | |
| 115 | + public function listBuckets(Request $request) | |
| 116 | + { | |
| 117 | + $data = (array) $request->settings; | |
| 118 | + $driver = sanitize_text_field($request->driver); | |
| 119 | + $query = sanitize_text_field($request->get('query', '')); | |
| 120 | + | |
| 121 | + $storageDriver = $this->resolveBucketActionDriver($driver, 'listBuckets'); | |
| 122 | + if (is_wp_error($storageDriver)) { | |
| 123 | + return $this->sendError([ | |
| 124 | + 'message' => $storageDriver->get_error_message(), | |
| 125 | + 'error_data' => $storageDriver->get_error_data() | |
| 126 | + ], 400); | |
| 127 | + } | |
| 128 | + | |
| 129 | + $result = $storageDriver->listBuckets($data, [ | |
| 130 | + 'query' => $query | |
| 131 | + ]); | |
| 132 | + | |
| 133 | + if (is_wp_error($result)) { | |
| 134 | + return $this->sendError([ | |
| 135 | + 'message' => $result->get_error_message(), | |
| 136 | + 'error_data' => $result->get_error_data() | |
| 137 | + ], 400); | |
| 138 | + } | |
| 139 | + | |
| 140 | + return $this->sendSuccess([ | |
| 141 | + 'options' => is_array($result) ? $result : [] | |
| 142 | + ]); | |
| 143 | + } | |
| 144 | + | |
| 145 | + public function resetSettings(Request $request) | |
| 146 | + { | |
| 147 | + $driver = sanitize_text_field($request->driver); | |
| 148 | + $storageDriver = $this->resolveStorageDriver($driver); | |
| 149 | + | |
| 150 | + if (is_wp_error($storageDriver)) { | |
| 151 | + return $this->sendError([ | |
| 152 | + 'message' => $storageDriver->get_error_message(), | |
| 153 | + 'error_data' => $storageDriver->get_error_data() | |
| 154 | + ], 400); | |
| 155 | + } | |
| 156 | + | |
| 157 | + $result = $storageDriver->resetSettings(); | |
| 158 | + | |
| 159 | + if (is_wp_error($result)) { | |
| 160 | + return $this->sendError([ | |
| 161 | + 'message' => $result->get_error_message(), | |
| 162 | + 'error_data' => $result->get_error_data() | |
| 163 | + ], 400); | |
| 164 | + } | |
| 165 | + | |
| 166 | + $responseData = is_array($result) | |
| 167 | + ? Arr::except($result, $storageDriver->hiddenSettingKeys()) | |
| 168 | + : $result; | |
| 169 | + | |
| 170 | + return $this->sendSuccess([ | |
| 171 | + 'message' => __('Settings reset successfully', 'fluent-cart'), | |
| 172 | + 'data' => $responseData | |
| 173 | + ]); | |
| 174 | + } | |
| 175 | + | |
| 176 | + public function changeStatus(Request $request) | |
| 177 | + { | |
| 178 | + $driver = sanitize_text_field($request->driver); | |
| 179 | + $status = sanitize_text_field($request->status); | |
| 180 | + | |
| 181 | + if (!in_array($status, ['yes', 'no'], true)) { | |
| 182 | + return $this->sendError([ | |
| 183 | + 'message' => __('Invalid status value', 'fluent-cart') | |
| 184 | + ], 422); | |
| 185 | + } | |
| 186 | + | |
| 187 | + $driver = (new FileManager($driver, null, null, true))->getDriver(); | |
| 188 | + if (empty($driver)) { | |
| 189 | + return $this->sendError([ | |
| 190 | + 'message' => __('Invalid driver', 'fluent-cart') | |
| 191 | + ], 404); | |
| 192 | + } | |
| 193 | + | |
| 194 | + $storageDriver = $driver->getStorageDriver(); | |
| 195 | + | |
| 196 | + if ($storageDriver->hasBucket()) { | |
| 197 | + return $this->sendError([ | |
| 198 | + 'message' => __('This storage driver must be managed from its settings page.', 'fluent-cart') | |
| 199 | + ], 400); | |
| 200 | + } | |
| 201 | + | |
| 202 | + $settings = (array) $storageDriver->getSettings(); | |
| 203 | + $settings['is_active'] = $status; | |
| 204 | + | |
| 205 | + $result = $storageDriver->saveSettings($settings); | |
| 206 | + | |
| 207 | + if (is_wp_error($result)) { | |
| 208 | + return $this->sendError([ | |
| 209 | + 'message' => $result->get_error_message() | |
| 210 | + ], 400); | |
| 211 | + } | |
| 212 | + | |
| 213 | + return $this->sendSuccess([ | |
| 214 | + 'message' => Arr::get($result, 'message', __('Status updated successfully', 'fluent-cart')), | |
| 215 | + 'data' => $result | |
| 216 | + ]); | |
| 217 | + } | |
| 218 | + | |
| 219 | + private function resolveBucketActionDriver(string $driver, string $method) | |
| 220 | + { | |
| 221 | + $driverInstance = $this->resolveDriverInstance($driver); | |
| 222 | + if (is_wp_error($driverInstance)) { | |
| 223 | + return $driverInstance; | |
| 224 | + } | |
| 225 | + | |
| 226 | + $storageDriver = $driverInstance->getStorageDriver(); | |
| 227 | + if (!method_exists($storageDriver, $method)) { | |
| 228 | + return new \WP_Error( | |
| 229 | + 'unsupported_bucket_api', | |
| 230 | + __('The selected storage driver does not support bucket management.', 'fluent-cart') | |
| 231 | + ); | |
| 232 | + } | |
| 233 | + | |
| 234 | + return $storageDriver; | |
| 235 | + } | |
| 236 | + | |
| 237 | + private function resolveStorageDriver(string $driver) | |
| 238 | + { | |
| 239 | + $driverInstance = $this->resolveDriverInstance($driver); | |
| 240 | + if (is_wp_error($driverInstance)) { | |
| 241 | + return $driverInstance; | |
| 242 | + } | |
| 243 | + | |
| 244 | + return $driverInstance->getStorageDriver(); | |
| 245 | + } | |
| 246 | + | |
| 247 | + private function resolveDriverInstance(string $driver) | |
| 248 | + { | |
| 249 | + try { | |
| 250 | + return (new FileManager($driver, null, null, true))->getDriver(); | |
| 251 | + } catch (\Throwable $e) { | |
| 252 | + return new \WP_Error('invalid_driver', __('Invalid driver', 'fluent-cart')); | |
| 81 | 253 | } |
| 82 | 254 | } |
| 83 | 255 | } |