| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) { |
| 3 |
exit; // Exit if accessed directly! |
| 4 |
} |
| 5 |
|
| 6 |
if ( ! class_exists( 'WPSC_ACB_Cookies' ) ) : |
| 7 |
|
| 8 |
final class WPSC_ACB_Cookies { |
| 9 |
|
| 10 |
/** |
| 11 |
* Initialize the cookies. |
| 12 |
*/ |
| 13 |
public static function init() {} |
| 14 |
|
| 15 |
/** |
| 16 |
* Resolve current request session identifier. |
| 17 |
* |
| 18 |
* @return string |
| 19 |
*/ |
| 20 |
public static function get_request_session_id() { |
| 21 |
|
| 22 |
$cookie_name = 'wpsc_acb_session_id'; |
| 23 |
$cookie_val = isset( $_COOKIE[ $cookie_name ] ) ? sanitize_text_field( wp_unslash( $_COOKIE[ $cookie_name ] ) ) : ''; |
| 24 |
|
| 25 |
if ( ! empty( $cookie_val ) ) { |
| 26 |
return $cookie_val; |
| 27 |
} |
| 28 |
|
| 29 |
$session_id = wp_generate_uuid4(); |
| 30 |
self::set_session_cookie( $cookie_name, $session_id ); |
| 31 |
return $session_id; |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Set guest session cookie. |
| 36 |
* |
| 37 |
* @param string $cookie_name Cookie name. |
| 38 |
* @param string $cookie_val Cookie value. |
| 39 |
* @return bool |
| 40 |
*/ |
| 41 |
public static function set_session_cookie( $cookie_name, $cookie_val ) { |
| 42 |
|
| 43 |
if ( headers_sent() ) { |
| 44 |
return false; |
| 45 |
} |
| 46 |
|
| 47 |
$expires = time() + HOUR_IN_SECONDS; |
| 48 |
$cookie_val = (string) $cookie_val; |
| 49 |
|
| 50 |
$paths = self::get_cookie_paths(); |
| 51 |
foreach ( $paths as $path ) { |
| 52 |
|
| 53 |
setcookie( |
| 54 |
$cookie_name, |
| 55 |
$cookie_val, |
| 56 |
self::get_cookie_options( $expires, $path ) |
| 57 |
); |
| 58 |
} |
| 59 |
$_COOKIE[ $cookie_name ] = $cookie_val; |
| 60 |
return true; |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Delete guest session cookie. |
| 65 |
* |
| 66 |
* @param string $cookie_name Cookie name. |
| 67 |
* @return bool |
| 68 |
*/ |
| 69 |
public static function delete_session_cookie( $cookie_name ) { |
| 70 |
|
| 71 |
if ( headers_sent() ) { |
| 72 |
unset( $_COOKIE[ $cookie_name ] ); |
| 73 |
return false; |
| 74 |
} |
| 75 |
|
| 76 |
$paths = self::get_cookie_paths(); |
| 77 |
foreach ( $paths as $path ) { |
| 78 |
|
| 79 |
$options = self::get_cookie_options( time() - DAY_IN_SECONDS, $path ); |
| 80 |
setcookie( $cookie_name, '', $options ); |
| 81 |
} |
| 82 |
unset( $_COOKIE[ $cookie_name ] ); |
| 83 |
return true; |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Logged-in users use their user ID; guests get a stable UUID cookie. |
| 88 |
* |
| 89 |
* @return string |
| 90 |
*/ |
| 91 |
public static function get_request_visitor_id() { |
| 92 |
|
| 93 |
$current_user = WPSC_Current_User::$current_user; |
| 94 |
if ( ! empty( $current_user->user->ID ) ) { |
| 95 |
return (string) absint( $current_user->user->ID ); |
| 96 |
} |
| 97 |
|
| 98 |
$cookie_name = 'wpsc_acb_visitor_id'; |
| 99 |
$cookie_val = isset( $_COOKIE[ $cookie_name ] ) ? sanitize_text_field( wp_unslash( $_COOKIE[ $cookie_name ] ) ) : ''; |
| 100 |
|
| 101 |
if ( ! empty( $cookie_val ) ) { |
| 102 |
return $cookie_val; |
| 103 |
} |
| 104 |
|
| 105 |
$visitor_id = wp_generate_uuid4(); |
| 106 |
self::set_visitor_cookie( $cookie_name, $visitor_id ); |
| 107 |
return $visitor_id; |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Set guest visitor cookie. |
| 112 |
* |
| 113 |
* @param string $cookie_name Cookie name. |
| 114 |
* @param string $cookie_val Cookie value. |
| 115 |
* @return bool |
| 116 |
*/ |
| 117 |
public static function set_visitor_cookie( $cookie_name, $cookie_val ) { |
| 118 |
|
| 119 |
if ( headers_sent() ) { |
| 120 |
return false; |
| 121 |
} |
| 122 |
|
| 123 |
$expires = time() + DAY_IN_SECONDS; |
| 124 |
$cookie_val = (string) $cookie_val; |
| 125 |
$paths = self::get_cookie_paths(); |
| 126 |
|
| 127 |
foreach ( $paths as $path ) { |
| 128 |
|
| 129 |
setcookie( |
| 130 |
$cookie_name, |
| 131 |
$cookie_val, |
| 132 |
self::get_cookie_options( $expires, $path ) |
| 133 |
); |
| 134 |
} |
| 135 |
|
| 136 |
$_COOKIE[ $cookie_name ] = $cookie_val; |
| 137 |
return true; |
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* Get cookie options for setting cookies. |
| 142 |
* |
| 143 |
* @param int $expires Expiration timestamp. |
| 144 |
* @param string $path Cookie path. |
| 145 |
* @return array |
| 146 |
*/ |
| 147 |
private static function get_cookie_options( $expires, $path ) { |
| 148 |
|
| 149 |
return array( |
| 150 |
'expires' => $expires, |
| 151 |
'path' => $path, |
| 152 |
'domain' => COOKIE_DOMAIN ? COOKIE_DOMAIN : '', |
| 153 |
'secure' => is_ssl(), |
| 154 |
'httponly' => true, |
| 155 |
'samesite' => 'Lax', |
| 156 |
); |
| 157 |
} |
| 158 |
|
| 159 |
/** |
| 160 |
* Get cookie paths used by WordPress for front-end cookies. |
| 161 |
* |
| 162 |
* @return array |
| 163 |
*/ |
| 164 |
private static function get_cookie_paths() { |
| 165 |
|
| 166 |
return array_unique( |
| 167 |
array_filter( |
| 168 |
array( |
| 169 |
COOKIEPATH ? COOKIEPATH : '/', |
| 170 |
SITECOOKIEPATH ? SITECOOKIEPATH : '/', |
| 171 |
) |
| 172 |
) |
| 173 |
); |
| 174 |
} |
| 175 |
} |
| 176 |
endif; |
| 177 |
WPSC_ACB_Cookies::init(); |
| 178 |
|