PluginProbe
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder / 6.2.2
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder v6.2.2
6.2.14 6.2.13 6.2.12 6.2.10 6.2.11 6.2.9 6.2.8 6.2.7 6.2.6 6.2.5 6.2.4 6.2.3 6.2.2 3.6.22 3.6.31 3.6.40 3.6.41 3.6.42 3.6.50 3.6.51 3.6.60 3.6.61 3.6.62 3.6.64 3.6.65 All 196 releases
fluentform / vendor / wpfluent / framework / src / WPFluent / Http / Cookie.php

Cookie.php in Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder 6.2.2, at vendor/wpfluent/framework/src/WPFluent/Http/Cookie.php

302 lines 7.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentForm\Framework\Http;
4
5 use DateTime;
6 use DateTimeInterface;
7 use InvalidArgumentException;
8 use FluentForm\Framework\Foundation\App;
9
10 class Cookie
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 */
40 public static function set(
41 $name,
42 $value,
43 $minutes = 0,
44 $path = '/',
45 $domain = '',
46 $secure = false,
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 }
67 }
68
69 /**
70 * Set a cookie that lasts 5 years.
71 *
72 * @param string $name
73 * @param mixed $value
74 * @param string $path = '/'
75 * @param string $domain
76 * @param bool $secure
77 * @param bool $httponly
78 * @param string $samesite
79 *
80 * @return void
81 */
82 public static function setForever(
83 $name,
84 $value,
85 $path = '/',
86 $domain = '',
87 $secure = false,
88 $httponly = false,
89 $samesite = 'Lax'
90 ) {
91 static::set(
92 $name,
93 $value,
94 new DateTime('+5 years'),
95 $path,
96 $domain,
97 $secure,
98 $httponly,
99 $samesite
100 );
101 }
102
103 /**
104 * Queue a cookie to be sent with headers.
105 *
106 * @param string $name
107 * @param mixed $value
108 * @param int|DateTimeInterface $minutes
109 * @param string $path
110 * @param string $domain
111 * @param bool $secure
112 * @param bool $httponly
113 * @param string $samesite
114 *
115 * @return void
116 */
117 public static function queue(
118 $name,
119 $value,
120 $minutes = 0,
121 $path = '/',
122 $domain = '',
123 $secure = false,
124 $httponly = false,
125 $samesite = 'Lax'
126 ) {
127 $cookie = static::make(
128 $name,
129 $value,
130 $minutes,
131 $path,
132 $domain,
133 $secure,
134 $httponly,
135 $samesite
136 );
137
138 if (App::request()->isRest()) {
139 $cookie['options']['expires'] = $minutes;
140 static::setCookieInResponse($cookie);
141 } else {
142 static::$queue[] = $cookie;
143 static::maybeAddSendHeadersAction();
144 }
145 }
146
147 /**
148 * Handle WordPress's send_headers action.
149 *
150 * @param mixed $wp
151 *
152 * @return void
153 */
154 public static function sendHeaders($wp)
155 {
156 foreach (static::$queue as $cookie) {
157 setcookie($cookie['name'], $cookie['value'], $cookie['options']);
158 }
159
160 static::$queue = [];
161 static::$actionRegistered = false;
162 }
163
164 /**
165 * Get a cookie value.
166 *
167 * @param string $name
168 * @param mixed $default
169 *
170 * @return mixed
171 */
172 public static function get($name, $default = null)
173 {
174 return array_key_exists($name, $_COOKIE) ? $_COOKIE[$name] : $default;
175 }
176
177 /**
178 * Delete a cookie.
179 *
180 * @param string $name
181 * @param string $path
182 * @param string $domain
183 * @param string $samesite
184 *
185 * @return void
186 */
187 public static function delete(
188 $name,
189 $path = '/',
190 $domain = '',
191 $samesite = 'Lax'
192 ) {
193 $options = [
194 'expires' => time() - 3600,
195 'path' => $path,
196 'domain' => $domain,
197 'secure' => false,
198 'httponly' => false,
199 'samesite' => $samesite,
200 ];
201
202 setcookie($name, '', $options);
203 unset($_COOKIE[$name]);
204 }
205
206 /**
207 * Create a cookie array for setcookie().
208 *
209 * @param string $name
210 * @param mixed $value
211 * @param int|DateTimeInterface $minutes
212 * @param string $path
213 * @param string $domain
214 * @param bool $secure
215 * @param bool $httponly
216 * @param string $samesite
217 *
218 * @return array
219 */
220 public static function make(
221 $name,
222 $value,
223 $minutes = 0,
224 $path = '/',
225 $domain = '',
226 $secure = false,
227 $httponly = false,
228 $samesite = 'Lax'
229 ) {
230 return [
231 'name' => $name,
232 'value' => $value,
233 'options' => [
234 'expires' => static::expiresAt($minutes),
235 'path' => $path,
236 'domain' => $domain,
237 'secure' => $secure,
238 'httponly' => $httponly,
239 'samesite' => $samesite,
240 ],
241 ];
242 }
243
244 /**
245 * Convert minutes to expiration timestamp.
246 *
247 * @param int|DateTimeInterface $minutes
248 *
249 * @return int
250 *
251 * @throws InvalidArgumentException
252 */
253 protected static function expiresAt($minutes = 0)
254 {
255 if ($minutes === 0) {
256 return 0;
257 }
258
259 if (is_int($minutes)) {
260 $minutes = new DateTime("+{$minutes} minutes");
261 }
262
263 if ($minutes instanceof DateTimeInterface) {
264 return $minutes->getTimestamp();
265 }
266
267 throw new InvalidArgumentException('Invalid expiration time provided.');
268 }
269
270 /**
271 * Register the send_headers action if not already registered.
272 *
273 * @return void
274 */
275 protected static function maybeAddSendHeadersAction()
276 {
277 if (!static::$actionRegistered) {
278 static::$actionRegistered = true;
279 App::addAction('send_headers', [static::class, 'sendHeaders']);
280 }
281 }
282
283 /**
284 * Set cookie in Response when in rest context.
285 *
286 * @param array $cookie
287 */
288 protected static function setCookieInResponse($cookie)
289 {
290 App::make('response')->withCookie(
291 $cookie['name'],
292 $cookie['value'],
293 $cookie['options']['expires'],
294 $cookie['options']['path'],
295 $cookie['options']['domain'],
296 $cookie['options']['secure'],
297 $cookie['options']['httponly'],
298 $cookie['options']['samesite']
299 );
300 }
301 }
302