1 /* global userSettings */
2 /* exported getUserSetting, setUserSetting, deleteUserSetting */
6 // The following functions are from Cookie.js class in TinyMCE, Moxiecode, used under LGPL.
8 each : function(obj, cb, scope) {
16 if ( typeof(obj.length) != 'undefined' ) {
17 for ( n = 0, l = obj.length; n < l; n++ ) {
18 if ( cb.call(scope, obj[n], n, obj) === false )
23 if ( obj.hasOwnProperty(n) ) {
24 if ( cb.call(scope, obj[n], n, obj) === false ) {
34 * Get a multi-values cookie.
35 * Returns a JS object with the name: 'value' pairs.
37 getHash : function(name) {
38 var all = this.get(name), ret;
41 this.each( all.split('&'), function(pair) {
42 pair = pair.split('=');
44 ret[pair[0]] = pair[1];
51 * Set a multi-values cookie.
53 * 'values_obj' is the JS object that is stored. It is encoded as URI in wpCookies.set().
55 setHash : function(name, values_obj, expires, path, domain, secure) {
58 this.each(values_obj, function(val, key) {
59 str += (!str ? '' : '&') + key + '=' + val;
62 this.set(name, str, expires, path, domain, secure);
68 get : function(name) {
70 cookie = document.cookie,
76 b = cookie.indexOf('; ' + p);
79 b = cookie.indexOf(p);
88 e = cookie.indexOf( ';', b );
93 return decodeURIComponent( cookie.substring(b + p.length, e) );
99 * The 'expires' arg can be either a JS Date() object set to the expiration date (back-compat)
100 * or the number of seconds until expiration
102 set : function(name, value, expires, path, domain, secure) {
105 if ( typeof(expires) == 'object' && expires.toGMTString ) {
106 expires = expires.toGMTString();
107 } else if ( parseInt(expires, 10) ) {
108 d.setTime( d.getTime() + ( parseInt(expires, 10) * 1000 ) ); // time must be in miliseconds
109 expires = d.toGMTString();
114 document.cookie = name + '=' + encodeURIComponent( value ) +
115 ( expires ? '; expires=' + expires : '' ) +
116 ( path ? '; path=' + path : '' ) +
117 ( domain ? '; domain=' + domain : '' ) +
118 ( secure ? '; secure' : '' );
124 * This is done by setting it to an empty value and setting the expiration time in the past.
126 remove : function(name, path) {
127 this.set(name, '', -1000, path);
131 // Returns the value as string. Second arg or empty string is returned when value is not set.
132 function getUserSetting( name, def ) {
133 var obj = getAllUserSettings();
135 if ( obj.hasOwnProperty(name) )
138 if ( typeof def != 'undefined' )
144 // Both name and value must be only ASCII letters, numbers or underscore
145 // and the shorter, the better (cookies can store maximum 4KB). Not suitable to store text.
146 function setUserSetting( name, value, _del ) {
147 if ( 'object' !== typeof userSettings )
150 var cookie = 'wp-settings-' + userSettings.uid, all = wpCookies.getHash(cookie) || {}, path = userSettings.url,
151 n = name.toString().replace(/[^A-Za-z0-9_]/, ''), v = value.toString().replace(/[^A-Za-z0-9_]/, '');
159 wpCookies.setHash(cookie, all, 31536000, path);
160 wpCookies.set('wp-settings-time-'+userSettings.uid, userSettings.time, 31536000, path);
165 function deleteUserSetting( name ) {
166 return setUserSetting( name, '', 1 );
169 // Returns all settings as js object.
170 function getAllUserSettings() {
171 if ( 'object' !== typeof userSettings )
174 return wpCookies.getHash('wp-settings-' + userSettings.uid) || {};