Completed
Pull Request — master (#415)
by Ibrahim
20:15
created

Give_Session::set_expiration_variant_time()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
cc 2
eloc 2
nc 2
nop 0
crap 6
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 24 and the first side effect is on line 16.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/**
3
 * Give Session
4
 *
5
 * This is a wrapper class for WP_Session / PHP $_SESSION and handles the storage of Give sessions
6
 *
7
 * @package     Give
8
 * @subpackage  Classes/Session
9
 * @copyright   Copyright (c) 2015, WordImpress
10
 * @license     http://opensource.org/licenses/gpl-2.0.php GNU Public License
11
 * @since       1.0
12
 */
13
14
// Exit if accessed directly
15
if ( ! defined( 'ABSPATH' ) ) {
16
	exit;
17
}
18
19
/**
20
 * Give_Session Class
21
 *
22
 * @since 1.0
23
 */
24
class Give_Session {
25
26
	/**
27
	 * Holds our session data
28
	 *
29
	 * @var array
30
	 * @access private
31
	 * @since  1.0
32
	 */
33
	private $session;
34
35
36
	/**
37
	 * Whether to use PHP $_SESSION or WP_Session
38
	 *
39
	 * @var bool
40
	 * @access private
41
	 * @since  1.0
42
	 */
43
	private $use_php_sessions = false;
44
45
	/**
46
	 * Expiration Time
47
	 *
48
	 * @var int
49
	 * @access private
50
	 * @since  1.0
51
	 */
52
	private $exp_option = false;
53
54
	/**
55
	 * Session index prefix
56
	 *
57
	 * @var string
58
	 * @access private
59
	 * @since  1.0
60
	 */
61
	private $prefix = '';
62
63
	/**
64
	 * Get things started
65
	 *
66
	 * Defines our WP_Session constants, includes the necessary libraries and
67
	 * retrieves the WP Session instance
68
	 *
69
	 * @since 1.0
70
	 */
71 2
	public function __construct() {
72
73 2
		$this->use_php_sessions = $this->use_php_sessions();
74 2
		$this->exp_option       = give_get_option( 'session_lifetime' );
75
76 2
		if ( $this->use_php_sessions ) {
77
78
			if ( is_multisite() ) {
79
80
				$this->prefix = '_' . get_current_blog_id();
81
82
			}
83
84
			// Use PHP SESSION (must be enabled via the GIVE_USE_PHP_SESSIONS constant)
85
			add_action( 'init', array( $this, 'maybe_start_session' ), - 2 );
86
87
		} else {
88
89
			// Use WP_Session (default)
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
90 2
			if ( ! defined( 'WP_SESSION_COOKIE' ) ) {
91 1
				define( 'WP_SESSION_COOKIE', 'give_wp_session' );
92 1
			}
93
94 2
			if ( ! class_exists( 'Recursive_ArrayAccess' ) ) {
95 1
				require_once GIVE_PLUGIN_DIR . 'includes/libraries/class-recursive-arrayaccess.php';
96 1
			}
97
98 2
			if ( ! class_exists( 'WP_Session' ) ) {
99 1
				require_once GIVE_PLUGIN_DIR . 'includes/libraries/class-wp-session.php';
100 1
				require_once GIVE_PLUGIN_DIR . 'includes/libraries/wp-session.php';
101 1
			}
102
103 2
			add_filter( 'wp_session_expiration_variant', array( $this, 'set_expiration_variant_time' ), 99999 );
104 2
			add_filter( 'wp_session_expiration', array( $this, 'set_expiration_time' ), 99999 );
105
106
		}
107
108 2
		if ( empty( $this->session ) && ! $this->use_php_sessions ) {
109 2
			add_action( 'plugins_loaded', array( $this, 'init' ), - 1 );
110 2
		} else {
111
			add_action( 'init', array( $this, 'init' ), - 1 );
112
		}
113
114 2
	}
115
116
	/**
117
	 * Setup the WP_Session instance
118
	 *
119
	 * @access public
120
	 * @since  1.0
121
	 * @return array $this->session
122
	 */
123
	public function init() {
124
125
		if ( $this->use_php_sessions ) {
126
			$this->session = isset( $_SESSION[ 'give' . $this->prefix ] ) && is_array( $_SESSION[ 'give' . $this->prefix ] ) ? $_SESSION[ 'give' . $this->prefix ] : array();
127
		} else {
128
			$this->session = WP_Session::get_instance();
129
		}
130
131
132
		return $this->session;
133
	}
134
135
136
	/**
137
	 * Retrieve session ID
138
	 *
139
	 * @access public
140
	 * @since  1.0
141
	 * @return string Session ID
142
	 */
143
	public function get_id() {
144
		return $this->session->session_id;
145
	}
146
147
148
	/**
149
	 * Retrieve a session variable
150
	 *
151
	 * @access public
152
	 * @since  1.0
153
	 *
154
	 * @param string $key Session key
155
	 *
156
	 * @return string Session variable
157
	 */
158 9
	public function get( $key ) {
159 9
		$key = sanitize_key( $key );
160
161 9
		return isset( $this->session[ $key ] ) ? maybe_unserialize( $this->session[ $key ] ) : false;
162
163
	}
164
165
	/**
166
	 * Set a session variable
167
	 *
168
	 * @since 1.0
169
	 *
170
	 * @param $key   $_SESSION key
0 ignored issues
show
Documentation introduced by
The doc-type $key could not be parsed: Unknown type name "$key" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
Bug introduced by
There is no parameter named $_SESSION. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
171
	 * @param $value $_SESSION variable
0 ignored issues
show
Documentation introduced by
The doc-type $value could not be parsed: Unknown type name "$value" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
Bug introduced by
There is no parameter named $_SESSION. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
172
	 *
173
	 * @return mixed Session variable
174
	 */
175 8
	public function set( $key, $value ) {
176
177 8
		$key = sanitize_key( $key );
178
179 8
		if ( is_array( $value ) ) {
180 6
			$this->session[ $key ] = serialize( $value );
181 6
		} else {
182 8
			$this->session[ $key ] = $value;
183
		}
184
185 8
		if ( $this->use_php_sessions ) {
186 8
			$_SESSION[ 'give' . $this->prefix ] = $this->session;
187 8
		}
188
189 8
		return $this->session[ $key ];
190
	}
191
192
	/**
193
	 * Set Cookie Variant Time
194
	 *
195
	 * @description Force the cookie expiration variant time to custom expiration option, less and hour; defaults to 23 hours (set_expiration_variant_time used in WP_Session)
196
	 *
197
	 * @access      public
198
	 * @since       1.0
199
	 *
200
	 * @return int
201
	 */
202
	public function set_expiration_variant_time() {
203
204
		return ( ! empty( $this->exp_option ) ? ( intval( $this->exp_option ) - 3600 ) : 30 * 60 * 23 );
205
	}
206
207
	/**
208
	 * Set the Cookie Expiration
209
	 *
210
	 * @description Force the cookie expiration time if set, default to 24 hours
211
	 *
212
	 * @access      public
213
	 * @since       1.0
214
	 *
215
	 * @return int
216
	 */
217
	public function set_expiration_time() {
218
219
		return ( ! empty( $this->exp_option ) ? intval( $this->exp_option ) : 30 * 60 * 24 );
220
	}
221
222
	/**
223
	 * Starts a new session if one hasn't started yet.
224
	 *
225
	 * @return null
226
	 * Checks to see if the server supports PHP sessions
227
	 * or if the GIVE_USE_PHP_SESSIONS constant is defined
228
	 *
229
	 * @access public
230
	 * @since  1.0
231
	 * @return bool $ret True if we are using PHP sessions, false otherwise
232
	 */
233 4
	public function use_php_sessions() {
234
235 4
		$ret = false;
236
237
		// If the database variable is already set, no need to run autodetection
238 4
		$give_use_php_sessions = (bool) get_option( 'give_use_php_sessions' );
239
240 4
		if ( ! $give_use_php_sessions ) {
241
242
			// Attempt to detect if the server supports PHP sessions
243
			if ( function_exists( 'session_start' ) && ! ini_get( 'safe_mode' ) ) {
244
245
				$this->set( 'give_use_php_sessions', 1 );
246
247
				if ( $this->get( 'give_use_php_sessions' ) ) {
248
249
					$ret = true;
250
251
					// Set the database option
252
					update_option( 'give_use_php_sessions', true );
253
254
				}
255
256
			}
257
258
		} else {
259 4
			$ret = $give_use_php_sessions;
260
		}
261
262
		// Enable or disable PHP Sessions based on the GIVE_USE_PHP_SESSIONS constant
263 4
		if ( defined( 'GIVE_USE_PHP_SESSIONS' ) && GIVE_USE_PHP_SESSIONS ) {
264
			$ret = true;
265 4
		} else if ( defined( 'GIVE_USE_PHP_SESSIONS' ) && ! GIVE_USE_PHP_SESSIONS ) {
266 4
			$ret = false;
267 4
		}
268
269 4
		return (bool) apply_filters( 'give_use_php_sessions', $ret );
270
	}
271
272
	/**
273
	 * Maybe Start Session
274
	 *
275
	 * @description Starts a new session if one hasn't started yet.
276
	 * @see         http://php.net/manual/en/function.session-set-cookie-params.php
277
	 */
278
	public function maybe_start_session() {
279
280
//		session_destroy(); //Uncomment for testing ONLY
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
281
282
		if ( ! session_id() && ! headers_sent() ) {
283
			$lifetime = current_time( 'timestamp' ) + $this->set_expiration_time();
284
			session_start();
285
			setcookie( session_name(), session_id(), $lifetime ); //
286
			setcookie( session_name() . '_expiration', $lifetime, $lifetime );
287
		}
288
	}
289
290
291
	/**
292
	 * Get Session Expiration
293
	 *
294
	 * @description  Looks at the session cookies and returns the expiration date for this session if applicable
295
	 *
296
	 */
297
	public function get_session_expiration() {
298
299
		$expiration = false;
300
301
		if ( session_id() && isset( $_COOKIE[ session_name() . '_expiration' ] ) ) {
302
303
			$expiration = date( 'D, d M Y h:i:s', intval( $_COOKIE[ session_name() . '_expiration' ] ) );
304
305
		}
306
307
		return $expiration;
308
309
	}
310
311
}
312
313