4 // The following functions are from Cookie.js class in TinyMCE, Moxiecode, used under LGPL.
6 each : function(obj, cb, scope) {
14 if ( typeof(obj.length) != 'undefined' ) {
15 for ( n = 0, l = obj.length; n < l; n++ ) {
16 if ( cb.call(scope, obj[n], n, obj) === false )
21 if ( obj.hasOwnProperty(n) ) {
22 if ( cb.call(scope, obj[n], n, obj) === false ) {
32 * Get a multi-values cookie.
33 * Returns a JS object with the name: 'value' pairs.
35 getHash : function(name) {
36 var all = this.get(name), ret;
39 this.each( all.split('&'), function(pair) {
40 pair = pair.split('=');
42 ret[pair[0]] = pair[1];
49 * Set a multi-values cookie.
51 * 'values_obj' is the JS object that is stored. It is encoded as URI in wpCookies.set().
53 setHash : function(name, values_obj, expires, path, domain, secure) {
56 this.each(values_obj, function(val, key) {
57 str += (!str ? '' : '&') + key + '=' + val;
60 this.set(name, str, expires, path, domain, secure);
66 get : function(name) {
67 var cookie = document.cookie, e, p = name + "=", b;
72 b = cookie.indexOf("; " + p);
75 b = cookie.indexOf(p);
84 e = cookie.indexOf(";", b);
89 return decodeURIComponent( cookie.substring(b + p.length, e) );
95 * The 'expires' arg can be either a JS Date() object set to the expiration date (back-compat)
96 * or the number of seconds until expiration
98 set : function(name, value, expires, path, domain, secure) {
101 if ( typeof(expires) == 'object' && expires.toGMTString ) {
102 expires = expires.toGMTString();
103 } else if ( parseInt(expires, 10) ) {
104 d.setTime( d.getTime() + ( parseInt(expires, 10) * 1000 ) ); // time must be in miliseconds
105 expires = d.toGMTString();
110 document.cookie = name + "=" + encodeURIComponent(value) +
111 ((expires) ? "; expires=" + expires : "") +
112 ((path) ? "; path=" + path : "") +
113 ((domain) ? "; domain=" + domain : "") +
114 ((secure) ? "; secure" : "");
120 * This is done by setting it to an empty value and setting the expiration time in the past.
122 remove : function(name, path) {
123 this.set(name, '', -1000, path);
127 // Returns the value as string. Second arg or empty string is returned when value is not set.
128 function getUserSetting( name, def ) {
129 var obj = getAllUserSettings();
131 if ( obj.hasOwnProperty(name) )
134 if ( typeof def != 'undefined' )
140 // Both name and value must be only ASCII letters, numbers or underscore
141 // and the shorter, the better (cookies can store maximum 4KB). Not suitable to store text.
142 function setUserSetting( name, value, _del ) {
143 if ( 'object' !== typeof userSettings )
146 var cookie = 'wp-settings-' + userSettings.uid, all = wpCookies.getHash(cookie) || {}, path = userSettings.url,
147 n = name.toString().replace(/[^A-Za-z0-9_]/, ''), v = value.toString().replace(/[^A-Za-z0-9_]/, '');
155 wpCookies.setHash(cookie, all, 31536000, path);
156 wpCookies.set('wp-settings-time-'+userSettings.uid, userSettings.time, 31536000, path);
161 function deleteUserSetting( name ) {
162 return setUserSetting( name, '', 1 );
165 // Returns all settings as js object.
166 function getAllUserSettings() {
167 if ( 'object' !== typeof userSettings )
170 return wpCookies.getHash('wp-settings-' + userSettings.uid) || {};