2023-06-05 11:23:16 +00:00
/ * *
* File : CacheGroups _Plugin _Admin _View . js
*
* @ since 2.1 . 0
*
* @ package W3TC
* /
jQuery ( function ( ) {
2023-10-22 22:21:26 +00:00
jQuery ( document ) . on ( 'submit' , '#cachegroups_form' , function ( ) {
var error = [ ] ;
var mobile _groups = jQuery ( '#mobile_groups li' ) ;
mobile _groups . each ( function ( index , mobile _group ) {
var $mobile _group = jQuery ( mobile _group ) ;
if ( $mobile _group . find ( '.mobile_group_enabled:checked' ) . length ) {
var name = $mobile _group . find ( '.mobile_group' ) . text ( ) ;
var theme = $mobile _group . find ( 'select' ) . val ( ) ;
var redirect = $mobile _group . find ( 'input[type=text]' ) . val ( ) ;
var agents = $mobile _group . find ( 'textarea' ) . val ( ) . split ( "\n" ) . filter ( function ( line ) { return line . trim ( ) !== '' } ) . map ( function ( line ) { return line . trim ( ) ; } ) ;
mobile _groups . not ( $mobile _group ) . each ( function ( index , compare _mobile _group ) {
var $compare _mobile _group = jQuery ( compare _mobile _group ) ;
if ( $compare _mobile _group . find ( '.mobile_group_enabled:checked' ) . length ) {
var compare _name = $compare _mobile _group . find ( '.mobile_group' ) . text ( ) ;
var compare _theme = $compare _mobile _group . find ( 'select' ) . val ( ) ;
var compare _redirect = $compare _mobile _group . find ( 'input[type=text]' ) . val ( ) ;
var compare _agents = $compare _mobile _group . find ( 'textarea' ) . val ( ) . split ( "\n" ) . filter ( function ( line ) { return line . trim ( ) !== '' } ) . map ( function ( line ) { return line . trim ( ) ; } ) ;
var groups = sort _array ( [ name , compare _name ] ) ;
if ( compare _redirect === '' && redirect === '' && compare _theme !== '' && compare _theme === theme ) {
error . push ( 'Duplicate theme "' + compare _theme + '" found in the user agent groups "' + groups [ 0 ] + '" and "' + groups [ 1 ] + '"' ) ;
}
if ( compare _redirect !== '' && compare _redirect === redirect ) {
error . push ( 'Duplicate redirect "' + compare _redirect + '" found in the user agent groups "' + groups [ 0 ] + '" and "' + groups [ 1 ] + '"' ) ;
}
jQuery . each ( compare _agents , function ( index , value ) {
if ( jQuery . inArray ( value , agents ) !== - 1 ) {
error . push ( 'Duplicate stem "' + value + '" found in the user agent groups "' + groups [ 0 ] + '" and "' + groups [ 1 ] + '"' ) ;
}
} ) ;
}
} ) ;
}
} ) ;
var referrer _groups = jQuery ( '#referrer_groups li' ) ;
referrer _groups . each ( function ( index , referrer _group ) {
var $referrer _group = jQuery ( referrer _group ) ;
if ( $referrer _group . find ( '.referrer_group_enabled:checked' ) . length ) {
var name = $referrer _group . find ( '.referrer_group' ) . text ( ) ;
var theme = $referrer _group . find ( 'select' ) . val ( ) ;
var redirect = $referrer _group . find ( 'input[type=text]' ) . val ( ) ;
var agents = $referrer _group . find ( 'textarea' ) . val ( ) . split ( "\n" ) . filter ( function ( line ) { return line . trim ( ) !== '' } ) . map ( function ( line ) { return line . trim ( ) ; } ) ;
referrer _groups . not ( $referrer _group ) . each ( function ( index , compare _referrer _group ) {
var $compare _referrer _group = jQuery ( compare _referrer _group ) ;
if ( $compare _referrer _group . find ( '.referrer_group_enabled:checked' ) . length ) {
var compare _name = $compare _referrer _group . find ( '.referrer_group' ) . text ( ) ;
var compare _theme = $compare _referrer _group . find ( 'select' ) . val ( ) ;
var compare _redirect = $compare _referrer _group . find ( 'input[type=text]' ) . val ( ) ;
var compare _agents = $compare _referrer _group . find ( 'textarea' ) . val ( ) . split ( "\n" ) . filter ( function ( line ) { return line . trim ( ) !== '' } ) . map ( function ( line ) { return line . trim ( ) ; } ) ;
var groups = sort _array ( [ name , compare _name ] ) ;
if ( compare _redirect === '' && redirect === '' && compare _theme !== '' && compare _theme === theme ) {
error . push ( 'Duplicate theme "' + compare _theme + '" found in the referrer groups "' + groups [ 0 ] + '" and "' + groups [ 1 ] + '"' ) ;
}
if ( compare _redirect !== '' && compare _redirect === redirect ) {
error . push ( 'Duplicate redirect "' + compare _redirect + '" found in the referrer groups "' + groups [ 0 ] + '" and "' + groups [ 1 ] + '"' ) ;
}
jQuery . each ( compare _agents , function ( index , value ) {
if ( jQuery . inArray ( value , agents ) !== - 1 ) {
error . push ( 'Duplicate stem "' + value + '" found in the referrer groups "' + groups [ 0 ] + '" and "' + groups [ 1 ] + '"' ) ;
}
} ) ;
}
} ) ;
}
} ) ;
var cookiegroups = jQuery ( '#cookiegroups li' ) ;
cookiegroups . each ( function ( index , cookiegroup ) {
var $cookiegroup = jQuery ( cookiegroup ) ;
if ( $cookiegroup . find ( '.cookiegroup_enabled:checked' ) . length ) {
var name = $cookiegroup . find ( '.cookiegroup_name' ) . text ( ) ;
var agents = $cookiegroup . find ( 'textarea' ) . val ( ) . split ( "\n" ) . filter ( function ( line ) { return line . trim ( ) !== '' } ) . map ( function ( line ) { return line . trim ( ) ; } ) ;
console . log ( agents ) ;
cookiegroups . not ( $cookiegroup ) . each ( function ( index , compare _cookiegroup ) {
var $compare _cookiegroup = jQuery ( compare _cookiegroup ) ;
if ( $compare _cookiegroup . find ( '.cookiegroup_enabled:checked' ) . length ) {
var compare _name = $compare _cookiegroup . find ( '.cookiegroup_name' ) . text ( ) ;
var compare _agents = $compare _cookiegroup . find ( 'textarea' ) . val ( ) . split ( "\n" ) . filter ( function ( line ) { return line . trim ( ) !== '' } ) . map ( function ( line ) { return line . trim ( ) ; } ) ;
console . log ( compare _agents ) ;
var groups = sort _array ( [ name , compare _name ] ) ;
jQuery . each ( compare _agents , function ( index , value ) {
if ( jQuery . inArray ( value , agents ) !== - 1 ) {
error . push ( 'Duplicate stem "' + value + '" found in the cookie groups "' + groups [ 0 ] + '" and "' + groups [ 1 ] + '"' ) ;
}
} ) ;
}
} ) ;
}
} ) ;
if ( error . length !== 0 ) {
alert ( unique _array ( error ) . join ( '\n' ) ) ;
2023-06-05 11:23:16 +00:00
return false ;
}
2023-10-22 22:21:26 +00:00
return true ;
2023-06-05 11:23:16 +00:00
} ) ;
2023-10-22 22:21:26 +00:00
jQuery ( document ) . on ( 'click' , '#mobile_add' , function ( ) {
2023-06-05 11:23:16 +00:00
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 ) {
2023-10-22 22:21:26 +00:00
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" class="mobile_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>' ) ;
2023-06-05 11:23:16 +00:00
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!' ) ;
}
}
} ) ;
2023-10-22 22:21:26 +00:00
jQuery ( document ) . on ( 'click' , '.mobile_delete' , function ( ) {
2023-06-05 11:23:16 +00:00
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.
2023-10-22 22:21:26 +00:00
jQuery ( document ) . on ( 'click' , '#referrer_add' , function ( ) {
2023-06-05 11:23:16 +00:00
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 ) {
2023-10-22 22:21:26 +00:00
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" class="referrer_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>' ) ;
2023-06-05 11:23:16 +00:00
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!' ) ;
}
}
} ) ;
2023-10-22 22:21:26 +00:00
jQuery ( document ) . on ( 'click' , '.referrer_delete' , function ( ) {
2023-06-05 11:23:16 +00:00
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.
2023-10-22 22:21:26 +00:00
jQuery ( document ) . on ( 'click' , '#w3tc_cookiegroup_add' , function ( ) {
2023-06-05 11:23:16 +00:00
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>' +
2023-10-22 22:21:26 +00:00
'<td>' +
'<span class="cookiegroup_number">' + ( jQuery ( '#cookiegroups li' ) . length + 1 ) + '.</span> ' +
2023-06-05 11:23:16 +00:00
'<span class="cookiegroup_name">' + group + '</span> ' +
2023-10-22 22:21:26 +00:00
'<input type="button" class="button w3tc_cookiegroup_delete" value="Delete group" />' +
'</td>' +
2023-06-05 11:23:16 +00:00
'</tr>' +
'<tr>' +
'<th><label for="cookiegroup_' + group + '_enabled">Enabled:</label></th>' +
'<td>' +
2023-10-22 22:21:26 +00:00
'<input id="cookiegroup_' + group + '_enabled" class="cookiegroup_enabled" type="checkbox" name="cookiegroups[' + group + '][enabled]" value="1" checked="checked" />' +
'</td>' +
2023-06-05 11:23:16 +00:00
'</tr>' +
'<tr>' +
'<th><label for="cookiegroup_' + group + '_cache">Cache:</label></th>' +
'<td>' +
2023-10-22 22:21:26 +00:00
'<input id="cookiegroup_' + group + '_cache" type="checkbox" name="cookiegroups[' + group + '][cache]" value="1" checked="checked" /></td>' +
'</tr>' +
2023-06-05 11:23:16 +00:00
'<tr>' +
'<th><label for="cookiegroups_' + group + '_cookies">Cookies:</label></th>' +
2023-10-22 22:21:26 +00:00
'<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>' ) ;
2023-06-05 11:23:16 +00:00
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!' ) ;
}
}
} ) ;
2023-10-22 22:21:26 +00:00
jQuery ( document ) . on ( 'click' , '.w3tc_cookiegroup_delete' , function ( ) {
2023-06-05 11:23:16 +00:00
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 ( ) ;
}
}
2023-10-22 22:21:26 +00:00
function unique _array ( array ) {
return jQuery . grep ( array , function ( el , i ) { return i === jQuery . inArray ( el , array ) } ) ;
}
function sort _array ( array ) {
return array . sort ( ) ;
}