admin.js
29.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
/* global ajaxurl */
import Smush from '../smush/smush';
const remove_element = function (el, timeout) {
if (typeof timeout === 'undefined') {
timeout = 100;
}
el.fadeTo(timeout, 0, function () {
el.slideUp(timeout, function () {
el.remove();
});
});
};
jQuery(function ($) {
'use strict';
/**
* Disable the action links *
*
* @param c_element
*/
const disable_links = function (c_element) {
const parent = c_element.parent();
//reduce parent opacity
parent.css({ opacity: '0.5' });
//Disable Links
parent.find('a').prop('disabled', true);
};
/**
* Enable the Action Links *
*
* @param c_element
*/
const enable_links = function (c_element) {
const parent = c_element.parent();
//reduce parent opacity
parent.css({ opacity: '1' });
//Disable Links
parent.find('a').prop('disabled', false);
};
/**
* Restore image request with a specified action for Media Library / NextGen Gallery
*
* @param {Object} e
* @param {string} currentButton
* @param {string} smushAction
* @param {string} action
*/
const process_smush_action = function (
e,
currentButton,
smushAction,
action
) {
e.preventDefault();
// If disabled.
if ( currentButton.attr( 'disabled' ) ) {
return;
}
// Remove Error.
$('.wp-smush-error').remove();
// Hide stats.
$('.smush-stats-wrapper').hide();
let mode = 'grid';
if ('smush_restore_image' === smushAction) {
if ($(document).find('div.media-modal.wp-core-ui').length > 0) {
mode = 'grid';
} else {
mode =
window.location.search.indexOf('item') > -1
? 'grid'
: 'list';
}
}
// Get the image ID and nonce.
const params = {
action: smushAction,
attachment_id: currentButton.data('id'),
mode,
_nonce: currentButton.data('nonce'),
};
// Reduce the opacity of stats and disable the click.
disable_links(currentButton);
const oldLabel = currentButton.html();
currentButton.html(
'<span class="spinner wp-smush-progress">' +
wp_smush_msgs[action] +
'</span>'
);
// Restore the image.
$.post(ajaxurl, params, function (r) {
// Reset all functionality.
enable_links(currentButton);
if (r.success && 'undefined' !== typeof r.data) {
// Replace in immediate parent for NextGEN.
if (
'undefined' !== typeof this.data &&
this.data.indexOf('nextgen') > -1
) {
// Show the smush button, and remove stats and restore option.
currentButton.parents().eq(1).html(r.data.stats);
} else if ('restore' === action) {
// Show the smush button, and remove stats and restore option.
currentButton.parents().eq(1).html(r.data.stats);
} else {
const wrapper = currentButton.parents().eq(1);
if ( wp_smush_msgs.failed_item_smushed && wrapper.hasClass('smush-failed-processing') ) {
wrapper.html( '<p class="smush-status smush-success">' + wp_smush_msgs.failed_item_smushed + '</p>' );
setTimeout(function(){
wrapper.html( r.data );
}, 2000);
} else {
wrapper.html(r.data);
}
}
if ('undefined' !== typeof r.data && 'restore' === action) {
Smush.updateImageStats(r.data.new_size);
}
} else if (r.data && r.data.error_msg) {
if (
-1 === this.data.indexOf('nextgen')
) {
currentButton.closest( '.smushit' ).find('.smush-status').addClass('smush-warning').html(r.data.error_msg);
} else {
// Show error.
currentButton.parent().append(r.data.error_msg);
}
// Reset label and disable button on error.
currentButton.attr('disabled', true);
currentButton.html( oldLabel );
}
});
};
/**
* Validates the Resize Width and Height against the Largest Thumbnail Width and Height
*
* @param wrapper_div jQuery object for the whole setting row wrapper div
* @param width_only Whether to validate only width
* @param height_only Validate only Height
* @return {boolean} All Good or not
*/
const validate_resize_settings = function (
wrapper_div,
width_only,
height_only
) {
const resize_checkbox = wrapper_div.find('#resize');
if (!height_only) {
var width_input = wrapper_div.find('#wp-smush-resize_width');
var width_error_note = wrapper_div.find(
'.sui-notice-info.wp-smush-update-width'
);
}
if (!width_only) {
var height_input = wrapper_div.find('#wp-smush-resize_height');
var height_error_note = wrapper_div.find(
'.sui-notice-info.wp-smush-update-height'
);
}
let width_error = false;
let height_error = false;
//If resize settings is not enabled, return true
if (!resize_checkbox.is(':checked')) {
return true;
}
//Check if we have localised width and height
if (
'undefined' === typeof wp_smushit_data.resize_sizes ||
'undefined' === typeof wp_smushit_data.resize_sizes.width
) {
//Rely on server validation
return true;
}
//Check for width
if (
!height_only &&
'undefined' !== typeof width_input &&
parseInt(wp_smushit_data.resize_sizes.width) >
parseInt(width_input.val())
) {
width_input.parent().addClass('sui-form-field-error');
width_error_note.show('slow');
width_error = true;
} else {
//Remove error class
width_input.parent().removeClass('sui-form-field-error');
width_error_note.hide();
if (height_input.hasClass('error')) {
height_error_note.show('slow');
}
}
//Check for height
if (
!width_only &&
'undefined' !== typeof height_input &&
parseInt(wp_smushit_data.resize_sizes.height) >
parseInt(height_input.val())
) {
height_input.parent().addClass('sui-form-field-error');
//If we are not showing the width error already
if (!width_error) {
height_error_note.show('slow');
}
height_error = true;
} else {
//Remove error class
height_input.parent().removeClass('sui-form-field-error');
height_error_note.hide();
if (width_input.hasClass('error')) {
width_error_note.show('slow');
}
}
if (width_error || height_error) {
return false;
}
return true;
};
/**
* Update the progress bar width if we have images that needs to be resmushed
*
* @param unsmushed_count
* @return {boolean}
*/
const update_progress_bar_resmush = function (unsmushed_count) {
if ('undefined' === typeof unsmushed_count) {
return false;
}
const smushed_count = wp_smushit_data.count_total - unsmushed_count;
//Update the Progress Bar Width
// get the progress bar
const $progress_bar = jQuery(
'.bulk-smush-wrapper .wp-smush-progress-inner'
);
if ($progress_bar.length < 1) {
return;
}
const width = (smushed_count / wp_smushit_data.count_total) * 100;
// increase progress
$progress_bar.css('width', width + '%');
};
const runRecheck = function (process_settings) {
const button = $('.wp-smush-scan');
// Add a "loading" state to the button.
button.addClass('sui-button-onload');
// Check if type is set in data attributes.
let scan_type = button.data('type');
scan_type = 'undefined' === typeof scan_type ? 'media' : scan_type;
// Remove the Skip resmush attribute from button.
$('.wp-smush-all').removeAttr('data-smush');
// Disable Bulk smush button and itself.
$('.wp-smush-all').prop('disabled', true);
// Hide Settings changed Notice.
$('.wp-smush-settings-changed').hide();
// Ajax params.
const params = {
action: 'scan_for_resmush',
type: scan_type,
get_ui: true,
process_settings,
wp_smush_options_nonce: jQuery('#wp_smush_options_nonce').val(),
};
// Send ajax request and get ids if any.
$.get(ajaxurl, params, function (r) {
// Check if we have the ids, initialize the local variable.
if ('undefined' !== typeof r.data) {
// Update Resmush id list.
if ('undefined' !== typeof r.data.resmush_ids) {
wp_smushit_data.resmush = r.data.resmush_ids;
// Update wp_smushit_data ( Smushed count, Smushed Percent, Image count, Super smush count, resize savings, conversion savings ).
if ('undefined' !== typeof wp_smushit_data) {
wp_smushit_data.count_total =
'undefined' !== typeof r.data.count_total
? parseInt( r.data.count_total )
: wp_smushit_data.count_total;
wp_smushit_data.count_smushed =
'undefined' !== typeof r.data.count_smushed
? r.data.count_smushed
: wp_smushit_data.count_smushed;
wp_smushit_data.count_supersmushed =
'undefined' !== typeof r.data.count_supersmushed
? r.data.count_supersmushed
: wp_smushit_data.count_supersmushed;
wp_smushit_data.count_images =
'undefined' !== typeof r.data.count_image
? r.data.count_image
: wp_smushit_data.count_images;
wp_smushit_data.size_before =
'undefined' !== typeof r.data.size_before
? r.data.size_before
: wp_smushit_data.size_before;
wp_smushit_data.size_after =
'undefined' !== typeof r.data.size_after
? r.data.size_after
: wp_smushit_data.size_after;
wp_smushit_data.savings_resize =
'undefined' !== typeof r.data.savings_resize
? r.data.savings_resize
: wp_smushit_data.savings_resize;
wp_smushit_data.savings_conversion =
'undefined' !== typeof r.data.savings_conversion
? r.data.savings_conversion
: wp_smushit_data.savings_conversion;
wp_smushit_data.count_resize =
'undefined' !== typeof r.data.count_resize
? r.data.count_resize
: wp_smushit_data.count_resize;
wp_smushit_data.unsmushed =
'undefined' !== typeof r.data.unsmushed
? r.data.unsmushed
: wp_smushit_data.unsmushed;
// Update grade data.
if ( wp_smushit_data.percent_grade ) {
wp_smushit_data.percent_grade = r.data.percent_grade || wp_smushit_data.percent_grade;
wp_smushit_data.percent_metric = r.data.percent_metric || 0;
wp_smushit_data.percent_optimized = r.data.percent_optimized || 0;
}
// Add remaining_count.
wp_smushit_data.remaining_count = r.data.remaining_count || 0;
}
if ('nextgen' === scan_type) {
wp_smushit_data.bytes =
parseInt(wp_smushit_data.size_before) -
parseInt(wp_smushit_data.size_after);
}
// Hide the Existing wrapper.
const notices = $(
'.bulk-smush-wrapper .sui-notice:not(.sui-notice-purple)'
);
if (notices.length > 0) {
notices.addClass('sui-hidden');
}
// Remove existing Re-Smush notices.
$('.wp-smush-resmush-notice').remove();
// Show Bulk wrapper.
$('.wp-smush-bulk-wrapper').removeClass('sui-hidden');
}
// If content is received, Prepend it.
if ('undefined' !== typeof r.data.content) {
$('#wp-smush-bulk-content').html(r.data.content);
}
// If we have any notice to show.
if ('undefined' !== typeof r.data.notice) {
let type = 'success';
if ('undefined' !== typeof r.data.noticeType) {
type = r.data.noticeType;
}
window.SUI.openNotice(
'wp-smush-ajax-notice',
'<p>' + r.data.notice + '</p>',
{ type, icon: 'check-tick' }
);
// If there is no images and the stats is changed, reload the page.
if ( r.data.no_images && wp_smushit_data.count_total > 0 ) {
window.location.reload();
}
}
// Hide errors.
$('div.smush-final-log').hide();
// Hide Super Smush notice if it's enabled in media settings.
if (
'undefined' !== typeof r.data.super_smush &&
r.data.super_smush &&
'undefined' !== r.data.super_smush_stat
) {
$('.super-smush-attachments .wp-smush-stats').html(
r.data.super_smush_stats
);
}
Smush.updateStats(scan_type);
Smush.updateScoreProgress();
const remainingCount = r.data.count || 0;
updateDisplayedContentAfterReCheck(remainingCount);
/**
* Trigger an event after re-checking images.
*
* @used updateGlobalStats for background optimization - background-process.js
*/
document.dispatchEvent( new Event( 'wpSmushAfterRecheckImages' ) );
}
}).always(function () {
// Hide the progress bar.
jQuery(
'.bulk-smush-wrapper .wp-smush-bulk-progress-bar-wrapper'
).addClass('sui-hidden');
// Add check complete status to button.
button
.removeClass('sui-button-onload')
.addClass('smush-button-check-success');
const $defaultText = button.find('.wp-smush-default-text'),
$completedText = button.find('.wp-smush-completed-text');
$defaultText.addClass('sui-hidden-important');
$completedText.removeClass('sui-hidden');
// Remove success message from button.
setTimeout(function () {
button.removeClass('smush-button-check-success');
$defaultText.removeClass('sui-hidden-important');
$completedText.addClass('sui-hidden');
}, 2000);
$('.wp-smush-all').prop('disabled', false);
});
};
const updateDisplayedContentAfterReCheck = function (count) {
const $pendingImagesWrappers = jQuery(
'.bulk-smush-wrapper .wp-smush-bulk-wrapper'
);
const $allDoneWrappers = jQuery(
'.bulk-smush-wrapper .wp-smush-all-done'
);
if ($pendingImagesWrappers.length && $allDoneWrappers.length) {
if (count === 0) {
$pendingImagesWrappers.addClass('sui-hidden');
$allDoneWrappers.find('p').html( wp_smush_msgs.all_smushed );
$allDoneWrappers.find('.sui-notice-icon').removeClass('sui-icon-info').addClass('sui-icon-check-tick');
$allDoneWrappers.removeClass('sui-notice-warning').addClass('sui-notice-success');
$allDoneWrappers.removeClass('sui-hidden');
} else {
$pendingImagesWrappers.removeClass('sui-hidden');
$allDoneWrappers.addClass('sui-hidden');
// Update texts mentioning the amount of unsmushed imagesin the summary icon tooltip.
const $unsmushedTooltip = jQuery(
'.sui-summary-smush .sui-summary-details .sui-tooltip'
);
// The tooltip doesn't exist in the NextGen page.
if ($unsmushedTooltip.length) {
const textForm = 1 === count ? 'singular' : 'plural',
tooltipText = $unsmushedTooltip
.data(textForm)
.replace('{count}', count);
$unsmushedTooltip.attr('data-tooltip', tooltipText);
}
}
}
// Total count in the progress bar.
jQuery('.wp-smush-total-count').text(count);
};
// Scroll the element to top of the page.
const goToByScroll = function (selector) {
// Scroll if element found.
if ($(selector).length > 0) {
$('html, body').animate(
{
scrollTop: $(selector).offset().top - 100,
},
'slow'
);
}
};
const update_cummulative_stats = function (stats) {
//Update Directory Smush Stats
if ('undefined' !== typeof stats.dir_smush) {
const stats_human = $(
'li.smush-dir-savings span.wp-smush-stats span.wp-smush-stats-human'
);
const stats_percent = $(
'li.smush-dir-savings span.wp-smush-stats span.wp-smush-stats-percent'
);
// Do not replace if 0 savings.
if (stats.dir_smush.bytes > 0) {
$('.wp-smush-dir-link').addClass('sui-hidden');
// Hide selector.
$('li.smush-dir-savings .wp-smush-stats-label-message').hide();
//Update Savings in bytes
if (stats_human.length > 0) {
stats_human.html(stats.dir_smush.human);
}
//Percentage section
if (stats.dir_smush.percent > 0) {
// Show size and percentage separator.
$(
'li.smush-dir-savings span.wp-smush-stats span.wp-smush-stats-sep'
).removeClass('sui-hidden');
//Update Optimisation percentage
if (stats_percent.length > 0) {
stats_percent.html(stats.dir_smush.percent + '%');
}
}
} else {
$('.wp-smush-dir-link').removeClass('sui-hidden');
}
}
//Update Combined stats
if (
'undefined' !== typeof stats.combined_stats &&
stats.combined_stats.length > 0
) {
const c_stats = stats.combined_stats;
let smush_percent = (c_stats.smushed / c_stats.total_count) * 100;
smush_percent = WP_Smush.helpers.precise_round(smush_percent, 1);
//Smushed Percent
if (smush_percent) {
$('div.wp-smush-count-total span.wp-smush-images-percent').html(
smush_percent
);
}
//Update Total Attachment Count
if (c_stats.total_count) {
$(
'span.wp-smush-count-total span.wp-smush-total-optimised'
).html(c_stats.total_count);
}
//Update Savings and Percent
if (c_stats.savings) {
$('span.wp-smush-savings span.wp-smush-stats-human').html(
c_stats.savings
);
}
if (c_stats.percent) {
$('span.wp-smush-savings span.wp-smush-stats-percent').html(
c_stats.percent
);
}
}
};
/**
* When 'All' is selected for the Image Sizes setting, select all available sizes.
*
* @since 3.2.1
*/
$('#all-image-sizes').on('change', function () {
$('input[name^="wp-smush-image_sizes"]').prop('checked', true);
});
/**
* Handles the tabs navigation on mobile.
*
* @since 3.8.4
*/
$('.sui-mobile-nav').on('change', (e) => {
window.location.assign($(e.currentTarget).val());
});
/**
* Handle re-check api status button click (Settings)
*
* @since 3.2.0.2
*/
$('#update-api-status').on('click', function (e) {
e.preventDefault();
//$(this).prop('disabled', true);
$(this).addClass('sui-button-onload');
$.post(ajaxurl, { action: 'recheck_api_status' }, function () {
location.reload();
});
});
/** Handle smush button click **/
$('body').on(
'click',
'.wp-smush-send:not(.wp-smush-resmush)',
function (e) {
// prevent the default action
e.preventDefault();
new Smush($(this), false);
}
);
/**
* Handle show in bulk smush button click.
*/
$( 'body' ).on( 'click', '.wp-smush-remove-skipped', function( e ) {
e.preventDefault();
const self = $( this );
// Send ajax request to remove the image from the skip list.
$.post( ajaxurl, {
action: 'remove_from_skip_list',
id: self.attr( 'data-id' ),
_ajax_nonce: self.attr( 'data-nonce' ),
} ).done( ( response ) => {
if ( response.success && 'undefined' !== typeof response.data.html ) {
self.parent().parent().html( response.data.html );
}
} );
} );
/** Handle NextGen Gallery smush button click **/
$('body').on('click', '.wp-smush-nextgen-send', function (e) {
// prevent the default action
e.preventDefault();
new Smush($(this), false, 'nextgen');
});
/** Handle NextGen Gallery Bulk smush button click **/
$('body').on('click', '.wp-smush-nextgen-bulk', function (e) {
// prevent the default action
e.preventDefault();
// Remove existing Re-Smush notices.
$('.wp-smush-resmush-notice').remove();
//Check for ids, if there is none (Unsmushed or lossless), don't call smush function
if (
'undefined' === typeof wp_smushit_data ||
(wp_smushit_data.unsmushed.length === 0 &&
wp_smushit_data.resmush.length === 0)
) {
return false;
}
jQuery('.wp-smush-all, .wp-smush-scan').prop('disabled', true);
$('.wp-smush-notice.wp-smush-remaining').hide();
new Smush($(this), true, 'nextgen');
});
/** Restore: Media Library **/
$('body').on('click', '.wp-smush-action.wp-smush-restore', function (e) {
const current_button = $(this);
process_smush_action(
e,
current_button,
'smush_restore_image',
'restore'
);
});
/** Resmush: Media Library **/
$('body').on('click', '.wp-smush-action.wp-smush-resmush', function (e) {
process_smush_action(e, $(this), 'smush_resmush_image', 'smushing');
});
/** Restore: NextGen Gallery **/
$('body').on(
'click',
'.wp-smush-action.wp-smush-nextgen-restore',
function (e) {
process_smush_action(
e,
$(this),
'smush_restore_nextgen_image',
'restore'
);
}
);
/** Resmush: NextGen Gallery **/
$('body').on(
'click',
'.wp-smush-action.wp-smush-nextgen-resmush',
function (e) {
process_smush_action(
e,
$(this),
'smush_resmush_nextgen_image',
'smushing'
);
}
);
//Scan For resmushing images
$('.wp-smush-scan').on('click', function (e) {
e.preventDefault();
runRecheck(false);
});
//Remove Notice
$('body').on('click', '.wp-smush-notice .icon-fi-close', function (e) {
e.preventDefault();
const $el = $(this).parent();
remove_element($el);
});
// On re-Smush click.
// TODO: This can be removed, but follow the logic and remove all excess code as well.
$('body').on('click', '.wp-smush-skip-resmush', function (e) {
e.preventDefault();
const self = jQuery(this),
container = self.parents().eq(1),
el = self.parent();
// Remove Parent div.
remove_element(el);
// Set button attribute to skip re-smush ids.
container.find('.wp-smush-all').attr('data-smush', 'skip_resmush');
// Update Smushed count.
wp_smushit_data.count_smushed =
parseInt(wp_smushit_data.count_smushed) +
wp_smushit_data.resmush.length;
wp_smushit_data.count_supersmushed =
parseInt(wp_smushit_data.count_supersmushed) +
wp_smushit_data.resmush.length;
// Update stats.
if (wp_smushit_data.count_smushed === wp_smushit_data.count_total) {
// Show all done notice.
$('.wp-smush-notice.wp-smush-all-done').removeClass('sui-hidden');
// Hide Smush button.
$('.wp-smush-bulk-wrapper ').addClass('sui-hidden');
}
// Remove re-Smush notice.
$('.wp-smush-resmush-notice').remove();
let type = $('.wp-smush-scan').data('type');
type = 'undefined' === typeof type ? 'media' : type;
const smushed_count =
'undefined' !== typeof wp_smushit_data.count_smushed
? wp_smushit_data.count_smushed
: 0;
let smush_percent = (smushed_count / wp_smushit_data.count_total) * 100;
smush_percent = WP_Smush.helpers.precise_round(smush_percent, 1);
$('.wp-smush-images-percent').html(smush_percent);
// Update the progress bar width. Get the progress bar.
const progress_bar = jQuery(
'.bulk-smush-wrapper .wp-smush-progress-inner'
);
if (progress_bar.length < 1) {
return;
}
// Increase progress.
progress_bar.css('width', smush_percent + '%');
// Show the default bulk smush notice.
$('.wp-smush-bulk-wrapper').removeClass('sui-hidden');
$('.wp-smush-bulk-wrapper .sui-notice').removeClass('sui-hidden');
const params = {
action: 'delete_resmush_list',
type,
};
//Delete resmush list, @todo: update stats from the ajax response
$.post(ajaxurl, params, function (res) {
// Remove the whole li element on success
if (res.success && 'undefined' !== typeof res.data.stats) {
const stats = res.data.stats;
// Update wp_smushit_data ( Smushed count, Smushed Percent, Image count, Super smush count, resize savings, conversion savings )
if ('undefined' !== typeof wp_smushit_data) {
wp_smushit_data.count_images =
'undefined' !== typeof stats.count_images
? parseInt(wp_smushit_data.count_images) +
stats.count_images
: wp_smushit_data.count_images;
wp_smushit_data.size_before =
'undefined' !== typeof stats.size_before
? parseInt(wp_smushit_data.size_before) +
stats.size_before
: wp_smushit_data.size_before;
wp_smushit_data.size_after =
'undefined' !== typeof stats.size_after
? parseInt(wp_smushit_data.size_after) +
stats.size_after
: wp_smushit_data.size_after;
wp_smushit_data.savings_resize =
'undefined' !== typeof stats.savings_resize
? parseInt(wp_smushit_data.savings_resize) +
stats.savings_resize
: wp_smushit_data.savings_resize;
wp_smushit_data.savings_conversion =
'undefined' !== typeof stats.savings_conversion
? parseInt(wp_smushit_data.savings_conversion) +
stats.savings_conversion
: wp_smushit_data.savings_conversion;
// Add directory smush stats.
if (
'undefined' !==
typeof wp_smushit_data.savings_dir_smush &&
'undefined' !==
typeof wp_smushit_data.savings_dir_smush.orig_size
) {
wp_smushit_data.size_before =
'undefined' !==
typeof wp_smushit_data.savings_dir_smush
? parseInt(wp_smushit_data.size_before) +
parseInt(
wp_smushit_data.savings_dir_smush
.orig_size
)
: wp_smushit_data.size_before;
wp_smushit_data.size_after =
'undefined' !==
typeof wp_smushit_data.savings_dir_smush
? parseInt(wp_smushit_data.size_after) +
parseInt(
wp_smushit_data.savings_dir_smush
.image_size
)
: wp_smushit_data.size_after;
}
wp_smushit_data.count_resize =
'undefined' !== typeof stats.count_resize
? parseInt(wp_smushit_data.count_resize) +
stats.count_resize
: wp_smushit_data.count_resize;
}
// If no images left, hide the notice, show all success notice.
if (
'undefined' !== typeof wp_smushit_data.unsmushed ||
wp_smushit_data.unsmushed.length === 0
) {
$('.wp-smush-bulk-wrapper .sui-notice').removeClass(
'sui-hidden'
);
$('.sui-notice-success.wp-smush-all-done').addClass(
'sui-hidden'
);
}
Smush.updateStats();
}
});
});
// Enable super smush on clicking link from stats area.
$('a.wp-smush-lossy-enable').on('click', function (e) {
e.preventDefault();
// Scroll down to settings area.
goToByScroll('#column-lossy');
});
// Enable resize on clicking link from stats area.
$('.wp-smush-resize-enable').on('click', function (e) {
e.preventDefault();
// Scroll down to settings area.
goToByScroll('#column-resize');
});
// If settings string is found in url, enable and scroll.
if ( window.location.hash ) {
const setting_hash = window.location.hash.substring( 1 );
let scrollTo = '';
switch ( setting_hash ) {
case 'enable-resize':
scrollTo = '#column-resize';
break;
case 'backup-label':
scrollTo = '#backup';
break;
case 'original-label':
scrollTo = '#original';
break;
case 'enable-lossy':
scrollTo = '#column-lossy';
break;
}
if ( '' !== scrollTo ) {
goToByScroll( scrollTo );
document.getElementById( scrollTo.replace( '#', '' ) ).focus();
}
}
//Trigger Bulk
$('body').on('click', '.wp-smush-trigger-bulk', function (e) {
e.preventDefault();
//Induce Setting button save click
if (
'undefined' !== typeof e.target.dataset.type &&
'nextgen' === e.target.dataset.type
) {
$('.wp-smush-nextgen-bulk').trigger('click');
} else {
$('.wp-smush-all').trigger('click');
}
$('span.sui-notice-dismiss').trigger('click');
});
//Trigger Bulk
$('body').on('click', '#bulk-smush-top-notice-close', function (e) {
e.preventDefault();
$(this).parent().parent().slideUp('slow');
});
//Allow the checkboxes to be Keyboard Accessible
$('.wp-smush-setting-row .toggle-checkbox').on('focus', function () {
//If Space is pressed
$(this).keypress(function (e) {
if (e.keyCode == 32) {
e.preventDefault();
$(this).find('.toggle-checkbox').trigger('click');
}
});
});
// Re-Validate Resize Width And Height.
$('body').on('blur', '.wp-smush-resize-input', function () {
const self = $(this);
const wrapper_div = self.parents().eq(4);
// Initiate the check.
validate_resize_settings(wrapper_div, false, false); // run the validation.
});
// Handle Resize Checkbox toggle, to show/hide width, height settings.
$('body').on('click', '#resize', function () {
const self = $(this);
const settings_wrap = $('#smush-resize-settings-wrap');
if (self.is(':checked')) {
settings_wrap.show();
} else {
settings_wrap.hide();
}
});
//Handle Re-check button functionality
$('#wp-smush-revalidate-member').on('click', function (e) {
e.preventDefault();
//Ajax Params
const params = {
action: 'smush_show_warning',
_ajax_nonce: window.wp_smush_msgs.nonce,
};
const link = $(this);
const parent = link.parents().eq(1);
parent.addClass('loading-notice');
$.get(ajaxurl, params, function (r) {
//remove the warning
parent.removeClass('loading-notice').addClass('loaded-notice');
if (0 == r) {
parent.attr('data-message', wp_smush_msgs.membership_valid);
remove_element(parent, 1000);
} else {
parent.attr('data-message', wp_smush_msgs.membership_invalid);
setTimeout(function remove_loader() {
parent.removeClass('loaded-notice');
}, 1000);
}
});
});
if ($('li.smush-dir-savings').length > 0) {
// Update Directory Smush, as soon as the page loads.
const stats_param = {
action: 'get_dir_smush_stats',
_ajax_nonce: window.wp_smush_msgs.nonce,
};
$.get(ajaxurl, stats_param, function (r) {
//Hide the spinner
$('li.smush-dir-savings .sui-icon-loader').hide();
//If there are no errors, and we have a message to display
if (!r.success && 'undefined' !== typeof r.data.message) {
$('div.wp-smush-scan-result div.content').prepend(
r.data.message
);
return;
}
//If there is no value in r
if (
'undefined' === typeof r.data ||
'undefined' === typeof r.data.dir_smush
) {
//Append the text
$('li.smush-dir-savings span.wp-smush-stats').append(
wp_smush_msgs.ajax_error
);
$('li.smush-dir-savings span.wp-smush-stats span').hide();
} else {
//Update the stats
update_cummulative_stats(r.data);
}
});
}
// Display dialogs that show up with no user action.
if ($('#smush-updated-dialog').length) {
// Displays the modal with the release's higlights if it exists.
window.SUI.openModal(
'smush-updated-dialog',
'wpbody-content',
undefined,
false
);
}
/**
* Toggle backup notice based on "Compress original images" setting.
* @since 3.9.1
*/
$( 'input#original' ).on( 'change', function() {
$( '#backup-notice' ).toggleClass( 'sui-hidden', $( this ).is(':checked') );
} );
});