| 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 |
* Read the requester's session cookie as-is, without the "mint a new one if |
| 36 |
* missing" side effect get_request_session_id() has. Callers that only want to |
| 37 |
* know what the requester's own cookie currently is - e.g. verifying a |
| 38 |
* client-submitted session id actually belongs to them - must use this instead: |
| 39 |
* generating (and Set-Cookie-ing) a fresh random id purely to fail a comparison |
| 40 |
* against it has no purpose, and clobbers whatever cookie value the requester |
| 41 |
* legitimately had if this call happens to run before anything else sets one. |
| 42 |
* |
| 43 |
* @return string Empty string if no session cookie is present. |
| 44 |
*/ |
| 45 |
public static function get_current_session_id() { |
| 46 |
|
| 47 |
$cookie_name = 'wpsc_acb_session_id'; |
| 48 |
return isset( $_COOKIE[ $cookie_name ] ) ? sanitize_text_field( wp_unslash( $_COOKIE[ $cookie_name ] ) ) : ''; |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Set guest session cookie. |
| 53 |
* |
| 54 |
* @param string $cookie_name Cookie name. |
| 55 |
* @param string $cookie_val Cookie value. |
| 56 |
* @return bool |
| 57 |
*/ |
| 58 |
public static function set_session_cookie( $cookie_name, $cookie_val ) { |
| 59 |
|
| 60 |
if ( headers_sent() ) { |
| 61 |
return false; |
| 62 |
} |
| 63 |
|
| 64 |
$expires = time() + HOUR_IN_SECONDS; |
| 65 |
$cookie_val = (string) $cookie_val; |
| 66 |
|
| 67 |
$paths = self::get_cookie_paths(); |
| 68 |
foreach ( $paths as $path ) { |
| 69 |
|
| 70 |
setcookie( |
| 71 |
$cookie_name, |
| 72 |
$cookie_val, |
| 73 |
self::get_cookie_options( $expires, $path ) |
| 74 |
); |
| 75 |
} |
| 76 |
$_COOKIE[ $cookie_name ] = $cookie_val; |
| 77 |
return true; |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Delete guest session cookie. |
| 82 |
* |
| 83 |
* @param string $cookie_name Cookie name. |
| 84 |
* @return bool |
| 85 |
*/ |
| 86 |
public static function delete_session_cookie( $cookie_name ) { |
| 87 |
|
| 88 |
if ( headers_sent() ) { |
| 89 |
unset( $_COOKIE[ $cookie_name ] ); |
| 90 |
return false; |
| 91 |
} |
| 92 |
|
| 93 |
$paths = self::get_cookie_paths(); |
| 94 |
foreach ( $paths as $path ) { |
| 95 |
|
| 96 |
$options = self::get_cookie_options( time() - DAY_IN_SECONDS, $path ); |
| 97 |
setcookie( $cookie_name, '', $options ); |
| 98 |
} |
| 99 |
unset( $_COOKIE[ $cookie_name ] ); |
| 100 |
return true; |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Logged-in users use their user ID; guests get a stable UUID cookie. |
| 105 |
* |
| 106 |
* @return string |
| 107 |
*/ |
| 108 |
public static function get_request_visitor_id() { |
| 109 |
|
| 110 |
$current_user = WPSC_Current_User::$current_user; |
| 111 |
if ( ! empty( $current_user->user->ID ) ) { |
| 112 |
return (string) absint( $current_user->user->ID ); |
| 113 |
} |
| 114 |
|
| 115 |
$cookie_name = 'wpsc_acb_visitor_id'; |
| 116 |
$cookie_val = isset( $_COOKIE[ $cookie_name ] ) ? sanitize_text_field( wp_unslash( $_COOKIE[ $cookie_name ] ) ) : ''; |
| 117 |
|
| 118 |
if ( ! empty( $cookie_val ) ) { |
| 119 |
return $cookie_val; |
| 120 |
} |
| 121 |
|
| 122 |
$visitor_id = wp_generate_uuid4(); |
| 123 |
self::set_visitor_cookie( $cookie_name, $visitor_id ); |
| 124 |
return $visitor_id; |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* Set guest visitor cookie. |
| 129 |
* |
| 130 |
* @param string $cookie_name Cookie name. |
| 131 |
* @param string $cookie_val Cookie value. |
| 132 |
* @return bool |
| 133 |
*/ |
| 134 |
public static function set_visitor_cookie( $cookie_name, $cookie_val ) { |
| 135 |
|
| 136 |
if ( headers_sent() ) { |
| 137 |
return false; |
| 138 |
} |
| 139 |
|
| 140 |
$expires = time() + DAY_IN_SECONDS; |
| 141 |
$cookie_val = (string) $cookie_val; |
| 142 |
$paths = self::get_cookie_paths(); |
| 143 |
|
| 144 |
foreach ( $paths as $path ) { |
| 145 |
|
| 146 |
setcookie( |
| 147 |
$cookie_name, |
| 148 |
$cookie_val, |
| 149 |
self::get_cookie_options( $expires, $path ) |
| 150 |
); |
| 151 |
} |
| 152 |
|
| 153 |
$_COOKIE[ $cookie_name ] = $cookie_val; |
| 154 |
return true; |
| 155 |
} |
| 156 |
|
| 157 |
/** |
| 158 |
* Get cookie options for setting cookies. |
| 159 |
* |
| 160 |
* @param int $expires Expiration timestamp. |
| 161 |
* @param string $path Cookie path. |
| 162 |
* @return array |
| 163 |
*/ |
| 164 |
private static function get_cookie_options( $expires, $path ) { |
| 165 |
|
| 166 |
return array( |
| 167 |
'expires' => $expires, |
| 168 |
'path' => $path, |
| 169 |
'domain' => COOKIE_DOMAIN ? COOKIE_DOMAIN : '', |
| 170 |
'secure' => is_ssl(), |
| 171 |
'httponly' => true, |
| 172 |
'samesite' => 'Lax', |
| 173 |
); |
| 174 |
} |
| 175 |
|
| 176 |
/** |
| 177 |
* Get cookie paths used by WordPress for front-end cookies. |
| 178 |
* |
| 179 |
* @return array |
| 180 |
*/ |
| 181 |
private static function get_cookie_paths() { |
| 182 |
|
| 183 |
return array_unique( |
| 184 |
array_filter( |
| 185 |
array( |
| 186 |
COOKIEPATH ? COOKIEPATH : '/', |
| 187 |
SITECOOKIEPATH ? SITECOOKIEPATH : '/', |
| 188 |
) |
| 189 |
) |
| 190 |
); |
| 191 |
} |
| 192 |
} |
| 193 |
endif; |
| 194 |
WPSC_ACB_Cookies::init(); |
| 195 |
|