PluginProbe
WP-Stateless – Google Cloud Storage / 3.0.2
WP-Stateless – Google Cloud Storage v3.0.2
4.4.3 2.1.7 2.1.8 2.1.9 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.2.7 2.3.0 2.3.1 2.3.2 3.0 3.0.1 3.0.2 3.0.3 3.0.4 3.1.0 3.1.1 3.2.0 3.2.1 3.2.2 All 62 releases
wp-stateless / static / scripts / wp-stateless.js

wp-stateless.js in WP-Stateless – Google Cloud Storage 3.0.2, at static/scripts/wp-stateless.js

1,027 lines 30.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 /**
2 *
3 *
4 */
5
6 var $wildcards_select = jQuery(".select-wildcards").select2({
7 tags: true,
8 tokenSeparators: ['/'],
9 createTag: function (params) {
10 var term = jQuery.trim(params.term);
11
12 if (term === '') {
13 return null;
14 }
15
16 let tags = [ "%site_id%", "%site_url%", "%site_url_host%", "%site_url_path%", "%date_year/date_month%" ];
17
18 // Remove special chars from tags
19 if (! /^[a-zA-Z0-9_\-.]+$/.test(term) && jQuery.inArray (term, tags) == -1) {
20 term = term.replace(/[^a-zA-Z0-9_\-.]/g,'');
21 }
22
23 return {
24 id: term,
25 text: term
26 }
27 },
28 templateSelection: function (state) {
29 // Add slash at the en of the tag
30 return state.text;
31 },
32 insertTag: function (data, tag) {
33 // Insert the tag at the end of the results
34 data.push(tag);
35 }
36 });
37 jQuery(".select-wildcards").on("select2:select", function (evt) {
38 var element = evt.params.data.element;
39 var $element = jQuery(element);
40 let $element_value = $element.value;
41
42 $element.detach();
43 jQuery(this).append($element);
44
45 /**
46 * add slash after tag
47 */
48 if ( $element_value !== '/' ) {
49 // Create the DOM option that is pre-selected by default
50 var newState = new Option('/', '/', true, true);
51 jQuery(newState).prop('disabled', true);
52 // Append it to the select
53 jQuery(this).append(newState);
54 }
55
56 jQuery(this).trigger("change");
57 prepare_preview_url();
58 });
59
60 jQuery(".select-wildcards").on("select2:unselect", function (evt) {
61 if (evt.params.data.id === '/') {
62 var element = evt.params.data.element;
63 var $element = jQuery(element);
64 $element.detach();
65 }
66 prepare_preview_url();
67 });
68
69 function prepare_preview_url () {
70 let root_dir = '';
71 let selected_wildcards = jQuery('.select-wildcards').val();
72
73 if (selected_wildcards !== null) {
74 root_dir = selected_wildcards.join('/');
75 }
76
77 jQuery("#sm_root_dir").val(root_dir);
78 jQuery("#sm_root_dir").trigger("change");
79 }
80
81 function replace_wildcard_to_the_end ( wildcards, remove_slashes = false ) {
82 if ( remove_slashes ) {
83 jQuery(".select-wildcards > option").each(function() {
84 if ( this.value == '/' ) {
85 jQuery(this).detach();
86 }
87 });
88 }
89 wildcards.forEach(function(wildcard) {
90 wildcard_exist = false;
91 jQuery(".select-wildcards > option").each(function () {
92 if (this.value === wildcard && this.value !== '/') {
93 jQuery(this).detach();
94 jQuery(".select-wildcards").append(jQuery(this));
95 wildcard_exist = true;
96 }
97 });
98 if (!wildcard_exist) {
99 var newState = new Option('/', '/', true, true);
100 jQuery(newState).prop('disabled', true);
101 // Append it to the select
102 jQuery(".select-wildcards").append(newState);
103 }
104 });
105 }
106
107 // Application
108 var wpStatelessApp = angular.module('wpStatelessApp', ['ngSanitize'])
109
110 // Controller
111 .controller('wpStatelessTools', ['$scope', '$http', function ($scope, $http) {
112
113 var WP_DEBUG = wp_stateless_configs.WP_DEBUG || false;
114 $scope.action = 'regenerate_images';
115 $scope.method = 'start';
116 $scope.bulk_size = 1;
117
118 /**
119 * Counters
120 * @type {number}
121 */
122 $scope.objectsCounter = 0;
123 $scope.objectsTotal = 0;
124
125 /**
126 * Error storage
127 * @type {boolean}
128 */
129 $scope.error = false;
130
131 /**
132 * Flags
133 * @type {boolean}
134 */
135 $scope.isRunning = false;
136 $scope.isLoading = false;
137 $scope.continue = true;
138
139 /**
140 *
141 * @type {{images: boolean, other: boolean}}
142 */
143 $scope.progresses = {
144 images: false,
145 other: false
146 };
147
148 /**
149 *
150 * @type {{images: boolean, other: boolean}}
151 */
152 $scope.fails = {
153 images: false,
154 other: false
155 }
156
157 /**
158 * IDs storage
159 * @type {Array}
160 */
161 $scope.objectIDs = [];
162
163 /**
164 *
165 * @type {Array}
166 */
167 $scope.chunkIDs = [];
168
169 /**
170 * Log
171 * @type {Array}
172 */
173 $scope.log = [];
174
175 /**
176 * Status message
177 * @type {String}
178 */
179 $scope.status = '';
180
181 /**
182 * Status message
183 * @type {String}
184 */
185 $scope.extraStatus = '';
186
187 /**
188 * Init
189 */
190 $scope.init = function() {
191 jQuery("#regenthumbs-bar").progressbar();
192 }
193
194 /**
195 * Get error message
196 */
197 $scope.getError = function(response, message) {
198 if(response.data && typeof response.data.data !== 'undefined' && typeof response.data.success !== 'undefined' && response.data.success == false){
199 $scope.extraStatus = response.data.data;
200 return message;
201 }
202
203 if(response.data && typeof response.data.data !== 'undefined'){
204 return response.data.data;
205 }
206
207 if(response.data && typeof response.data == 'string'){
208 $scope.extraStatus = response.data;
209 return message;
210 }
211
212 if(!response.data && response.statusText){
213 return response.statusText + " (" + stateless_l10n.response_code + response.status + ")";
214 }
215
216 if(!response.data && response.status == -1){
217 return stateless_l10n.unable_to_connect_to_the_server;
218 }
219
220 return message;
221 }
222
223 /**
224 *
225 * @param callback
226 */
227 $scope.getBucketFolder = function( callback ) {
228 $scope.isLoading = true;
229
230 $http({
231 method: 'GET',
232 url: ajaxurl,
233 params: {
234 action: 'stateless_get_bucket_folder'
235 }
236 }).then(function(response){
237 var data = response.data || {};
238
239 if ( data.success ) {
240 if ( typeof data.data !== 'undefined' ) {
241 $scope.root_dir = data.data.bucket_folder;
242
243 if ( 'function' === typeof callback ) {
244 callback();
245 }
246 } else {
247 console.error( stateless_l10n.could_not_retrieve_progress );
248 }
249 } else {
250 console.error( stateless_l10n.could_not_retrieve_progress );
251 }
252
253 $scope.isLoading = false;
254 }, function(response) {
255 console.error( stateless_l10n.could_not_retrieve_progress );
256
257 $scope.isLoading = false;
258 });
259 };
260
261 /**
262 *
263 */
264 $scope.getBucketFolder();
265
266 /**
267 *
268 * @param callback
269 */
270 $scope.getCurrentProgresses = function( callback ) {
271 $scope.isLoading = true;
272
273 $http({
274 method: 'GET',
275 url: ajaxurl,
276 params: {
277 action: 'stateless_get_current_progresses'
278 }
279 }).then(function(response){
280 var data = response.data || {};
281
282 if ( data.success ) {
283 if ( typeof data.data !== 'undefined' ) {
284 $scope.progresses.images = data.data.images;
285 $scope.progresses.other = data.data.other;
286 $scope.startFrom = null;
287 if($scope.action == 'regenerate_images' && typeof $scope.progresses.images[1] == "number"){
288 // Subtracting 1 so user get where from the sync will start instead of where it ended. We will add 1 in class-ajax.php.
289 $scope.startFrom = $scope.progresses.images[1] - 1;
290 }
291 else if($scope.action == 'sync_non_images' && typeof $scope.progresses.other[1] == "number"){
292 // Subtracting 1 so user get where from the sync will start instead of where it ended. We will add 1 in class-ajax.php.
293 $scope.startFrom = $scope.progresses.other[1] - 1;
294 }
295
296 if ( 'function' === typeof callback ) {
297 callback();
298 }
299 } else {
300 console.error( stateless_l10n.could_not_retrieve_progress );
301 }
302 } else {
303 console.error( stateless_l10n.could_not_retrieve_progress );
304 }
305
306 $scope.isLoading = false;
307 }, function(response) {
308 console.error( stateless_l10n.could_not_retrieve_progress );
309
310 $scope.isLoading = false;
311 });
312 };
313
314 /**
315 *
316 */
317 $scope.getCurrentProgresses();
318
319 /**
320 *
321 * @param callback
322 */
323 function getAllFails( callback ) {
324 $scope.isLoading = true;
325
326 $http({
327 method: 'GET',
328 url: ajaxurl,
329 params: {
330 action: 'stateless_get_all_fails'
331 }
332 }).then(function(response){
333 var data = response.data || {};
334
335 if ( data.success ) {
336 if ( typeof data.data !== 'undefined' ) {
337
338 $scope.fails.images = data.data.images;
339 $scope.fails.other = data.data.other;
340
341 if ( 'function' === typeof callback ) {
342 callback();
343 }
344 } else {
345 console.error( stateless_l10n.could_not_get_fails );
346 }
347 } else {
348 console.error( stateless_l10n.could_not_get_fails );
349 }
350
351 $scope.isLoading = false;
352 }, function(response) {
353 console.error( stateless_l10n.could_not_get_fails );
354
355 $scope.isLoading = false;
356 });
357 };
358
359 getAllFails();
360
361 /**
362 * Form submit handler
363 * @param e
364 * @returns {boolean}
365 */
366 $scope.processStart = function(e) {
367
368 $scope.error = false;
369 $scope.status = '';
370 $scope.extraStatus = '';
371 $scope.objectsCounter = 0;
372 $scope.objectsTotal = 0;
373 $scope.objectIDs = [];
374 $scope.chunkIDs = [];
375
376 if ( $scope.method === 'fix' ) {
377
378 if ( $scope.action ) {
379 switch( $scope.action ) {
380 case 'regenerate_images':
381 $scope.objectIDs = $scope.fails.images;
382 $scope.regenerateImages();
383 break;
384 case 'sync_non_images':
385 $scope.objectIDs = $scope.fails.other;
386 $scope.syncFiles();
387 break;
388 default: break;
389 }
390 }
391
392 return false;
393 }
394
395 var cont = 0;
396 if ( 'continue' === $scope.method ) {
397 cont = 1;
398 }
399
400 if ( $scope.action ) {
401 switch( $scope.action ) {
402 case 'regenerate_images':
403 $scope.getImagesMedia( $scope.regenerateImages, cont, $scope.use_wildcards );
404 break;
405 case 'sync_non_images':
406 $scope.getOtherMedia( $scope.syncFiles, cont, $scope.use_wildcards );
407 break;
408 case 'sync_non_library_files':
409 $scope.getNonLibraryFiles( $scope.syncNonLibraryFiles, cont );
410 break;
411 default: break;
412 }
413 }
414
415 return false;
416 };
417
418 /**
419 * Stop process
420 */
421 $scope.processStop = function() {
422 $scope.status = stateless_l10n.stopping;
423 $scope.continue = false;
424 };
425
426 function array_bulk( arr, bulk_size ) {
427 var groups = [];
428 var i;
429
430 for ( i = 0; i < bulk_size; i++ ) {
431 groups[ i ] = [];
432 }
433
434 for ( i = 0; i < arr.length; i ++ ) {
435 groups[ i % bulk_size ].push( arr[ i ] );
436 }
437
438 return groups;
439 }
440
441 $scope.finishProcess = function( chunk_id ) {
442 var mode = 'images';
443 if ( 'sync_non_images' === $scope.action ) {
444 mode = 'other';
445 }
446
447 if ( $scope.objectsCounter >= $scope.objectsTotal ) {
448 // process finished
449
450 $http({
451 method: 'GET',
452 url: ajaxurl,
453 params: {
454 action: 'stateless_reset_progress',
455 mode: mode
456 }
457 }).then(function(response){
458 $scope.progresses[ mode ] = false;
459
460 $scope.status = stateless_l10n.finished;
461 $scope.isLoading = false;
462 $scope.isRunning = false;
463 }, function(response) {
464 console.error( stateless_l10n.could_not_reset_progress );
465 });
466 } else if ( 'undefined' !== typeof chunk_id ) {
467 // process cancelled, but this is only a chunk finishing request
468
469 $scope.chunkIDs[ chunk_id ] = false;
470 var all_done = true;
471 for ( var i in $scope.chunkIDs ) {
472 if ( false !== $scope.chunkIDs[ i ] ) {
473 all_done = false;
474 break;
475 }
476 }
477 if ( all_done ) {
478 $scope.getCurrentProgresses( function() {
479 $scope.status = stateless_l10n.cancelled;
480 $scope.isRunning = false;
481 });
482 }
483 } else {
484 // process cancelled
485
486 $scope.getCurrentProgresses( function() {
487 $scope.status = stateless_l10n.cancelled;
488 $scope.isRunning = false;
489 });
490 }
491 };
492
493 /**
494 * Load images IDs
495 * @param callback
496 */
497 $scope.getImagesMedia = function( callback, cont, use_wildcards ) {
498
499 $scope.continue = true;
500 $scope.isLoading = true;
501 $scope.status = stateless_l10n.loading_images_media_objects;
502
503 $http({
504 method: 'GET',
505 url: ajaxurl,
506 params: {
507 action: 'get_images_media_ids',
508 start_from: $scope.startFrom,
509 continue: cont
510 }
511 }).then(function(response){
512 var data = response.data || {};
513
514 if ( data.success ) {
515 if ( typeof callback === 'function' ) {
516 if ( typeof data.data !== 'undefined' ) {
517 $scope.objectIDs = data.data;
518 $scope.use_wildcards = use_wildcards;
519 callback();
520 } else {
521 $scope.status = stateless_l10n.ids_are_malformed;
522 $scope.error = true;
523 }
524 }
525 } else {
526 $scope.status = $scope.getError(response, stateless_l10n.unable_to_get_images_media_id);
527 $scope.error = true;
528 }
529
530 $scope.isLoading = false;
531
532 if(WP_DEBUG){
533 console.log(stateless_l10n.wp_stateless_get_images_media_id, response);
534 }
535
536 }, function(response) {
537 $scope.error = true;
538 $scope.status = $scope.getError(response, get_images_media_id + ": " + stateless_l10n.request_failed);
539 $scope.isLoading = false;
540
541 if(WP_DEBUG){
542 console.log(stateless_l10n.wp_stateless_get_images_media_id + ": " + stateless_l10n.request_failed, response, typeof response.headers === 'function'?response.headers() : '');
543 }
544 });
545
546 };
547
548 /**
549 * Load non-images media files
550 * @param callback
551 */
552 $scope.getOtherMedia = function( callback, cont, use_wildcards ) {
553
554 $scope.continue = true;
555 $scope.isLoading = true;
556 $scope.status = stateless_l10n.loading_non_image_media_objects;
557
558 $http({
559 method: 'GET',
560 url: ajaxurl,
561 params: {
562 action: 'get_other_media_ids',
563 start_from: $scope.startFrom,
564 continue: cont
565 }
566 }).then(function(response){
567 var data = response.data || {};
568
569 if ( data.success ) {
570 if ( typeof callback === 'function' ) {
571 if ( typeof data.data !== 'undefined' ) {
572 $scope.objectIDs = data.data;
573 $scope.use_wildcards = use_wildcards;
574 callback();
575 } else {
576 $scope.status = $scope.getError(response, stateless_l10n.ids_are_malformed);
577 $scope.error = true;
578 }
579 }
580 } else {
581 $scope.status = $scope.getError(response, stateless_l10n.unable_to_get_non_images_media_id);
582 $scope.error = true;
583 }
584
585 $scope.isLoading = false;
586
587 if(WP_DEBUG){
588 console.log("WP-Stateless get non Images Media ID:", response, typeof response.headers === 'function'?response.headers(): "");
589 }
590 }, function(response) {
591 $scope.error = true;
592 $scope.status = $scope.getError(response, stateless_l10n.get_non_images_media_id_request_failed);
593 $scope.isLoading = false;
594 if(WP_DEBUG){
595 console.log("WP-Stateless get non Images Media ID: Request failed", response, typeof response.headers === 'function'?response.headers(): "");
596 }
597 });
598
599 };
600
601 /**
602 * Load non-images media files
603 * @param callback
604 */
605 $scope.getNonLibraryFiles = function( callback, cont ) {
606
607 $scope.continue = true;
608 $scope.isLoading = true;
609 $scope.status = stateless_l10n.loading_non_library_objects;
610
611 $http({
612 method: 'GET',
613 url: ajaxurl,
614 params: {
615 action: 'get_non_library_files_id',
616 continue: cont
617 }
618 }).then(function(response){
619 var data = response.data || {};
620
621 if ( data.success ) {
622 if ( typeof callback === 'function' ) {
623 if ( typeof data.data !== 'undefined' ) {
624 $scope.objectIDs = data.data;
625 callback();
626 } else {
627 $scope.status = $scope.getError(response, stateless_l10n.ids_are_malformed);
628 $scope.error = true;
629 }
630 }
631 } else {
632 $scope.error = true;
633 $scope.status = $scope.getError(response, stateless_l10n.non_libraries_files_are_not_found);
634 }
635
636 $scope.isLoading = false;
637
638 if(WP_DEBUG){
639 console.log("WP-Stateless get non library files:", response, typeof response.headers === 'function'?response.headers(): "");
640 }
641 }, function(response) {
642 $scope.error = true;
643 $scope.status = $scope.getError(response, stateless_l10n.get_non_library_files_request_failed);
644 $scope.isLoading = false;
645
646 if(WP_DEBUG){
647 console.log("WP-Stateless get non library files: Request failed", response, typeof response.headers === 'function'?response.headers(): "");
648 }
649 });
650
651 };
652
653 /**
654 * Run sync for files
655 */
656 $scope.syncNonLibraryFiles = function() {
657 $scope.isRunning = true;
658 $scope.objectsTotal = $scope.objectIDs.length;
659 $scope.objectsCounter = 0;
660 $scope.status = stateless_l10n.processing_files + $scope.objectsTotal + stateless_l10n._total___;
661
662 jQuery("#regenthumbs-bar").progressbar("value", 0);
663 jQuery("#regenthumbs-bar-percent").html( "0%" );
664
665 if ( $scope.objectIDs.length ) {
666 //$scope.syncSingleNonLibraryFile( $scope.objectIDs.shift() );
667 $scope.chunkIDs = array_bulk( $scope.objectIDs, $scope.bulk_size );
668 for ( var i in $scope.chunkIDs ) {
669 $scope.syncSingleNonLibraryFile( $scope.chunkIDs[ i ].shift(), i );
670 }
671 }
672 }
673
674 /**
675 * Run sync for files
676 */
677 $scope.syncFiles = function() {
678 $scope.isRunning = true;
679 $scope.objectsTotal = $scope.objectIDs.length;
680 $scope.objectsCounter = 0;
681 $scope.status = stateless_l10n.processing_files + $scope.objectsTotal + stateless_l10n._total___;
682
683 jQuery("#regenthumbs-bar").progressbar("value", 0);
684 jQuery("#regenthumbs-bar-percent").html( "0%" );
685
686 if ( $scope.objectIDs.length ) {
687 //$scope.syncSingleFile( $scope.objectIDs.shift() );
688 $scope.chunkIDs = array_bulk( $scope.objectIDs, $scope.bulk_size );
689 for ( var i in $scope.chunkIDs ) {
690 $scope.syncSingleFile( $scope.chunkIDs[ i ].shift(), $scope.use_wildcards, i );
691 }
692 }
693 }
694
695 /**
696 * Run images regeneration
697 * @param ids
698 */
699 $scope.regenerateImages = function() {
700 $scope.isRunning = true;
701 $scope.objectsTotal = $scope.objectIDs.length;
702 $scope.objectsCounter = 0;
703 $scope.status = stateless_l10n.processing_images + $scope.objectsTotal + stateless_l10n._total___;
704
705 jQuery("#regenthumbs-bar").progressbar("value", 0);
706 jQuery("#regenthumbs-bar-percent").html( "0%" );
707
708 if ( $scope.objectIDs.length ) {
709 //$scope.regenerateSingle( $scope.objectIDs.shift() );
710 $scope.chunkIDs = array_bulk( $scope.objectIDs, $scope.bulk_size );
711 for ( var i in $scope.chunkIDs ) {
712 $scope.regenerateSingle( $scope.chunkIDs[ i ].shift(), $scope.use_wildcards, i );
713 }
714 }
715 };
716
717 /**
718 * Process Single Image
719 * @param id
720 */
721 $scope.regenerateSingle = function( id, use_wildcards, chunk_id ) {
722
723 $http({
724 method: 'GET',
725 url: ajaxurl,
726 params: {
727 action: "stateless_process_image",
728 id: id,
729 use_wildcards: use_wildcards
730 }
731 }).then(
732 function(response) {
733 var data = response.data || {};
734 $scope.log.push({message:data.data || stateless_l10n.regenerate_single_image_failed});
735
736 jQuery("#regenthumbs-bar").progressbar( "value", ( ++$scope.objectsCounter / $scope.objectsTotal ) * 100 );
737 jQuery("#regenthumbs-bar-percent").html( Math.round( ( $scope.objectsCounter / $scope.objectsTotal ) * 1000 ) / 10 + "%" );
738
739 if(typeof response.data.success == 'undefined' || response.data.success == false){
740 $scope.error = true;
741 $scope.status = $scope.getError(response, stateless_l10n.regenerate_single_image_failed);
742 // $scope.isRunning = false;
743 }
744
745 if ( 'undefined' !== typeof chunk_id ) {
746 if ( $scope.chunkIDs[ chunk_id ].length && $scope.continue ) {
747 $scope.regenerateSingle( $scope.chunkIDs[ chunk_id ].shift(), use_wildcards, chunk_id );
748 } else {
749 $scope.finishProcess( chunk_id );
750 }
751 } else {
752 if ( $scope.objectIDs.length && $scope.continue ) {
753 $scope.regenerateSingle( $scope.objectIDs.shift(), use_wildcards );
754 } else {
755 $scope.finishProcess();
756 }
757 }
758 if(WP_DEBUG){
759 console.log("WP-Stateless regenerate single image:", response, typeof response.headers === 'function'?response.headers(): "");
760 }
761 },
762 function(response) {
763 $scope.error = true;
764 $scope.status = $scope.getError(response, stateless_l10n.regenerate_single_image_request_failed);
765 $scope.isRunning = false;
766 if(WP_DEBUG){
767 console.log("WP-Stateless regenerate single image: Request failed", response, typeof response.headers === 'function'?response.headers(): "");
768 }
769 }
770 );
771
772 }
773
774 /**
775 * Process single file
776 * @param id
777 */
778 $scope.syncSingleFile = function( id, use_wildcards, chunk_id ) {
779
780 $http({
781 method: 'GET',
782 url: ajaxurl,
783 params: {
784 action: "stateless_process_file",
785 id: id,
786 use_wildcards: use_wildcards
787 }
788 }).then(
789 function(response) {
790 var data = response.data || {};
791 $scope.log.push({message: $scope.getError(response, stateless_l10n.sync_single_file_failed)});
792
793 jQuery("#regenthumbs-bar").progressbar( "value", ( ++$scope.objectsCounter / $scope.objectsTotal ) * 100 );
794 jQuery("#regenthumbs-bar-percent").html( Math.round( ( $scope.objectsCounter / $scope.objectsTotal ) * 1000 ) / 10 + "%" );
795
796 if(typeof response.data.success == 'undefined' || response.data.success == false){
797 $scope.error = true;
798 $scope.status = $scope.getError(response, stateless_l10n.sync_single_file_failed);
799 // $scope.isRunning = false;
800 }
801
802 if ( 'undefined' !== typeof chunk_id ) {
803 if ( $scope.chunkIDs[ chunk_id ].length && $scope.continue ) {
804 $scope.syncSingleFile( $scope.chunkIDs[ chunk_id ].shift(), use_wildcards, chunk_id );
805 } else {
806 $scope.finishProcess( chunk_id );
807 }
808 } else {
809 if ( $scope.objectIDs.length && $scope.continue ) {
810 $scope.syncSingleFile( $scope.objectIDs.shift(), use_wildcards );
811 } else {
812 $scope.finishProcess();
813 }
814 }
815
816 if(WP_DEBUG){
817 console.log("WP-Stateless sync single file:", response, typeof response.headers === 'function'?response.headers(): "");
818 }
819 },
820 function(response) {
821 $scope.error = true;
822 $scope.status = $scope.getError(response, stateless_l10n.sync_single_file_request_failed);
823 $scope.isRunning = false;
824
825 if(WP_DEBUG){
826 console.log("WP-Stateless sync single file: Request failed", response, typeof response.headers === 'function'?response.headers(): "");
827 }
828 }
829 );
830
831 }
832
833 /**
834 * Process single file
835 * @param id
836 */
837 $scope.syncSingleNonLibraryFile = function( id, chunk_id ) {
838
839 $http({
840 method: 'GET',
841 url: ajaxurl,
842 params: {
843 action: "stateless_process_non_library_file",
844 file_path: id
845 }
846 }).then(
847 function(response) {
848 var data = response.data || {};
849 $scope.log.push({message: $scope.getError(response, stateless_l10n.failed_to_sync + id)});
850
851 jQuery("#regenthumbs-bar").progressbar( "value", ( ++$scope.objectsCounter / $scope.objectsTotal ) * 100 );
852 jQuery("#regenthumbs-bar-percent").html( Math.round( ( $scope.objectsCounter / $scope.objectsTotal ) * 1000 ) / 10 + "%" );
853
854 if(typeof response.data.success == 'undefined' || response.data.success == false){
855 $scope.error = true;
856 $scope.status = $scope.getError(response, stateless_l10n.sync_non_library_file_failed);
857 // $scope.isRunning = false;
858 }
859
860 if ( 'undefined' !== typeof chunk_id ) {
861 if ( $scope.chunkIDs[ chunk_id ].length && $scope.continue ) {
862 $scope.syncSingleNonLibraryFile( $scope.chunkIDs[ chunk_id ].shift(), chunk_id );
863 } else {
864 $scope.finishProcess( chunk_id );
865 }
866 } else {
867 if ( $scope.objectIDs.length && $scope.continue ) {
868 $scope.syncSingleNonLibraryFile( $scope.objectIDs.shift() );
869 } else {
870 $scope.finishProcess();
871 }
872 }
873
874 if(WP_DEBUG){
875 console.log("WP-Stateless sync non library file:", response, typeof response.headers === 'function'?response.headers(): "");
876 }
877 },
878 function(response) {
879 $scope.error = true;
880 $scope.status = $scope.getError(response, stateless_l10n.sync_non_library_file_request_failed);
881 $scope.isRunning = false;
882
883 if(WP_DEBUG){
884 console.log("WP-Stateless sync non library file: Request failed", response, typeof response.headers === 'function'?response.headers():{});
885 }
886 }
887 );
888
889 }
890
891 }])
892 .controller('wpStatelessSettings', function($scope, $filter) {
893 $scope.backup = {};
894 $scope.sm = wp_stateless_settings || {};
895 $scope.sm.readonly = $scope.sm.readonly || {}
896
897 if ($scope.sm.network_admin) {
898 $scope.sm.hashify_file_name = 'true';
899 $scope.sm.readonly.hashify_file_name = true;
900 }
901
902 $scope.$watch('sm.mode', function(value) {
903 if( (value == 'stateless' || value == 'ephemeral') && $scope.sm.readonly.hashify_file_name != 'constant'){
904 $scope.backup.hashify_file_name = $scope.sm.hashify_file_name;
905 $scope.sm.hashify_file_name = 'true';
906 // $scope.apply();
907 }
908 else{
909 if($scope.backup.hashify_file_name){
910 $scope.sm.hashify_file_name = $scope.backup.hashify_file_name;
911 // $scope.apply();
912 }
913 }
914 });
915
916 $scope.$watch('sm.bucket_folder_type', function(value) {
917 if(value == 'single-site'){
918 replace_wildcard_to_the_end( [ "/", "%date_year/date_month%", "/"], true );
919 $wildcards_select.val([ "/", "%date_year/date_month%", "/" ]).trigger("change");
920 }
921 else if(value == 'multi-site'){
922 //changins wildcards position
923 replace_wildcard_to_the_end( [ "/", "sites", "/", "%site_id%", "/", "%date_year/date_month%", "/" ],true );
924 $wildcards_select.val([ "/", "sites", "/", "%site_id%", "/", "%date_year/date_month%", "/" ]).trigger("change");
925 }
926 else if(value == '' && $scope.sm.network_admin){
927 $wildcards_select.val(null).trigger("change");
928 }
929 prepare_preview_url();
930 });
931
932 $scope.$watch('sm.root_dir', function(value) {
933
934 if(value == '%date_year/date_month%'){
935 $scope.sm.bucket_folder_type = 'single-site';
936 }
937 else if(value == 'sites/%site_id%/%date_year/date_month%'){
938 $scope.sm.bucket_folder_type = 'multi-site';
939 }
940 else if(value == '' && $scope.sm.network_admin){
941 $scope.sm.bucket_folder_type = '';
942 }
943 else{
944 $scope.sm.bucket_folder_type = 'custom';
945 }
946
947 setTimeout(function(){
948 jQuery( '#permalink_structure' ).trigger('change');
949 }, 1);
950 });
951
952 var readonlyTag = $scope.sm.readonly.root_dir || false;
953 if(readonlyTag){
954 jQuery('.available-structure-tags .button').off('click').css('opacity', '.5');
955 }
956 $scope.tagClicked = function(){
957 if(readonlyTag) return false;
958 $scope.sm.bucket_folder_type = 'custom';
959 setTimeout(function(){
960 jQuery( '#permalink_structure' ).trigger('change');
961 }, 1);
962 }
963
964 $scope.sm.showNotice = function(option){
965 if($scope.sm.readonly && $scope.sm.readonly[option]){
966 var slug = $scope.sm.readonly[option];
967 return $scope.sm.strings[slug];
968 }
969 }
970
971 $scope.sm.generatePreviewUrl = function() {
972 $scope.sm.is_custom_domain = false;
973 var host = 'https://storage.googleapis.com/';
974 var hash = $scope.sm.hashify_file_name == 'true' ? Date.now().toString(36) + '-' : '';
975 var is_ssl = $scope.sm.custom_domain.indexOf('https://');
976 var custom_domain = $scope.sm.custom_domain.toString();
977 var root_dir = $scope.sm.root_dir ? $scope.sm.root_dir : '';
978
979 jQuery.each($scope.sm.wildcards, function(index, item){
980 var reg = new RegExp(index, 'g');
981 root_dir = root_dir.replace(reg, item[0]);
982 });
983 let tags = [ "%date_year%", "%date_month%", "%site_id%", "%site_url%", "%site_url_host%", "%site_url_path%" ];
984 let value_splitted = root_dir.split("/");
985 for ( let i = 0; i < value_splitted.length; i ++ ) {
986 if (! /^[a-zA-Z0-9_.]+$/.test(value_splitted[i]) && value_splitted[i] != '' && jQuery.inArray (value_splitted[i], tags) == -1) {
987 value_splitted[i] = value_splitted[i].replace(/[^a-zA-Z0-9_.]/g,'');
988 }
989 }
990 root_dir = value_splitted.join('/');
991 root_dir = root_dir.replace(/(\/+)/g, '/');
992 root_dir = root_dir.replace(/^\//, '');
993 root_dir = root_dir.replace(/\/$/, '');
994 if(root_dir){
995 root_dir = root_dir + "/";
996 }
997
998 custom_domain = custom_domain.replace(/\/+$/, ''); // removing trailing slashes
999 custom_domain = custom_domain.replace(/https?:\/\//, ''); // removing http:// or https:// from the beginning.
1000 host += $scope.sm.bucket ? $scope.sm.bucket : '{bucket-name}';
1001
1002 if ( custom_domain !== 'storage.googleapis.com' && $scope.sm.bucket && custom_domain && ( is_ssl === 0 || custom_domain == $scope.sm.bucket ) ) {
1003 $scope.sm.is_custom_domain = true;
1004 $scope.sm.is_ssl = is_ssl === 0 ? true : false;
1005 host = is_ssl === 0 ? 'https://' : 'http://'; // bucketname will be host
1006 host += custom_domain;
1007 }
1008
1009 $scope.sm.preview_url = host + "/" + root_dir + hash + "your-image-name.jpeg";
1010 }
1011
1012 $scope.sm.generatePreviewUrl();
1013
1014 })
1015 .controller('wpStatelessCompatibility', function($scope, $filter) {
1016 $scope.modules = wp_stateless_compatibility || {};
1017 })
1018 .controller('noJSWarning', function($scope, $filter) {
1019 $scope.jsLoaded = true;
1020 });
1021
1022 wpStatelessApp.filter("trust", ['$sce', function($sce) {
1023 return function(htmlCode){
1024 return $sce.trustAsHtml(htmlCode);
1025 }
1026 }]);
1027