forked from BoldGrid/w3-total-cache
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCacheGroups_Plugin_Admin_View.js
324 lines (273 loc) · 13.1 KB
/
CacheGroups_Plugin_Admin_View.js
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
/**
* File: CacheGroups_Plugin_Admin_View.js
*
* @since 2.1.0
*
* @package W3TC
*/
jQuery(function() {
// User agent groups.
jQuery('#mobile_form').on( 'submit', function() {
var error = false;
jQuery('#mobile_groups li').each(function() {
if (jQuery(this).find(':checked').length) {
var group = jQuery(this).find('.mobile_group').text();
var theme = jQuery(this).find(':selected').val();
var redirect = jQuery(this).find('input[type=text]').val();
var agents = jQuery.trim(jQuery(this).find('textarea').val()).split("\n");
jQuery('#mobile_groups li').each(function() {
if (jQuery(this).find(':checked').length) {
var compare_group = jQuery(this).find('.mobile_group').text();
if (compare_group != group) {
var compare_theme = jQuery(this).find(':selected').val();
var compare_redirect = jQuery(this).find('input[type=text]').val();
var compare_agents = jQuery.trim(jQuery(this).find('textarea').val()).split("\n");
if (compare_redirect == '' && redirect == '' && compare_theme != '' && compare_theme == theme) {
alert('Duplicate theme "' + compare_theme + '" found in the group "' + group + '".');
error = true;
return false;
}
if (compare_redirect != '' && compare_redirect == redirect) {
alert('Duplicate redirect "' + compare_redirect + '" found in the group "' + group + '".');
error = true;
return false;
}
jQuery.each(compare_agents, function(index, value) {
if (jQuery.inArray(value, agents) != -1) {
alert('Duplicate stem "' + value + '" found in the group "' + compare_group + '".');
error = true;
return false;
}
});
}
}
});
if (error) {
return false;
}
}
});
if (error) {
return false;
}
});
jQuery('#mobile_add').on( 'click', function() {
var group = prompt('Enter group name (only "0-9", "a-z", "_" symbols are allowed).');
if (group !== null) {
group = group.toLowerCase();
group = group.replace(/[^0-9a-z_]+/g, '_');
group = group.replace(/^_+/, '');
group = group.replace(/_+$/, '');
if (group) {
var exists = false;
jQuery('.mobile_group').each(function() {
if (jQuery(this).html() == group) {
alert('Group already exists!');
exists = true;
return false;
}
});
if (!exists) {
var li = jQuery('<li id="mobile_group_' + group + '"><table class="form-table"><tr><th>Group name:</th><td><span class="mobile_group_number">' + (jQuery('#mobile_groups li').length + 1) + '.</span> <span class="mobile_group">' + group + '</span> <input type="button" class="button mobile_delete" value="Delete group" /></td></tr><tr><th><label for="mobile_groups_' + group + '_enabled">Enabled:</label></th><td><input type="hidden" name="mobile_groups[' + group + '][enabled]" value="0" /><input id="mobile_groups_' + group + '_enabled" type="checkbox" name="mobile_groups[' + group + '][enabled]" value="1" checked="checked" /></td></tr><tr><th><label for="mobile_groups_' + group + '_theme">Theme:</label></th><td><select id="mobile_groups_' + group + '_theme" name="mobile_groups[' + group + '][theme]"><option value="">-- Pass-through --</option></select><p class="description">Assign this group of user agents to a specific them. Leaving this option "Active Theme" allows any plugins you have (e.g. mobile plugins) to properly handle requests for these user agents. If the "redirect users to" field is not empty, this setting is ignored.</p></td></tr><tr><th><label for="mobile_groups_' + group + '_redirect">Redirect users to:</label></th><td><input id="mobile_groups_' + group + '_redirect" type="text" name="mobile_groups[' + group + '][redirect]" value="" size="60" /><p class="description">A 302 redirect is used to send this group of users to another hostname (domain); recommended if a 3rd party service provides a mobile version of your site.</p></td></tr><tr><th><label for="mobile_groups_' + group + '_agents">User agents:</label></th><td><textarea id="mobile_groups_' + group + '_agents" name="mobile_groups[' + group + '][agents]" rows="10" cols="50"></textarea><p class="description">Specify the user agents for this group.</p></td></tr></table></li>');
var select = li.find('select');
jQuery.each(mobile_themes, function(index, value) {
select.append(jQuery('<option />').val(index).html(value));
});
jQuery('#mobile_groups').append(li);
w3tc_mobile_groups_clear();
window.location.hash = '#mobile_group_' + group;
li.find('textarea').focus();
}
} else {
alert('Empty group name!');
}
}
});
jQuery('.mobile_delete').on('click', function () {
if (confirm('Are you sure want to delete this group?')) {
jQuery(this).parents('#mobile_groups li').remove();
w3tc_mobile_groups_clear();
w3tc_beforeupload_bind();
}
});
w3tc_mobile_groups_clear();
// Referrer groups.
jQuery('#referrer_form').on( 'submit', function() {
var error = false;
jQuery('#referrer_groups li').each(function() {
if (jQuery(this).find(':checked').length) {
var group = jQuery(this).find('.referrer_group').text();
var theme = jQuery(this).find(':selected').val();
var redirect = jQuery(this).find('input[type=text]').val();
var agents = jQuery.trim(jQuery(this).find('textarea').val()).split("\n");
jQuery('#referrer_groups li').each(function() {
if (jQuery(this).find(':checked').length) {
var compare_group = jQuery(this).find('.referrer_group').text();
if (compare_group != group) {
var compare_theme = jQuery(this).find(':selected').val();
var compare_redirect = jQuery(this).find('input[type=text]').val();
var compare_agents = jQuery.trim(jQuery(this).find('textarea').val()).split("\n");
if (compare_redirect == '' && redirect == '' && compare_theme != '' && compare_theme == theme) {
alert('Duplicate theme "' + compare_theme + '" found in the group "' + group + '".');
error = true;
return false;
}
if (compare_redirect != '' && compare_redirect == redirect) {
alert('Duplicate redirect "' + compare_redirect + '" found in the group "' + group + '".');
error = true;
return false;
}
jQuery.each(compare_agents, function(index, value) {
if (jQuery.inArray(value, agents) != -1) {
alert('Duplicate stem "' + value + '" found in the group "' + compare_group + '".');
error = true;
return false;
}
});
}
}
});
if (error) {
return false;
}
}
});
if (error) {
return false;
}
});
jQuery('#referrer_add').on( 'click', function() {
var group = prompt('Enter group name (only "0-9", "a-z", "_" symbols are allowed).');
if (group !== null) {
group = group.toLowerCase();
group = group.replace(/[^0-9a-z_]+/g, '_');
group = group.replace(/^_+/, '');
group = group.replace(/_+$/, '');
if (group) {
var exists = false;
jQuery('.referrer_group').each(function() {
if (jQuery(this).html() == group) {
alert('Group already exists!');
exists = true;
return false;
}
});
if (!exists) {
var li = jQuery('<li id="referrer_group_' + group + '"><table class="form-table"><tr><th>Group name:</th><td><span class="referrer_group_number">' + (jQuery('#referrer_groups li').length + 1) + '.</span> <span class="referrer_group">' + group + '</span> <input type="button" class="button referrer_delete" value="Delete group" /></td></tr><tr><th><label for="referrer_groups_' + group + '_enabled">Enabled:</label></th><td><input type="hidden" name="referrer_groups[' + group + '][enabled]" value="0" /><input id="referrer_groups_' + group + '_enabled" type="checkbox" name="referrer_groups[' + group + '][enabled]" value="1" checked="checked" /></td></tr><tr><th><label for="referrer_groups_' + group + '_theme">Theme:</label></th><td><select id="referrer_groups_' + group + '_theme" name="referrer_groups[' + group + '][theme]"><option value="">-- Pass-through --</option></select><p class="description">Assign this group of referrers to a specific them. Leaving this option "Active Theme" allows any plugins you have (e.g. referrer plugins) to properly handle requests for these referrers. If the "redirect users to" field is not empty, this setting is ignored.</p></td></tr><tr><th><label for="referrer_groups_' + group + '_redirect">Redirect users to:</label></th><td><input id="referrer_groups_' + group + '_redirect" type="text" name="referrer_groups[' + group + '][redirect]" value="" size="60" /><p class="description">A 302 redirect is used to send this group of users to another hostname (domain); recommended if a 3rd party service provides a referrer version of your site.</p></td></tr><tr><th><label for="referrer_groups_' + group + '_referrers">Referrers:</label></th><td><textarea id="referrer_groups_' + group + '_referrers" name="referrer_groups[' + group + '][referrers]" rows="10" cols="50"></textarea><p class="description">Specify the referrers for this group.</p></td></tr></table></li>');
var select = li.find('select');
jQuery.each(referrer_themes, function(index, value) {
select.append(jQuery('<option />').val(index).html(value));
});
jQuery('#referrer_groups').append(li);
w3tc_referrer_groups_clear();
window.location.hash = '#referrer_group_' + group;
li.find('textarea').focus();
}
} else {
alert('Empty group name!');
}
}
});
jQuery('.referrer_delete').on('click', function () {
if (confirm('Are you sure want to delete this group?')) {
jQuery(this).parents('#referrer_groups li').remove();
w3tc_referrer_groups_clear();
w3tc_beforeupload_bind();
}
});
w3tc_referrer_groups_clear();
// Cookie groups.
jQuery( '#w3tc_cookiegroup_add' ).on( 'click', function() {
var group = prompt('Enter group name (only "0-9", "a-z", "_" symbols are allowed).');
if (group !== null) {
group = group.toLowerCase();
group = group.replace(/[^0-9a-z_]+/g, '_');
group = group.replace(/^_+/, '');
group = group.replace(/_+$/, '');
if (group) {
var exists = false;
jQuery('.cookiegroup_name').each(function() {
if (jQuery(this).html() == group) {
alert('Group already exists!');
exists = true;
return false;
}
});
if (!exists) {
var li = jQuery('<li id="cookiegroup_' + group + '">' +
'<table class="form-table">' +
'<tr>' +
'<th>Group name:</th>' +
'<td><span class="cookiegroup_number">' + (jQuery('#cookiegroups li').length + 1) + '.</span> ' +
'<span class="cookiegroup_name">' + group + '</span> ' +
'<input type="button" class="button cookiegroup_delete" value="Delete group" /></td>' +
'</tr>' +
'<tr>' +
'<th><label for="cookiegroup_' + group + '_enabled">Enabled:</label></th>' +
'<td>' +
'<input id="cookiegroup_' + group + '_enabled" type="checkbox" name="cookiegroups[' +
group + '][enabled]" value="1" checked="checked" /></td>' +
'</tr>' +
'<tr>' +
'<th><label for="cookiegroup_' + group + '_cache">Cache:</label></th>' +
'<td>' +
'<input id="cookiegroup_' + group + '_cache" type="checkbox" name="cookiegroups[' +
group + '][cache]" value="1" checked="checked" /></td></tr>' +
'<tr>' +
'<th><label for="cookiegroups_' + group + '_cookies">Cookies:</label></th>' +
'<td><textarea id="cookiegroups_' + group + '_cookies" name="cookiegroups[' +
group + '][cookies]" rows="10" cols="50"></textarea>' +
'<p class="description">Specify the cookies for this group. Values like \'cookie\', \'cookie=value\', and cookie[a-z]+=value[a-z]+are supported. Remember to escape special characters like spaces, dots or dashes with a backslash. Regular expressions are also supported.</p></td></tr>' +
'</table></li>');
var select = li.find('select');
jQuery('#cookiegroups').append(li);
w3tc_cookiegroups_clear();
window.location.hash = '#cookiegroup_' + group;
li.find('textarea').focus();
}
} else {
alert('Empty group name!');
}
}
});
jQuery('.w3tc_cookiegroup_delete').on( 'click', function () {
if (confirm('Are you sure want to delete this group?')) {
jQuery(this).parents('#cookiegroups li').remove();
w3tc_cookiegroups_clear();
w3tc_beforeupload_bind();
}
});
w3tc_cookiegroups_clear();
// Add sortable.
if (jQuery.ui && jQuery.ui.sortable) {
jQuery('#cookiegroups').sortable({
axis: 'y',
stop: function() {
jQuery('#cookiegroups').find('.cookiegroup_number').each(function(index) {
jQuery(this).html((index + 1) + '.');
});
}
});
}
});
function w3tc_mobile_groups_clear() {
if (!jQuery('#mobile_groups li').length) {
jQuery('#mobile_groups_empty').show();
} else {
jQuery('#mobile_groups_empty').hide();
}
}
function w3tc_referrer_groups_clear() {
if (!jQuery('#referrer_groups li').length) {
jQuery('#referrer_groups_empty').show();
} else {
jQuery('#referrer_groups_empty').hide();
}
}
function w3tc_cookiegroups_clear() {
if (!jQuery('#cookiegroups li').length) {
jQuery('#cookiegroups_empty').show();
} else {
jQuery('#cookiegroups_empty').hide();
}
}