settings.js
4.18 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
/* global ajaxurl */
/* global wp_smush_msgs */
/* global WP_Smush */
/* global SUI */
( function( $ ) {
'use strict';
/**
* Bulk compress page.
*/
$( 'form#smush-bulk-form' ).on( 'submit', function( e ) {
e.preventDefault();
$( '#save-settings-button' ).addClass( 'sui-button-onload' );
saveSettings( $( this ).serialize(), 'bulk' );
runReCheck();
} );
/**
* Lazy load page.
*/
$( 'form#smush-lazy-load-form' ).on( 'submit', function( e ) {
e.preventDefault();
$( '#save-settings-button' ).addClass( 'sui-button-onload-text' );
saveSettings( $( this ).serialize(), 'lazy-load' );
} );
/**
* CDN page.
*/
$( 'form#smush-cdn-form' ).on( 'submit', function( e ) {
e.preventDefault();
$( '#save-settings-button' ).addClass( 'sui-button-onload-text' );
saveSettings( $( this ).serialize(), 'cdn' );
} );
/**
* Integrations page.
*/
$( 'form#smush-integrations-form' ).on( 'submit', function( e ) {
e.preventDefault();
$( '#save-settings-button' ).addClass( 'sui-button-onload-text' );
saveSettings( $( this ).serialize(), 'integrations' );
} );
/**
* Settings page.
*/
$( 'form#smush-settings-form' ).on( 'submit', function( e ) {
e.preventDefault();
$( '#save-settings-button' ).addClass( 'sui-button-onload-text' );
saveSettings( $( this ).serialize(), 'settings' );
} );
/**
* Save settings.
*
* @param {string} settings JSON string of settings.
* @param {string} page Settings page.
*/
function saveSettings( settings, page ) {
const xhr = new XMLHttpRequest();
xhr.open( 'POST', ajaxurl + '?action=smush_save_settings', true );
xhr.setRequestHeader(
'Content-type',
'application/x-www-form-urlencoded'
);
xhr.onload = () => {
$( '#save-settings-button' ).removeClass(
'sui-button-onload-text sui-button-onload'
);
if ( 200 === xhr.status ) {
const res = JSON.parse( xhr.response );
if ( 'undefined' !== typeof res.success && res.success ) {
showSuccessNotice( wp_smush_msgs.settingsUpdated );
} else if ( res.data && res.data.message ) {
WP_Smush.helpers.showErrorNotice( res.data.message );
} else {
WP_Smush.helpers.showErrorNotice( 'Request failed.' );
}
} else {
WP_Smush.helpers.showErrorNotice( 'Request failed. Returned status of ' + xhr.status );
}
};
xhr.send( 'page=' + page + '&' + settings + '&_ajax_nonce=' + wp_smush_msgs.nonce );
}
/**
* Show successful update notice.
*
* @param {string} msg Notice message.
*/
function showSuccessNotice( msg ) {
const noticeMessage = `<p>${ msg }</p>`,
noticeOptions = {
type: 'success',
icon: 'check',
};
SUI.openNotice( 'wp-smush-ajax-notice', noticeMessage, noticeOptions );
const loadingButton = document.querySelector( '.sui-button-onload' );
if ( loadingButton ) {
loadingButton.classList.remove( 'sui-button-onload' );
}
}
/**
* Re-check images from bulk smush and integrations pages.
*/
function runReCheck() {
$( '#save-settings-button' ).addClass( 'sui-button-onload' );
const param = {
action: 'scan_for_resmush',
wp_smush_options_nonce: $( '#wp_smush_options_nonce' ).val(),
type: 'media',
};
// Send ajax, Update Settings, And Check For resmush.
$.post( ajaxurl, $.param( param ) ).done( function() {
$( '#save-settings-button' ).removeClass( 'sui-button-onload' );
} );
}
/**
* Parse remove data change.
*/
$( 'input[name=keep_data]' ).on( 'change', function( e ) {
const otherClass =
'keep_data-true' === e.target.id
? 'keep_data-false'
: 'keep_data-true';
e.target.parentNode.classList.add( 'active' );
document
.getElementById( otherClass )
.parentNode.classList.remove( 'active' );
} );
/**
* Handle auto-detect checkbox toggle, to show/hide highlighting notice.
*/
$( 'input#detection' ).on( 'click', function() {
const noticeDiv = $( '.smush-highlighting-notice' );
const warningDiv = $( '.smush-highlighting-warning' );
// Setting enabled.
if ( $( this ).is( ':checked' ) ) {
// Highlighting is already active and setting not saved.
if ( noticeDiv.length > 0 ) {
noticeDiv.show();
} else {
warningDiv.show();
}
} else {
noticeDiv.hide();
warningDiv.hide();
}
} );
}( jQuery ) );