utils.js 4.6 KB

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