1 /* global userSettings */
2 /* exported getUserSetting, setUserSetting, deleteUserSetting */
6 // The following functions are from Cookie.js class in TinyMCE 3, Moxiecode, used under LGPL.
8 each: function( obj, cb, scope ) {
17 if ( typeof( obj.length ) !== 'undefined' ) {
18 for ( n = 0, l = obj.length; n < l; n++ ) {
19 if ( cb.call( scope, obj[n], n, obj ) === false ) {
25 if ( obj.hasOwnProperty(n) ) {
26 if ( cb.call( scope, obj[n], n, obj ) === false ) {
36 * Get a multi-values cookie.
37 * Returns a JS object with the name: 'value' pairs.
39 getHash: function( name ) {
40 var cookie = this.get( name ), values;
43 this.each( cookie.split('&'), function( pair ) {
44 pair = pair.split('=');
45 values = values || {};
46 values[pair[0]] = pair[1];
54 * Set a multi-values cookie.
56 * 'values_obj' is the JS object that is stored. It is encoded as URI in wpCookies.set().
58 setHash: function( name, values_obj, expires, path, domain, secure ) {
61 this.each( values_obj, function( val, key ) {
62 str += ( ! str ? '' : '&' ) + key + '=' + val;
65 this.set( name, str, expires, path, domain, secure );
71 get: function( name ) {
73 cookie = document.cookie,
80 b = cookie.indexOf( '; ' + p );
83 b = cookie.indexOf(p);
92 e = cookie.indexOf( ';', b );
98 return decodeURIComponent( cookie.substring( b + p.length, e ) );
104 * The 'expires' arg can be either a JS Date() object set to the expiration date (back-compat)
105 * or the number of seconds until expiration
107 set: function( name, value, expires, path, domain, secure ) {
110 if ( typeof( expires ) === 'object' && expires.toGMTString ) {
111 expires = expires.toGMTString();
112 } else if ( parseInt( expires, 10 ) ) {
113 d.setTime( d.getTime() + ( parseInt( expires, 10 ) * 1000 ) ); // time must be in miliseconds
114 expires = d.toGMTString();
119 document.cookie = name + '=' + encodeURIComponent( value ) +
120 ( expires ? '; expires=' + expires : '' ) +
121 ( path ? '; path=' + path : '' ) +
122 ( domain ? '; domain=' + domain : '' ) +
123 ( secure ? '; secure' : '' );
129 * This is done by setting it to an empty value and setting the expiration time in the past.
131 remove: function( name, path, domain, secure ) {
132 this.set( name, '', -1000, path, domain, secure );
136 // Returns the value as string. Second arg or empty string is returned when value is not set.
137 function getUserSetting( name, def ) {
138 var settings = getAllUserSettings();
140 if ( settings.hasOwnProperty( name ) ) {
141 return settings[name];
144 if ( typeof def !== 'undefined' ) {
151 // Both name and value must be only ASCII letters, numbers or underscore
152 // and the shorter, the better (cookies can store maximum 4KB). Not suitable to store text.
153 // The value is converted and stored as string.
154 function setUserSetting( name, value, _del ) {
155 if ( 'object' !== typeof userSettings ) {
159 var uid = userSettings.uid,
160 settings = wpCookies.getHash( 'wp-settings-' + uid ),
161 path = userSettings.url,
162 secure = !! userSettings.secure;
164 name = name.toString().replace( /[^A-Za-z0-9_-]/g, '' );
166 if ( typeof value === 'number' ) {
167 value = parseInt( value, 10 );
169 value = value.toString().replace( /[^A-Za-z0-9_-]/g, '' );
172 settings = settings || {};
175 delete settings[name];
177 settings[name] = value;
180 wpCookies.setHash( 'wp-settings-' + uid, settings, 31536000, path, '', secure );
181 wpCookies.set( 'wp-settings-time-' + uid, userSettings.time, 31536000, path, '', secure );
186 function deleteUserSetting( name ) {
187 return setUserSetting( name, '', 1 );
190 // Returns all settings as js object.
191 function getAllUserSettings() {
192 if ( 'object' !== typeof userSettings ) {
196 return wpCookies.getHash( 'wp-settings-' + userSettings.uid ) || {};