abstract-wc-session.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. /**
  3. * Handle data for the current customers session
  4. *
  5. * @class WC_Session
  6. * @version 2.0.0
  7. * @package WooCommerce/Abstracts
  8. */
  9. if ( ! defined( 'ABSPATH' ) ) {
  10. exit;
  11. }
  12. /**
  13. * WC_Session
  14. */
  15. abstract class WC_Session {
  16. /**
  17. * Customer ID.
  18. *
  19. * @var int $_customer_id Customer ID.
  20. */
  21. protected $_customer_id;
  22. /**
  23. * Session Data.
  24. *
  25. * @var array $_data Data array.
  26. */
  27. protected $_data = array();
  28. /**
  29. * Dirty when the session needs saving.
  30. *
  31. * @var bool $_dirty When something changes
  32. */
  33. protected $_dirty = false;
  34. /**
  35. * Init hooks and session data. Extended by child classes.
  36. *
  37. * @since 3.3.0
  38. */
  39. public function init() {}
  40. /**
  41. * Cleanup session data. Extended by child classes.
  42. */
  43. public function cleanup_sessions() {}
  44. /**
  45. * Magic get method.
  46. *
  47. * @param mixed $key Key to get.
  48. * @return mixed
  49. */
  50. public function __get( $key ) {
  51. return $this->get( $key );
  52. }
  53. /**
  54. * Magic set method.
  55. *
  56. * @param mixed $key Key to set.
  57. * @param mixed $value Value to set.
  58. */
  59. public function __set( $key, $value ) {
  60. $this->set( $key, $value );
  61. }
  62. /**
  63. * Magic isset method.
  64. *
  65. * @param mixed $key Key to check.
  66. * @return bool
  67. */
  68. public function __isset( $key ) {
  69. return isset( $this->_data[ sanitize_title( $key ) ] );
  70. }
  71. /**
  72. * Magic unset method.
  73. *
  74. * @param mixed $key Key to unset.
  75. */
  76. public function __unset( $key ) {
  77. if ( isset( $this->_data[ $key ] ) ) {
  78. unset( $this->_data[ $key ] );
  79. $this->_dirty = true;
  80. }
  81. }
  82. /**
  83. * Get a session variable.
  84. *
  85. * @param string $key Key to get.
  86. * @param mixed $default used if the session variable isn't set.
  87. * @return array|string value of session variable
  88. */
  89. public function get( $key, $default = null ) {
  90. $key = sanitize_key( $key );
  91. return isset( $this->_data[ $key ] ) ? maybe_unserialize( $this->_data[ $key ] ) : $default;
  92. }
  93. /**
  94. * Set a session variable.
  95. *
  96. * @param string $key Key to set.
  97. * @param mixed $value Value to set.
  98. */
  99. public function set( $key, $value ) {
  100. if ( $value !== $this->get( $key ) ) {
  101. $this->_data[ sanitize_key( $key ) ] = maybe_serialize( $value );
  102. $this->_dirty = true;
  103. }
  104. }
  105. /**
  106. * Get customer ID.
  107. *
  108. * @return int
  109. */
  110. public function get_customer_id() {
  111. return $this->_customer_id;
  112. }
  113. }