dashboard.js
791 lines
| 1 | /* |
| 2 | Plugin Name: Media Cleaner |
| 3 | Description: Clean your Media Library and Uploads Folder. |
| 4 | Author: Jordy Meow |
| 5 | */ |
| 6 | |
| 7 | function wpmc_pop_array(items, count) { |
| 8 | var newItems = []; |
| 9 | while ( newItems.length < count && items.length > 0 ) { |
| 10 | newItems.push( items.pop() ); |
| 11 | } |
| 12 | return newItems; |
| 13 | } |
| 14 | |
| 15 | /** |
| 16 | * |
| 17 | * RECOVER |
| 18 | * |
| 19 | */ |
| 20 | |
| 21 | function wpmc_recover() { |
| 22 | var items = []; |
| 23 | jQuery('#wpmc-table input:checked').each(function (index) { |
| 24 | if (jQuery(this)[0].value != 'on') { |
| 25 | items.push(jQuery(this)[0].value); |
| 26 | } |
| 27 | }); |
| 28 | wpmc_recover_do(items, items.length); |
| 29 | } |
| 30 | |
| 31 | function wpmc_recover_all() { |
| 32 | var items = []; |
| 33 | var data = { action: 'wpmc_get_all_deleted' }; |
| 34 | jQuery.post(ajaxurl, data, function (response) { |
| 35 | reply = jQuery.parseJSON(response); |
| 36 | if ( !reply.success ) { |
| 37 | alert( reply.message ); |
| 38 | return; |
| 39 | } |
| 40 | wpmc_recover_do(reply.results.ids, reply.results.ids.length); |
| 41 | }); |
| 42 | } |
| 43 | |
| 44 | function wpmc_recover_do(items, totalcount) { |
| 45 | wpmc_update_progress(totalcount - items.length, totalcount); |
| 46 | if (items.length > 0) { |
| 47 | newItems = wpmc_pop_array(items, 5); |
| 48 | data = { action: 'wpmc_recover_do', data: newItems }; |
| 49 | } |
| 50 | else { |
| 51 | jQuery('#wpmc_pause').hide(); |
| 52 | jQuery('#wpmc_progression').html("Done. Please <a href='?page=media-cleaner'>refresh</a> this page."); |
| 53 | return; |
| 54 | } |
| 55 | jQuery.post(ajaxurl, data, function (response) { |
| 56 | reply = jQuery.parseJSON(response); |
| 57 | if ( !reply.success ) { |
| 58 | alert( reply.message ); |
| 59 | return; |
| 60 | } |
| 61 | wpmc_recover_do(items, totalcount); |
| 62 | }); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * |
| 67 | * DELETE |
| 68 | * |
| 69 | */ |
| 70 | |
| 71 | function wpmc_ignore() { |
| 72 | var items = []; |
| 73 | jQuery('#wpmc-table input:checked').each(function (index) { |
| 74 | if (jQuery(this)[0].value != 'on') { |
| 75 | items.push(jQuery(this)[0].value); |
| 76 | } |
| 77 | }); |
| 78 | wpmc_ignore_do(items, items.length); |
| 79 | } |
| 80 | |
| 81 | function wpmc_delete() { |
| 82 | var items = []; |
| 83 | jQuery('#wpmc-table input:checked').each(function (index) { |
| 84 | if (jQuery(this)[0].value != 'on') { |
| 85 | items.push(jQuery(this)[0].value); |
| 86 | } |
| 87 | }); |
| 88 | wpmc_delete_do(items, items.length); |
| 89 | } |
| 90 | |
| 91 | function wpmc_delete_all(isTrash, filter = '') { |
| 92 | var items = []; |
| 93 | var data = { |
| 94 | action: 'wpmc_get_all_issues', |
| 95 | isTrash: isTrash ? 1 : 0, |
| 96 | s: filter |
| 97 | }; |
| 98 | |
| 99 | jQuery.post(ajaxurl, data, function (response) { |
| 100 | reply = jQuery.parseJSON(response); |
| 101 | if ( !reply.success ) { |
| 102 | alert( reply.message ); |
| 103 | return; |
| 104 | } |
| 105 | wpmc_delete_do(reply.results.ids, reply.results.ids.length); |
| 106 | }); |
| 107 | } |
| 108 | |
| 109 | function wpmc_update_progress(current, totalcount, isDeleting) { |
| 110 | if (isDeleting === undefined) |
| 111 | isDeleting = false; |
| 112 | var action = isDeleting ? "Deleting" : "Analyzing"; |
| 113 | jQuery('#wpmc_progression').html('<span class="dashicons dashicons-controls-play"></span> ' + action + ' ' + current + "/" + totalcount + " (" + Math.round(current / totalcount * 100) + "%)"); |
| 114 | } |
| 115 | |
| 116 | function wpmc_delete_do(items, totalcount) { |
| 117 | wpmc_update_progress(totalcount - items.length, totalcount, true); |
| 118 | if (items.length > 0) { |
| 119 | newItems = wpmc_pop_array(items, 5); |
| 120 | data = { action: 'wpmc_delete_do', data: newItems }; |
| 121 | } |
| 122 | else { |
| 123 | jQuery('#wpmc_progression').html("Done. Please <a href='?page=media-cleaner'>refresh</a> this page."); |
| 124 | return; |
| 125 | } |
| 126 | jQuery.post(ajaxurl, data, function (response) { |
| 127 | reply = jQuery.parseJSON(response); |
| 128 | if ( !reply.success ) { |
| 129 | alert( reply.message ); |
| 130 | return; |
| 131 | } |
| 132 | wpmc_delete_do(items, totalcount); |
| 133 | }); |
| 134 | } |
| 135 | |
| 136 | function wpmc_ignore_do(items, totalcount) { |
| 137 | wpmc_update_progress(totalcount - items.length, totalcount); |
| 138 | if (items.length > 0) { |
| 139 | newItems = wpmc_pop_array(items, 5); |
| 140 | data = { action: 'wpmc_ignore_do', data: newItems }; |
| 141 | } |
| 142 | else { |
| 143 | jQuery('#wpmc_progression').html("Done. Please <a href='?page=media-cleaner'>refresh</a> this page."); |
| 144 | return; |
| 145 | } |
| 146 | jQuery.post(ajaxurl, data, function (response) { |
| 147 | reply = jQuery.parseJSON(response); |
| 148 | if ( !reply.success ) { |
| 149 | alert( reply.message ); |
| 150 | return; |
| 151 | } |
| 152 | wpmc_ignore_do(items, totalcount); |
| 153 | }); |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * |
| 158 | * SCAN |
| 159 | * |
| 160 | */ |
| 161 | |
| 162 | var wpmc = wpmc_new_context(); |
| 163 | |
| 164 | /** |
| 165 | * Creates a context object that preserves various states and variables for scanning |
| 166 | * @return {object} |
| 167 | */ |
| 168 | function wpmc_new_context() { |
| 169 | return { |
| 170 | dirs: [], |
| 171 | files: [], |
| 172 | medias: [], |
| 173 | current: 0, |
| 174 | total: 0, |
| 175 | issues: 0, |
| 176 | isPause: false, |
| 177 | isPendingPause: false, |
| 178 | currentPhase: null, |
| 179 | |
| 180 | phases: { |
| 181 | preparePosts: { |
| 182 | init: function () { |
| 183 | this.progress = 0; // Scanned posts count |
| 184 | this.progressPrev = 0; |
| 185 | return this; |
| 186 | }, |
| 187 | run: function () { |
| 188 | wpmc_prepare(); |
| 189 | }, |
| 190 | pause: function () { |
| 191 | jQuery('#wpmc_progression').html('<span class="dashicons dashicons-controls-pause"></span> Paused at preparing posts (' + this.progress + ' posts)'); |
| 192 | }, |
| 193 | skip: function () { |
| 194 | this.progress += wpmc_cfg.postsBuffer; |
| 195 | }, |
| 196 | nextPhase: function () { |
| 197 | if (wpmc_cfg.scanMedia) |
| 198 | return wpmc.phases.prepareMedia; |
| 199 | if (wpmc_cfg.scanFiles) |
| 200 | return wpmc.phases.prepareFiles; |
| 201 | console.error('Configuration Error'); // This shouldn't happen |
| 202 | } |
| 203 | }, |
| 204 | prepareFiles: { |
| 205 | init: function () { |
| 206 | this.progress = null; // The last scanned directory |
| 207 | this.progressPrev = null; |
| 208 | return this; |
| 209 | }, |
| 210 | run: function () { |
| 211 | wpmc_scan_type('files', this.progress); |
| 212 | }, |
| 213 | pause: function () { |
| 214 | jQuery('#wpmc_progression').html('<span class="dashicons dashicons-controls-pause"></span> Paused at preparing files (' + this.progress + ')'); |
| 215 | }, |
| 216 | skip: function () { |
| 217 | var dir = wpmc.dirs.pop(); |
| 218 | if (dir) { |
| 219 | wpmc.currentPhase.progressPrev = wpmc.currentPhase.progress; |
| 220 | wpmc.currentPhase.progress = dir; |
| 221 | } |
| 222 | else |
| 223 | wpmc.currentPhase = this.nextPhase().init(); |
| 224 | }, |
| 225 | nextPhase: function () { |
| 226 | return wpmc.phases.analyze; |
| 227 | } |
| 228 | }, |
| 229 | prepareMedia: { |
| 230 | init: function () { |
| 231 | this.progress = 0; // Scanned media count |
| 232 | this.progressPrev = 0; |
| 233 | return this; |
| 234 | }, |
| 235 | run: function () { |
| 236 | wpmc_scan_type('medias', null, this.progress); |
| 237 | }, |
| 238 | pause: function () { |
| 239 | jQuery('#wpmc_progression').html('<span class="dashicons dashicons-controls-pause"></span> Paused at preparing media (' + this.progress + ' media)'); |
| 240 | }, |
| 241 | skip: function () { |
| 242 | this.progress += wpmc_cfg.mediasBuffer; |
| 243 | }, |
| 244 | nextPhase: function () { |
| 245 | return wpmc.phases.analyze; |
| 246 | } |
| 247 | }, |
| 248 | analyze: { |
| 249 | init: function () { |
| 250 | this.currentFiles = []; |
| 251 | this.currentMedia = []; |
| 252 | return this; |
| 253 | }, |
| 254 | run: function () { |
| 255 | wpmc_scan_do(); |
| 256 | }, |
| 257 | pause: function () { |
| 258 | var current = wpmc.total - (wpmc.files.length + wpmc.medias.length); |
| 259 | var totalcount = wpmc.total; |
| 260 | jQuery('#wpmc_progression').html('<span class="dashicons dashicons-controls-pause"></span> Paused at ' + current + "/" + totalcount + " (" + Math.round(current / totalcount * 100) + "%)"); |
| 261 | }, |
| 262 | skip: function () { |
| 263 | if (wpmc.files.length) |
| 264 | this.currentFiles = wpmc_pop_array(wpmc.files, wpmc_cfg.analysisBuffer); |
| 265 | if (wpmc.medias.lenght) |
| 266 | this.currentMedia = wpmc_pop_array(wpmc.medias, wpmc_cfg.analysisBuffer); |
| 267 | }, |
| 268 | rollback: function () { |
| 269 | wpmc.files = wpmc.files.concat(this.currentFiles.reverse()); // @see wpmc_pop_array |
| 270 | this.currentFiles = []; |
| 271 | wpmc.medias = wpmc.medias.concat(this.currentMedia.reverse()); // @see wpmc_pop_array |
| 272 | this.currentMedia = []; |
| 273 | } |
| 274 | } |
| 275 | } |
| 276 | }; |
| 277 | } |
| 278 | |
| 279 | // WPMC GET INITIAL INFO |
| 280 | |
| 281 | function wpmc_scan_type_finished() { |
| 282 | |
| 283 | } |
| 284 | |
| 285 | function wpmc_scan_type_next(type, path) { |
| 286 | |
| 287 | } |
| 288 | |
| 289 | function wpmc_prepare() { |
| 290 | if (!wpmc.currentPhase) return; // Aborted |
| 291 | |
| 292 | if (wpmc.isPendingPause) |
| 293 | return wpmc_update_to_pause(); |
| 294 | |
| 295 | setTimeout( |
| 296 | function() { |
| 297 | if (!wpmc.currentPhase) return; // Aborted |
| 298 | |
| 299 | jQuery('#wpmc_progression').html('<span class="dashicons dashicons-portfolio"></span> Preparing posts (' + wpmc.currentPhase.progress + ' posts)...'); |
| 300 | |
| 301 | jQuery.ajax({ |
| 302 | type: 'POST', |
| 303 | url: ajaxurl, |
| 304 | dataType: 'text', |
| 305 | data: { |
| 306 | action: 'wpmc_prepare_do', |
| 307 | limit: wpmc.currentPhase.progress |
| 308 | }, |
| 309 | timeout: wpmc_cfg.timeout + 5000 // Extra 5sec for fail-safe |
| 310 | |
| 311 | }).done(function (response) { |
| 312 | if (!wpmc.currentPhase) return; // Aborted |
| 313 | |
| 314 | var reply = wpmc_parse_response(response); |
| 315 | |
| 316 | if (!reply.success) |
| 317 | return wpmc_handle_error(reply.message); |
| 318 | |
| 319 | if (!reply.finished) { |
| 320 | wpmc.currentPhase.progressPrev = wpmc.currentPhase.progress; |
| 321 | wpmc.currentPhase.progress = reply.limit; |
| 322 | } |
| 323 | else wpmc.currentPhase = wpmc.currentPhase.nextPhase().init(); |
| 324 | |
| 325 | return wpmc.currentPhase.run(); |
| 326 | |
| 327 | }).fail(function (e) { // Server Error |
| 328 | wpmc_handle_error(e.statusText, e.status); |
| 329 | }); |
| 330 | |
| 331 | }, wpmc_cfg.delay |
| 332 | ); |
| 333 | } |
| 334 | |
| 335 | function wpmc_scan_type(type, path = null, limit = 0) { |
| 336 | if (!wpmc.currentPhase) return; // Aborted |
| 337 | |
| 338 | if (wpmc.isPendingPause) |
| 339 | return wpmc_update_to_pause(); |
| 340 | |
| 341 | if (path) { |
| 342 | elpath = path.replace(/^.*[\\\/]/, ''); |
| 343 | jQuery('#wpmc_progression').html('<span class="dashicons dashicons-portfolio"></span> Preparing files (' + elpath + ')...'); |
| 344 | } |
| 345 | else if (type === 'medias') |
| 346 | jQuery('#wpmc_progression').html('<span class="dashicons dashicons-admin-media"></span> Preparing medias (' + limit + ' medias)...'); |
| 347 | else |
| 348 | jQuery('#wpmc_progression').html('<span class="dashicons dashicons-portfolio"></span> Preparing files...'); |
| 349 | |
| 350 | setTimeout( |
| 351 | function() { |
| 352 | jQuery.ajax({ |
| 353 | type: 'POST', |
| 354 | url: ajaxurl, |
| 355 | dataType: 'text', |
| 356 | data: { |
| 357 | action: 'wpmc_scan', |
| 358 | medias: type === 'medias', |
| 359 | files: type === 'files', |
| 360 | path: path, |
| 361 | limit: limit |
| 362 | }, |
| 363 | timeout: wpmc_cfg.timeout + 5000 // Extra 5sec for fail-safe |
| 364 | |
| 365 | }).done(function (response) { |
| 366 | if (!wpmc.currentPhase) return; // Aborted |
| 367 | |
| 368 | var reply = wpmc_parse_response(response); |
| 369 | |
| 370 | if (!reply.success) |
| 371 | return wpmc_handle_error(reply.message); |
| 372 | |
| 373 | // Store results |
| 374 | for (var i = 0, len = reply.results.length; i < len; i++) { |
| 375 | var r = reply.results[i]; |
| 376 | if (type === 'files') { |
| 377 | if ( r.type === 'dir' ) |
| 378 | wpmc.dirs.push( r.path ); |
| 379 | else if ( r.type === 'file' ) { |
| 380 | wpmc.files.push( r.path ); |
| 381 | wpmc.total++; |
| 382 | } |
| 383 | } |
| 384 | else if (type === 'medias') { |
| 385 | wpmc.medias.push( r ); |
| 386 | wpmc.total++; |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | // Next query |
| 391 | if (type === 'medias') { |
| 392 | if (wpmc_cfg.scanFiles || !reply.finished) { |
| 393 | wpmc.currentPhase.progressPrev = wpmc.currentPhase.progress; |
| 394 | wpmc.currentPhase.progress = reply.limit; |
| 395 | } |
| 396 | else |
| 397 | wpmc.currentPhase = wpmc.currentPhase.nextPhase().init(); |
| 398 | } |
| 399 | else if (type === 'files') { |
| 400 | var dir = wpmc.dirs.pop(); |
| 401 | if (dir) { |
| 402 | wpmc.currentPhase.progressPrev = wpmc.currentPhase.progress; |
| 403 | wpmc.currentPhase.progress = dir; |
| 404 | } |
| 405 | else |
| 406 | wpmc.currentPhase = wpmc.currentPhase.nextPhase().init(); |
| 407 | } |
| 408 | wpmc.currentPhase.run(); |
| 409 | |
| 410 | }).fail(function (e) { // Server Error |
| 411 | wpmc_handle_error(e.statusText, e.status); |
| 412 | }); |
| 413 | |
| 414 | }, wpmc_cfg.delay |
| 415 | ); |
| 416 | } |
| 417 | |
| 418 | function wpmc_pause() { |
| 419 | if (wpmc.isPause) { // Resume |
| 420 | jQuery('#wpmc_pause').html('<span style="top: 3px; position: relative; left: -5px;" class="dashicons dashicons-controls-pause"></span>Pause'); |
| 421 | wpmc.isPause = false; |
| 422 | wpmc.currentPhase.run(); |
| 423 | } |
| 424 | else if (wpmc.isPendingPause) { |
| 425 | wpmc.isPendingPause = false; |
| 426 | } |
| 427 | else { |
| 428 | jQuery('#wpmc_pause').html('<span style="top: 3px; position: relative; left: -5px;" class="dashicons dashicons-controls-pause"></span>Pausing...'); |
| 429 | wpmc.isPendingPause = true; |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | function wpmc_update_to_pause() { |
| 434 | if (wpmc.isPendingPause) { |
| 435 | wpmc.currentPhase.pause(); |
| 436 | |
| 437 | jQuery('#wpmc_pause').html('<span style="top: 3px; position: relative; left: -5px;" class="dashicons dashicons-controls-play"></span>Continue'); |
| 438 | wpmc.isPendingPause = false; |
| 439 | wpmc.isPause = true; |
| 440 | } |
| 441 | } |
| 442 | |
| 443 | /** |
| 444 | * Parses a Ajax response into a valid JSON |
| 445 | * @param {string} response The response content to parse |
| 446 | * @return {object} The parsed result |
| 447 | */ |
| 448 | function wpmc_parse_response(response) { |
| 449 | if (typeof response == 'object') return response; |
| 450 | |
| 451 | var r; |
| 452 | try { |
| 453 | r = jQuery.parseJSON(response); |
| 454 | } catch (e) { |
| 455 | r = null; |
| 456 | } |
| 457 | if (!r) { // Couldn't parse as a valid JSON |
| 458 | r = { |
| 459 | success: false, |
| 460 | message: 'The reply from the server is broken. The reply will be displayed in your Javascript console. You should also check your PHP Error Logs.' |
| 461 | }; |
| 462 | console.error('Media File Cleaner got a broken reply from the server:', response); |
| 463 | } |
| 464 | return r; |
| 465 | } |
| 466 | |
| 467 | /** |
| 468 | * Pauses the scan and Displays an error dialog |
| 469 | * @param {string} msg=null The error message |
| 470 | * @param {int} status=null The actual status code that the server responded |
| 471 | */ |
| 472 | function wpmc_handle_error(msg = null, status = null) { |
| 473 | wpmc.isPendingPause = true; |
| 474 | wpmc_update_to_pause(); |
| 475 | |
| 476 | var errDialog = jQuery('#wpmc-error-dialog'); |
| 477 | if (!msg) msg = 'An error happened'; // Default error message |
| 478 | if (status) { |
| 479 | console.error('Media Cleaner got an error from server:', status + ' ' + msg); |
| 480 | msg = '<span class="error-status">' + status + '</span> ' + msg; |
| 481 | } else |
| 482 | console.error(msg); |
| 483 | errDialog.find('h3').html(msg); |
| 484 | errDialog.dialog('open'); |
| 485 | } |
| 486 | |
| 487 | function wpmc_update_to_error() { |
| 488 | var current = wpmc.total - (wpmc.files.length + wpmc.medias.length); |
| 489 | var totalcount = wpmc.total; |
| 490 | jQuery('#wpmc_progression').html('<span class="dashicons dashicons-controls-pause"></span> Error at ' + current + "/" + totalcount + " (" + Math.round(current / totalcount * 100) + "%): " + error); |
| 491 | jQuery('#wpmc_pause').html('<span style="top: 3px; position: relative; left: -5px;" class="dashicons dashicons-controls-play"></span>Retry'); |
| 492 | wpmc.isPendingPause = false; |
| 493 | wpmc.isPause = true; |
| 494 | } |
| 495 | |
| 496 | function wpmc_scan_do() { |
| 497 | if (!wpmc.currentPhase) return; // Aborted |
| 498 | |
| 499 | if (wpmc.isPendingPause) |
| 500 | return wpmc_update_to_pause(); |
| 501 | |
| 502 | wpmc_update_progress(wpmc.total - (wpmc.files.length + wpmc.medias.length), wpmc.total); |
| 503 | var data = {}; |
| 504 | var expectedSuccess = 0; |
| 505 | if (wpmc.files.length > 0) { |
| 506 | wpmc.currentPhase.currentFiles = wpmc_pop_array(wpmc.files, wpmc_cfg.analysisBuffer); |
| 507 | expectedSuccess = wpmc.currentPhase.currentFiles.length; |
| 508 | data = { action: 'wpmc_scan_do', type: 'file', data: wpmc.currentPhase.currentFiles }; |
| 509 | } |
| 510 | else if (wpmc.medias.length > 0) { |
| 511 | wpmc.currentPhase.currentMedia = wpmc_pop_array(wpmc.medias, wpmc_cfg.analysisBuffer); |
| 512 | expectedSuccess = wpmc.currentPhase.currentMedia.length; |
| 513 | data = { action: 'wpmc_scan_do', type: 'media', data: wpmc.currentPhase.currentMedia }; |
| 514 | } |
| 515 | else { |
| 516 | jQuery('#wpmc_progression').html(wpmc.issues + " issue(s) found. <a href='?page=media-cleaner'></span>Refresh</a>."); |
| 517 | |
| 518 | wpmc = wpmc_new_context(); // Reset the context |
| 519 | jQuery('#wpmc_pause').hide(); |
| 520 | jQuery('#wpmc_scan').html('<span style="top: 3px; position: relative; left: -5px;" class="dashicons dashicons-search"></span>Start Scan'); |
| 521 | jQuery('#wpmc_actions').trigger('idle'); |
| 522 | return; |
| 523 | } |
| 524 | |
| 525 | setTimeout( |
| 526 | function () { |
| 527 | jQuery.ajax({ |
| 528 | type: 'POST', |
| 529 | url: ajaxurl, |
| 530 | dataType: 'text', |
| 531 | data: data, |
| 532 | timeout: wpmc_cfg.timeout + 5000 // Extra 5sec for fail-safe |
| 533 | |
| 534 | }).done(function (response) { |
| 535 | var reply = wpmc_parse_response(response); |
| 536 | |
| 537 | if (!reply.success) |
| 538 | return wpmc_handle_error(reply.message); |
| 539 | |
| 540 | if (reply.result) |
| 541 | wpmc.issues += expectedSuccess - reply.result.success; |
| 542 | |
| 543 | wpmc_scan_do(); |
| 544 | |
| 545 | }).fail(function (e) { // Server Error |
| 546 | wpmc_handle_error(e.statusText, e.status); |
| 547 | }); |
| 548 | |
| 549 | }, wpmc_cfg.delay |
| 550 | ); |
| 551 | } |
| 552 | |
| 553 | /** |
| 554 | * Opens a dialog |
| 555 | * @param {object} content |
| 556 | * @param {string} content.title=null The title of the dialog |
| 557 | * @param {string} content.head=null The heading |
| 558 | * @param {string} content.body=null The body |
| 559 | * @param {jQuery} content.prepend=null Additional element to prepend |
| 560 | * @param {jQuery} content.append=null Additional element to append |
| 561 | * @return {jQuery} The dialog element |
| 562 | */ |
| 563 | function wpmc_open_dialog(content) { |
| 564 | var dialog = jQuery('#wpmc-dialog'); |
| 565 | dialog.html(''); |
| 566 | if (content.title) |
| 567 | dialog.dialog('option', 'title', content.title); |
| 568 | if (content.head) |
| 569 | dialog.append('<h3 class="head">' + content.head + '</h3>'); |
| 570 | if (content.body) |
| 571 | dialog.append('<div class="body">' + content.body + '</div>'); |
| 572 | if (content.prepend) |
| 573 | dialog.prepend(content.prepend); |
| 574 | if (content.append) |
| 575 | dialog.append(content.append); |
| 576 | dialog.dialog('open'); |
| 577 | return dialog; |
| 578 | } |
| 579 | |
| 580 | /** |
| 581 | * |
| 582 | * INIT |
| 583 | * |
| 584 | */ |
| 585 | (function ($) { |
| 586 | |
| 587 | // Bulk Selection |
| 588 | $('#wpmc-cb-select-all').on('change', function (cb) { |
| 589 | $('#wpmc-table input').prop('checked', cb.target.checked); |
| 590 | }); |
| 591 | |
| 592 | // Actions |
| 593 | (function () { |
| 594 | var wrap = $('#wpmc_actions'); |
| 595 | |
| 596 | // Events: Busy, Idle |
| 597 | wrap.on('busy', function () { |
| 598 | wrap.addClass('busy'); |
| 599 | wrap.find('a.exclusive').addClass('disabled'); |
| 600 | wrap.find('input.exclusive').attr('disabled', true); |
| 601 | |
| 602 | }).on('idle', function () { |
| 603 | wrap.find('a.exclusive').removeClass('disabled'); |
| 604 | wrap.find('input.exclusive').attr('disabled', false); |
| 605 | wrap.removeClass('busy'); |
| 606 | }); |
| 607 | |
| 608 | // Scan Button |
| 609 | wrap.find('#wpmc_scan').on('click', function (ev) { |
| 610 | ev.preventDefault(); |
| 611 | if (wpmc.currentPhase) { // Abort scan |
| 612 | wpmc = wpmc_new_context(); // Reset the current context |
| 613 | $('#wpmc_pause').hide(); |
| 614 | $('#wpmc_pause').html('<span style="top: 3px; position: relative; left: -5px;" class="dashicons dashicons-controls-pause"></span>Pause'); |
| 615 | $('#wpmc_progression').text(''); |
| 616 | $('#wpmc_scan').html('<span style="top: 3px; position: relative; left: -5px;" class="dashicons dashicons-search"></span>Start Scan'); |
| 617 | wrap.trigger('idle'); |
| 618 | return; |
| 619 | } |
| 620 | wrap.trigger('busy'); |
| 621 | $('#wpmc_scan').html('Stop Scan'); |
| 622 | $('#wpmc_pause').show(); |
| 623 | wpmc.currentPhase = wpmc.phases.preparePosts.init(); |
| 624 | wpmc.currentPhase.run(); |
| 625 | }); |
| 626 | |
| 627 | // Delete Button |
| 628 | wrap.find('#wpmc_delete').on('click', function (ev) { |
| 629 | ev.preventDefault(); |
| 630 | if ($(this).hasClass('disabled')) return; |
| 631 | wpmc_delete(); |
| 632 | }); |
| 633 | |
| 634 | // Delete All (Search Results) Button |
| 635 | wrap.find('#wpmc_delete_all').on('click', function (ev) { |
| 636 | ev.preventDefault(); |
| 637 | var $this = $(this); |
| 638 | if ($this.hasClass('disabled')) return; |
| 639 | var filter = $this.data('filter') || ''; |
| 640 | if (filter && filter != wrap.find('.search-box input[name="s"]').val()) { |
| 641 | var dialog = wpmc_open_dialog({ |
| 642 | title: 'Continue?', |
| 643 | body: 'You have modified the search terms and did not click on "Search".<br>The <b>current</b> results will be deleted. Do you want to continue?', |
| 644 | append: $('<div class="prompt">') |
| 645 | .append( |
| 646 | // Cancel Button |
| 647 | $('<a class="button cancel" href="#">Cancel</a>').on('click', function (ev) { |
| 648 | ev.preventDefault(); |
| 649 | dialog.dialog('close'); |
| 650 | }) |
| 651 | ).append( |
| 652 | // Continue Button |
| 653 | $('<a class="button button-primary continue" href="#">Continue</a>').on('click', function (ev) { |
| 654 | ev.preventDefault(); |
| 655 | dialog.dialog('close'); |
| 656 | wpmc_delete_all(false, filter); |
| 657 | }) |
| 658 | ) |
| 659 | }); |
| 660 | return; |
| 661 | } |
| 662 | wpmc_delete_all(false, filter); |
| 663 | }); |
| 664 | |
| 665 | // Ignore Button |
| 666 | wrap.find('#wpmc_ignore').on('click', function (ev) { |
| 667 | ev.preventDefault(); |
| 668 | if ($(this).hasClass('disabled')) return; |
| 669 | wpmc_ignore(); |
| 670 | }); |
| 671 | |
| 672 | // Recover All Button |
| 673 | wrap.find('#wpmc_recover_all').on('click', function (ev) { |
| 674 | ev.preventDefault(); |
| 675 | if ($(this).hasClass('disabled')) return; |
| 676 | wpmc_recover_all(); |
| 677 | }); |
| 678 | |
| 679 | // Empty Trash Button |
| 680 | wrap.find('#wpmc_empty_trash').on('click', function (ev) { |
| 681 | ev.preventDefault(); |
| 682 | if ($(this).hasClass('disabled')) return; |
| 683 | wpmc_delete_all(true); |
| 684 | }); |
| 685 | |
| 686 | // Reset Button |
| 687 | wrap.find('#wpmc_reset').on('click', function (ev) { |
| 688 | ev.preventDefault(); |
| 689 | var $this = $(this); |
| 690 | var dialog = wpmc_open_dialog({ |
| 691 | title: "Reset", |
| 692 | body: "This will reset the Media Cleaner database. All the information related to your trash, your latest scan and ignored entries will be lost. Do you want to continue?", |
| 693 | append: $('<div class="prompt">') |
| 694 | .append( |
| 695 | // Cancel Button |
| 696 | $('<a class="button cancel" href="#">Cancel</a>').on('click', function (ev) { |
| 697 | ev.preventDefault(); |
| 698 | dialog.dialog('close'); |
| 699 | }) |
| 700 | ).append( |
| 701 | // Continue Button |
| 702 | $('<a class="button button-primary continue" href="#">Continue</a>').on('click', function (ev) { |
| 703 | ev.preventDefault(); |
| 704 | dialog.dialog('close'); |
| 705 | location.href = $this.attr('href'); |
| 706 | }) |
| 707 | ) |
| 708 | }); |
| 709 | }); |
| 710 | })(); |
| 711 | |
| 712 | // Dialog |
| 713 | (function () { |
| 714 | var dialog = $('#wpmc-dialog'); |
| 715 | |
| 716 | dialog.dialog({ |
| 717 | dialogClass: 'wp-dialog', |
| 718 | autoOpen: false, |
| 719 | draggable: true, |
| 720 | width: 'auto', |
| 721 | modal: true, |
| 722 | resizable: false, |
| 723 | closeOnEscape: true, |
| 724 | position: { |
| 725 | my: "center", |
| 726 | at: "center", |
| 727 | of: window |
| 728 | }, |
| 729 | open: function () { |
| 730 | // Close dialog by clicking the overlay behind it |
| 731 | $('.ui-widget-overlay').on('click', function () { |
| 732 | dialog.dialog('close'); |
| 733 | }); |
| 734 | }, |
| 735 | create: function () { |
| 736 | // Style fix for WordPress admin |
| 737 | $('.ui-dialog-titlebar-close').addClass('ui-button'); |
| 738 | } |
| 739 | }); |
| 740 | })(); |
| 741 | |
| 742 | // Error Dialog |
| 743 | (function () { |
| 744 | var errDialog = $('#wpmc-error-dialog'); |
| 745 | |
| 746 | errDialog.dialog({ |
| 747 | title: 'ERROR', |
| 748 | dialogClass: 'wp-dialog', |
| 749 | autoOpen: false, |
| 750 | draggable: true, |
| 751 | width: 'auto', |
| 752 | modal: true, |
| 753 | resizable: false, |
| 754 | closeOnEscape: true, |
| 755 | position: { |
| 756 | my: "center", |
| 757 | at: "center", |
| 758 | of: window |
| 759 | }, |
| 760 | open: function () { |
| 761 | $('.ui-widget-overlay').on('click', function () { |
| 762 | errDialog.dialog('close'); |
| 763 | }); |
| 764 | }, |
| 765 | create: function () { |
| 766 | $('.ui-dialog-titlebar-close').addClass('ui-button'); |
| 767 | } |
| 768 | }); |
| 769 | |
| 770 | // Retry |
| 771 | errDialog.find('a.retry').on('click', function (ev) { |
| 772 | ev.preventDefault(); |
| 773 | |
| 774 | wpmc_pause(); // Resume |
| 775 | errDialog.dialog('close'); |
| 776 | }); |
| 777 | |
| 778 | // Skip (Ignore error) |
| 779 | errDialog.find('a.skip').on('click', function (ev) { |
| 780 | ev.preventDefault(); |
| 781 | |
| 782 | wpmc.currentPhase.skip(); |
| 783 | console.warn('1 error is ignored by you'); |
| 784 | |
| 785 | wpmc_pause(); // Resume |
| 786 | errDialog.dialog('close'); |
| 787 | }); |
| 788 | })(); |
| 789 | |
| 790 | })(jQuery); |
| 791 |