| 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 |
var WP_DEBUG = wp_stateless_configs.WP_DEBUG || false; |
| 13 |
$scope.action = 'regenerate_images'; |
| 14 |
$scope.method = 'start'; |
| 15 |
$scope.bulk_size = 1; |
| 16 |
|
| 17 |
/** |
| 18 |
* Counters |
| 19 |
* @type {number} |
| 20 |
*/ |
| 21 |
$scope.objectsCounter = 0; |
| 22 |
$scope.objectsTotal = 0; |
| 23 |
|
| 24 |
/** |
| 25 |
* Error storage |
| 26 |
* @type {boolean} |
| 27 |
*/ |
| 28 |
$scope.error = false; |
| 29 |
|
| 30 |
/** |
| 31 |
* Flags |
| 32 |
* @type {boolean} |
| 33 |
*/ |
| 34 |
$scope.isRunning = false; |
| 35 |
$scope.isLoading = false; |
| 36 |
$scope.continue = true; |
| 37 |
|
| 38 |
/** |
| 39 |
* |
| 40 |
* @type {{images: boolean, other: boolean}} |
| 41 |
*/ |
| 42 |
$scope.progresses = { |
| 43 |
images: false, |
| 44 |
other: false |
| 45 |
}; |
| 46 |
|
| 47 |
/** |
| 48 |
* |
| 49 |
* @type {{images: boolean, other: boolean}} |
| 50 |
*/ |
| 51 |
$scope.fails = { |
| 52 |
images: false, |
| 53 |
other: false |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* IDs storage |
| 58 |
* @type {Array} |
| 59 |
*/ |
| 60 |
$scope.objectIDs = []; |
| 61 |
|
| 62 |
/** |
| 63 |
* |
| 64 |
* @type {Array} |
| 65 |
*/ |
| 66 |
$scope.chunkIDs = []; |
| 67 |
|
| 68 |
/** |
| 69 |
* Log |
| 70 |
* @type {Array} |
| 71 |
*/ |
| 72 |
$scope.log = []; |
| 73 |
|
| 74 |
/** |
| 75 |
* Status message |
| 76 |
* @type {String} |
| 77 |
*/ |
| 78 |
$scope.status = ''; |
| 79 |
|
| 80 |
/** |
| 81 |
* Status message |
| 82 |
* @type {String} |
| 83 |
*/ |
| 84 |
$scope.extraStatus = ''; |
| 85 |
|
| 86 |
/** |
| 87 |
* Init |
| 88 |
*/ |
| 89 |
$scope.init = function() { |
| 90 |
jQuery("#regenthumbs-bar").progressbar(); |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Get error message |
| 95 |
*/ |
| 96 |
$scope.getError = function(response, message) { |
| 97 |
if(response.data && typeof response.data.data !== 'undefined' && typeof response.data.success !== 'undefined' && response.data.success == false){ |
| 98 |
$scope.extraStatus = response.data.data; |
| 99 |
return message; |
| 100 |
} |
| 101 |
|
| 102 |
if(response.data && typeof response.data.data !== 'undefined'){ |
| 103 |
return response.data.data; |
| 104 |
} |
| 105 |
|
| 106 |
if(response.data && typeof response.data == 'string'){ |
| 107 |
$scope.extraStatus = response.data; |
| 108 |
return message; |
| 109 |
} |
| 110 |
|
| 111 |
if(!response.data && response.statusText){ |
| 112 |
return response.statusText + " (" + stateless_l10n.response_code + response.status + ")"; |
| 113 |
} |
| 114 |
|
| 115 |
if(!response.data && response.status == -1){ |
| 116 |
return stateless_l10n.unable_to_connect_to_the_server; |
| 117 |
} |
| 118 |
|
| 119 |
return message; |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* |
| 124 |
* @param callback |
| 125 |
*/ |
| 126 |
$scope.getCurrentProgresses = function( callback ) { |
| 127 |
$scope.isLoading = true; |
| 128 |
|
| 129 |
$http({ |
| 130 |
method: 'GET', |
| 131 |
url: ajaxurl, |
| 132 |
params: { |
| 133 |
action: 'stateless_get_current_progresses' |
| 134 |
} |
| 135 |
}).then(function(response){ |
| 136 |
var data = response.data || {}; |
| 137 |
|
| 138 |
if ( data.success ) { |
| 139 |
if ( typeof data.data !== 'undefined' ) { |
| 140 |
$scope.progresses.images = data.data.images; |
| 141 |
$scope.progresses.other = data.data.other; |
| 142 |
if ( 'function' === typeof callback ) { |
| 143 |
callback(); |
| 144 |
} |
| 145 |
} else { |
| 146 |
console.error( stateless_l10n.could_not_retrieve_progress ); |
| 147 |
} |
| 148 |
} else { |
| 149 |
console.error( stateless_l10n.could_not_retrieve_progress ); |
| 150 |
} |
| 151 |
|
| 152 |
$scope.isLoading = false; |
| 153 |
}, function(response) { |
| 154 |
console.error( stateless_l10n.could_not_retrieve_progress ); |
| 155 |
|
| 156 |
$scope.isLoading = false; |
| 157 |
}); |
| 158 |
}; |
| 159 |
|
| 160 |
/** |
| 161 |
* |
| 162 |
*/ |
| 163 |
$scope.getCurrentProgresses(); |
| 164 |
|
| 165 |
/** |
| 166 |
* |
| 167 |
* @param callback |
| 168 |
*/ |
| 169 |
function getAllFails( callback ) { |
| 170 |
$scope.isLoading = true; |
| 171 |
|
| 172 |
$http({ |
| 173 |
method: 'GET', |
| 174 |
url: ajaxurl, |
| 175 |
params: { |
| 176 |
action: 'stateless_get_all_fails' |
| 177 |
} |
| 178 |
}).then(function(response){ |
| 179 |
var data = response.data || {}; |
| 180 |
|
| 181 |
if ( data.success ) { |
| 182 |
if ( typeof data.data !== 'undefined' ) { |
| 183 |
|
| 184 |
$scope.fails.images = data.data.images; |
| 185 |
$scope.fails.other = data.data.other; |
| 186 |
|
| 187 |
if ( 'function' === typeof callback ) { |
| 188 |
callback(); |
| 189 |
} |
| 190 |
} else { |
| 191 |
console.error( stateless_l10n.could_not_get_fails ); |
| 192 |
} |
| 193 |
} else { |
| 194 |
console.error( stateless_l10n.could_not_get_fails ); |
| 195 |
} |
| 196 |
|
| 197 |
$scope.isLoading = false; |
| 198 |
}, function(response) { |
| 199 |
console.error( stateless_l10n.could_not_get_fails ); |
| 200 |
|
| 201 |
$scope.isLoading = false; |
| 202 |
}); |
| 203 |
}; |
| 204 |
|
| 205 |
getAllFails(); |
| 206 |
|
| 207 |
/** |
| 208 |
* Form submit handler |
| 209 |
* @param e |
| 210 |
* @returns {boolean} |
| 211 |
*/ |
| 212 |
$scope.processStart = function(e) { |
| 213 |
|
| 214 |
$scope.error = false; |
| 215 |
$scope.status = ''; |
| 216 |
$scope.extraStatus = ''; |
| 217 |
$scope.objectsCounter = 0; |
| 218 |
$scope.objectsTotal = 0; |
| 219 |
$scope.objectIDs = []; |
| 220 |
$scope.chunkIDs = []; |
| 221 |
|
| 222 |
if ( $scope.method === 'fix' ) { |
| 223 |
|
| 224 |
if ( $scope.action ) { |
| 225 |
switch( $scope.action ) { |
| 226 |
case 'regenerate_images': |
| 227 |
$scope.objectIDs = $scope.fails.images; |
| 228 |
$scope.regenerateImages(); |
| 229 |
break; |
| 230 |
case 'sync_non_images': |
| 231 |
$scope.objectIDs = $scope.fails.other; |
| 232 |
$scope.syncFiles(); |
| 233 |
break; |
| 234 |
default: break; |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
return false; |
| 239 |
} |
| 240 |
|
| 241 |
var cont = 0; |
| 242 |
if ( 'continue' === $scope.method ) { |
| 243 |
cont = 1; |
| 244 |
} |
| 245 |
|
| 246 |
if ( $scope.action ) { |
| 247 |
switch( $scope.action ) { |
| 248 |
case 'regenerate_images': |
| 249 |
$scope.getImagesMedia( $scope.regenerateImages, cont ); |
| 250 |
break; |
| 251 |
case 'sync_non_images': |
| 252 |
$scope.getOtherMedia( $scope.syncFiles, cont ); |
| 253 |
break; |
| 254 |
case 'sync_non_library_files': |
| 255 |
$scope.getNonLibraryFiles( $scope.syncNonLibraryFiles, cont ); |
| 256 |
break; |
| 257 |
default: break; |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
return false; |
| 262 |
}; |
| 263 |
|
| 264 |
/** |
| 265 |
* Stop process |
| 266 |
*/ |
| 267 |
$scope.processStop = function() { |
| 268 |
$scope.status = stateless_l10n.stopping; |
| 269 |
$scope.continue = false; |
| 270 |
}; |
| 271 |
|
| 272 |
function array_bulk( arr, bulk_size ) { |
| 273 |
var groups = []; |
| 274 |
var i; |
| 275 |
|
| 276 |
for ( i = 0; i < bulk_size; i++ ) { |
| 277 |
groups[ i ] = []; |
| 278 |
} |
| 279 |
|
| 280 |
for ( i = 0; i < arr.length; i ++ ) { |
| 281 |
groups[ i % bulk_size ].push( arr[ i ] ); |
| 282 |
} |
| 283 |
|
| 284 |
return groups; |
| 285 |
} |
| 286 |
|
| 287 |
$scope.finishProcess = function( chunk_id ) { |
| 288 |
var mode = 'images'; |
| 289 |
if ( 'sync_non_images' === $scope.action ) { |
| 290 |
mode = 'other'; |
| 291 |
} |
| 292 |
|
| 293 |
if ( $scope.objectsCounter >= $scope.objectsTotal ) { |
| 294 |
// process finished |
| 295 |
|
| 296 |
$http({ |
| 297 |
method: 'GET', |
| 298 |
url: ajaxurl, |
| 299 |
params: { |
| 300 |
action: 'stateless_reset_progress', |
| 301 |
mode: mode |
| 302 |
} |
| 303 |
}).then(function(response){ |
| 304 |
$scope.progresses[ mode ] = false; |
| 305 |
|
| 306 |
$scope.status = stateless_l10n.finished; |
| 307 |
$scope.isLoading = false; |
| 308 |
$scope.isRunning = false; |
| 309 |
}, function(response) { |
| 310 |
console.error( stateless_l10n.could_not_reset_progress ); |
| 311 |
}); |
| 312 |
} else if ( 'undefined' !== typeof chunk_id ) { |
| 313 |
// process cancelled, but this is only a chunk finishing request |
| 314 |
|
| 315 |
$scope.chunkIDs[ chunk_id ] = false; |
| 316 |
var all_done = true; |
| 317 |
for ( var i in $scope.chunkIDs ) { |
| 318 |
if ( false !== $scope.chunkIDs[ i ] ) { |
| 319 |
all_done = false; |
| 320 |
break; |
| 321 |
} |
| 322 |
} |
| 323 |
if ( all_done ) { |
| 324 |
$scope.getCurrentProgresses( function() { |
| 325 |
$scope.status = stateless_l10n.cancelled; |
| 326 |
$scope.isRunning = false; |
| 327 |
}); |
| 328 |
} |
| 329 |
} else { |
| 330 |
// process cancelled |
| 331 |
|
| 332 |
$scope.getCurrentProgresses( function() { |
| 333 |
$scope.status = stateless_l10n.cancelled; |
| 334 |
$scope.isRunning = false; |
| 335 |
}); |
| 336 |
} |
| 337 |
}; |
| 338 |
|
| 339 |
/** |
| 340 |
* Load images IDs |
| 341 |
* @param callback |
| 342 |
*/ |
| 343 |
$scope.getImagesMedia = function( callback, cont ) { |
| 344 |
|
| 345 |
$scope.continue = true; |
| 346 |
$scope.isLoading = true; |
| 347 |
$scope.status = stateless_l10n.loading_images_media_objects; |
| 348 |
|
| 349 |
$http({ |
| 350 |
method: 'GET', |
| 351 |
url: ajaxurl, |
| 352 |
params: { |
| 353 |
action: 'get_images_media_ids', |
| 354 |
continue: cont |
| 355 |
} |
| 356 |
}).then(function(response){ |
| 357 |
var data = response.data || {}; |
| 358 |
|
| 359 |
if ( data.success ) { |
| 360 |
if ( typeof callback === 'function' ) { |
| 361 |
if ( typeof data.data !== 'undefined' ) { |
| 362 |
$scope.objectIDs = data.data; |
| 363 |
callback(); |
| 364 |
} else { |
| 365 |
$scope.status = stateless_l10n.ids_are_malformed; |
| 366 |
$scope.error = true; |
| 367 |
} |
| 368 |
} |
| 369 |
} else { |
| 370 |
$scope.status = $scope.getError(response, stateless_l10n.unable_to_get_images_media_id); |
| 371 |
$scope.error = true; |
| 372 |
} |
| 373 |
|
| 374 |
$scope.isLoading = false; |
| 375 |
|
| 376 |
if(WP_DEBUG){ |
| 377 |
console.log(stateless_l10n.wp_stateless_get_images_media_id, response); |
| 378 |
} |
| 379 |
|
| 380 |
}, function(response) { |
| 381 |
$scope.error = true; |
| 382 |
$scope.status = $scope.getError(response, get_images_media_id + ": " + stateless_l10n.request_failed); |
| 383 |
$scope.isLoading = false; |
| 384 |
|
| 385 |
if(WP_DEBUG){ |
| 386 |
console.log(stateless_l10n.wp_stateless_get_images_media_id + ": " + stateless_l10n.request_failed, response, typeof response.headers === 'function'?response.headers() : ''); |
| 387 |
} |
| 388 |
}); |
| 389 |
|
| 390 |
}; |
| 391 |
|
| 392 |
/** |
| 393 |
* Load non-images media files |
| 394 |
* @param callback |
| 395 |
*/ |
| 396 |
$scope.getOtherMedia = function( callback, cont ) { |
| 397 |
|
| 398 |
$scope.continue = true; |
| 399 |
$scope.isLoading = true; |
| 400 |
$scope.status = stateless_l10n.loading_non_image_media_objects; |
| 401 |
|
| 402 |
$http({ |
| 403 |
method: 'GET', |
| 404 |
url: ajaxurl, |
| 405 |
params: { |
| 406 |
action: 'get_other_media_ids', |
| 407 |
continue: cont |
| 408 |
} |
| 409 |
}).then(function(response){ |
| 410 |
var data = response.data || {}; |
| 411 |
|
| 412 |
if ( data.success ) { |
| 413 |
if ( typeof callback === 'function' ) { |
| 414 |
if ( typeof data.data !== 'undefined' ) { |
| 415 |
$scope.objectIDs = data.data; |
| 416 |
callback(); |
| 417 |
} else { |
| 418 |
$scope.status = $scope.getError(response, stateless_l10n.ids_are_malformed); |
| 419 |
$scope.error = true; |
| 420 |
} |
| 421 |
} |
| 422 |
} else { |
| 423 |
$scope.status = $scope.getError(response, stateless_l10n.unable_to_get_non_images_media_id); |
| 424 |
$scope.error = true; |
| 425 |
} |
| 426 |
|
| 427 |
$scope.isLoading = false; |
| 428 |
|
| 429 |
if(WP_DEBUG){ |
| 430 |
console.log("WP-Stateless get non Images Media ID:", response, typeof response.headers === 'function'?response.headers(): ""); |
| 431 |
} |
| 432 |
}, function(response) { |
| 433 |
$scope.error = true; |
| 434 |
$scope.status = $scope.getError(response, stateless_l10n.get_non_images_media_id_request_failed); |
| 435 |
$scope.isLoading = false; |
| 436 |
if(WP_DEBUG){ |
| 437 |
console.log("WP-Stateless get non Images Media ID: Request failed", response, typeof response.headers === 'function'?response.headers(): ""); |
| 438 |
} |
| 439 |
}); |
| 440 |
|
| 441 |
}; |
| 442 |
|
| 443 |
/** |
| 444 |
* Load non-images media files |
| 445 |
* @param callback |
| 446 |
*/ |
| 447 |
$scope.getNonLibraryFiles = function( callback, cont ) { |
| 448 |
|
| 449 |
$scope.continue = true; |
| 450 |
$scope.isLoading = true; |
| 451 |
$scope.status = stateless_l10n.loading_non_library_objects; |
| 452 |
|
| 453 |
$http({ |
| 454 |
method: 'GET', |
| 455 |
url: ajaxurl, |
| 456 |
params: { |
| 457 |
action: 'get_non_library_files_id', |
| 458 |
continue: cont |
| 459 |
} |
| 460 |
}).then(function(response){ |
| 461 |
var data = response.data || {}; |
| 462 |
|
| 463 |
if ( data.success ) { |
| 464 |
if ( typeof callback === 'function' ) { |
| 465 |
if ( typeof data.data !== 'undefined' ) { |
| 466 |
$scope.objectIDs = data.data; |
| 467 |
callback(); |
| 468 |
} else { |
| 469 |
$scope.status = $scope.getError(response, stateless_l10n.ids_are_malformed); |
| 470 |
$scope.error = true; |
| 471 |
} |
| 472 |
} |
| 473 |
} else { |
| 474 |
$scope.error = true; |
| 475 |
$scope.status = $scope.getError(response, stateless_l10n.non_libraries_files_are_not_found); |
| 476 |
} |
| 477 |
|
| 478 |
$scope.isLoading = false; |
| 479 |
|
| 480 |
if(WP_DEBUG){ |
| 481 |
console.log("WP-Stateless get non library files:", response, typeof response.headers === 'function'?response.headers(): ""); |
| 482 |
} |
| 483 |
}, function(response) { |
| 484 |
$scope.error = true; |
| 485 |
$scope.status = $scope.getError(response, stateless_l10n.get_non_library_files_request_failed); |
| 486 |
$scope.isLoading = false; |
| 487 |
|
| 488 |
if(WP_DEBUG){ |
| 489 |
console.log("WP-Stateless get non library files: Request failed", response, typeof response.headers === 'function'?response.headers(): ""); |
| 490 |
} |
| 491 |
}); |
| 492 |
|
| 493 |
}; |
| 494 |
|
| 495 |
/** |
| 496 |
* Run sync for files |
| 497 |
*/ |
| 498 |
$scope.syncNonLibraryFiles = function() { |
| 499 |
$scope.isRunning = true; |
| 500 |
$scope.objectsTotal = $scope.objectIDs.length; |
| 501 |
$scope.objectsCounter = 0; |
| 502 |
$scope.status = stateless_l10n.processing_files + $scope.objectsTotal + stateless_l10n._total___; |
| 503 |
|
| 504 |
jQuery("#regenthumbs-bar").progressbar("value", 0); |
| 505 |
jQuery("#regenthumbs-bar-percent").html( "0%" ); |
| 506 |
|
| 507 |
if ( $scope.objectIDs.length ) { |
| 508 |
//$scope.syncSingleNonLibraryFile( $scope.objectIDs.shift() ); |
| 509 |
$scope.chunkIDs = array_bulk( $scope.objectIDs, $scope.bulk_size ); |
| 510 |
for ( var i in $scope.chunkIDs ) { |
| 511 |
$scope.syncSingleNonLibraryFile( $scope.chunkIDs[ i ].shift(), i ); |
| 512 |
} |
| 513 |
} |
| 514 |
} |
| 515 |
|
| 516 |
/** |
| 517 |
* Run sync for files |
| 518 |
*/ |
| 519 |
$scope.syncFiles = function() { |
| 520 |
$scope.isRunning = true; |
| 521 |
$scope.objectsTotal = $scope.objectIDs.length; |
| 522 |
$scope.objectsCounter = 0; |
| 523 |
$scope.status = stateless_l10n.processing_files + $scope.objectsTotal + stateless_l10n._total___; |
| 524 |
|
| 525 |
jQuery("#regenthumbs-bar").progressbar("value", 0); |
| 526 |
jQuery("#regenthumbs-bar-percent").html( "0%" ); |
| 527 |
|
| 528 |
if ( $scope.objectIDs.length ) { |
| 529 |
//$scope.syncSingleFile( $scope.objectIDs.shift() ); |
| 530 |
$scope.chunkIDs = array_bulk( $scope.objectIDs, $scope.bulk_size ); |
| 531 |
for ( var i in $scope.chunkIDs ) { |
| 532 |
$scope.syncSingleFile( $scope.chunkIDs[ i ].shift(), i ); |
| 533 |
} |
| 534 |
} |
| 535 |
} |
| 536 |
|
| 537 |
/** |
| 538 |
* Run images regeneration |
| 539 |
* @param ids |
| 540 |
*/ |
| 541 |
$scope.regenerateImages = function() { |
| 542 |
$scope.isRunning = true; |
| 543 |
$scope.objectsTotal = $scope.objectIDs.length; |
| 544 |
$scope.objectsCounter = 0; |
| 545 |
$scope.status = stateless_l10n.processing_images + $scope.objectsTotal + stateless_l10n._total___; |
| 546 |
|
| 547 |
jQuery("#regenthumbs-bar").progressbar("value", 0); |
| 548 |
jQuery("#regenthumbs-bar-percent").html( "0%" ); |
| 549 |
|
| 550 |
if ( $scope.objectIDs.length ) { |
| 551 |
//$scope.regenerateSingle( $scope.objectIDs.shift() ); |
| 552 |
$scope.chunkIDs = array_bulk( $scope.objectIDs, $scope.bulk_size ); |
| 553 |
for ( var i in $scope.chunkIDs ) { |
| 554 |
$scope.regenerateSingle( $scope.chunkIDs[ i ].shift(), i ); |
| 555 |
} |
| 556 |
} |
| 557 |
}; |
| 558 |
|
| 559 |
/** |
| 560 |
* Process Single Image |
| 561 |
* @param id |
| 562 |
*/ |
| 563 |
$scope.regenerateSingle = function( id, chunk_id ) { |
| 564 |
|
| 565 |
$http({ |
| 566 |
method: 'GET', |
| 567 |
url: ajaxurl, |
| 568 |
params: { |
| 569 |
action: "stateless_process_image", |
| 570 |
id: id |
| 571 |
} |
| 572 |
}).then( |
| 573 |
function(response) { |
| 574 |
var data = response.data || {}; |
| 575 |
$scope.log.push({message:data.data || stateless_l10n.regenerate_single_image_failed}); |
| 576 |
|
| 577 |
jQuery("#regenthumbs-bar").progressbar( "value", ( ++$scope.objectsCounter / $scope.objectsTotal ) * 100 ); |
| 578 |
jQuery("#regenthumbs-bar-percent").html( Math.round( ( $scope.objectsCounter / $scope.objectsTotal ) * 1000 ) / 10 + "%" ); |
| 579 |
|
| 580 |
if(typeof response.data.success == 'undefined' || response.data.success == false){ |
| 581 |
$scope.error = true; |
| 582 |
$scope.status = $scope.getError(response, stateless_l10n.regenerate_single_image_failed); |
| 583 |
// $scope.isRunning = false; |
| 584 |
} |
| 585 |
|
| 586 |
if ( 'undefined' !== typeof chunk_id ) { |
| 587 |
if ( $scope.chunkIDs[ chunk_id ].length && $scope.continue ) { |
| 588 |
$scope.regenerateSingle( $scope.chunkIDs[ chunk_id ].shift(), chunk_id ); |
| 589 |
} else { |
| 590 |
$scope.finishProcess( chunk_id ); |
| 591 |
} |
| 592 |
} else { |
| 593 |
if ( $scope.objectIDs.length && $scope.continue ) { |
| 594 |
$scope.regenerateSingle( $scope.objectIDs.shift() ); |
| 595 |
} else { |
| 596 |
$scope.finishProcess(); |
| 597 |
} |
| 598 |
} |
| 599 |
if(WP_DEBUG){ |
| 600 |
console.log("WP-Stateless regenerate single image:", response, typeof response.headers === 'function'?response.headers(): ""); |
| 601 |
} |
| 602 |
}, |
| 603 |
function(response) { |
| 604 |
$scope.error = true; |
| 605 |
$scope.status = $scope.getError(response, stateless_l10n.regenerate_single_image_request_failed); |
| 606 |
$scope.isRunning = false; |
| 607 |
if(WP_DEBUG){ |
| 608 |
console.log("WP-Stateless regenerate single image: Request failed", response, typeof response.headers === 'function'?response.headers(): ""); |
| 609 |
} |
| 610 |
} |
| 611 |
); |
| 612 |
|
| 613 |
} |
| 614 |
|
| 615 |
/** |
| 616 |
* Process single file |
| 617 |
* @param id |
| 618 |
*/ |
| 619 |
$scope.syncSingleFile = function( id, chunk_id ) { |
| 620 |
|
| 621 |
$http({ |
| 622 |
method: 'GET', |
| 623 |
url: ajaxurl, |
| 624 |
params: { |
| 625 |
action: "stateless_process_file", |
| 626 |
id: id |
| 627 |
} |
| 628 |
}).then( |
| 629 |
function(response) { |
| 630 |
var data = response.data || {}; |
| 631 |
$scope.log.push({message: $scope.getError(response, stateless_l10n.sync_single_file_failed)}); |
| 632 |
|
| 633 |
jQuery("#regenthumbs-bar").progressbar( "value", ( ++$scope.objectsCounter / $scope.objectsTotal ) * 100 ); |
| 634 |
jQuery("#regenthumbs-bar-percent").html( Math.round( ( $scope.objectsCounter / $scope.objectsTotal ) * 1000 ) / 10 + "%" ); |
| 635 |
|
| 636 |
if(typeof response.data.success == 'undefined' || response.data.success == false){ |
| 637 |
$scope.error = true; |
| 638 |
$scope.status = $scope.getError(response, stateless_l10n.sync_single_file_failed); |
| 639 |
// $scope.isRunning = false; |
| 640 |
} |
| 641 |
|
| 642 |
if ( 'undefined' !== typeof chunk_id ) { |
| 643 |
if ( $scope.chunkIDs[ chunk_id ].length && $scope.continue ) { |
| 644 |
$scope.syncSingleFile( $scope.chunkIDs[ chunk_id ].shift(), chunk_id ); |
| 645 |
} else { |
| 646 |
$scope.finishProcess( chunk_id ); |
| 647 |
} |
| 648 |
} else { |
| 649 |
if ( $scope.objectIDs.length && $scope.continue ) { |
| 650 |
$scope.syncSingleFile( $scope.objectIDs.shift() ); |
| 651 |
} else { |
| 652 |
$scope.finishProcess(); |
| 653 |
} |
| 654 |
} |
| 655 |
|
| 656 |
if(WP_DEBUG){ |
| 657 |
console.log("WP-Stateless sync single file:", response, typeof response.headers === 'function'?response.headers(): ""); |
| 658 |
} |
| 659 |
}, |
| 660 |
function(response) { |
| 661 |
$scope.error = true; |
| 662 |
$scope.status = $scope.getError(response, stateless_l10n.sync_single_file_request_failed); |
| 663 |
$scope.isRunning = false; |
| 664 |
|
| 665 |
if(WP_DEBUG){ |
| 666 |
console.log("WP-Stateless sync single file: Request failed", response, typeof response.headers === 'function'?response.headers(): ""); |
| 667 |
} |
| 668 |
} |
| 669 |
); |
| 670 |
|
| 671 |
} |
| 672 |
|
| 673 |
/** |
| 674 |
* Process single file |
| 675 |
* @param id |
| 676 |
*/ |
| 677 |
$scope.syncSingleNonLibraryFile = function( id, chunk_id ) { |
| 678 |
|
| 679 |
$http({ |
| 680 |
method: 'GET', |
| 681 |
url: ajaxurl, |
| 682 |
params: { |
| 683 |
action: "stateless_process_non_library_file", |
| 684 |
file_path: id |
| 685 |
} |
| 686 |
}).then( |
| 687 |
function(response) { |
| 688 |
var data = response.data || {}; |
| 689 |
$scope.log.push({message: $scope.getError(response, stateless_l10n.failed_to_sync + id)}); |
| 690 |
|
| 691 |
jQuery("#regenthumbs-bar").progressbar( "value", ( ++$scope.objectsCounter / $scope.objectsTotal ) * 100 ); |
| 692 |
jQuery("#regenthumbs-bar-percent").html( Math.round( ( $scope.objectsCounter / $scope.objectsTotal ) * 1000 ) / 10 + "%" ); |
| 693 |
|
| 694 |
if(typeof response.data.success == 'undefined' || response.data.success == false){ |
| 695 |
$scope.error = true; |
| 696 |
$scope.status = $scope.getError(response, stateless_l10n.sync_non_library_file_failed); |
| 697 |
// $scope.isRunning = false; |
| 698 |
} |
| 699 |
|
| 700 |
if ( 'undefined' !== typeof chunk_id ) { |
| 701 |
if ( $scope.chunkIDs[ chunk_id ].length && $scope.continue ) { |
| 702 |
$scope.syncSingleNonLibraryFile( $scope.chunkIDs[ chunk_id ].shift(), chunk_id ); |
| 703 |
} else { |
| 704 |
$scope.finishProcess( chunk_id ); |
| 705 |
} |
| 706 |
} else { |
| 707 |
if ( $scope.objectIDs.length && $scope.continue ) { |
| 708 |
$scope.syncSingleNonLibraryFile( $scope.objectIDs.shift() ); |
| 709 |
} else { |
| 710 |
$scope.finishProcess(); |
| 711 |
} |
| 712 |
} |
| 713 |
|
| 714 |
if(WP_DEBUG){ |
| 715 |
console.log("WP-Stateless sync non library file:", response, typeof response.headers === 'function'?response.headers(): ""); |
| 716 |
} |
| 717 |
}, |
| 718 |
function(response) { |
| 719 |
$scope.error = true; |
| 720 |
$scope.status = $scope.getError(response, stateless_l10n.sync_non_library_file_request_failed); |
| 721 |
$scope.isRunning = false; |
| 722 |
|
| 723 |
if(WP_DEBUG){ |
| 724 |
console.log("WP-Stateless sync non library file: Request failed", response, typeof response.headers === 'function'?response.headers():{}); |
| 725 |
} |
| 726 |
} |
| 727 |
); |
| 728 |
|
| 729 |
} |
| 730 |
|
| 731 |
}]) |
| 732 |
.controller('wpStatelessSettings', function($scope, $filter) { |
| 733 |
$scope.backup = {}; |
| 734 |
$scope.sm = wp_stateless_settings || {}; |
| 735 |
|
| 736 |
$scope.$watch('sm.mode', function(value) { |
| 737 |
if(value == 'stateless'){ |
| 738 |
$scope.backup.hashify_file_name = $scope.sm.hashify_file_name; |
| 739 |
$scope.sm.hashify_file_name = 'true'; |
| 740 |
// $scope.apply(); |
| 741 |
} |
| 742 |
else{ |
| 743 |
if($scope.backup.hashify_file_name){ |
| 744 |
$scope.sm.hashify_file_name = $scope.backup.hashify_file_name; |
| 745 |
// $scope.apply(); |
| 746 |
} |
| 747 |
} |
| 748 |
}); |
| 749 |
|
| 750 |
$scope.sm.showNotice = function(option){ |
| 751 |
if($scope.sm.readonly && $scope.sm.readonly[option]){ |
| 752 |
var slug = $scope.sm.readonly[option]; |
| 753 |
return $scope.sm.strings[slug]; |
| 754 |
} |
| 755 |
} |
| 756 |
|
| 757 |
$scope.sm.generatePreviewUrl = function() { |
| 758 |
$scope.sm.is_custom_domain = false; |
| 759 |
var host = 'https://storage.googleapis.com/'; |
| 760 |
var rootdir = $scope.sm.root_dir ? $scope.sm.root_dir + '/' : ''; |
| 761 |
var subdir = $scope.sm.organize_media == '1' ? $filter('date')(Date.now(), 'yyyy/MM') + '/' : ''; |
| 762 |
var hash = $scope.sm.hashify_file_name == 'true' ? Date.now().toString(36) + '-' : ''; |
| 763 |
var is_ssl = $scope.sm.custom_domain.indexOf('https://'); |
| 764 |
var custom_domain = $scope.sm.custom_domain.toString(); |
| 765 |
|
| 766 |
custom_domain = custom_domain.replace(/\/+$/, ''); // removing trailing slashes |
| 767 |
custom_domain = custom_domain.replace(/https?:\/\//, ''); // removing http:// or https:// from the beginning. |
| 768 |
host += $scope.sm.bucket ? $scope.sm.bucket : '{bucket-name}'; |
| 769 |
|
| 770 |
if ( custom_domain !== 'storage.googleapis.com' && $scope.sm.bucket && custom_domain && ( is_ssl === 0 || custom_domain != $scope.sm.bucket ) ) { |
| 771 |
$scope.sm.is_custom_domain = true; |
| 772 |
host = is_ssl === 0 ? 'https://' : 'http://'; // bucketname will be host |
| 773 |
host += custom_domain; |
| 774 |
} |
| 775 |
|
| 776 |
$scope.sm.preview_url = host + "/" + rootdir + subdir + hash + "your-image-name.jpeg"; |
| 777 |
} |
| 778 |
|
| 779 |
$scope.sm.generatePreviewUrl(); |
| 780 |
|
| 781 |
}) |
| 782 |
.controller('wpStatelessCompatibility', function($scope, $filter) { |
| 783 |
$scope.modules = wp_stateless_compatibility || {}; |
| 784 |
}); |
| 785 |
|
| 786 |
wpStatelessApp.filter("trust", ['$sce', function($sce) { |
| 787 |
return function(htmlCode){ |
| 788 |
return $sce.trustAsHtml(htmlCode); |
| 789 |
} |
| 790 |
}]); |
| 791 |
|