2013-11-15 00:26:10 -05:00
|
|
|
/* global userSettings */
|
|
|
|
/* exported getUserSetting, setUserSetting, deleteUserSetting */
|
2012-08-22 20:04:18 -04:00
|
|
|
// utility functions
|
|
|
|
|
|
|
|
var wpCookies = {
|
2014-08-02 16:09:16 -04:00
|
|
|
// The following functions are from Cookie.js class in TinyMCE 3, Moxiecode, used under LGPL.
|
2012-08-22 20:04:18 -04:00
|
|
|
|
2014-08-02 16:09:16 -04:00
|
|
|
each: function( obj, cb, scope ) {
|
2012-08-22 20:04:18 -04:00
|
|
|
var n, l;
|
|
|
|
|
2014-08-02 16:09:16 -04:00
|
|
|
if ( ! obj ) {
|
2012-08-22 20:04:18 -04:00
|
|
|
return 0;
|
2014-08-02 16:09:16 -04:00
|
|
|
}
|
2012-08-22 20:04:18 -04:00
|
|
|
|
|
|
|
scope = scope || obj;
|
|
|
|
|
2014-08-02 16:09:16 -04:00
|
|
|
if ( typeof( obj.length ) !== 'undefined' ) {
|
2012-08-22 20:04:18 -04:00
|
|
|
for ( n = 0, l = obj.length; n < l; n++ ) {
|
2014-08-02 16:09:16 -04:00
|
|
|
if ( cb.call( scope, obj[n], n, obj ) === false ) {
|
2012-08-22 20:04:18 -04:00
|
|
|
return 0;
|
2014-08-02 16:09:16 -04:00
|
|
|
}
|
2012-08-22 20:04:18 -04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for ( n in obj ) {
|
|
|
|
if ( obj.hasOwnProperty(n) ) {
|
2014-08-02 16:09:16 -04:00
|
|
|
if ( cb.call( scope, obj[n], n, obj ) === false ) {
|
2012-08-22 20:04:18 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a multi-values cookie.
|
|
|
|
* Returns a JS object with the name: 'value' pairs.
|
|
|
|
*/
|
2014-08-02 16:09:16 -04:00
|
|
|
getHash: function( name ) {
|
|
|
|
var cookie = this.get( name ), values;
|
2012-08-22 20:04:18 -04:00
|
|
|
|
2014-08-02 16:09:16 -04:00
|
|
|
if ( cookie ) {
|
|
|
|
this.each( cookie.split('&'), function( pair ) {
|
2012-08-22 20:04:18 -04:00
|
|
|
pair = pair.split('=');
|
2014-08-02 16:09:16 -04:00
|
|
|
values = values || {};
|
|
|
|
values[pair[0]] = pair[1];
|
2012-08-22 20:04:18 -04:00
|
|
|
});
|
|
|
|
}
|
2014-08-02 16:09:16 -04:00
|
|
|
|
|
|
|
return values;
|
2012-08-22 20:04:18 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set a multi-values cookie.
|
|
|
|
*
|
|
|
|
* 'values_obj' is the JS object that is stored. It is encoded as URI in wpCookies.set().
|
|
|
|
*/
|
2014-08-02 16:09:16 -04:00
|
|
|
setHash: function( name, values_obj, expires, path, domain, secure ) {
|
2012-08-22 20:04:18 -04:00
|
|
|
var str = '';
|
|
|
|
|
2014-08-02 16:09:16 -04:00
|
|
|
this.each( values_obj, function( val, key ) {
|
|
|
|
str += ( ! str ? '' : '&' ) + key + '=' + val;
|
2012-08-22 20:04:18 -04:00
|
|
|
});
|
|
|
|
|
2014-08-02 16:09:16 -04:00
|
|
|
this.set( name, str, expires, path, domain, secure );
|
2012-08-22 20:04:18 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a cookie.
|
|
|
|
*/
|
2014-08-02 16:09:16 -04:00
|
|
|
get: function( name ) {
|
2013-11-15 00:26:10 -05:00
|
|
|
var e, b,
|
|
|
|
cookie = document.cookie,
|
|
|
|
p = name + '=';
|
2012-08-22 20:04:18 -04:00
|
|
|
|
2014-08-02 16:09:16 -04:00
|
|
|
if ( ! cookie ) {
|
2012-08-22 20:04:18 -04:00
|
|
|
return;
|
2014-08-02 16:09:16 -04:00
|
|
|
}
|
2012-08-22 20:04:18 -04:00
|
|
|
|
2014-08-02 16:09:16 -04:00
|
|
|
b = cookie.indexOf( '; ' + p );
|
2012-08-22 20:04:18 -04:00
|
|
|
|
2014-08-02 16:09:16 -04:00
|
|
|
if ( b === -1 ) {
|
2012-08-22 20:04:18 -04:00
|
|
|
b = cookie.indexOf(p);
|
|
|
|
|
2014-08-02 16:09:16 -04:00
|
|
|
if ( b !== 0 ) {
|
2012-08-22 20:04:18 -04:00
|
|
|
return null;
|
2014-08-02 16:09:16 -04:00
|
|
|
}
|
2012-08-22 20:04:18 -04:00
|
|
|
} else {
|
|
|
|
b += 2;
|
|
|
|
}
|
|
|
|
|
2013-11-15 00:26:10 -05:00
|
|
|
e = cookie.indexOf( ';', b );
|
2012-08-22 20:04:18 -04:00
|
|
|
|
2014-08-02 16:09:16 -04:00
|
|
|
if ( e === -1 ) {
|
2012-08-22 20:04:18 -04:00
|
|
|
e = cookie.length;
|
2014-08-02 16:09:16 -04:00
|
|
|
}
|
2012-08-22 20:04:18 -04:00
|
|
|
|
2014-08-02 16:09:16 -04:00
|
|
|
return decodeURIComponent( cookie.substring( b + p.length, e ) );
|
2012-08-22 20:04:18 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set a cookie.
|
|
|
|
*
|
|
|
|
* The 'expires' arg can be either a JS Date() object set to the expiration date (back-compat)
|
|
|
|
* or the number of seconds until expiration
|
|
|
|
*/
|
2014-08-02 16:09:16 -04:00
|
|
|
set: function( name, value, expires, path, domain, secure ) {
|
2012-08-22 20:04:18 -04:00
|
|
|
var d = new Date();
|
|
|
|
|
2014-08-02 16:09:16 -04:00
|
|
|
if ( typeof( expires ) === 'object' && expires.toGMTString ) {
|
2012-08-22 20:04:18 -04:00
|
|
|
expires = expires.toGMTString();
|
2014-08-02 16:09:16 -04:00
|
|
|
} else if ( parseInt( expires, 10 ) ) {
|
2016-10-31 02:28:32 -04:00
|
|
|
d.setTime( d.getTime() + ( parseInt( expires, 10 ) * 1000 ) ); // time must be in milliseconds
|
2012-08-22 20:04:18 -04:00
|
|
|
expires = d.toGMTString();
|
|
|
|
} else {
|
|
|
|
expires = '';
|
|
|
|
}
|
|
|
|
|
2013-11-15 00:26:10 -05:00
|
|
|
document.cookie = name + '=' + encodeURIComponent( value ) +
|
|
|
|
( expires ? '; expires=' + expires : '' ) +
|
|
|
|
( path ? '; path=' + path : '' ) +
|
|
|
|
( domain ? '; domain=' + domain : '' ) +
|
|
|
|
( secure ? '; secure' : '' );
|
2012-08-22 20:04:18 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove a cookie.
|
|
|
|
*
|
|
|
|
* This is done by setting it to an empty value and setting the expiration time in the past.
|
|
|
|
*/
|
2014-08-12 22:47:16 -04:00
|
|
|
remove: function( name, path, domain, secure ) {
|
|
|
|
this.set( name, '', -1000, path, domain, secure );
|
2012-08-22 20:04:18 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Returns the value as string. Second arg or empty string is returned when value is not set.
|
|
|
|
function getUserSetting( name, def ) {
|
2014-08-02 16:09:16 -04:00
|
|
|
var settings = getAllUserSettings();
|
2012-08-22 20:04:18 -04:00
|
|
|
|
2014-08-02 16:09:16 -04:00
|
|
|
if ( settings.hasOwnProperty( name ) ) {
|
|
|
|
return settings[name];
|
|
|
|
}
|
2012-08-22 20:04:18 -04:00
|
|
|
|
2014-08-02 16:09:16 -04:00
|
|
|
if ( typeof def !== 'undefined' ) {
|
2012-08-22 20:04:18 -04:00
|
|
|
return def;
|
2014-08-02 16:09:16 -04:00
|
|
|
}
|
2012-08-22 20:04:18 -04:00
|
|
|
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2014-08-02 16:09:16 -04:00
|
|
|
// The value is converted and stored as string.
|
2012-08-22 20:04:18 -04:00
|
|
|
function setUserSetting( name, value, _del ) {
|
2014-08-02 16:09:16 -04:00
|
|
|
if ( 'object' !== typeof userSettings ) {
|
2012-08-22 20:04:18 -04:00
|
|
|
return false;
|
2014-08-02 16:09:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
var uid = userSettings.uid,
|
|
|
|
settings = wpCookies.getHash( 'wp-settings-' + uid ),
|
2014-08-12 22:47:16 -04:00
|
|
|
path = userSettings.url,
|
|
|
|
secure = !! userSettings.secure;
|
2012-08-22 20:04:18 -04:00
|
|
|
|
2015-09-08 12:44:26 -04:00
|
|
|
name = name.toString().replace( /[^A-Za-z0-9_-]/g, '' );
|
2014-08-02 16:09:16 -04:00
|
|
|
|
|
|
|
if ( typeof value === 'number' ) {
|
|
|
|
value = parseInt( value, 10 );
|
|
|
|
} else {
|
2015-09-08 12:44:26 -04:00
|
|
|
value = value.toString().replace( /[^A-Za-z0-9_-]/g, '' );
|
2014-08-02 16:09:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
settings = settings || {};
|
2012-08-22 20:04:18 -04:00
|
|
|
|
|
|
|
if ( _del ) {
|
2014-08-02 16:09:16 -04:00
|
|
|
delete settings[name];
|
2012-08-22 20:04:18 -04:00
|
|
|
} else {
|
2014-08-02 16:09:16 -04:00
|
|
|
settings[name] = value;
|
2012-08-22 20:04:18 -04:00
|
|
|
}
|
|
|
|
|
2014-08-12 22:47:16 -04:00
|
|
|
wpCookies.setHash( 'wp-settings-' + uid, settings, 31536000, path, '', secure );
|
|
|
|
wpCookies.set( 'wp-settings-time-' + uid, userSettings.time, 31536000, path, '', secure );
|
2012-08-22 20:04:18 -04:00
|
|
|
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
function deleteUserSetting( name ) {
|
|
|
|
return setUserSetting( name, '', 1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns all settings as js object.
|
|
|
|
function getAllUserSettings() {
|
2014-08-02 16:09:16 -04:00
|
|
|
if ( 'object' !== typeof userSettings ) {
|
2012-08-22 20:04:18 -04:00
|
|
|
return {};
|
2014-08-02 16:09:16 -04:00
|
|
|
}
|
|
|
|
|
2014-08-12 22:47:16 -04:00
|
|
|
return wpCookies.getHash( 'wp-settings-' + userSettings.uid ) || {};
|
2012-08-22 20:04:18 -04:00
|
|
|
}
|