Refresh wpCookie, fixes #20078

git-svn-id: https://develop.svn.wordpress.org/trunk@19954 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Andrew Ozz 2012-02-19 23:08:10 +00:00
parent d219e90d41
commit 789c4388df
1 changed files with 84 additions and 57 deletions

View File

@ -3,23 +3,23 @@
var wpCookies = { var wpCookies = {
// The following functions are from Cookie.js class in TinyMCE, Moxiecode, used under LGPL. // The following functions are from Cookie.js class in TinyMCE, Moxiecode, used under LGPL.
each : function(o, cb, s) { each : function(obj, cb, scope) {
var n, l; var n, l;
if (!o) if ( !obj )
return 0; return 0;
s = s || o; scope = scope || obj;
if (typeof(o.length) != 'undefined') { if ( typeof(obj.length) != 'undefined' ) {
for (n=0, l = o.length; n<l; n++) { for ( n = 0, l = obj.length; n < l; n++ ) {
if (cb.call(s, o[n], n, o) === false) if ( cb.call(scope, obj[n], n, obj) === false )
return 0; return 0;
} }
} else { } else {
for (n in o) { for ( n in obj ) {
if (o.hasOwnProperty(n)) { if ( obj.hasOwnProperty(n) ) {
if (cb.call(s, o[n], n, o) === false) { if ( cb.call(scope, obj[n], n, obj) === false ) {
return 0; return 0;
} }
} }
@ -28,78 +28,108 @@ var wpCookies = {
return 1; return 1;
}, },
getHash : function(n) { /**
var v = this.get(n), h; * Get a multi-values cookie.
* Returns a JS object with the name: 'value' pairs.
*/
getHash : function(name) {
var all = this.get(name), ret;
if (v) { if ( all ) {
this.each(v.split('&'), function(v) { this.each( all.split('&'), function(pair) {
v = v.split('='); pair = pair.split('=');
h = h || {}; ret = ret || {};
h[v[0]] = v[1]; ret[pair[0]] = pair[1];
}); });
} }
return h; return ret;
}, },
setHash : function(n, v, e, p, d, s) { /**
var o = ''; * Set a multi-values cookie.
*
* 'values_obj' is the JS object that is stored. It is encoded as URI in wpCookies.set().
*/
setHash : function(name, values_obj, expires, path, domain, secure) {
var str = '';
this.each(v, function(v, k) { this.each(values_obj, function(val, key) {
o += (!o ? '' : '&') + k + '=' + v; str += (!str ? '' : '&') + key + '=' + val;
}); });
this.set(n, o, e, p, d, s); this.set(name, str, expires, path, domain, secure);
}, },
get : function(n) { /**
var c = document.cookie, e, p = n + "=", b; * Get a cookie.
*/
get : function(name) {
var cookie = document.cookie, e, p = name + "=", b;
if (!c) if ( !cookie )
return; return;
b = c.indexOf("; " + p); b = cookie.indexOf("; " + p);
if (b == -1) { if ( b == -1 ) {
b = c.indexOf(p); b = cookie.indexOf(p);
if (b != 0) if ( b != 0 )
return null; return null;
} else { } else {
b += 2; b += 2;
} }
e = c.indexOf(";", b); e = cookie.indexOf(";", b);
if (e == -1) if ( e == -1 )
e = c.length; e = cookie.length;
return decodeURIComponent(c.substring(b + p.length, e)); return decodeURIComponent( cookie.substring(b + p.length, e) );
}, },
set : function(n, v, e, p, d, s) { /**
document.cookie = n + "=" + encodeURIComponent(v) + * Set a cookie.
((e) ? "; expires=" + e.toGMTString() : "") + *
((p) ? "; path=" + p : "") + * The 'expires' arg can be either a JS Date() object set to the expiration date (back-compat)
((d) ? "; domain=" + d : "") + * or the number of seconds until expiration
((s) ? "; secure" : ""); */
}, set : function(name, value, expires, path, domain, secure) {
remove : function(n, p) {
var d = new Date(); var d = new Date();
d.setTime(d.getTime() - 1000); if ( typeof(expires) == 'object' && expires.toGMTString ) {
expires = expires.toGMTString();
} else if ( parseInt(expires, 10) ) {
d.setTime( d.getTime() + ( parseInt(expires, 10) * 1000 ) ); // time must be in miliseconds
expires = d.toGMTString();
} else {
expires = '';
}
this.set(n, '', d, p, d); document.cookie = name + "=" + encodeURIComponent(value) +
((expires) ? "; expires=" + expires : "") +
((path) ? "; path=" + path : "") +
((domain) ? "; domain=" + domain : "") +
((secure) ? "; secure" : "");
},
/**
* Remove a cookie.
*
* This is done by setting it to an empty value and setting the expiration time in the past.
*/
remove : function(name, path) {
this.set(name, '', -1000, path);
} }
}; };
// Returns the value as string. Second arg or empty string is returned when value is not set. // Returns the value as string. Second arg or empty string is returned when value is not set.
function getUserSetting( name, def ) { function getUserSetting( name, def ) {
var o = getAllUserSettings(); var obj = getAllUserSettings();
if ( o.hasOwnProperty(name) ) if ( obj.hasOwnProperty(name) )
return o[name]; return obj[name];
if ( typeof def != 'undefined' ) if ( typeof def != 'undefined' )
return def; return def;
@ -109,24 +139,21 @@ function getUserSetting( name, def ) {
// Both name and value must be only ASCII letters, numbers or underscore // Both name and value must be only ASCII letters, numbers or underscore
// and the shorter, the better (cookies can store maximum 4KB). Not suitable to store text. // and the shorter, the better (cookies can store maximum 4KB). Not suitable to store text.
function setUserSetting( name, value, del ) { function setUserSetting( name, value, _del ) {
if ( 'object' !== typeof userSettings ) if ( 'object' !== typeof userSettings )
return false; return false;
var c = 'wp-settings-' + userSettings.uid, o = wpCookies.getHash(c) || {}, d = new Date(), p, var cookie = 'wp-settings-' + userSettings.uid, all = wpCookies.getHash(cookie) || {}, path = userSettings.url,
n = name.toString().replace(/[^A-Za-z0-9_]/, ''), v = value.toString().replace(/[^A-Za-z0-9_]/, ''); n = name.toString().replace(/[^A-Za-z0-9_]/, ''), v = value.toString().replace(/[^A-Za-z0-9_]/, '');
if ( del ) { if ( _del ) {
delete o[n]; delete all[n];
} else { } else {
o[n] = v; all[n] = v;
} }
d.setTime( d.getTime() + 31536000000 ); wpCookies.setHash(cookie, all, 31536000, path);
p = userSettings.url; wpCookies.set('wp-settings-time-'+userSettings.uid, userSettings.time, 31536000, path);
wpCookies.setHash(c, o, d, p);
wpCookies.set('wp-settings-time-'+userSettings.uid, userSettings.time, d, p);
return name; return name;
} }