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 +345 -28 1.5.0 → 2.5.0 View file →
@@ -1,64 +1,381 @@
1 1 <?php
2 2
3 3 namespace FluentBooking\Framework\Http;
4 4
5 +use DateTime;
6 +use DateTimeInterface;
7 +use InvalidArgumentException;
8 +use FluentBooking\Framework\Foundation\App;
9 +
5 10 class Cookie
6 -{
11 +{
12 + /**
13 + * Cookie queue
14 + *
15 + * @var array
16 + */
17 + protected static $queue = [];
18 +
19 + /**
20 + * Action registered indicator.
21 + *
22 + * @var bool
23 + */
24 + protected static $actionRegistered = false;
25 +
26 + /**
27 + * Set a cookie immediately.
28 + *
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 + *
38 + * @return void
39 + */
7 40 public static function set(
8 41 $name,
9 42 $value,
10 43 $minutes = 0,
11 - $path = '',
44 + $path = '/',
12 45 $domain = '',
13 46 $secure = false,
14 - $httponly = false
15 - )
16 - {
17 - $time = ($minutes == 0) ? 0 : static::expiresAt($minutes * 60);
18 -
19 - setcookie($name, $value, $time, $path, $domain, $secure, $httponly);
47 + $httponly = false,
48 + $samesite = 'Lax'
49 + ) {
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 + }
20 67 }
21 68
69 + /**
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 + *
86 + * @return void
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 + */
22 162 public static function setForever(
23 163 $name,
24 164 $value,
25 - $path = '',
165 + $path = '/',
26 166 $domain = '',
27 167 $secure = false,
28 - $httponly = false
29 - )
168 + $httponly = false,
169 + $samesite = 'Lax'
170 + ) {
171 + static::set(
172 + $name,
173 + $value,
174 + new DateTime('+5 years'),
175 + $path,
176 + $domain,
177 + $secure,
178 + $httponly,
179 + $samesite
180 + );
181 + }
182 +
183 + /**
184 + * Queue a cookie to be sent with headers.
185 + *
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 + *
195 + * @return void
196 + */
197 + public static function queue(
198 + $name,
199 + $value,
200 + $minutes = 0,
201 + $path = '/',
202 + $domain = '',
203 + $secure = false,
204 + $httponly = false,
205 + $samesite = 'Lax'
206 + ) {
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();
224 + }
225 + }
226 +
227 + /**
228 + * Handle WordPress's send_headers action.
229 + *
230 + * @param mixed $wp
231 + *
232 + * @return void
233 + */
234 + public static function sendHeaders($wp)
30 235 {
31 - $fiveYears = static::expiresAt((365 * 24 * 60 * 60) * 5);
236 + foreach (static::$queue as $cookie) {
237 + setcookie($cookie['name'], $cookie['value'], $cookie['options']);
238 + }
32 239
33 - setcookie($name, $value, $fiveYears, $path, $domain, $secure, $httponly);
240 + static::$queue = [];
241 + static::$actionRegistered = false;
34 242 }
35 243
244 + /**
245 + * Get a cookie value.
246 + *
247 + * @param string $name
248 + * @param mixed $default
249 + *
250 + * @return mixed
251 + */
36 252 public static function get($name, $default = null)
37 253 {
38 - if (array_key_exists($name, $_COOKIE)) {
39 - return $_COOKIE[$name];
40 - }
254 + return array_key_exists($name, $_COOKIE) ? $_COOKIE[$name] : $default;
255 + }
41 256
42 - return $default;
257 + /**
258 + * Delete a cookie.
259 + *
260 + * @param string $name
261 + * @param string $path
262 + * @param string $domain
263 + * @param string $samesite
264 + *
265 + * @return void
266 + */
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 + ];
281 +
282 + setcookie($name, '', $options);
283 + unset($_COOKIE[$name]);
43 284 }
44 285
45 - public static function delete($name, $path = '', $domain = '') {
46 - setcookie($name, '', time() - 3600, $path, $domain);
286 + /**
287 + * Create a cookie array for setcookie().
288 + *
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 + }
47 323
48 - if (array_key_exists($name, $_COOKIE)) {
49 - unset($_COOKIE[$name]);
324 + /**
325 + * Convert minutes to expiration timestamp.
326 + *
327 + * @param int|DateTimeInterface $minutes
328 + *
329 + * @return int
330 + *
331 + * @throws InvalidArgumentException
332 + */
333 + protected static function expiresAt($minutes = 0)
334 + {
335 + if ($minutes === 0) {
336 + return 0;
50 337 }
338 +
339 + if (is_int($minutes)) {
340 + $minutes = new DateTime("+{$minutes} minutes");
341 + }
342 +
343 + if ($minutes instanceof DateTimeInterface) {
344 + return $minutes->getTimestamp();
345 + }
346 +
347 + throw new InvalidArgumentException('Invalid expiration time provided.');
51 348 }
52 349
53 - protected static function expiresAt($value = 0)
350 + /**
351 + * Register the send_headers action if not already registered.
352 + *
353 + * @return void
354 + */
355 + protected static function maybeAddSendHeadersAction()
54 356 {
55 - if (!$value instanceof \DateTimeInterface) {
56 -
57 - $value = is_numeric($value) ? (int) $value : 0;
58 -
59 - $value = new \DateTime('+' . $value . ' seconds');
357 + if (!static::$actionRegistered) {
358 + static::$actionRegistered = true;
359 + App::addAction('send_headers', [static::class, 'sendHeaders']);
60 360 }
361 + }
61 362
62 - return $value->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 + );
63 380 }
64 381 }