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