PluginProbe
King Addons for Elementor – 80+ Elementor Widgets, 4 000+ Elementor Templates, WooCommerce, Mega Menu, Popup Builder / 51.1.78
King Addons for Elementor – 80+ Elementor Widgets, 4 000+ Elementor Templates, WooCommerce, Mega Menu, Popup Builder v51.1.78
51.1.83 51.1.82 51.1.81 51.1.79 51.1.78 51.1.77 51.1.76 51.1.74 51.1.75 51.1.65 51.1.64 51.1.63 trunk 51.1.14 51.1.2 51.1.35 51.1.36 51.1.37 51.1.38 51.1.39 51.1.44 51.1.45 51.1.46 51.1.47 51.1.49 All 37 releases
king-addons / includes / wishlist / Wishlist_Session.php

Wishlist_Session.php in King Addons for Elementor – 80+ Elementor Widgets, 4 000+ Elementor Templates, WooCommerce, Mega Menu, Popup Builder 51.1.78, at includes/wishlist/Wishlist_Session.php

81 lines 1.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace King_Addons\Wishlist;
4
5 if (!defined('ABSPATH')) {
6 exit;
7 }
8
9 /**
10 * Manages wishlist session keys for guests and logged-in users.
11 */
12 class Wishlist_Session
13 {
14 private const COOKIE_NAME = 'ka_wishlist_session';
15 private const COOKIE_TTL_SECONDS = 2592000; // 30 days
16
17 /**
18 * Get or create a session key tied to a browser.
19 *
20 * @return string Session key.
21 */
22 public function get_session_key(): string
23 {
24 $existing = isset($_COOKIE[self::COOKIE_NAME]) ? sanitize_text_field(wp_unslash($_COOKIE[self::COOKIE_NAME])) : '';
25 if (!empty($existing)) {
26 return $existing;
27 }
28
29 $session_key = wp_generate_uuid4();
30 $this->persist_cookie($session_key);
31
32 return $session_key;
33 }
34
35 /**
36 * Persist the wishlist session cookie.
37 *
38 * @param string $session_key Session key to store.
39 * @return void
40 */
41 public function persist_cookie(string $session_key): void
42 {
43 setcookie(
44 self::COOKIE_NAME,
45 $session_key,
46 [
47 'expires' => time() + self::COOKIE_TTL_SECONDS,
48 'path' => COOKIEPATH ? COOKIEPATH : '/',
49 'secure' => is_ssl(),
50 'httponly' => true,
51 'samesite' => 'Lax',
52 ]
53 );
54 $_COOKIE[self::COOKIE_NAME] = $session_key;
55 }
56
57 /**
58 * Clear the wishlist session cookie.
59 *
60 * @return void
61 */
62 public function clear(): void
63 {
64 setcookie(
65 self::COOKIE_NAME,
66 '',
67 [
68 'expires' => time() - YEAR_IN_SECONDS,
69 'path' => COOKIEPATH ? COOKIEPATH : '/',
70 'secure' => is_ssl(),
71 'httponly' => true,
72 'samesite' => 'Lax',
73 ]
74 );
75 unset($_COOKIE[self::COOKIE_NAME]);
76 }
77 }
78
79
80
81