You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

198 lines
4.4 KiB

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