/includes/Session.php

  1. <?php 
  2. /** 
  3. * NF Session 
  4. * 
  5. * This is a wrapper class for WP_Session / PHP $_SESSION and handles the storage of cart items, purchase sessions, etc 
  6. * 
  7. * @package Ninja Forms 
  8. * @subpackage Classes/Session 
  9. * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License 
  10. * @since 2.9.18 
  11. */ 
  12. // Exit if accessed directly 
  13. if ( ! defined( 'ABSPATH' ) ) exit; 
  14. /** 
  15. * NF_Session Class 
  16. * 
  17. * @since 1.5 
  18. */ 
  19. class NF_Session { 
  20. /** 
  21. * Holds our session data 
  22. * 
  23. * @var array 
  24. * @access private 
  25. * @since 2.9.18 
  26. */ 
  27. private $session; 
  28. /** 
  29. * Session index prefix 
  30. * 
  31. * @var string 
  32. * @access private 
  33. * @since 2.9.18 
  34. */ 
  35. private $prefix = ''; 
  36. /** 
  37. * Get things started 
  38. * 
  39. * Defines our WP_Session constants, includes the necessary libraries and 
  40. * retrieves the WP Session instance 
  41. * 
  42. * @since 2.9.18 
  43. */ 
  44. public function __construct() { 
  45. // Use WP_Session (default) 
  46. if ( ! defined( 'WP_SESSION_COOKIE' ) ) { 
  47. define( 'WP_SESSION_COOKIE', 'nf_wp_session' ); 
  48. if ( ! class_exists( 'Recursive_ArrayAccess' ) ) { 
  49. require_once Ninja_Forms::$dir . 'includes/Libraries/Session/class-recursive-arrayaccess.php'; 
  50. if ( ! class_exists( 'WP_Session' ) ) { 
  51. require_once Ninja_Forms::$dir . 'includes/Libraries/Session/class-wp-session.php'; 
  52. require_once Ninja_Forms::$dir . 'includes/Libraries/Session/wp-session.php'; 
  53.  
  54. add_filter( 'wp_session_expiration_variant', array( $this, 'set_expiration_variant_time' ), 99999 ); 
  55. add_filter( 'wp_session_expiration', array( $this, 'set_expiration_time' ), 99999 ); 
  56.  
  57. add_action( 'plugins_loaded', array( $this, 'init' ), -1 ); 
  58.  
  59. /** 
  60. * Setup the WP_Session instance 
  61. * 
  62. * @access public 
  63. * @since 2.9.18 
  64. * @return void 
  65. */ 
  66. public function init() { 
  67. $this->session = WP_Session::get_instance(); 
  68. return $this->session; 
  69. /** 
  70. * Retrieve session ID 
  71. * 
  72. * @access public 
  73. * @since 2.9.18 
  74. * @return string Session ID 
  75. */ 
  76. public function get_id() { 
  77. return $this->session->session_id; 
  78. /** 
  79. * Retrieve a session variable 
  80. * 
  81. * @access public 
  82. * @since 2.9.18 
  83. * @param string $key Session key 
  84. * @return string Session variable 
  85. */ 
  86. public function get( $key ) { 
  87. $key = sanitize_key( $key ); 
  88. return isset( $this->session[ $key ] ) ? maybe_unserialize( $this->session[ $key ] ) : false; 
  89. /** 
  90. * Set a session variable 
  91. * 
  92. * @since 2.9.18 
  93. * @param string $key Session key 
  94. * @param integer $value Session variable 
  95. * @return string Session variable 
  96. */ 
  97. public function set( $key, $value ) { 
  98.  
  99. /** 
  100. * Manually Set Cookie 
  101. */ 
  102. $this->session->set_cookie(); 
  103.  
  104. $key = sanitize_key( $key ); 
  105. if ( is_array( $value ) ) { 
  106. $this->session[ $key ] = serialize( $value ); 
  107. } else { 
  108. $this->session[ $key ] = $value; 
  109. return $this->session[ $key ]; 
  110.  
  111. /** 
  112. * Delete a session variable 
  113. * 
  114. * @since 2.9.28 
  115. * @param string $key 
  116. * @return void 
  117. */ 
  118. public function delete() { 
  119. delete_option( '_wp_session_' . $this->session->session_id ); 
  120. delete_option( '_wp_session_expires_' . $this->session->session_id ); 
  121.  
  122. /** 
  123. * Force the cookie expiration variant time to 23 minutes 
  124. * 
  125. * @access public 
  126. * @since 2.9.18 
  127. * @param int $exp Default expiration (1 hour) 
  128. * @return int 
  129. */ 
  130. public function set_expiration_variant_time( $exp ) { 
  131. return 60 * 23; 
  132. /** 
  133. * Force the cookie expiration time to 24 minutes 
  134. * 
  135. * @access public 
  136. * @since 2.9.18 
  137. * @param int $exp Default expiration (1 hour) 
  138. * @return int 
  139. */ 
  140. public function set_expiration_time( $exp ) { 
  141. return 60 * 24; 
.