30 |
34 |
31 /** |
35 /** |
32 * Get a multi-values cookie. |
36 * Get a multi-values cookie. |
33 * Returns a JS object with the name: 'value' pairs. |
37 * Returns a JS object with the name: 'value' pairs. |
34 */ |
38 */ |
35 getHash : function(name) { |
39 getHash: function( name ) { |
36 var all = this.get(name), ret; |
40 var cookie = this.get( name ), values; |
37 |
41 |
38 if ( all ) { |
42 if ( cookie ) { |
39 this.each( all.split('&'), function(pair) { |
43 this.each( cookie.split('&'), function( pair ) { |
40 pair = pair.split('='); |
44 pair = pair.split('='); |
41 ret = ret || {}; |
45 values = values || {}; |
42 ret[pair[0]] = pair[1]; |
46 values[pair[0]] = pair[1]; |
43 }); |
47 }); |
44 } |
48 } |
45 return ret; |
49 |
|
50 return values; |
46 }, |
51 }, |
47 |
52 |
48 /** |
53 /** |
49 * Set a multi-values cookie. |
54 * Set a multi-values cookie. |
50 * |
55 * |
51 * 'values_obj' is the JS object that is stored. It is encoded as URI in wpCookies.set(). |
56 * 'values_obj' is the JS object that is stored. It is encoded as URI in wpCookies.set(). |
52 */ |
57 */ |
53 setHash : function(name, values_obj, expires, path, domain, secure) { |
58 setHash: function( name, values_obj, expires, path, domain, secure ) { |
54 var str = ''; |
59 var str = ''; |
55 |
60 |
56 this.each(values_obj, function(val, key) { |
61 this.each( values_obj, function( val, key ) { |
57 str += (!str ? '' : '&') + key + '=' + val; |
62 str += ( ! str ? '' : '&' ) + key + '=' + val; |
58 }); |
63 }); |
59 |
64 |
60 this.set(name, str, expires, path, domain, secure); |
65 this.set( name, str, expires, path, domain, secure ); |
61 }, |
66 }, |
62 |
67 |
63 /** |
68 /** |
64 * Get a cookie. |
69 * Get a cookie. |
65 */ |
70 */ |
66 get : function(name) { |
71 get: function( name ) { |
67 var cookie = document.cookie, e, p = name + "=", b; |
72 var e, b, |
|
73 cookie = document.cookie, |
|
74 p = name + '='; |
68 |
75 |
69 if ( !cookie ) |
76 if ( ! cookie ) { |
70 return; |
77 return; |
|
78 } |
71 |
79 |
72 b = cookie.indexOf("; " + p); |
80 b = cookie.indexOf( '; ' + p ); |
73 |
81 |
74 if ( b == -1 ) { |
82 if ( b === -1 ) { |
75 b = cookie.indexOf(p); |
83 b = cookie.indexOf(p); |
76 |
84 |
77 if ( b != 0 ) |
85 if ( b !== 0 ) { |
78 return null; |
86 return null; |
79 |
87 } |
80 } else { |
88 } else { |
81 b += 2; |
89 b += 2; |
82 } |
90 } |
83 |
91 |
84 e = cookie.indexOf(";", b); |
92 e = cookie.indexOf( ';', b ); |
85 |
93 |
86 if ( e == -1 ) |
94 if ( e === -1 ) { |
87 e = cookie.length; |
95 e = cookie.length; |
|
96 } |
88 |
97 |
89 return decodeURIComponent( cookie.substring(b + p.length, e) ); |
98 return decodeURIComponent( cookie.substring( b + p.length, e ) ); |
90 }, |
99 }, |
91 |
100 |
92 /** |
101 /** |
93 * Set a cookie. |
102 * Set a cookie. |
94 * |
103 * |
95 * The 'expires' arg can be either a JS Date() object set to the expiration date (back-compat) |
104 * The 'expires' arg can be either a JS Date() object set to the expiration date (back-compat) |
96 * or the number of seconds until expiration |
105 * or the number of seconds until expiration |
97 */ |
106 */ |
98 set : function(name, value, expires, path, domain, secure) { |
107 set: function( name, value, expires, path, domain, secure ) { |
99 var d = new Date(); |
108 var d = new Date(); |
100 |
109 |
101 if ( typeof(expires) == 'object' && expires.toGMTString ) { |
110 if ( typeof( expires ) === 'object' && expires.toGMTString ) { |
102 expires = expires.toGMTString(); |
111 expires = expires.toGMTString(); |
103 } else if ( parseInt(expires, 10) ) { |
112 } else if ( parseInt( expires, 10 ) ) { |
104 d.setTime( d.getTime() + ( parseInt(expires, 10) * 1000 ) ); // time must be in miliseconds |
113 d.setTime( d.getTime() + ( parseInt( expires, 10 ) * 1000 ) ); // time must be in miliseconds |
105 expires = d.toGMTString(); |
114 expires = d.toGMTString(); |
106 } else { |
115 } else { |
107 expires = ''; |
116 expires = ''; |
108 } |
117 } |
109 |
118 |
110 document.cookie = name + "=" + encodeURIComponent(value) + |
119 document.cookie = name + '=' + encodeURIComponent( value ) + |
111 ((expires) ? "; expires=" + expires : "") + |
120 ( expires ? '; expires=' + expires : '' ) + |
112 ((path) ? "; path=" + path : "") + |
121 ( path ? '; path=' + path : '' ) + |
113 ((domain) ? "; domain=" + domain : "") + |
122 ( domain ? '; domain=' + domain : '' ) + |
114 ((secure) ? "; secure" : ""); |
123 ( secure ? '; secure' : '' ); |
115 }, |
124 }, |
116 |
125 |
117 /** |
126 /** |
118 * Remove a cookie. |
127 * Remove a cookie. |
119 * |
128 * |
120 * This is done by setting it to an empty value and setting the expiration time in the past. |
129 * This is done by setting it to an empty value and setting the expiration time in the past. |
121 */ |
130 */ |
122 remove : function(name, path) { |
131 remove: function( name, path, domain, secure ) { |
123 this.set(name, '', -1000, path); |
132 this.set( name, '', -1000, path, domain, secure ); |
124 } |
133 } |
125 }; |
134 }; |
126 |
135 |
127 // Returns the value as string. Second arg or empty string is returned when value is not set. |
136 // Returns the value as string. Second arg or empty string is returned when value is not set. |
128 function getUserSetting( name, def ) { |
137 function getUserSetting( name, def ) { |
129 var obj = getAllUserSettings(); |
138 var settings = getAllUserSettings(); |
130 |
139 |
131 if ( obj.hasOwnProperty(name) ) |
140 if ( settings.hasOwnProperty( name ) ) { |
132 return obj[name]; |
141 return settings[name]; |
|
142 } |
133 |
143 |
134 if ( typeof def != 'undefined' ) |
144 if ( typeof def !== 'undefined' ) { |
135 return def; |
145 return def; |
|
146 } |
136 |
147 |
137 return ''; |
148 return ''; |
138 } |
149 } |
139 |
150 |
140 // Both name and value must be only ASCII letters, numbers or underscore |
151 // Both name and value must be only ASCII letters, numbers or underscore |
141 // and the shorter, the better (cookies can store maximum 4KB). Not suitable to store text. |
152 // and the shorter, the better (cookies can store maximum 4KB). Not suitable to store text. |
|
153 // The value is converted and stored as string. |
142 function setUserSetting( name, value, _del ) { |
154 function setUserSetting( name, value, _del ) { |
143 if ( 'object' !== typeof userSettings ) |
155 if ( 'object' !== typeof userSettings ) { |
144 return false; |
156 return false; |
|
157 } |
145 |
158 |
146 var cookie = 'wp-settings-' + userSettings.uid, all = wpCookies.getHash(cookie) || {}, path = userSettings.url, |
159 var uid = userSettings.uid, |
147 n = name.toString().replace(/[^A-Za-z0-9_]/, ''), v = value.toString().replace(/[^A-Za-z0-9_]/, ''); |
160 settings = wpCookies.getHash( 'wp-settings-' + uid ), |
|
161 path = userSettings.url, |
|
162 secure = !! userSettings.secure; |
|
163 |
|
164 name = name.toString().replace( /[^A-Za-z0-9_]/, '' ); |
|
165 |
|
166 if ( typeof value === 'number' ) { |
|
167 value = parseInt( value, 10 ); |
|
168 } else { |
|
169 value = value.toString().replace( /[^A-Za-z0-9_]/, '' ); |
|
170 } |
|
171 |
|
172 settings = settings || {}; |
148 |
173 |
149 if ( _del ) { |
174 if ( _del ) { |
150 delete all[n]; |
175 delete settings[name]; |
151 } else { |
176 } else { |
152 all[n] = v; |
177 settings[name] = value; |
153 } |
178 } |
154 |
179 |
155 wpCookies.setHash(cookie, all, 31536000, path); |
180 wpCookies.setHash( 'wp-settings-' + uid, settings, 31536000, path, '', secure ); |
156 wpCookies.set('wp-settings-time-'+userSettings.uid, userSettings.time, 31536000, path); |
181 wpCookies.set( 'wp-settings-time-' + uid, userSettings.time, 31536000, path, '', secure ); |
157 |
182 |
158 return name; |
183 return name; |
159 } |
184 } |
160 |
185 |
161 function deleteUserSetting( name ) { |
186 function deleteUserSetting( name ) { |
162 return setUserSetting( name, '', 1 ); |
187 return setUserSetting( name, '', 1 ); |
163 } |
188 } |
164 |
189 |
165 // Returns all settings as js object. |
190 // Returns all settings as js object. |
166 function getAllUserSettings() { |
191 function getAllUserSettings() { |
167 if ( 'object' !== typeof userSettings ) |
192 if ( 'object' !== typeof userSettings ) { |
168 return {}; |
193 return {}; |
|
194 } |
169 |
195 |
170 return wpCookies.getHash('wp-settings-' + userSettings.uid) || {}; |
196 return wpCookies.getHash( 'wp-settings-' + userSettings.uid ) || {}; |
171 } |
197 } |