Нет описания

Cookies.php 1.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. namespace MailPoet\Util;
  3. if (!defined('ABSPATH')) exit;
  4. use InvalidArgumentException;
  5. class Cookies {
  6. const DEFAULT_OPTIONS = [
  7. 'expires' => 0,
  8. 'path' => '',
  9. 'domain' => '',
  10. 'secure' => false,
  11. 'httponly' => false,
  12. ];
  13. public function set($name, $value, array $options = []) {
  14. $options = $options + self::DEFAULT_OPTIONS;
  15. $value = json_encode($value, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
  16. $error = json_last_error();
  17. if ($error || ($value === false)) {
  18. throw new InvalidArgumentException();
  19. }
  20. // on PHP_VERSION_ID >= 70300 we'll be able to simply setcookie($name, $value, $options);
  21. setcookie(
  22. $name,
  23. $value,
  24. $options['expires'],
  25. $options['path'],
  26. $options['domain'],
  27. $options['secure'],
  28. $options['httponly']
  29. );
  30. }
  31. public function get($name) {
  32. if (!array_key_exists($name, $_COOKIE)) {
  33. return null;
  34. }
  35. $value = json_decode(stripslashes($_COOKIE[$name]), true);
  36. $error = json_last_error();
  37. if ($error) {
  38. return null;
  39. }
  40. return $value;
  41. }
  42. public function delete($name) {
  43. unset($_COOKIE[$name]);
  44. }
  45. }