PluginProbe
Fluent Booking – The Ultimate Appointments Scheduling, Events Booking, Events Calendar Solution / 2.5.0
Fluent Booking – The Ultimate Appointments Scheduling, Events Booking, Events Calendar Solution v2.5.0
2.5.0 2.4.0 2.3.0 2.2.5 2.2.0 2.1.2 2.1.1 trunk 1.10.0 1.10.01 1.10.02 1.5.0 1.5.01 1.5.02 1.5.1 1.5.10 1.5.20 1.5.21 1.5.22 1.5.23 1.5.24 1.5.25 1.6.0 1.7.0 1.7.1 All 34 releases
← All changes | vendor/wpfluent/framework/src/WPFluent/Http/Cookie.php +281 -93 1.7.1 → 2.5.0 View file →
@@ -10,29 +10,32 @@
10 10 class Cookie
11 11 {
12 12 /**
13 13 * Cookie queue
14 + *
14 15 * @var array
15 16 */
16 17 protected static $queue = [];
17 18
18 19 /**
19 - * Action registered iindicator.
20 + * Action registered indicator.
20 21 *
21 - * @var boolean
22 + * @var bool
22 23 */
23 24 protected static $actionRegistered = false;
24 25
25 26 /**
26 - * Sets the cookie.
27 + * Set a cookie immediately.
27 28 *
28 - * @param string $name
29 - * @param mixed $value
30 - * @param integer $minutes
31 - * @param string $path
32 - * @param string $domain
33 - * @param boolean $secure
34 - * @param boolean $httponly
29 + * @param string $name
30 + * @param mixed $value
31 + * @param int|DateTimeInterface $minutes
32 + * @param string $path
33 + * @param string $domain
34 + * @param bool $secure
35 + * @param bool $httponly
36 + * @param string $samesite
37 + *
35 38 * @return void
36 39 */
37 40 public static function set(
38 41 $name,
@@ -37,52 +40,159 @@
37 40 public static function set(
38 41 $name,
39 42 $value,
40 43 $minutes = 0,
41 - $path = '',
44 + $path = '/',
42 45 $domain = '',
43 46 $secure = false,
44 - $httponly = false
47 + $httponly = false,
48 + $samesite = 'Lax'
45 49 ) {
46 - $time = static::expiresAt($minutes);
47 -
48 - setcookie($name, $value, $time, $path, $domain, $secure, $httponly);
50 + $cookie = static::make(
51 + $name,
52 + $value,
53 + $minutes,
54 + $path = '/',
55 + $domain,
56 + $secure,
57 + $httponly,
58 + $samesite
59 + );
60 +
61 + if (App::request()->isRest()) {
62 + $cookie['options']['expires'] = $minutes;
63 + static::setCookieInResponse($cookie);
64 + } else {
65 + setcookie($cookie['name'], $cookie['value'], $cookie['options']);
66 + }
49 67 }
50 68
51 69 /**
52 - * Sets the cookie forever (5 years).
53 - *
54 - * @param string $name
55 - * @param mixed $value
56 - * @param string $path
57 - * @param string $domain
58 - * @param boolean $secure
59 - * @param boolean $httponly
70 + * Set a cookie whose value is base64(json($value)).
71 + *
72 + * Pairs with Request::cookie(), which auto-detects this exact format
73 + * and decodes back to the original value on read. Use this when you
74 + * need to store arrays/objects in a cookie — strings, numbers, and
75 + * scalars work just fine through plain set() and don't need encoding.
76 + *
77 + * @param string $name
78 + * @param mixed $value Anything json_encode can serialize
79 + * @param int|DateTimeInterface $minutes
80 + * @param string $path
81 + * @param string $domain
82 + * @param bool $secure
83 + * @param bool $httponly
84 + * @param string $samesite
85 + *
60 86 * @return void
61 87 */
88 + public static function setEncoded(
89 + $name,
90 + $value,
91 + $minutes = 0,
92 + $path = '/',
93 + $domain = '',
94 + $secure = false,
95 + $httponly = false,
96 + $samesite = 'Lax'
97 + ) {
98 + static::set(
99 + $name,
100 + base64_encode(json_encode($value)),
101 + $minutes,
102 + $path,
103 + $domain,
104 + $secure,
105 + $httponly,
106 + $samesite
107 + );
108 + }
109 +
110 + /**
111 + * Queue a cookie whose value is base64(json($value)) for send_headers.
112 + *
113 + * Same encoding contract as setEncoded() — pairs with Request::cookie()
114 + * auto-decode on the read side.
115 + *
116 + * @param string $name
117 + * @param mixed $value
118 + * @param int|DateTimeInterface $minutes
119 + * @param string $path
120 + * @param string $domain
121 + * @param bool $secure
122 + * @param bool $httponly
123 + * @param string $samesite
124 + *
125 + * @return void
126 + */
127 + public static function queueEncoded(
128 + $name,
129 + $value,
130 + $minutes = 0,
131 + $path = '/',
132 + $domain = '',
133 + $secure = false,
134 + $httponly = false,
135 + $samesite = 'Lax'
136 + ) {
137 + static::queue(
138 + $name,
139 + base64_encode(json_encode($value)),
140 + $minutes,
141 + $path,
142 + $domain,
143 + $secure,
144 + $httponly,
145 + $samesite
146 + );
147 + }
148 +
149 + /**
150 + * Set a cookie that lasts 5 years.
151 + *
152 + * @param string $name
153 + * @param mixed $value
154 + * @param string $path = '/'
155 + * @param string $domain
156 + * @param bool $secure
157 + * @param bool $httponly
158 + * @param string $samesite
159 + *
160 + * @return void
161 + */
62 162 public static function setForever(
63 163 $name,
64 164 $value,
65 - $path = '',
165 + $path = '/',
66 166 $domain = '',
67 167 $secure = false,
68 - $httponly = false
168 + $httponly = false,
169 + $samesite = 'Lax'
69 170 ) {
70 - $fiveYears = time() + (5 * 365 * 24 * 60 * 60);
71 -
72 - setcookie($name, $value, $fiveYears, $path, $domain, $secure, $httponly);
171 + static::set(
172 + $name,
173 + $value,
174 + new DateTime('+5 years'),
175 + $path,
176 + $domain,
177 + $secure,
178 + $httponly,
179 + $samesite
180 + );
73 181 }
74 182
75 183 /**
76 - * Add a cookie into the queue.
184 + * Queue a cookie to be sent with headers.
77 185 *
78 - * @param string $name
79 - * @param mixed $value
80 - * @param integer $minutes
81 - * @param string $path
82 - * @param string $domain
83 - * @param boolean $secure
84 - * @param boolean $httponly
186 + * @param string $name
187 + * @param mixed $value
188 + * @param int|DateTimeInterface $minutes
189 + * @param string $path
190 + * @param string $domain
191 + * @param bool $secure
192 + * @param bool $httponly
193 + * @param string $samesite
194 + *
85 195 * @return void
86 196 */
87 197 public static function queue(
88 198 $name,
@@ -87,107 +197,185 @@
87 197 public static function queue(
88 198 $name,
89 199 $value,
90 200 $minutes = 0,
91 - $path = '',
201 + $path = '/',
92 202 $domain = '',
93 203 $secure = false,
94 - $httponly = false
204 + $httponly = false,
205 + $samesite = 'Lax'
95 206 ) {
96 - if (!static::$actionRegistered) {
97 - static::$actionRegistered = true;
98 - App::addAction(
99 - 'send_headers', [static::class, 'sendHeaders']
100 - );
207 + $cookie = static::make(
208 + $name,
209 + $value,
210 + $minutes,
211 + $path,
212 + $domain,
213 + $secure,
214 + $httponly,
215 + $samesite
216 + );
217 +
218 + if (App::request()->isRest()) {
219 + $cookie['options']['expires'] = $minutes;
220 + static::setCookieInResponse($cookie);
221 + } else {
222 + static::$queue[] = $cookie;
223 + static::maybeAddSendHeadersAction();
101 224 }
102 -
103 - $time = static::expiresAt($minutes);
104 -
105 - static::$queue[] = [
106 - 'name' => $name,
107 - 'value' => $value,
108 - 'expires' => $time,
109 - 'path' => $path,
110 - 'domain' => $domain,
111 - 'port' => $secure,
112 - 'host_only' => $httponly
113 - ];
114 225 }
115 226
116 227 /**
117 - * WordPress's send_headers action handler.
228 + * Handle WordPress's send_headers action.
118 229 *
119 - * @param WordPress Object $wp
230 + * @param mixed $wp
231 + *
120 232 * @return void
121 233 */
122 234 public static function sendHeaders($wp)
123 235 {
124 236 foreach (static::$queue as $cookie) {
125 - static::set(
126 - $cookie['name'],
127 - $cookie['value'],
128 - $cookie['expires'],
129 - $cookie['path'],
130 - $cookie['domain'],
131 - $cookie['port'],
132 - $cookie['host_only'],
133 - );
237 + setcookie($cookie['name'], $cookie['value'], $cookie['options']);
134 238 }
239 +
240 + static::$queue = [];
241 + static::$actionRegistered = false;
135 242 }
136 243
137 244 /**
138 - * Retrieve a cookie.
245 + * Get a cookie value.
139 246 *
140 - * @param string $name
141 - * @param mixed $default
247 + * @param string $name
248 + * @param mixed $default
249 + *
142 250 * @return mixed
143 251 */
144 252 public static function get($name, $default = null)
145 253 {
146 - if (array_key_exists($name, $_COOKIE)) {
147 - return $_COOKIE[$name];
148 - }
149 -
150 - return $default;
254 + return array_key_exists($name, $_COOKIE) ? $_COOKIE[$name] : $default;
151 255 }
152 256
153 257 /**
154 258 * Delete a cookie.
155 259 *
156 - * @param string $name
157 - * @param string $path
158 - * @param string $domain
260 + * @param string $name
261 + * @param string $path
262 + * @param string $domain
263 + * @param string $samesite
264 + *
159 265 * @return void
160 266 */
161 - public static function delete($name, $path = '', $domain = '') {
162 - setcookie($name, '', time() - 3600, $path, $domain);
267 + public static function delete(
268 + $name,
269 + $path = '/',
270 + $domain = '',
271 + $samesite = 'Lax'
272 + ) {
273 + $options = [
274 + 'expires' => time() - 3600,
275 + 'path' => $path,
276 + 'domain' => $domain,
277 + 'secure' => false,
278 + 'httponly' => false,
279 + 'samesite' => $samesite,
280 + ];
163 281
164 - if (array_key_exists($name, $_COOKIE)) {
165 - unset($_COOKIE[$name]);
166 - }
282 + setcookie($name, '', $options);
283 + unset($_COOKIE[$name]);
167 284 }
168 285
169 286 /**
170 - * Prepares expiration time in minutes.
287 + * Create a cookie array for setcookie().
171 288 *
172 - * @param int|DateTimeInterface $minutes
289 + * @param string $name
290 + * @param mixed $value
291 + * @param int|DateTimeInterface $minutes
292 + * @param string $path
293 + * @param string $domain
294 + * @param bool $secure
295 + * @param bool $httponly
296 + * @param string $samesite
297 + *
298 + * @return array
299 + */
300 + public static function make(
301 + $name,
302 + $value,
303 + $minutes = 0,
304 + $path = '/',
305 + $domain = '',
306 + $secure = false,
307 + $httponly = false,
308 + $samesite = 'Lax'
309 + ) {
310 + return [
311 + 'name' => $name,
312 + 'value' => $value,
313 + 'options' => [
314 + 'expires' => static::expiresAt($minutes),
315 + 'path' => $path,
316 + 'domain' => $domain,
317 + 'secure' => $secure,
318 + 'httponly' => $httponly,
319 + 'samesite' => $samesite,
320 + ],
321 + ];
322 + }
323 +
324 + /**
325 + * Convert minutes to expiration timestamp.
326 + *
327 + * @param int|DateTimeInterface $minutes
328 + *
173 329 * @return int
330 + *
331 + * @throws InvalidArgumentException
174 332 */
175 333 protected static function expiresAt($minutes = 0)
176 334 {
335 + if ($minutes === 0) {
336 + return 0;
337 + }
338 +
177 339 if (is_int($minutes)) {
178 - if ($minutes === 0) {
179 - return $minutes;
180 - }
340 + $minutes = new DateTime("+{$minutes} minutes");
341 + }
181 342
182 - $minutes = new DateTime('+' . $minutes . ' minutes');
343 + if ($minutes instanceof DateTimeInterface) {
344 + return $minutes->getTimestamp();
183 345 }
184 346
185 - if (!($minutes instanceof DateTimeInterface)) {
186 - throw new InvalidArgumentException(
187 - 'Invalid expiration time provided.'
188 - );
347 + throw new InvalidArgumentException('Invalid expiration time provided.');
348 + }
349 +
350 + /**
351 + * Register the send_headers action if not already registered.
352 + *
353 + * @return void
354 + */
355 + protected static function maybeAddSendHeadersAction()
356 + {
357 + if (!static::$actionRegistered) {
358 + static::$actionRegistered = true;
359 + App::addAction('send_headers', [static::class, 'sendHeaders']);
189 360 }
361 + }
190 362
191 - return $minutes->getTimestamp();
363 + /**
364 + * Set cookie in Response when in rest context.
365 + *
366 + * @param array $cookie
367 + */
368 + protected static function setCookieInResponse($cookie)
369 + {
370 + App::make('response')->withCookie(
371 + $cookie['name'],
372 + $cookie['value'],
373 + $cookie['options']['expires'],
374 + $cookie['options']['path'],
375 + $cookie['options']['domain'],
376 + $cookie['options']['secure'],
377 + $cookie['options']['httponly'],
378 + $cookie['options']['samesite']
379 + );
192 380 }
193 381 }