PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 28.3
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v28.3
28.6 28.5 28.4 28.3 28.2 28.1 28.0 27.9 27.8 27.7 27.6 27.5 trunk 18.0 18.1 18.2 18.3 18.4 18.4.1 18.5 18.5.1 18.6 18.7 18.8 18.9 All 130 releases
wordpress-seo / vendor_prefixed / guzzlehttp / guzzle / src / Cookie / SessionCookieJar.php

SessionCookieJar.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 28.3, at vendor_prefixed/guzzlehttp/guzzle/src/Cookie/SessionCookieJar.php

89 lines 2.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace YoastSEO_Vendor\GuzzleHttp\Cookie;
4
5 /**
6 * Persists cookies in the client session
7 */
8 class SessionCookieJar extends \YoastSEO_Vendor\GuzzleHttp\Cookie\CookieJar
9 {
10 /**
11 * @var string session key
12 */
13 private $sessionKey;
14 /**
15 * @var bool Control whether to persist session cookies or not.
16 */
17 private $storeSessionCookies;
18 /**
19 * Create a new SessionCookieJar object
20 *
21 * @param string $sessionKey Session key name to store the cookie
22 * data in session
23 * @param bool $storeSessionCookies Set to true to store session cookies
24 * in the cookie jar.
25 */
26 public function __construct(string $sessionKey, bool $storeSessionCookies = \false)
27 {
28 parent::__construct();
29 $this->sessionKey = $sessionKey;
30 $this->storeSessionCookies = $storeSessionCookies;
31 $this->load();
32 }
33 /**
34 * Saves cookies to session when shutting down
35 */
36 public function __destruct()
37 {
38 $this->save();
39 }
40 /**
41 * Save cookies to the client session
42 */
43 public function save() : void
44 {
45 $json = [];
46 /** @var SetCookie $cookie */
47 foreach ($this as $cookie) {
48 if (\YoastSEO_Vendor\GuzzleHttp\Cookie\CookieJar::shouldPersist($cookie, $this->storeSessionCookies)) {
49 $data = $cookie->toArray();
50 $data['HostOnly'] = $cookie->getHostOnly();
51 $json[] = $data;
52 }
53 }
54 $json = \json_encode($json);
55 if (\false === $json) {
56 throw new \RuntimeException('Unable to encode cookie data');
57 }
58 $_SESSION[$this->sessionKey] = $json;
59 }
60 /**
61 * Load the contents of the client session into the data array
62 */
63 protected function load() : void
64 {
65 if (!isset($_SESSION[$this->sessionKey])) {
66 return;
67 }
68 $json = $_SESSION[$this->sessionKey];
69 if (!\is_string($json)) {
70 throw new \RuntimeException('Invalid cookie data');
71 }
72 $data = \json_decode($json, \true);
73 if (\is_array($data)) {
74 $cookies = [];
75 foreach ($data as $cookie) {
76 if (!\is_array($cookie) || !\array_key_exists('HostOnly', $cookie) || !\is_bool($cookie['HostOnly'])) {
77 throw new \RuntimeException('Invalid cookie data');
78 }
79 $cookies[] = new \YoastSEO_Vendor\GuzzleHttp\Cookie\SetCookie($cookie);
80 }
81 foreach ($cookies as $cookie) {
82 $this->setCookie($cookie);
83 }
84 } elseif (\is_scalar($data) && \strlen((string) $data)) {
85 throw new \RuntimeException('Invalid cookie data');
86 }
87 }
88 }
89