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