PluginProbe ʕ •ᴥ•ʔ
Kirki – Freeform Page Builder, Website Builder & Customizer / 6.2.0
Kirki – Freeform Page Builder, Website Builder & Customizer v6.2.0
6.2.0 6.1.1 6.1.0 6.0.14 6.0.13 6.0.12 6.0.11 6.0.10 6.0.9 6.0.8 6.0.7 6.0.6 6.0.5 6.0.4 6.0.3 6.0.2 6.0.1 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 3.1.8 3.1.9 4.0.19 4.0.20 4.0.21 4.0.22 4.0.23 4.0.24 4.1 4.2.0 5.0.0 5.1.0 5.1.1 5.2.0 5.2.1 5.2.2 5.2.3 6.0.0 trunk 3.0.40 3.0.41 3.0.42 3.0.43 3.0.44 3.0.45 3.1.0 3.1.1 3.1.2
kirki / app / Supports / Session.php
kirki / app / Supports Last commit date
Facades 5 days ago Form 5 days ago ActionHooks.php 5 days ago Canvas.php 1 month ago CollectionItem.php 5 days ago ContentManager.php 1 month ago DateTime.php 1 month ago EditorPreview.php 2 weeks ago FileHandler.php 2 weeks ago FilterHooks.php 2 weeks ago PageUrl.php 5 days ago Recaptcha.php 5 days ago Role.php 1 month ago Session.php 5 days ago Template.php 5 days ago
Session.php
129 lines
1 <?php
2 /**
3 * Session support
4 *
5 * Lightweight, cookie-identified session store backed by WordPress transients.
6 * Replaces the deprecated session helpers in {@see \Kirki\HelperFunctions}. The
7 * cookie name, transient key and expirations are kept identical so this stays
8 * interoperable with data written by the legacy helpers (e.g. the front-end
9 * preview renderer).
10 *
11 * @package kirki
12 */
13
14 namespace Kirki\App\Supports;
15
16 use Kirki\Framework\Sanitizer;
17
18 if (!defined('ABSPATH')) {
19 exit; // Exit if accessed directly.
20 }
21
22 class Session
23 {
24 /**
25 * Cookie name that stores the session identifier.
26 *
27 * @var string
28 */
29 public const COOKIE_NAME = 'kirki_session_id';
30
31 /**
32 * Prefix used for the session transient key.
33 *
34 * @var string
35 */
36 public const TRANSIENT_PREFIX = 'kirki_session_';
37
38 /**
39 * In-request cache of the resolved session id.
40 *
41 * @var string|null
42 */
43 protected static $session_id = null;
44
45 /**
46 * Get the current session id, generating and persisting one when missing.
47 *
48 * @return string
49 */
50 public static function get_session_id()
51 {
52 if (static::$session_id) {
53 return static::$session_id;
54 }
55
56 if (isset($_COOKIE[static::COOKIE_NAME])) {
57 static::$session_id = Sanitizer::apply_rule(wp_unslash($_COOKIE[static::COOKIE_NAME]), Sanitizer::TEXT);
58
59 return static::$session_id;
60 }
61
62 static::$session_id = wp_generate_uuid4();
63
64 setcookie(
65 static::COOKIE_NAME,
66 static::$session_id,
67 time() + (DAY_IN_SECONDS * 7),
68 COOKIEPATH,
69 COOKIE_DOMAIN,
70 is_ssl(),
71 true
72 );
73
74 return static::$session_id;
75 }
76
77 /**
78 * Get a value from the session store.
79 *
80 * @param string $key The key to retrieve.
81 * @param mixed $default Value returned when the key is absent.
82 * @return mixed
83 */
84 public static function get(string $key, $default = null)
85 {
86 $session_data = get_transient(static::TRANSIENT_PREFIX . static::get_session_id());
87
88 if (is_array($session_data) && isset($session_data[$key])) {
89 return $session_data[$key];
90 }
91
92 return $default;
93 }
94
95 /**
96 * Add or update a value in the session store.
97 *
98 * @param string $key The key of the session data.
99 * @param mixed $value The value of the session data.
100 * @return void
101 */
102 public static function put(string $key, $value)
103 {
104 $transient_key = static::TRANSIENT_PREFIX . static::get_session_id();
105 $session_data = get_transient($transient_key) ?: [];
106
107 $session_data[$key] = $value;
108
109 set_transient($transient_key, $session_data, DAY_IN_SECONDS);
110 }
111
112 /**
113 * Remove a value from the session store.
114 *
115 * @param string $key The key of the session data to delete.
116 * @return void
117 */
118 public static function forget(string $key)
119 {
120 $transient_key = static::TRANSIENT_PREFIX . static::get_session_id();
121 $session_data = get_transient($transient_key);
122
123 if (is_array($session_data) && isset($session_data[$key])) {
124 unset($session_data[$key]);
125 set_transient($transient_key, $session_data, DAY_IN_SECONDS);
126 }
127 }
128 }
129