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