Naguro_Session_Model

The WooCommerce Naguro Connect Naguro Session Model class.

Defined (1)

The class is defined in the following location(s).

/includes/naguro/models/class-session-model.php  
  1. class Naguro_Session_Model extends Naguro_Model { 
  2. /** @var int */ 
  3. private $id; 
  4.  
  5. /** @var bool */ 
  6. private $saved; 
  7.  
  8. /** @var array */ 
  9. private $data = array(); 
  10.  
  11. public function __construct( $id = null ) { 
  12. $this->saved = true; 
  13.  
  14. if ( null === $id ) { 
  15. $this->id = $this->generate_session_id(); 
  16. } else { 
  17. $this->id = $id; 
  18. $this->data = $this->get_session(); 
  19.  
  20. add_action( 'shutdown', array( $this, 'save_data' ), 20 ); 
  21.  
  22. public function get_id() { 
  23. return $this->id; 
  24.  
  25. private function generate_session_id() { 
  26. $max = get_option( 'naguro_max_session_id', 0 ); 
  27. $max = $max + 1; 
  28. update_option( 'naguro_max_session_id', $max ); 
  29. return $max; 
  30.  
  31. private function get_session() { 
  32. return (array) get_option( 'naguro_session_' . $this->id, array() ); 
  33.  
  34. public function get( $key ) { 
  35. if ( isset( $this->data[ $key ] ) ) { 
  36. return $this->data[ $key ]; 
  37.  
  38. return null; 
  39.  
  40. public function set( $key, $value ) { 
  41. $this->data[ $key ] = $value; 
  42. $this->saved = false; 
  43. return true; 
  44.  
  45. public function save_data() { 
  46. if ( ! $this->saved ) { 
  47. update_option( 'naguro_session_' . $this->id, $this->data ); 
  48. $this->saved = true;