summaryrefslogtreecommitdiffstatshomepage
path: root/src/wp-includes/js/utils.js
diff options
context:
space:
mode:
authorAndrew Nacin <nacin@git.wordpress.org>2013-08-07 05:25:25 +0000
committerAndrew Nacin <nacin@git.wordpress.org>2013-08-07 05:25:25 +0000
commitb43712e0f79a9f5bea52217e06155e2f471c490c (patch)
tree23d331e7b2f29689571f03cfa5f9b7b3b6cafab8 /src/wp-includes/js/utils.js
parent5bbd08e1d17079a2e852517351f7405884a156b3 (diff)
downloadwordpress-b43712e0f79a9f5bea52217e06155e2f471c490c.tar.gz
wordpress-b43712e0f79a9f5bea52217e06155e2f471c490c.zip
New develop.svn.wordpress.org repository based on the old core.svn repository.
* All WordPress files move to a src/ directory. * New task runner (Grunt), configured to copy a built WordPress to build/. * svn:ignore and .gitignore for Gruntfile.js, wp-config.php, and node.js. * Remove Akismet external from develop.svn. Still exists in core.svn. * Drop minified files from src/. The build process will now generate these. props koop. see #24976. and see http://wp.me/p2AvED-1AI. git-svn-id: https://develop.svn.wordpress.org/trunk@25001 602fd350-edb4-49c9-b593-d223f7449a82
Diffstat (limited to 'src/wp-includes/js/utils.js')
-rw-r--r--src/wp-includes/js/utils.js171
1 files changed, 171 insertions, 0 deletions
diff --git a/src/wp-includes/js/utils.js b/src/wp-includes/js/utils.js
new file mode 100644
index 0000000000..d7566362a4
--- /dev/null
+++ b/src/wp-includes/js/utils.js
@@ -0,0 +1,171 @@
+// utility functions
+
+var wpCookies = {
+// The following functions are from Cookie.js class in TinyMCE, Moxiecode, used under LGPL.
+
+ each : function(obj, cb, scope) {
+ var n, l;
+
+ if ( !obj )
+ return 0;
+
+ scope = scope || obj;
+
+ if ( typeof(obj.length) != 'undefined' ) {
+ for ( n = 0, l = obj.length; n < l; n++ ) {
+ 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 ) {
+ return 0;
+ }
+ }
+ }
+ }
+ return 1;
+ },
+
+ /**
+ * Get a multi-values cookie.
+ * Returns a JS object with the name: 'value' pairs.
+ */
+ getHash : function(name) {
+ var all = this.get(name), ret;
+
+ if ( all ) {
+ this.each( all.split('&'), function(pair) {
+ pair = pair.split('=');
+ ret = ret || {};
+ ret[pair[0]] = pair[1];
+ });
+ }
+ return ret;
+ },
+
+ /**
+ * Set a multi-values cookie.
+ *
+ * '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) {
+ var str = '';
+
+ this.each(values_obj, function(val, key) {
+ str += (!str ? '' : '&') + key + '=' + val;
+ });
+
+ this.set(name, str, expires, path, domain, secure);
+ },
+
+ /**
+ * Get a cookie.
+ */
+ get : function(name) {
+ var cookie = document.cookie, e, p = name + "=", b;
+
+ if ( !cookie )
+ return;
+
+ b = cookie.indexOf("; " + p);
+
+ if ( b == -1 ) {
+ b = cookie.indexOf(p);
+
+ if ( b != 0 )
+ return null;
+
+ } else {
+ b += 2;
+ }
+
+ e = cookie.indexOf(";", b);
+
+ if ( e == -1 )
+ e = cookie.length;
+
+ return decodeURIComponent( cookie.substring(b + p.length, e) );
+ },
+
+ /**
+ * 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
+ */
+ set : function(name, value, expires, path, domain, secure) {
+ var d = new Date();
+
+ 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
+ expires = d.toGMTString();
+ } else {
+ expires = '';
+ }
+
+ document.cookie = name + "=" + encodeURIComponent(value) +
+ ((expires) ? "; expires=" + expires : "") +
+ ((path) ? "; path=" + path : "") +
+ ((domain) ? "; domain=" + domain : "") +
+ ((secure) ? "; secure" : "");
+ },
+
+ /**
+ * Remove a cookie.
+ *
+ * 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);
+ }
+};
+
+// Returns the value as string. Second arg or empty string is returned when value is not set.
+function getUserSetting( name, def ) {
+ var obj = getAllUserSettings();
+
+ if ( obj.hasOwnProperty(name) )
+ return obj[name];
+
+ 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.
+function setUserSetting( name, value, _del ) {
+ 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_]/, '');
+
+ if ( _del ) {
+ delete all[n];
+ } else {
+ all[n] = v;
+ }
+
+ wpCookies.setHash(cookie, all, 31536000, path);
+ wpCookies.set('wp-settings-time-'+userSettings.uid, userSettings.time, 31536000, path);
+
+ return name;
+}
+
+function deleteUserSetting( name ) {
+ return setUserSetting( name, '', 1 );
+}
+
+// Returns all settings as js object.
+function getAllUserSettings() {
+ if ( 'object' !== typeof userSettings )
+ return {};
+
+ return wpCookies.getHash('wp-settings-' + userSettings.uid) || {};
+}