| 1 |
<?php |
| 2 |
|
| 3 |
namespace Cookiez\Classes\Utils; |
| 4 |
|
| 5 |
if ( ! defined( 'ABSPATH' ) ) { |
| 6 |
exit; // Exit if accessed directly |
| 7 |
} |
| 8 |
|
| 9 |
/** |
| 10 |
* Class Cookie |
| 11 |
*/ |
| 12 |
class Cookie { |
| 13 |
/** |
| 14 |
* get |
| 15 |
* @param string $cookie_name |
| 16 |
* |
| 17 |
* @return string|false |
| 18 |
*/ |
| 19 |
public function get( string $cookie_name ) { |
| 20 |
if ( ! isset( $_COOKIE[ $cookie_name ] ) ) { |
| 21 |
return false; |
| 22 |
} |
| 23 |
return sanitize_text_field( wp_unslash( $_COOKIE[ $cookie_name ] ) ); |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* set |
| 28 |
* |
| 29 |
* @param string $cookie_name |
| 30 |
* @param string $cookie_value |
| 31 |
* @param int|null $expires |
| 32 |
* @param string $path |
| 33 |
* @param bool $httponly |
| 34 |
* |
| 35 |
* @codeCoverageIgnore |
| 36 |
*/ |
| 37 |
public function set( string $cookie_name, string $cookie_value = '', ?int $expires = null, string $path = '/', bool $httponly = true ): void { |
| 38 |
if ( null === $expires ) { |
| 39 |
$expires = time() + 360; |
| 40 |
} else { |
| 41 |
$expires = time() + $expires; |
| 42 |
} |
| 43 |
setcookie( |
| 44 |
$cookie_name, |
| 45 |
$cookie_value, |
| 46 |
$expires, |
| 47 |
$path, |
| 48 |
COOKIE_DOMAIN, |
| 49 |
is_ssl(), |
| 50 |
$httponly |
| 51 |
); |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* delete |
| 56 |
* @param string $cookie_name |
| 57 |
* @codeCoverageIgnore |
| 58 |
*/ |
| 59 |
public function delete( string $cookie_name ): void { |
| 60 |
setcookie( |
| 61 |
$cookie_name, |
| 62 |
'', |
| 63 |
time() - 3600 |
| 64 |
); |
| 65 |
} |
| 66 |
} |
| 67 |
|