| 1 |
/** |
| 2 |
* |
| 3 |
* |
| 4 |
*/ |
| 5 |
|
| 6 |
// Application |
| 7 |
var wpStatelessApp = angular.module('wpStatelessApp', []) |
| 8 |
|
| 9 |
// Controller |
| 10 |
.controller('wpStatelessTools', ['$scope', '$http', function ($scope, $http) { |
| 11 |
|
| 12 |
$scope.action = 'regenerate_images'; |
| 13 |
$scope.method = 'start'; |
| 14 |
$scope.bulk_size = 1; |
| 15 |
|
| 16 |
/** |
| 17 |
* Counters |
| 18 |
* @type {number} |
| 19 |
*/ |
| 20 |
$scope.objectsCounter = 0; |
| 21 |
$scope.objectsTotal = 0; |
| 22 |
|
| 23 |
/** |
| 24 |
* Error storage |
| 25 |
* @type {boolean} |
| 26 |
*/ |
| 27 |
$scope.error = false; |
| 28 |
|
| 29 |
/** |
| 30 |
* Flags |
| 31 |
* @type {boolean} |
| 32 |
*/ |
| 33 |
$scope.isRunning = false; |
| 34 |
$scope.isLoading = false; |
| 35 |
$scope.continue = true; |
| 36 |
|
| 37 |
/** |
| 38 |
* |
| 39 |
* @type {{images: boolean, other: boolean}} |
| 40 |
*/ |
| 41 |
$scope.progresses = { |
| 42 |
images: false, |
| 43 |
other: false |
| 44 |
}; |
| 45 |
|
| 46 |
/** |
| 47 |
* |
| 48 |
* @type {{images: boolean, other: boolean}} |
| 49 |
*/ |
| 50 |
$scope.fails = { |
| 51 |
images: false, |
| 52 |
other: false |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* IDs storage |
| 57 |
* @type {Array} |
| 58 |
*/ |
| 59 |
$scope.objectIDs = []; |
| 60 |
|
| 61 |
/** |
| 62 |
* |
| 63 |
* @type {Array} |
| 64 |
*/ |
| 65 |
$scope.chunkIDs = []; |
| 66 |
|
| 67 |
/** |
| 68 |
* Log |
| 69 |
* @type {Array} |
| 70 |
*/ |
| 71 |
$scope.log = []; |
| 72 |
|
| 73 |
/** |
| 74 |
* Status text |
| 75 |
* @type {boolean} |
| 76 |
*/ |
| 77 |
$scope.status = false; |
| 78 |
|
| 79 |
/** |
| 80 |
* Init |
| 81 |
*/ |
| 82 |
$scope.init = function() { |
| 83 |
jQuery("#regenthumbs-bar").progressbar(); |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* |
| 88 |
* @param callback |
| 89 |
*/ |
| 90 |
$scope.getCurrentProgresses = function( callback ) { |
| 91 |
$scope.isLoading = true; |
| 92 |
|
| 93 |
$http({ |
| 94 |
method: 'GET', |
| 95 |
url: ajaxurl, |
| 96 |
params: { |
| 97 |
action: 'stateless_get_current_progresses' |
| 98 |
} |
| 99 |
}).then(function(response){ |
| 100 |
var data = response.data || {}; |
| 101 |
|
| 102 |
if ( data.success ) { |
| 103 |
if ( typeof data.data !== 'undefined' ) { |
| 104 |
$scope.progresses.images = data.data.images; |
| 105 |
$scope.progresses.other = data.data.other; |
| 106 |
if ( 'function' === typeof callback ) { |
| 107 |
callback(); |
| 108 |
} |
| 109 |
} else { |
| 110 |
console.error( 'Could not retrieve progress' ); |
| 111 |
} |
| 112 |
} else { |
| 113 |
console.error( 'Could not retrieve progress' ); |
| 114 |
} |
| 115 |
|
| 116 |
$scope.isLoading = false; |
| 117 |
}, function(response) { |
| 118 |
console.error( 'Could not retrieve progress' ); |
| 119 |
|
| 120 |
$scope.isLoading = false; |
| 121 |
}); |
| 122 |
}; |
| 123 |
|
| 124 |
/** |
| 125 |
* |
| 126 |
*/ |
| 127 |
$scope.getCurrentProgresses(); |
| 128 |
|
| 129 |
/** |
| 130 |
* |
| 131 |
* @param callback |
| 132 |
*/ |
| 133 |
function getAllFails( callback ) { |
| 134 |
$scope.isLoading = true; |
| 135 |
|
| 136 |
$http({ |
| 137 |
method: 'GET', |
| 138 |
url: ajaxurl, |
| 139 |
params: { |
| 140 |
action: 'stateless_get_all_fails' |
| 141 |
} |
| 142 |
}).then(function(response){ |
| 143 |
var data = response.data || {}; |
| 144 |
|
| 145 |
if ( data.success ) { |
| 146 |
if ( typeof data.data !== 'undefined' ) { |
| 147 |
|
| 148 |
$scope.fails.images = data.data.images; |
| 149 |
$scope.fails.other = data.data.other; |
| 150 |
|
| 151 |
if ( 'function' === typeof callback ) { |
| 152 |
callback(); |
| 153 |
} |
| 154 |
} else { |
| 155 |
console.error( 'Could not get fails' ); |
| 156 |
} |
| 157 |
} else { |
| 158 |
console.error( 'Could not get fails' ); |
| 159 |
} |
| 160 |
|
| 161 |
$scope.isLoading = false; |
| 162 |
}, function(response) { |
| 163 |
console.error( 'Could not get fails' ); |
| 164 |
|
| 165 |
$scope.isLoading = false; |
| 166 |
}); |
| 167 |
}; |
| 168 |
|
| 169 |
getAllFails(); |
| 170 |
|
| 171 |
/** |
| 172 |
* Form submit handler |
| 173 |
* @param e |
| 174 |
* @returns {boolean} |
| 175 |
*/ |
| 176 |
$scope.processStart = function(e) { |
| 177 |
|
| 178 |
$scope.error = false; |
| 179 |
$scope.objectsCounter = 0; |
| 180 |
$scope.objectsTotal = 0; |
| 181 |
$scope.objectIDs = []; |
| 182 |
$scope.chunkIDs = []; |
| 183 |
|
| 184 |
if ( $scope.method === 'fix' ) { |
| 185 |
|
| 186 |
if ( $scope.action ) { |
| 187 |
switch( $scope.action ) { |
| 188 |
case 'regenerate_images': |
| 189 |
$scope.objectIDs = $scope.fails.images; |
| 190 |
$scope.regenerateImages(); |
| 191 |
break; |
| 192 |
case 'sync_non_images': |
| 193 |
$scope.objectIDs = $scope.fails.other; |
| 194 |
$scope.syncFiles(); |
| 195 |
break; |
| 196 |
default: break; |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
return false; |
| 201 |
} |
| 202 |
|
| 203 |
var cont = 0; |
| 204 |
if ( 'continue' === $scope.method ) { |
| 205 |
cont = 1; |
| 206 |
} |
| 207 |
|
| 208 |
if ( $scope.action ) { |
| 209 |
switch( $scope.action ) { |
| 210 |
case 'regenerate_images': |
| 211 |
$scope.getImagesMedia( $scope.regenerateImages, cont ); |
| 212 |
break; |
| 213 |
case 'sync_non_images': |
| 214 |
$scope.getOtherMedia( $scope.syncFiles, cont ); |
| 215 |
break; |
| 216 |
default: break; |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
return false; |
| 221 |
}; |
| 222 |
|
| 223 |
/** |
| 224 |
* Stop process |
| 225 |
*/ |
| 226 |
$scope.processStop = function() { |
| 227 |
$scope.status = 'Stopping...'; |
| 228 |
$scope.continue = false; |
| 229 |
}; |
| 230 |
|
| 231 |
function array_bulk( arr, bulk_size ) { |
| 232 |
var groups = []; |
| 233 |
var i; |
| 234 |
|
| 235 |
for ( i = 0; i < bulk_size; i++ ) { |
| 236 |
groups[ i ] = []; |
| 237 |
} |
| 238 |
|
| 239 |
for ( i = 0; i < arr.length; i ++ ) { |
| 240 |
groups[ i % bulk_size ].push( arr[ i ] ); |
| 241 |
} |
| 242 |
|
| 243 |
return groups; |
| 244 |
} |
| 245 |
|
| 246 |
$scope.finishProcess = function( chunk_id ) { |
| 247 |
var mode = 'images'; |
| 248 |
if ( 'sync_non_images' === $scope.action ) { |
| 249 |
mode = 'other'; |
| 250 |
} |
| 251 |
|
| 252 |
if ( $scope.objectsCounter >= $scope.objectsTotal ) { |
| 253 |
// process finished |
| 254 |
|
| 255 |
$http({ |
| 256 |
method: 'GET', |
| 257 |
url: ajaxurl, |
| 258 |
params: { |
| 259 |
action: 'stateless_reset_progress', |
| 260 |
mode: mode |
| 261 |
} |
| 262 |
}).then(function(response){ |
| 263 |
$scope.progresses[ mode ] = false; |
| 264 |
|
| 265 |
$scope.status = 'Finished'; |
| 266 |
$scope.isRunning = false; |
| 267 |
}, function(response) { |
| 268 |
console.error( 'Could not reset progress' ); |
| 269 |
}); |
| 270 |
} else if ( 'undefined' !== typeof chunk_id ) { |
| 271 |
// process cancelled, but this is only a chunk finishing request |
| 272 |
|
| 273 |
$scope.chunkIDs[ chunk_id ] = false; |
| 274 |
var all_done = true; |
| 275 |
for ( var i in $scope.chunkIDs ) { |
| 276 |
if ( false !== $scope.chunkIDs[ i ] ) { |
| 277 |
all_done = false; |
| 278 |
break; |
| 279 |
} |
| 280 |
} |
| 281 |
if ( all_done ) { |
| 282 |
$scope.getCurrentProgresses( function() { |
| 283 |
$scope.status = 'Cancelled'; |
| 284 |
$scope.isRunning = false; |
| 285 |
}); |
| 286 |
} |
| 287 |
} else { |
| 288 |
// process cancelled |
| 289 |
|
| 290 |
$scope.getCurrentProgresses( function() { |
| 291 |
$scope.status = 'Cancelled'; |
| 292 |
$scope.isRunning = false; |
| 293 |
}); |
| 294 |
} |
| 295 |
}; |
| 296 |
|
| 297 |
/** |
| 298 |
* Load images IDs |
| 299 |
* @param callback |
| 300 |
*/ |
| 301 |
$scope.getImagesMedia = function( callback, cont ) { |
| 302 |
|
| 303 |
$scope.continue = true; |
| 304 |
$scope.isLoading = true; |
| 305 |
$scope.status = 'Loading Images Media Objects...'; |
| 306 |
|
| 307 |
$http({ |
| 308 |
method: 'GET', |
| 309 |
url: ajaxurl, |
| 310 |
params: { |
| 311 |
action: 'get_images_media_ids', |
| 312 |
continue: cont |
| 313 |
} |
| 314 |
}).then(function(response){ |
| 315 |
var data = response.data || {}; |
| 316 |
|
| 317 |
if ( data.success ) { |
| 318 |
if ( typeof callback === 'function' ) { |
| 319 |
if ( typeof data.data !== 'undefined' ) { |
| 320 |
$scope.objectIDs = data.data; |
| 321 |
callback(); |
| 322 |
} else { |
| 323 |
$scope.status = 'Error appeared'; |
| 324 |
$scope.error = "IDs are malformed"; |
| 325 |
} |
| 326 |
} |
| 327 |
} else { |
| 328 |
$scope.status = 'Error appeared'; |
| 329 |
$scope.error = data.data || "Request failed"; |
| 330 |
} |
| 331 |
|
| 332 |
$scope.isLoading = false; |
| 333 |
|
| 334 |
}, function(response) { |
| 335 |
$scope.error = response.data || "Request failed"; |
| 336 |
$scope.status = 'Error appeared'; |
| 337 |
$scope.isLoading = false; |
| 338 |
}); |
| 339 |
|
| 340 |
}; |
| 341 |
|
| 342 |
/** |
| 343 |
* Load non-images media files |
| 344 |
* @param callback |
| 345 |
*/ |
| 346 |
$scope.getOtherMedia = function( callback, cont ) { |
| 347 |
|
| 348 |
$scope.continue = true; |
| 349 |
$scope.isLoading = true; |
| 350 |
$scope.status = 'Loading non-image Media Objects...'; |
| 351 |
|
| 352 |
$http({ |
| 353 |
method: 'GET', |
| 354 |
url: ajaxurl, |
| 355 |
params: { |
| 356 |
action: 'get_other_media_ids', |
| 357 |
continue: cont |
| 358 |
} |
| 359 |
}).then(function(response){ |
| 360 |
var data = response.data || {}; |
| 361 |
|
| 362 |
if ( data.success ) { |
| 363 |
if ( typeof callback === 'function' ) { |
| 364 |
if ( typeof data.data !== 'undefined' ) { |
| 365 |
$scope.objectIDs = data.data; |
| 366 |
callback(); |
| 367 |
} else { |
| 368 |
$scope.status = 'Error appeared'; |
| 369 |
$scope.error = "IDs are malformed"; |
| 370 |
} |
| 371 |
} |
| 372 |
} else { |
| 373 |
$scope.status = 'Error appeared'; |
| 374 |
$scope.error = data.data || "Request failed"; |
| 375 |
} |
| 376 |
|
| 377 |
$scope.isLoading = false; |
| 378 |
|
| 379 |
}, function(response) { |
| 380 |
$scope.error = response.data || "Request failed"; |
| 381 |
$scope.status = 'Error appeared'; |
| 382 |
$scope.isLoading = false; |
| 383 |
}); |
| 384 |
|
| 385 |
}; |
| 386 |
|
| 387 |
/** |
| 388 |
* Run sync for files |
| 389 |
*/ |
| 390 |
$scope.syncFiles = function() { |
| 391 |
$scope.isRunning = true; |
| 392 |
$scope.objectsTotal = $scope.objectIDs.length; |
| 393 |
$scope.objectsCounter = 0; |
| 394 |
$scope.status = 'Processing files (' + $scope.objectsTotal + ' total)...'; |
| 395 |
|
| 396 |
jQuery("#regenthumbs-bar").progressbar("value", 0); |
| 397 |
jQuery("#regenthumbs-bar-percent").html( "0%" ); |
| 398 |
|
| 399 |
if ( $scope.objectIDs.length ) { |
| 400 |
//$scope.syncSingleFile( $scope.objectIDs.shift() ); |
| 401 |
$scope.chunkIDs = array_bulk( $scope.objectIDs, $scope.bulk_size ); |
| 402 |
for ( var i in $scope.chunkIDs ) { |
| 403 |
$scope.syncSingleFile( $scope.chunkIDs[ i ].shift(), i ); |
| 404 |
} |
| 405 |
} |
| 406 |
} |
| 407 |
|
| 408 |
/** |
| 409 |
* Run images regeneration |
| 410 |
* @param ids |
| 411 |
*/ |
| 412 |
$scope.regenerateImages = function() { |
| 413 |
$scope.isRunning = true; |
| 414 |
$scope.objectsTotal = $scope.objectIDs.length; |
| 415 |
$scope.objectsCounter = 0; |
| 416 |
$scope.status = 'Processing images (' + $scope.objectsTotal + ' total)...'; |
| 417 |
|
| 418 |
jQuery("#regenthumbs-bar").progressbar("value", 0); |
| 419 |
jQuery("#regenthumbs-bar-percent").html( "0%" ); |
| 420 |
|
| 421 |
if ( $scope.objectIDs.length ) { |
| 422 |
//$scope.regenerateSingle( $scope.objectIDs.shift() ); |
| 423 |
$scope.chunkIDs = array_bulk( $scope.objectIDs, $scope.bulk_size ); |
| 424 |
for ( var i in $scope.chunkIDs ) { |
| 425 |
$scope.regenerateSingle( $scope.chunkIDs[ i ].shift(), i ); |
| 426 |
} |
| 427 |
} |
| 428 |
}; |
| 429 |
|
| 430 |
/** |
| 431 |
* Process Single Image |
| 432 |
* @param id |
| 433 |
*/ |
| 434 |
$scope.regenerateSingle = function( id, chunk_id ) { |
| 435 |
|
| 436 |
$http({ |
| 437 |
method: 'GET', |
| 438 |
url: ajaxurl, |
| 439 |
params: { |
| 440 |
action: "stateless_process_image", |
| 441 |
id: id |
| 442 |
} |
| 443 |
}).then( |
| 444 |
function(response) { |
| 445 |
var data = response.data || {}; |
| 446 |
$scope.log.push({message:data.data}); |
| 447 |
|
| 448 |
jQuery("#regenthumbs-bar").progressbar( "value", ( ++$scope.objectsCounter / $scope.objectsTotal ) * 100 ); |
| 449 |
jQuery("#regenthumbs-bar-percent").html( Math.round( ( $scope.objectsCounter / $scope.objectsTotal ) * 1000 ) / 10 + "%" ); |
| 450 |
|
| 451 |
if ( 'undefined' !== typeof chunk_id ) { |
| 452 |
if ( $scope.chunkIDs[ chunk_id ].length && $scope.continue ) { |
| 453 |
$scope.regenerateSingle( $scope.chunkIDs[ chunk_id ].shift(), chunk_id ); |
| 454 |
} else { |
| 455 |
$scope.finishProcess( chunk_id ); |
| 456 |
} |
| 457 |
} else { |
| 458 |
if ( $scope.objectIDs.length && $scope.continue ) { |
| 459 |
$scope.regenerateSingle( $scope.objectIDs.shift() ); |
| 460 |
} else { |
| 461 |
$scope.finishProcess(); |
| 462 |
} |
| 463 |
} |
| 464 |
}, |
| 465 |
function(response) { |
| 466 |
$scope.error = response.data || "Request failed"; |
| 467 |
$scope.status = 'Error appeared'; |
| 468 |
$scope.isRunning = false; |
| 469 |
} |
| 470 |
); |
| 471 |
|
| 472 |
} |
| 473 |
|
| 474 |
/** |
| 475 |
* Process single file |
| 476 |
* @param id |
| 477 |
*/ |
| 478 |
$scope.syncSingleFile = function( id, chunk_id ) { |
| 479 |
|
| 480 |
$http({ |
| 481 |
method: 'GET', |
| 482 |
url: ajaxurl, |
| 483 |
params: { |
| 484 |
action: "stateless_process_file", |
| 485 |
id: id |
| 486 |
} |
| 487 |
}).then( |
| 488 |
function(response) { |
| 489 |
var data = response.data || {}; |
| 490 |
$scope.log.push({message:data.data}); |
| 491 |
|
| 492 |
jQuery("#regenthumbs-bar").progressbar( "value", ( ++$scope.objectsCounter / $scope.objectsTotal ) * 100 ); |
| 493 |
jQuery("#regenthumbs-bar-percent").html( Math.round( ( $scope.objectsCounter / $scope.objectsTotal ) * 1000 ) / 10 + "%" ); |
| 494 |
|
| 495 |
if ( 'undefined' !== typeof chunk_id ) { |
| 496 |
if ( $scope.chunkIDs[ chunk_id ].length && $scope.continue ) { |
| 497 |
$scope.syncSingleFile( $scope.chunkIDs[ chunk_id ].shift(), chunk_id ); |
| 498 |
} else { |
| 499 |
$scope.finishProcess( chunk_id ); |
| 500 |
} |
| 501 |
} else { |
| 502 |
if ( $scope.objectIDs.length && $scope.continue ) { |
| 503 |
$scope.syncSingleFile( $scope.objectIDs.shift() ); |
| 504 |
} else { |
| 505 |
$scope.finishProcess(); |
| 506 |
} |
| 507 |
} |
| 508 |
}, |
| 509 |
function(response) { |
| 510 |
$scope.error = response.data || "Request failed"; |
| 511 |
$scope.status = 'Error appeared'; |
| 512 |
$scope.isRunning = false; |
| 513 |
} |
| 514 |
); |
| 515 |
|
| 516 |
} |
| 517 |
|
| 518 |
}]) |
| 519 |
.controller('wpStatelessSettings', function($scope, $filter) { |
| 520 |
$scope.sm = wp_stateless_settings || {}; |
| 521 |
|
| 522 |
$scope.sm.showNotice = function(option){ |
| 523 |
if($scope.sm.readonly && $scope.sm.readonly[option]){ |
| 524 |
var slug = $scope.sm.readonly[option]; |
| 525 |
return $scope.sm.strings[slug]; |
| 526 |
} |
| 527 |
} |
| 528 |
|
| 529 |
$scope.sm.generatePreviewUrl = function() { |
| 530 |
var host = 'https://storage.googleapis.com/'; |
| 531 |
|
| 532 |
if ( $scope.sm.bucket && $scope.sm.custom_domain == $scope.sm.bucket) { |
| 533 |
host = 'http://'; // bucketname will be host |
| 534 |
} |
| 535 |
host += $scope.sm.bucket ? $scope.sm.bucket : '{bucket-name}'; |
| 536 |
var rootdir = $scope.sm.root_dir ? $scope.sm.root_dir + '/' : ''; |
| 537 |
var subdir = $scope.sm.organize_media == '1' ? $filter('date')(Date.now(), 'yyyy/MM') + '/' : ''; |
| 538 |
var hash = $scope.sm.hashify_file_name == 'true' ? Date.now().toString(36) + '-' : ''; |
| 539 |
$scope.sm.preview_url = host + "/" + rootdir + subdir + hash + "your-image-name.jpeg"; |
| 540 |
} |
| 541 |
|
| 542 |
$scope.sm.generatePreviewUrl(); |
| 543 |
|
| 544 |
}); |
| 545 |
|