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