[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-includes/js/ -> utils.js (source)

   1  /**
   2   * Cookie functions.
   3   *
   4   * @output wp-includes/js/utils.js
   5   */
   6  
   7  /* global userSettings, getAllUserSettings, wpCookies, setUserSetting */
   8  /* exported getUserSetting, setUserSetting, deleteUserSetting */
   9  
  10  window.wpCookies = {
  11  // The following functions are from Cookie.js class in TinyMCE 3, Moxiecode, used under LGPL.
  12  
  13      each: function( obj, cb, scope ) {
  14          var n, l;
  15  
  16          if ( ! obj ) {
  17              return 0;
  18          }
  19  
  20          scope = scope || obj;
  21  
  22          if ( typeof( obj.length ) !== 'undefined' ) {
  23              for ( n = 0, l = obj.length; n < l; n++ ) {
  24                  if ( cb.call( scope, obj[n], n, obj ) === false ) {
  25                      return 0;
  26                  }
  27              }
  28          } else {
  29              for ( n in obj ) {
  30                  if ( obj.hasOwnProperty(n) ) {
  31                      if ( cb.call( scope, obj[n], n, obj ) === false ) {
  32                          return 0;
  33                      }
  34                  }
  35              }
  36          }
  37          return 1;
  38      },
  39  
  40      /**
  41       * Get a multi-values cookie.
  42       * Returns a JS object with the name: 'value' pairs.
  43       *
  44       * @param {string} name The name of the cookie.
  45       * @return {Object} The cookie values as a JS object.
  46       */
  47      getHash: function( name ) {
  48          var cookie = this.get( name ), values;
  49  
  50          if ( cookie ) {
  51              this.each( cookie.split('&'), function( pair ) {
  52                  pair = pair.split('=');
  53                  values = values || {};
  54                  values[pair[0]] = pair[1];
  55              });
  56          }
  57  
  58          return values;
  59      },
  60  
  61      /**
  62       * Set a multi-values cookie.
  63       *
  64       * 'values_obj' is the JS object that is stored. It is encoded as URI in wpCookies.set().
  65       *
  66       * @param {string}      name         The name of the cookie.
  67       * @param {Object}      values_obj   The values to store in the cookie.
  68       * @param {number|Date} [expires]    Optional. Expiration time in seconds or a Date object.
  69       * @param {string}      [path]       Optional. The path on the server in which the cookie will be available on.
  70       * @param {string}      [domain]     Optional. The domain that the cookie is available to.
  71       * @param {boolean}     [secure]     Optional. Whether the cookie should only be transmitted over a secure HTTPS connection.
  72       */
  73      setHash: function( name, values_obj, expires, path, domain, secure ) {
  74          var str = '';
  75  
  76          this.each( values_obj, function( val, key ) {
  77              str += ( ! str ? '' : '&' ) + key + '=' + val;
  78          });
  79  
  80          this.set( name, str, expires, path, domain, secure );
  81      },
  82  
  83      /**
  84       * Get a cookie.
  85       * @param {string} name The name of the cookie.
  86       * @return {void|string} The cookie value.
  87       */
  88      get: function( name ) {
  89          var e, b,
  90              cookie = document.cookie,
  91              p = name + '=';
  92  
  93          if ( ! cookie ) {
  94              return;
  95          }
  96  
  97          b = cookie.indexOf( '; ' + p );
  98  
  99          if ( b === -1 ) {
 100              b = cookie.indexOf(p);
 101  
 102              if ( b !== 0 ) {
 103                  return null;
 104              }
 105          } else {
 106              b += 2;
 107          }
 108  
 109          e = cookie.indexOf( ';', b );
 110  
 111          if ( e === -1 ) {
 112              e = cookie.length;
 113          }
 114  
 115          return decodeURIComponent( cookie.substring( b + p.length, e ) );
 116      },
 117  
 118      /**
 119       * Set a cookie.
 120       *
 121       * The 'expires' arg can be either a JS Date() object set to the expiration date (back-compat)
 122       * or the number of seconds until expiration.
 123       *
 124       * @param {string}      name      The name of the cookie.
 125       * @param {string}      value     The value of the cookie.
 126       * @param {number|Date} [expires] Optional. Expiration time in seconds or a Date object.
 127       * @param {string}      [path]    Optional. The path on the server in which the cookie will be available on.
 128       * @param {string}      [domain]  Optional. The domain that the cookie is available to.
 129       * @param {boolean}     [secure]  Optional. Whether the cookie should only be transmitted over a secure HTTPS connection.
 130       */
 131      set: function( name, value, expires, path, domain, secure ) {
 132          var d = new Date();
 133  
 134          if ( typeof( expires ) === 'object' && expires.toGMTString ) {
 135              expires = expires.toGMTString();
 136          } else if ( parseInt( expires, 10 ) ) {
 137              d.setTime( d.getTime() + ( parseInt( expires, 10 ) * 1000 ) ); // Time must be in milliseconds.
 138              expires = d.toGMTString();
 139          } else {
 140              expires = '';
 141          }
 142  
 143          document.cookie = name + '=' + encodeURIComponent( value ) +
 144              ( expires ? '; expires=' + expires : '' ) +
 145              ( path    ? '; path=' + path       : '' ) +
 146              ( domain  ? '; domain=' + domain   : '' ) +
 147              ( secure  ? '; secure'             : '' );
 148      },
 149  
 150      /**
 151       * Remove a cookie.
 152       *
 153       * This is done by setting it to an empty value and setting the expiration time in the past.
 154       *
 155       * @param {string}  name     The name of the cookie.
 156       * @param {string}  [path]   Optional. The path on the server in which the cookie will be available on.
 157       * @param {string}  [domain] Optional. The domain that the cookie is available to.
 158       * @param {boolean} [secure] Optional. Whether the cookie should only be transmitted over a secure HTTPS connection.
 159       */
 160      remove: function( name, path, domain, secure ) {
 161          this.set( name, '', -1000, path, domain, secure );
 162      }
 163  };
 164  
 165  // Returns the value as string. Second arg or empty string is returned when value is not set.
 166  window.getUserSetting = function( name, def ) {
 167      var settings = getAllUserSettings();
 168  
 169      if ( settings.hasOwnProperty( name ) ) {
 170          return settings[name];
 171      }
 172  
 173      if ( typeof def !== 'undefined' ) {
 174          return def;
 175      }
 176  
 177      return '';
 178  };
 179  
 180  /*
 181   * Both name and value must be only ASCII letters, numbers or underscore
 182   * and the shorter, the better (cookies can store maximum 4KB). Not suitable to store text.
 183   * The value is converted and stored as string.
 184   */
 185  window.setUserSetting = function( name, value, _del ) {
 186      if ( 'object' !== typeof userSettings ) {
 187          return false;
 188      }
 189  
 190      var uid = userSettings.uid,
 191          settings = wpCookies.getHash( 'wp-settings-' + uid ),
 192          path = userSettings.url,
 193          secure = !! userSettings.secure;
 194  
 195      name = name.toString().replace( /[^A-Za-z0-9_-]/g, '' );
 196  
 197      if ( typeof value === 'number' ) {
 198          value = parseInt( value, 10 );
 199      } else {
 200          value = value.toString().replace( /[^A-Za-z0-9_-]/g, '' );
 201      }
 202  
 203      settings = settings || {};
 204  
 205      if ( _del ) {
 206          delete settings[name];
 207      } else {
 208          settings[name] = value;
 209      }
 210  
 211      wpCookies.setHash( 'wp-settings-' + uid, settings, 31536000, path, '', secure );
 212      wpCookies.set( 'wp-settings-time-' + uid, userSettings.time, 31536000, path, '', secure );
 213  
 214      return name;
 215  };
 216  
 217  window.deleteUserSetting = function( name ) {
 218      return setUserSetting( name, '', 1 );
 219  };
 220  
 221  // Returns all settings as JS object.
 222  window.getAllUserSettings = function() {
 223      if ( 'object' !== typeof userSettings ) {
 224          return {};
 225      }
 226  
 227      return wpCookies.getHash( 'wp-settings-' + userSettings.uid ) || {};
 228  };


Generated : Mon Sep 7 08:20:28 2026 Cross-referenced by PHPXref