wp/wp-includes/js/utils.js
changeset 5 5e2f62d02dcd
parent 0 d970ebf37754
child 7 cf61fcea0001
--- a/wp/wp-includes/js/utils.js	Mon Jun 08 16:11:51 2015 +0000
+++ b/wp/wp-includes/js/utils.js	Tue Jun 09 03:35:32 2015 +0200
@@ -1,25 +1,29 @@
+/* global userSettings */
+/* exported getUserSetting, setUserSetting, deleteUserSetting */
 // utility functions
 
 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 3, Moxiecode, used under LGPL.
 
-	each : function(obj, cb, scope) {
+	each: function( obj, cb, scope ) {
 		var n, l;
 
-		if ( !obj )
+		if ( ! obj ) {
 			return 0;
+		}
 
 		scope = scope || obj;
 
-		if ( typeof(obj.length) != 'undefined' ) {
+		if ( typeof( obj.length ) !== 'undefined' ) {
 			for ( n = 0, l = obj.length; n < l; n++ ) {
-				if ( cb.call(scope, obj[n], n, obj) === false )
+				if ( cb.call( scope, obj[n], n, obj ) === false ) {
 					return 0;
+				}
 			}
 		} else {
 			for ( n in obj ) {
 				if ( obj.hasOwnProperty(n) ) {
-					if ( cb.call(scope, obj[n], n, obj) === false ) {
+					if ( cb.call( scope, obj[n], n, obj ) === false ) {
 						return 0;
 					}
 				}
@@ -32,17 +36,18 @@
 	 * Get a multi-values cookie.
 	 * Returns a JS object with the name: 'value' pairs.
 	 */
-	getHash : function(name) {
-		var all = this.get(name), ret;
+	getHash: function( name ) {
+		var cookie = this.get( name ), values;
 
-		if ( all ) {
-			this.each( all.split('&'), function(pair) {
+		if ( cookie ) {
+			this.each( cookie.split('&'), function( pair ) {
 				pair = pair.split('=');
-				ret = ret || {};
-				ret[pair[0]] = pair[1];
+				values = values || {};
+				values[pair[0]] = pair[1];
 			});
 		}
-		return ret;
+
+		return values;
 	},
 
 	/**
@@ -50,43 +55,47 @@
 	 *
 	 * '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) {
+	setHash: function( name, values_obj, expires, path, domain, secure ) {
 		var str = '';
 
-		this.each(values_obj, function(val, key) {
-			str += (!str ? '' : '&') + key + '=' + val;
+		this.each( values_obj, function( val, key ) {
+			str += ( ! str ? '' : '&' ) + key + '=' + val;
 		});
 
-		this.set(name, str, expires, path, domain, secure);
+		this.set( name, str, expires, path, domain, secure );
 	},
 
 	/**
 	 * Get a cookie.
 	 */
-	get : function(name) {
-		var cookie = document.cookie, e, p = name + "=", b;
+	get: function( name ) {
+		var e, b,
+			cookie = document.cookie,
+			p = name + '=';
 
-		if ( !cookie )
+		if ( ! cookie ) {
 			return;
+		}
 
-		b = cookie.indexOf("; " + p);
+		b = cookie.indexOf( '; ' + p );
 
-		if ( b == -1 ) {
+		if ( b === -1 ) {
 			b = cookie.indexOf(p);
 
-			if ( b != 0 )
+			if ( b !== 0 ) {
 				return null;
-
+			}
 		} else {
 			b += 2;
 		}
 
-		e = cookie.indexOf(";", b);
+		e = cookie.indexOf( ';', b );
 
-		if ( e == -1 )
+		if ( e === -1 ) {
 			e = cookie.length;
+		}
 
-		return decodeURIComponent( cookie.substring(b + p.length, e) );
+		return decodeURIComponent( cookie.substring( b + p.length, e ) );
 	},
 
 	/**
@@ -95,23 +104,23 @@
 	 * The 'expires' arg can be either a JS Date() object set to the expiration date (back-compat)
 	 * or the number of seconds until expiration
 	 */
-	set : function(name, value, expires, path, domain, secure) {
+	set: function( name, value, expires, path, domain, secure ) {
 		var d = new Date();
 
-		if ( typeof(expires) == 'object' && expires.toGMTString ) {
+		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
+		} else if ( parseInt( expires, 10 ) ) {
+			d.setTime( d.getTime() + ( parseInt( expires, 10 ) * 1000 ) ); // time must be in miliseconds
 			expires = d.toGMTString();
 		} else {
 			expires = '';
 		}
 
-		document.cookie = name + "=" + encodeURIComponent(value) +
-			((expires) ? "; expires=" + expires : "") +
-			((path) ? "; path=" + path : "") +
-			((domain) ? "; domain=" + domain : "") +
-			((secure) ? "; secure" : "");
+		document.cookie = name + '=' + encodeURIComponent( value ) +
+			( expires ? '; expires=' + expires : '' ) +
+			( path    ? '; path=' + path       : '' ) +
+			( domain  ? '; domain=' + domain   : '' ) +
+			( secure  ? '; secure'             : '' );
 	},
 
 	/**
@@ -119,41 +128,57 @@
 	 *
 	 * 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);
+	remove: function( name, path, domain, secure ) {
+		this.set( name, '', -1000, path, domain, secure );
 	}
 };
 
 // Returns the value as string. Second arg or empty string is returned when value is not set.
 function getUserSetting( name, def ) {
-	var obj = getAllUserSettings();
+	var settings = getAllUserSettings();
 
-	if ( obj.hasOwnProperty(name) )
-		return obj[name];
+	if ( settings.hasOwnProperty( name ) ) {
+		return settings[name];
+	}
 
-	if ( typeof def != 'undefined' )
+	if ( typeof def !== 'undefined' ) {
 		return def;
+	}
 
 	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.
+// The value is converted and stored as string.
 function setUserSetting( name, value, _del ) {
-	if ( 'object' !== typeof userSettings )
+	if ( 'object' !== typeof userSettings ) {
 		return false;
+	}
 
-	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_]/, '');
+	var uid = userSettings.uid,
+		settings = wpCookies.getHash( 'wp-settings-' + uid ),
+		path = userSettings.url,
+		secure = !! userSettings.secure;
+
+	name = name.toString().replace( /[^A-Za-z0-9_]/, '' );
+
+	if ( typeof value === 'number' ) {
+		value = parseInt( value, 10 );
+	} else {
+		value = value.toString().replace( /[^A-Za-z0-9_]/, '' );
+	}
+
+	settings = settings || {};
 
 	if ( _del ) {
-		delete all[n];
+		delete settings[name];
 	} else {
-		all[n] = v;
+		settings[name] = value;
 	}
 
-	wpCookies.setHash(cookie, all, 31536000, path);
-	wpCookies.set('wp-settings-time-'+userSettings.uid, userSettings.time, 31536000, path);
+	wpCookies.setHash( 'wp-settings-' + uid, settings, 31536000, path, '', secure );
+	wpCookies.set( 'wp-settings-time-' + uid, userSettings.time, 31536000, path, '', secure );
 
 	return name;
 }
@@ -164,8 +189,9 @@
 
 // Returns all settings as js object.
 function getAllUserSettings() {
-	if ( 'object' !== typeof userSettings )
+	if ( 'object' !== typeof userSettings ) {
 		return {};
+	}
 
-	return wpCookies.getHash('wp-settings-' + userSettings.uid) || {};
+	return wpCookies.getHash( 'wp-settings-' + userSettings.uid ) || {};
 }