| @@ -30,21 +30,39 @@ | ||
| 30 | 30 | */ |
| 31 | 31 | |
| 32 | 32 | public function register_routes() { |
| 33 | 33 | $this->add_route( '/verifyCredentials', 'verifyCredentials', 'POST' ); |
| 34 | - $this->add_route( '/permissions/checkExist', 'verifyBucketExist', 'POST' ); | |
| 35 | - $this->add_route( '/permissions/addNewBucket', 'createBucket', 'POST' ); | |
| 36 | - $this->add_route( '/permissions/write', 'verifyObjectWrite', 'POST' ); | |
| 34 | + $this->add_route( '/bucket/checkExist', 'verifyBucketExist', 'POST' ); | |
| 35 | + $this->add_route( '/bucket/addNew', 'createBucket', 'POST' ); | |
| 36 | + $this->add_route( '/permissions/status', 'verifyStatus', 'POST' ); | |
| 37 | + $this->add_route( '/permissions/write', 'verifyWrite', 'POST' ); | |
| 37 | 38 | $this->add_route( '/permissions/delete', 'verifyObjectDelete', 'POST' ); |
| 38 | - $this->add_route( '/permissions/read', 'verifyObjectRead', 'POST' ); | |
| 39 | + $this->add_route( '/permissions/read', 'verifyRead', 'POST' ); | |
| 39 | 40 | |
| 40 | 41 | $this->add_route( '/security/get_security', 'getBucketSecuritySettings', 'POST' ); |
| 41 | 42 | $this->add_route( '/security/block_public_access', 'changePublicAccess', 'POST' ); |
| 42 | 43 | $this->add_route( '/security/object_ownership_enforce', 'changeObjectOwnership', 'POST' ); |
| 43 | 44 | |
| 45 | + $this->add_route( '/getSettings', 'getSettings' ); | |
| 44 | 46 | $this->add_route( '/saveConfig', 'saveConfig', 'POST' ); |
| 45 | - $this->add_route( '/getSettings', 'getSettings' ); | |
| 46 | - $this->add_route( '/uploadCredentials', 'uploadCredentials', 'POST' ); | |
| 47 | + $this->add_route( '/refreshCounts', 'refreshCounts', 'POST' ); | |
| 48 | + $this->add_route( '/getErrorLogs', 'getErrorLogs', 'POST' ); | |
| 49 | + $this->add_route( '/clearErrorLogs', 'clearErrorLogs', 'POST' ); | |
| 50 | + | |
| 51 | + // Upgrade Database Routes | |
| 52 | + $this->add_route( '/upgrade/status', 'getUpgradeStatus' ); | |
| 53 | + $this->add_route( '/upgrade/start', 'startUpgrade', 'POST' ); | |
| 54 | + | |
| 55 | + // Sync Routes | |
| 56 | + $this->add_route( '/sync/status', 'getStatus' ); | |
| 57 | + $this->add_route( '/sync/start', 'startSync', 'POST' ); | |
| 58 | + $this->add_route( '/sync/pause', 'pauseSync', 'POST' ); | |
| 59 | + $this->add_route( '/sync/resume', 'resumeSync', 'POST' ); | |
| 60 | + $this->add_route( '/sync/stop', 'stopSync', 'POST' ); | |
| 61 | + $this->add_route( '/sync/tick', 'tickSync', 'POST' ); | |
| 62 | + $this->add_route( '/sync/setMethod', 'setSyncMethod', 'POST' ); | |
| 63 | + | |
| 64 | + $this->add_route( '/sync/retrySingle', 'retrySingle', 'POST' ); | |
| 47 | 65 | } |
| 48 | 66 | |
| 49 | 67 | /** |
| 50 | 68 | * Verify the Service Credentials |
| @@ -66,17 +84,31 @@ | ||
| 66 | 84 | public function createBucket( $data ) { |
| 67 | 85 | return new WP_REST_Response( Service::instance()->createBucket($data->get_params()), 200 ); |
| 68 | 86 | } |
| 69 | 87 | |
| 70 | - | |
| 71 | 88 | /** |
| 72 | - * Verify the Bucket Write Permission | |
| 89 | + * Saved connection status checks (storage, CDN). | |
| 90 | + * @since 1.3.11 | |
| 73 | 91 | */ |
| 74 | - public function verifyObjectWrite( $data ) { | |
| 75 | - return new WP_REST_Response( Service::instance()->verifyObjectWritePermission($data->get_params()), 200 ); | |
| 92 | + public function verifyStatus( $data ) { | |
| 93 | + $params = $data->get_params(); | |
| 94 | + $check = isset( $params['check'] ) ? sanitize_text_field( $params['check'] ) : 'storage'; | |
| 95 | + return new WP_REST_Response( Service::instance()->verifyStatus( $check ), 200 ); | |
| 76 | 96 | } |
| 77 | 97 | |
| 78 | 98 | /** |
| 99 | + * Verify bucket write permission (configure wizard) or saved storage status (legacy route). | |
| 100 | + * @since 1.3.11 | |
| 101 | + */ | |
| 102 | + public function verifyWrite( $data ) { | |
| 103 | + $params = $data->get_params(); | |
| 104 | + if ( !empty( $params['service'] ) ) { | |
| 105 | + return new WP_REST_Response( Service::instance()->verifyObjectWritePermission( $params ), 200 ); | |
| 106 | + } | |
| 107 | + return new WP_REST_Response( Service::instance()->verifyStatus( 'storage' ), 200 ); | |
| 108 | + } | |
| 109 | + | |
| 110 | + /** | |
| 79 | 111 | * Verify the Bucket delete Permission |
| 80 | 112 | */ |
| 81 | 113 | public function verifyObjectDelete( $data ) { |
| 82 | 114 | return new WP_REST_Response( Service::instance()->verifyObjectDeletePermission($data->get_params()), 200 ); |
| @@ -82,12 +114,13 @@ | ||
| 82 | 114 | return new WP_REST_Response( Service::instance()->verifyObjectDeletePermission($data->get_params()), 200 ); |
| 83 | 115 | } |
| 84 | 116 | |
| 85 | 117 | /** |
| 86 | - * Verify the Bucket Read Permission | |
| 118 | + * Verify delivery read access (legacy route). | |
| 119 | + * @since 1.3.11 | |
| 87 | 120 | */ |
| 88 | - public function verifyObjectRead( $data ) { | |
| 89 | - return new WP_REST_Response( Service::instance()->verifyObjectReadPermission($data->get_params()), 200 ); | |
| 121 | + public function verifyRead( $data ) { | |
| 122 | + return new WP_REST_Response( Service::instance()->verifyStatus( 'cdn' ), 200 ); | |
| 90 | 123 | } |
| 91 | 124 | |
| 92 | 125 | /** |
| 93 | 126 | * Get the Security Settings |
| @@ -115,16 +148,30 @@ | ||
| 115 | 148 | /** |
| 116 | 149 | * Get Settings |
| 117 | 150 | */ |
| 118 | 151 | public function getSettings( ) { |
| 152 | + // Fetch and update media counts for make sure they are up to date | |
| 153 | + Counter::fetch_and_update(); | |
| 154 | + | |
| 119 | 155 | $data = [ |
| 120 | - 'credentials' => Utils::get_credentials(), | |
| 121 | - 'common' => [ | |
| 122 | - 'version' => WPMCS_VERSION, | |
| 123 | - 'uploaded' => Counter::get( 'uploaded' ), | |
| 156 | + 'credentials' => Utils::get_credentials( '', [], true ), | |
| 157 | + 'serviceEnabled' => Utils::is_service_enabled(), | |
| 158 | + 'serviceError' => Utils::get_service_configuration_error(), | |
| 159 | + 'common' => [ | |
| 160 | + 'version' => defined('WPMCS_PRO_VERSION') ? WPMCS_PRO_VERSION : WPMCS_VERSION, | |
| 161 | + 'counts' => [ | |
| 162 | + 'all' => Counter::get_count(), | |
| 163 | + 'categorized' => $this->getSortedMediaCounts() | |
| 164 | + ], | |
| 124 | 165 | 'status' => Utils::get_status('', false), |
| 125 | 166 | 'settings' => Utils::get_settings(), |
| 126 | - ] | |
| 167 | + ], | |
| 168 | + 'sync' => Sync::instance()->get_status(), | |
| 169 | + 'license' => defined('WPMCS_PRO_VERSION') ? Utils::get_safe_license_data() : [], | |
| 170 | + 'file_manager' => defined('WPMCS_PRO_VERSION') ? [ | |
| 171 | + 'delete_enabled' => (bool) Utils::get_option('delete_enabled', false, Schema::getConstant('FILE_MANAGER_SETTINGS_KEY')), | |
| 172 | + 'root_folder' => Utils::get_option('root_folder', '', Schema::getConstant('FILE_MANAGER_SETTINGS_KEY')), | |
| 173 | + ] : [], | |
| 127 | 174 | ]; |
| 128 | 175 | |
| 129 | 176 | return new WP_REST_Response( $data, 200 ); |
| 130 | 177 | } |
| @@ -145,8 +192,9 @@ | ||
| 145 | 192 | $service = isset($saveData['service']) ? $saveData['service'] : false; |
| 146 | 193 | $serviceLabel = isset($saveData['serviceLabel']) ? $saveData['serviceLabel'] : ''; |
| 147 | 194 | $cdn = isset($saveData['cdn']) ? $saveData['cdn'] : []; |
| 148 | 195 | $config = isset($saveData['config']) ? $saveData['config'] : false; |
| 196 | + $configSource = isset($saveData['configSource']) ? $saveData['configSource'] : 'database'; | |
| 149 | 197 | $bucketConfig = isset($saveData['bucketConfig']) ? $saveData['bucketConfig'] : []; |
| 150 | 198 | $security = isset($saveData['security']) ? $saveData['security'] : []; |
| 151 | 199 | $settings = isset($saveData['settings']) ? $saveData['settings'] : []; |
| 152 | 200 | |
| @@ -160,13 +208,16 @@ | ||
| 160 | 208 | $updated = Utils::update_option('credentials', $existing, Schema::getConstant('GLOBAL_SETTINGS_KEY')); |
| 161 | 209 | $updatedSettings = Utils::update_option('settings', $settings, Schema::getConstant('GLOBAL_SETTINGS_KEY')); |
| 162 | 210 | |
| 163 | 211 | if(!$updated) $serviceOk = false; |
| 212 | + if(!$updatedSettings) $settingsOk = false; | |
| 164 | 213 | } |
| 165 | 214 | |
| 166 | 215 | |
| 167 | 216 | if($action === 'all' || $action === 'service'){ |
| 168 | - if ( $service === false || empty($config) || empty($bucketConfig)) return new WP_REST_Response( $result, 200 ); | |
| 217 | + // In 'config' source mode the credentials live in the WPMCS_CONFIG constant, | |
| 218 | + // so an empty config payload is expected and must not block the save. | |
| 219 | + if ( $service === false || ($configSource !== 'config' && empty($config)) || empty($bucketConfig)) return new WP_REST_Response( $result, 200 ); | |
| 169 | 220 | |
| 170 | 221 | $updated = Utils::update_option( |
| 171 | 222 | 'credentials', |
| 172 | 223 | [ |
| @@ -172,9 +223,10 @@ | ||
| 172 | 223 | [ |
| 173 | 224 | 'service' => $service, |
| 174 | 225 | 'serviceLabel' => $serviceLabel, |
| 175 | 226 | 'cdn' => $cdn, |
| 176 | - 'config' => $config, | |
| 227 | + 'config' => $configSource === 'config' ? [] : $config, | |
| 228 | + 'configSource' => $configSource, | |
| 177 | 229 | 'bucketConfig' => $bucketConfig, |
| 178 | 230 | 'security' => $security |
| 179 | 231 | ], |
| 180 | 232 | Schema::getConstant('GLOBAL_SETTINGS_KEY') |
| @@ -179,8 +231,21 @@ | ||
| 179 | 231 | ], |
| 180 | 232 | Schema::getConstant('GLOBAL_SETTINGS_KEY') |
| 181 | 233 | ); |
| 182 | 234 | if(!$updated) $serviceOk = false; |
| 235 | + | |
| 236 | + if($serviceOk) { | |
| 237 | + Utils::set_status('cdnRead', [ | |
| 238 | + 'status' => false, | |
| 239 | + 'message' => '', | |
| 240 | + 'lastChecked' => null | |
| 241 | + ]); | |
| 242 | + Utils::set_status('storageCredentials', [ | |
| 243 | + 'status' => false, | |
| 244 | + 'message' => '', | |
| 245 | + 'lastChecked' => null | |
| 246 | + ]); | |
| 247 | + } | |
| 183 | 248 | } |
| 184 | 249 | |
| 185 | 250 | if(($action === 'all' || $action === 'settings')) { |
| 186 | 251 | $updatedSettings = Utils::update_option('settings', $settings, Schema::getConstant('GLOBAL_SETTINGS_KEY')); |
| @@ -186,15 +251,24 @@ | ||
| 186 | 251 | $updatedSettings = Utils::update_option('settings', $settings, Schema::getConstant('GLOBAL_SETTINGS_KEY')); |
| 187 | 252 | if(!$updatedSettings) $settingsOk = false; |
| 188 | 253 | } |
| 189 | 254 | |
| 255 | + // Clear plugin caches and refresh counts after any configuration save. | |
| 256 | + if(in_array($action, ['all', 'service', 'settings', 'cdn'], true)) { | |
| 257 | + if($action === 'all' || $action === 'service' || $action === 'settings') { | |
| 258 | + Integration::instance()->clear_all_meta(false); | |
| 259 | + } | |
| 190 | 260 | |
| 261 | + if($action === 'all' || $action === 'settings') { | |
| 262 | + Utils::clear_all_content_meta(false, false); | |
| 263 | + } | |
| 264 | + | |
| 265 | + Cache::flush_object_cache(); | |
| 266 | + Counter::instance()->fetch_and_update(); | |
| 267 | + } | |
| 268 | + | |
| 269 | + | |
| 191 | 270 | if($serviceOk && $settingsOk) { |
| 192 | - Utils::set_status('cdnRead', [ | |
| 193 | - 'status' => false, | |
| 194 | - 'message' => '', | |
| 195 | - 'lastChecked' => null | |
| 196 | - ]); | |
| 197 | 271 | $result = [ |
| 198 | 272 | 'success' => true, |
| 199 | 273 | 'message' => esc_html__('Configuration saved successfully', 'media-cloud-sync') |
| 200 | 274 | ]; |
| @@ -204,95 +278,407 @@ | ||
| 204 | 278 | } |
| 205 | 279 | |
| 206 | 280 | |
| 207 | 281 | /** |
| 208 | - * Upload Credentials | |
| 209 | - * @since 1.0.0 | |
| 210 | - */ | |
| 211 | - public function uploadCredentials($request) { | |
| 212 | - if (isset($_FILES['file']) && !empty($_FILES['file'])) { | |
| 213 | - $config_file = $_FILES['file']; | |
| 214 | - if (isset($config_file['type']) && $config_file['type'] == 'application/json') { | |
| 215 | - if (file_exists($config_file["tmp_name"])) { | |
| 282 | + * Refresh the Media Counts | |
| 283 | + * | |
| 284 | + */ | |
| 285 | + public function refreshCounts( ) { | |
| 286 | + Cache::flush_object_cache(); | |
| 287 | + Integration::instance()->clear_all_meta(); | |
| 288 | + Counter::instance()->fetch_and_update(); | |
| 216 | 289 | |
| 217 | - add_filter( 'upload_dir', [ $this, 'change_file_upload_dir' ]); | |
| 218 | - add_filter( 'mime_types', [ $this, 'add_custom_mime_type_json' ]); | |
| 290 | + $result = [ | |
| 291 | + 'success' => true, | |
| 292 | + 'counts' => [ | |
| 293 | + 'all' => Counter::get_count(), | |
| 294 | + 'categorized' => $this->getSortedMediaCounts() | |
| 295 | + ], | |
| 296 | + 'message' => esc_html__('Media counts refreshed successfully', 'media-cloud-sync') | |
| 297 | + ]; | |
| 298 | + return new WP_REST_Response( $result, 200 ); | |
| 299 | + } | |
| 219 | 300 | |
| 220 | - if ( ! function_exists( 'wp_handle_upload' ) ) { | |
| 221 | - require_once( ABSPATH . 'wp-admin/includes/file.php' ); | |
| 222 | - } | |
| 223 | 301 | |
| 224 | - $upload_overrides = [ | |
| 225 | - 'test_form' => false, | |
| 226 | - 'test_type' => true, | |
| 227 | - 'mimes' => [ 'json'=>'application/json' ] | |
| 228 | - ]; | |
| 302 | + /** | |
| 303 | + * Start Upgrade | |
| 304 | + * @since 1.3.6 | |
| 305 | + */ | |
| 306 | + public function startUpgrade( $request ) { | |
| 307 | + $result = Upgrade::instance()->start_upgrade(); | |
| 308 | + return new WP_REST_Response( $result, 200 ); | |
| 309 | + } | |
| 229 | 310 | |
| 230 | - $movefile = wp_handle_upload( $config_file, $upload_overrides ); | |
| 231 | 311 | |
| 232 | - remove_filter( 'upload_dir', [ $this, 'change_file_upload_dir' ]); | |
| 233 | - remove_filter( 'mime_types', [ $this, 'add_custom_mime_type_json' ]); | |
| 312 | + /** | |
| 313 | + * Get Upgrade Status | |
| 314 | + * @since 1.3.6 | |
| 315 | + */ public function getUpgradeStatus( $request ) { | |
| 316 | + $result = Upgrade::instance()->get_progress(); | |
| 317 | + return new WP_REST_Response( $result, 200 ); | |
| 318 | + } | |
| 234 | 319 | |
| 235 | - if ($movefile && !isset($movefile['error'])) { | |
| 236 | - $result = [ | |
| 237 | - 'success' => true, | |
| 238 | - 'file_path' => $movefile['file'], | |
| 239 | - 'file_name' => basename($movefile['file']), | |
| 240 | - ]; | |
| 241 | - return new WP_REST_Response($result, 200); | |
| 242 | - } else { | |
| 243 | - $result = [ | |
| 244 | - 'success' => false, | |
| 245 | - 'message' => $movefile['error'], | |
| 246 | - ]; | |
| 247 | - return new WP_REST_Response($result, 200); | |
| 248 | - } | |
| 249 | - } | |
| 250 | - } else { | |
| 251 | - $result = [ | |
| 252 | - 'success' => false, | |
| 253 | - 'message' => esc_html__('Invalid file format', 'media-cloud-sync'), | |
| 254 | - ]; | |
| 255 | - return new WP_REST_Response($result, 200); | |
| 256 | - } | |
| 257 | - } else { | |
| 258 | - $result = [ | |
| 259 | - 'success' => false, | |
| 260 | - 'message' => esc_html__('Insufficient data. Please try again', 'media-cloud-sync'), | |
| 320 | + | |
| 321 | + /** | |
| 322 | + * Get Media Error Logs | |
| 323 | + * @since 1.2.13 | |
| 324 | + */ | |
| 325 | + public function getErrorLogs( $request ) { | |
| 326 | + $params = $request->get_params(); | |
| 327 | + $type = isset($params['type']) ? $params['type'] : 'all'; | |
| 328 | + $page = isset($params['page']) ? $params['page'] : null; | |
| 329 | + $per_page = isset($params['per_page']) ? $params['per_page'] : null; | |
| 330 | + | |
| 331 | + if($type !== 'all') { | |
| 332 | + $logs = Logger::instance()->get_logs_by_type($type, $page, $per_page); | |
| 333 | + } else { | |
| 334 | + $logs = Logger::instance()->get_all_logs($page, $per_page); | |
| 335 | + } | |
| 336 | + | |
| 337 | + $result = [ | |
| 338 | + 'success' => true, | |
| 339 | + 'logs' => $logs, | |
| 340 | + 'message' => esc_html__('Error logs fetched successfully', 'media-cloud-sync') | |
| 341 | + ]; | |
| 342 | + return new WP_REST_Response( $result, 200 ); | |
| 343 | + } | |
| 344 | + | |
| 345 | + | |
| 346 | + /** | |
| 347 | + * Clear all error logs for a sync type (e.g. stale entries from earlier failed attempts). | |
| 348 | + * @since 1.3.13 | |
| 349 | + */ | |
| 350 | + public function clearErrorLogs( $request ) { | |
| 351 | + $type = isset($request->get_params()['type']) ? $request->get_params()['type'] : ''; | |
| 352 | + | |
| 353 | + if (empty($type) || !Sync::instance()->get_class_by_action($type)) { | |
| 354 | + return new WP_REST_Response( [ | |
| 355 | + 'success' => false, | |
| 356 | + 'message' => esc_html__('Invalid action', 'media-cloud-sync') | |
| 357 | + ], 200 ); | |
| 358 | + } | |
| 359 | + | |
| 360 | + Logger::instance()->remove_logs_by_type($type); | |
| 361 | + | |
| 362 | + $result = [ | |
| 363 | + 'success' => true, | |
| 364 | + 'message' => esc_html__('Error logs cleared', 'media-cloud-sync') | |
| 365 | + ]; | |
| 366 | + return new WP_REST_Response( $result, 200 ); | |
| 367 | + } | |
| 368 | + | |
| 369 | + | |
| 370 | + /** | |
| 371 | + * Get Status | |
| 372 | + * @since 1.2.13 | |
| 373 | + */ | |
| 374 | + public function getStatus( $request ) { | |
| 375 | + // Fetch and update media counts for make sure they are up to date | |
| 376 | + $action = isset($request->get_params()['action']) ? $request->get_params()['action'] : 'all'; | |
| 377 | + $status = Sync::instance()->get_status(); | |
| 378 | + | |
| 379 | + if($action !== 'all') { | |
| 380 | + $status = isset($status[$action]) ? $status[$action] : []; | |
| 381 | + } | |
| 382 | + | |
| 383 | + $result = [ | |
| 384 | + 'success' => true, | |
| 385 | + 'status' => $status, | |
| 386 | + 'counts' => [ | |
| 387 | + 'all' => Counter::get_count(), | |
| 388 | + 'categorized' => $this->getSortedMediaCounts() | |
| 389 | + ], | |
| 390 | + 'message' => esc_html__('Status fetched successfully', 'media-cloud-sync') | |
| 391 | + ]; | |
| 392 | + | |
| 393 | + return new WP_REST_Response( $result, 200 ); | |
| 394 | + } | |
| 395 | + | |
| 396 | + | |
| 397 | + /** | |
| 398 | + * Start Sync | |
| 399 | + * @since 1.2.13 | |
| 400 | + */ | |
| 401 | + public function startSync( $data ) { | |
| 402 | + $action = isset($data->get_params()['action']) ? $data->get_params()['action'] : ''; | |
| 403 | + if ( ! Sync::instance()->get_class_by_action( $action ) ) { | |
| 404 | + $result = [ | |
| 405 | + 'success' => false, | |
| 406 | + 'message' => esc_html__('Invalid action', 'media-cloud-sync') | |
| 261 | 407 | ]; |
| 262 | - return new WP_REST_Response($result, 200); | |
| 263 | - } | |
| 264 | - } | |
| 408 | + return new WP_REST_Response( $result, 200 ); | |
| 409 | + } | |
| 410 | + if(!Utils::is_service_enabled()) { | |
| 411 | + $result = [ | |
| 412 | + 'success' => false, | |
| 413 | + 'message' => Utils::get_service_configuration_error() ?: esc_html__('Storage is not configured correctly.', 'media-cloud-sync') | |
| 414 | + ]; | |
| 415 | + return new WP_REST_Response( $result, 200 ); | |
| 416 | + } | |
| 417 | + $status = Sync::instance()->start($action); | |
| 265 | 418 | |
| 419 | + // Sync::start() passes a job's own precondition-failure array straight through | |
| 420 | + // (e.g. no destination configured) instead of masking it as a started status. | |
| 421 | + if (is_array($status) && isset($status['success']) && $status['success'] === false) { | |
| 422 | + return new WP_REST_Response( $status, 200 ); | |
| 423 | + } | |
| 424 | + | |
| 425 | + $result = [ | |
| 426 | + 'success' => true, | |
| 427 | + 'status' => $status, | |
| 428 | + 'message' => esc_html__('Sync started successfully', 'media-cloud-sync') | |
| 429 | + ]; | |
| 430 | + return new WP_REST_Response( $result, 200 ); | |
| 431 | + } | |
| 432 | + | |
| 433 | + | |
| 266 | 434 | /** |
| 267 | - * Change File Upload Directory | |
| 268 | - * @since 1.0.0 | |
| 269 | - */ | |
| 270 | - public function change_file_upload_dir($upload) { | |
| 271 | - $wpmcs_path = '/' . Schema::getConstant('UPLOADS'); | |
| 272 | - $path = $upload['basedir'] . $wpmcs_path; | |
| 273 | - $url = $upload['baseurl'] . $wpmcs_path; | |
| 435 | + * Pause Sync | |
| 436 | + * @since 1.2.13 | |
| 437 | + */ | |
| 438 | + public function pauseSync( $data ) { | |
| 439 | + $action = isset($data->get_params()['action']) ? $data->get_params()['action'] : ''; | |
| 440 | + if ( ! Sync::instance()->get_class_by_action( $action ) ) { | |
| 441 | + $result = [ | |
| 442 | + 'success' => false, | |
| 443 | + 'message' => esc_html__('Invalid action', 'media-cloud-sync') | |
| 444 | + ]; | |
| 445 | + return new WP_REST_Response( $result, 200 ); | |
| 446 | + } | |
| 447 | + $status = Sync::instance()->pause($action); | |
| 274 | 448 | |
| 275 | - if (!is_dir($path)) { | |
| 276 | - do_action( $this->token.'_create_plugin_dir' ); | |
| 277 | - } | |
| 449 | + $result = [ | |
| 450 | + 'success' => true, | |
| 451 | + 'status' => $status, | |
| 452 | + 'message' => esc_html__('Sync paused successfully', 'media-cloud-sync') | |
| 453 | + ]; | |
| 454 | + return new WP_REST_Response( $result, 200 ); | |
| 455 | + } | |
| 278 | 456 | |
| 279 | - $upload['subdir'] = $wpmcs_path; | |
| 280 | - $upload['path'] = $upload['basedir'] . $wpmcs_path; | |
| 281 | - $upload['url'] = $upload['baseurl'] . $wpmcs_path; | |
| 282 | 457 | |
| 283 | - return $upload; | |
| 284 | - } | |
| 458 | + /** | |
| 459 | + * Save the sync method ('ajax'|'cron'|'mixed') preference for a sync type. | |
| 460 | + * @since 1.3.13 | |
| 461 | + */ | |
| 462 | + public function setSyncMethod( $data ) { | |
| 463 | + $params = $data->get_params(); | |
| 464 | + $action = isset($params['action']) ? sanitize_text_field($params['action']) : ''; | |
| 465 | + $method = isset($params['sync_method']) ? sanitize_text_field($params['sync_method']) : ''; | |
| 285 | 466 | |
| 286 | - /** | |
| 287 | - * Add Custom Mime Type JSON | |
| 288 | - * @since 1.0.0 | |
| 289 | - */ | |
| 290 | - public function add_custom_mime_type_json($mimes) { | |
| 291 | - $mimes['json'] = 'application/json'; | |
| 292 | - // Return the array back to the function with our added mime type. | |
| 293 | - return $mimes; | |
| 294 | - } | |
| 467 | + if ( ! Sync::instance()->get_class_by_action( $action ) ) { | |
| 468 | + return new WP_REST_Response( [ | |
| 469 | + 'success' => false, | |
| 470 | + 'message' => esc_html__('Invalid action', 'media-cloud-sync') | |
| 471 | + ], 200 ); | |
| 472 | + } | |
| 473 | + | |
| 474 | + $saved = Sync::instance()->set_sync_method($action, $method); | |
| 475 | + if(!$saved) { | |
| 476 | + return new WP_REST_Response( [ | |
| 477 | + 'success' => false, | |
| 478 | + 'message' => esc_html__('Invalid sync method', 'media-cloud-sync') | |
| 479 | + ], 200 ); | |
| 480 | + } | |
| 481 | + | |
| 482 | + $result = [ | |
| 483 | + 'success' => true, | |
| 484 | + 'sync_method' => $method, | |
| 485 | + 'status' => Sync::instance()->get_class_by_action($action)->get_status(), | |
| 486 | + ]; | |
| 487 | + return new WP_REST_Response( $result, 200 ); | |
| 488 | + } | |
| 489 | + | |
| 490 | + /** | |
| 491 | + * Resume Sync | |
| 492 | + * @since 1.2.13 | |
| 493 | + */ | |
| 494 | + public function resumeSync( $data ) { | |
| 495 | + $action = isset($data->get_params()['action']) ? $data->get_params()['action'] : ''; | |
| 496 | + if ( ! Sync::instance()->get_class_by_action( $action ) ) { | |
| 497 | + $result = [ | |
| 498 | + 'success' => false, | |
| 499 | + 'message' => esc_html__('Invalid action', 'media-cloud-sync') | |
| 500 | + ]; | |
| 501 | + return new WP_REST_Response( $result, 200 ); | |
| 502 | + } | |
| 503 | + $status = Sync::instance()->resume($action); | |
| 504 | + | |
| 505 | + $result = [ | |
| 506 | + 'success' => true, | |
| 507 | + 'status' => $status, | |
| 508 | + 'message' => esc_html__('Sync resumed successfully', 'media-cloud-sync') | |
| 509 | + ]; | |
| 510 | + return new WP_REST_Response( $result, 200 ); | |
| 511 | + } | |
| 512 | + | |
| 513 | + | |
| 514 | + /** | |
| 515 | + * Stop Sync | |
| 516 | + * @since 1.2.13 | |
| 517 | + */ | |
| 518 | + public function stopSync( $data ) { | |
| 519 | + $action = isset($data->get_params()['action']) ? $data->get_params()['action'] : ''; | |
| 520 | + if ( ! Sync::instance()->get_class_by_action( $action ) ) { | |
| 521 | + $result = [ | |
| 522 | + 'success' => false, | |
| 523 | + 'message' => esc_html__('Invalid action', 'media-cloud-sync') | |
| 524 | + ]; | |
| 525 | + return new WP_REST_Response( $result, 200 ); | |
| 526 | + } | |
| 527 | + $status = Sync::instance()->stop($action); | |
| 528 | + | |
| 529 | + $result = [ | |
| 530 | + 'success' => true, | |
| 531 | + 'status' => $status, | |
| 532 | + 'counts' => [ | |
| 533 | + 'all' => Counter::get_count(), | |
| 534 | + 'categorized' => $this->getSortedMediaCounts() | |
| 535 | + ], | |
| 536 | + 'message' => esc_html__('Sync stopped successfully', 'media-cloud-sync') | |
| 537 | + ]; | |
| 538 | + return new WP_REST_Response( $result, 200 ); | |
| 539 | + } | |
| 540 | + | |
| 541 | + | |
| 542 | + /** | |
| 543 | + * Tick Sync — process a small batch synchronously, driving Ajax/Mixed sync mode. | |
| 544 | + * @since 1.3.13 | |
| 545 | + */ | |
| 546 | + public function tickSync( $data ) { | |
| 547 | + $params = $data->get_params(); | |
| 548 | + $action = isset($params['action']) ? $params['action'] : ''; | |
| 549 | + | |
| 550 | + if ( ! Sync::instance()->get_class_by_action( $action ) ) { | |
| 551 | + $result = [ | |
| 552 | + 'success' => false, | |
| 553 | + 'message' => esc_html__('Invalid action', 'media-cloud-sync') | |
| 554 | + ]; | |
| 555 | + return new WP_REST_Response( $result, 200 ); | |
| 556 | + } | |
| 557 | + | |
| 558 | + // Defense in depth — the frontend should never call this in free/unlicensed mode, but | |
| 559 | + // the endpoint must not trust that, and must not silently no-op: an unlicensed call | |
| 560 | + // needs a clear rejection so the frontend's tick loop stops instead of retrying forever. | |
| 561 | + if ( ! Utils::is_pro_licensed() ) { | |
| 562 | + $result = [ | |
| 563 | + 'success' => false, | |
| 564 | + 'message' => esc_html__('Ajax sync requires an active Pro license', 'media-cloud-sync') | |
| 565 | + ]; | |
| 566 | + return new WP_REST_Response( $result, 200 ); | |
| 567 | + } | |
| 568 | + | |
| 569 | + // Clamp regardless of what the frontend sends — the endpoint itself must enforce the | |
| 570 | + // ceiling, since a batch of slow uploads processed sequentially in one request risks | |
| 571 | + // exceeding reverse-proxy/webserver timeouts that process_iterations() can't see. | |
| 572 | + $batch = isset($params['batch']) ? (int) $params['batch'] : 5; | |
| 573 | + $batch = min(10, max(1, $batch)); | |
| 574 | + | |
| 575 | + $status = Sync::instance()->tick($action, $batch); | |
| 576 | + | |
| 577 | + $result = [ | |
| 578 | + 'success' => true, | |
| 579 | + 'status' => $status, | |
| 580 | + 'message' => esc_html__('Sync ticked successfully', 'media-cloud-sync') | |
| 581 | + ]; | |
| 582 | + | |
| 583 | + // Deliberately omitted on every tick (this endpoint may be hit ~once/second while a | |
| 584 | + // sync runs, and Counter::get_count() is a real query) — but the Dashboard's own | |
| 585 | + // Statistics widget reads a *different* redux slice (common.counts) than the Sync | |
| 586 | + // screen's progress (sync[action]), only refreshed via the slower ~2-3s useSyncPolling | |
| 587 | + // cadence. That's an acceptable few-seconds lag while a sync is actively running, but | |
| 588 | + // right when it finishes it becomes a visibly wrong, stuck-looking number on the | |
| 589 | + // Dashboard until the next poll happens to catch up. Only pay for the query at that | |
| 590 | + // one transition, not on every tick. | |
| 591 | + if ( isset( $status['status'] ) && $status['status'] !== 'running' ) { | |
| 592 | + $result['counts'] = [ | |
| 593 | + 'all' => Counter::get_count(), | |
| 594 | + 'categorized' => $this->getSortedMediaCounts() | |
| 595 | + ]; | |
| 596 | + } | |
| 597 | + return new WP_REST_Response( $result, 200 ); | |
| 598 | + } | |
| 599 | + | |
| 600 | + | |
| 601 | + /** | |
| 602 | + * Retry a Media Sync Error | |
| 603 | + */ | |
| 604 | + public function retrySingle( $data ) { | |
| 605 | + $params = $data->get_params(); | |
| 606 | + $id = isset($params['id']) ? $params['id'] : ''; | |
| 607 | + $source_type = isset($params['source_type']) ? $params['source_type'] : 'media_library'; | |
| 608 | + $type = isset($params['type']) ? $params['type'] : ''; | |
| 609 | + $status = false; | |
| 610 | + if(empty($id) || empty($source_type) || empty($type)) { | |
| 611 | + $result = [ | |
| 612 | + 'success' => false, | |
| 613 | + 'message' => esc_html__('Invalid parameters', 'media-cloud-sync') | |
| 614 | + ]; | |
| 615 | + return new WP_REST_Response( $result, 200 ); | |
| 616 | + } | |
| 617 | + // Retry Sync | |
| 618 | + $handler = Sync::instance()->get_class_by_action($type); | |
| 619 | + if(!$handler) { | |
| 620 | + $result = [ | |
| 621 | + 'success' => false, | |
| 622 | + 'message' => esc_html__('Invalid action', 'media-cloud-sync') | |
| 623 | + ]; | |
| 624 | + return new WP_REST_Response( $result, 200 ); | |
| 625 | + } | |
| 626 | + if(!method_exists($handler, 'retry_single')) { | |
| 627 | + $result = [ | |
| 628 | + 'success' => false, | |
| 629 | + 'message' => esc_html__('Retry is not available for this sync type.', 'media-cloud-sync') | |
| 630 | + ]; | |
| 631 | + return new WP_REST_Response( $result, 200 ); | |
| 632 | + } | |
| 633 | + $handler->retry_single($id, $source_type); | |
| 634 | + | |
| 635 | + $status = Utils::is_empty(Logger::instance()->get_log($type, $id, $source_type)) ? true : false; | |
| 636 | + | |
| 637 | + $result = [ | |
| 638 | + 'success' => $status, | |
| 639 | + 'message' => $status ? esc_html__('Media synced successfully', 'media-cloud-sync') : esc_html__('Media sync failed', 'media-cloud-sync') | |
| 640 | + ]; | |
| 641 | + return new WP_REST_Response( $result, 200 ); | |
| 642 | + } | |
| 643 | + | |
| 644 | + | |
| 645 | + /** | |
| 646 | + * Get Sorted Media Counts | |
| 647 | + * Sorts media counts by source type | |
| 648 | + * @since 1.2.13 | |
| 649 | + */ | |
| 650 | + private function getSortedMediaCounts() { | |
| 651 | + $sorted_counts = []; | |
| 652 | + $detailed_counts = Counter::get_count( 'all', false ); | |
| 653 | + if(!empty($detailed_counts)) { | |
| 654 | + $source_labels = Integration::$source_labels; | |
| 655 | + if(!empty($source_labels)) { | |
| 656 | + $prefixes = array_keys($source_labels); | |
| 657 | + foreach($detailed_counts as $source_type => $counts) { | |
| 658 | + $found_prefix = false; | |
| 659 | + foreach($prefixes as $prefix) { | |
| 660 | + $found_prefix = strpos($source_type, $prefix) === 0 ? $prefix : false; | |
| 661 | + if($found_prefix !== false) break; | |
| 662 | + } | |
| 663 | + | |
| 664 | + if($found_prefix !== false) { | |
| 665 | + $sorted_counts[$found_prefix] = [ | |
| 666 | + 'label' => $source_labels[$found_prefix], | |
| 667 | + 'uploaded' => isset( $sorted_counts[ $found_prefix ]['uploaded'] ) | |
| 668 | + ? $sorted_counts[ $found_prefix ]['uploaded'] + $counts['uploaded'] | |
| 669 | + : $counts['uploaded'], | |
| 670 | + 'total' => isset( $sorted_counts[ $found_prefix ]['total'] ) | |
| 671 | + ? $sorted_counts[ $found_prefix ]['total'] + $counts['total'] | |
| 672 | + : $counts['total'] | |
| 673 | + ]; | |
| 674 | + } | |
| 675 | + } | |
| 676 | + } | |
| 677 | + } | |
| 678 | + | |
| 679 | + return $sorted_counts; | |
| 680 | + } | |
| 295 | 681 | |
| 296 | 682 | |
| 297 | 683 | /** |
| 298 | 684 | * Helper function to create Adding Route |