Passed
Push — master ( 8e64ed...bf19df )
by Stiofan
04:11
created
includes/class-wpinv-session-handler.php 1 patch
Spacing   +55 added lines, -55 removed lines patch added patch discarded remove patch
@@ -5,7 +5,7 @@  discard block
 block discarded – undo
5 5
  *
6 6
  */
7 7
 
8
-defined( 'ABSPATH' ) || exit;
8
+defined('ABSPATH') || exit;
9 9
 
10 10
 /**
11 11
  * Session handler class.
@@ -52,11 +52,11 @@  discard block
 block discarded – undo
52 52
 	 */
53 53
 	public function __construct() {
54 54
 
55
-	    $this->_cookie = apply_filters( 'wpinv_cookie', 'wpinv_session_' . COOKIEHASH );
56
-        add_action( 'init', array( $this, 'init' ), -1 );
57
-		add_action( 'wp_logout', array( $this, 'destroy_session' ) );
58
-		add_action( 'wp', array( $this, 'set_customer_session_cookie' ), 10 );
59
-		add_action( 'shutdown', array( $this, 'save_data' ), 20 );
55
+	    $this->_cookie = apply_filters('wpinv_cookie', 'wpinv_session_' . COOKIEHASH);
56
+        add_action('init', array($this, 'init'), -1);
57
+		add_action('wp_logout', array($this, 'destroy_session'));
58
+		add_action('wp', array($this, 'set_customer_session_cookie'), 10);
59
+		add_action('shutdown', array($this, 'save_data'), 20);
60 60
 
61 61
 	}
62 62
 
@@ -68,8 +68,8 @@  discard block
 block discarded – undo
68 68
 	public function init() {
69 69
 		$this->init_session_cookie();
70 70
 
71
-		if ( ! is_user_logged_in() ) {
72
-			add_filter( 'nonce_user_logged_out', array( $this, 'nonce_user_logged_out' ), 10, 2 );
71
+		if (!is_user_logged_in()) {
72
+			add_filter('nonce_user_logged_out', array($this, 'nonce_user_logged_out'), 10, 2);
73 73
 		}
74 74
 	}
75 75
 
@@ -81,7 +81,7 @@  discard block
 block discarded – undo
81 81
 	public function init_session_cookie() {
82 82
 		$cookie = $this->get_session_cookie();
83 83
 
84
-		if ( $cookie ) {
84
+		if ($cookie) {
85 85
 			$this->_customer_id        = $cookie[0];
86 86
 			$this->_session_expiration = $cookie[1];
87 87
 			$this->_session_expiring   = $cookie[2];
@@ -89,17 +89,17 @@  discard block
 block discarded – undo
89 89
 			$this->_data               = $this->get_session_data();
90 90
 
91 91
 			// If the user logs in, update session.
92
-			if ( is_user_logged_in() && get_current_user_id() != $this->_customer_id ) {
92
+			if (is_user_logged_in() && get_current_user_id() != $this->_customer_id) {
93 93
 				$this->_customer_id = get_current_user_id();
94 94
 				$this->_dirty       = true;
95 95
 				$this->save_data();
96
-				$this->set_customer_session_cookie( true );
96
+				$this->set_customer_session_cookie(true);
97 97
 			}
98 98
 
99 99
 			// Update session if its close to expiring.
100
-			if ( time() > $this->_session_expiring ) {
100
+			if (time() > $this->_session_expiring) {
101 101
 				$this->set_session_expiration();
102
-				$this->update_session_timestamp( $this->_customer_id, $this->_session_expiration );
102
+				$this->update_session_timestamp($this->_customer_id, $this->_session_expiration);
103 103
 			}
104 104
 		} else {
105 105
 			$this->set_session_expiration();
@@ -117,25 +117,25 @@  discard block
 block discarded – undo
117 117
 	 *
118 118
 	 * @param bool $set Should the session cookie be set.
119 119
 	 */
120
-	public function set_customer_session_cookie( $set ) {
121
-		if ( $set ) {
120
+	public function set_customer_session_cookie($set) {
121
+		if ($set) {
122 122
 			$to_hash           = $this->_customer_id . '|' . $this->_session_expiration;
123
-			$cookie_hash       = hash_hmac( 'md5', $to_hash, wp_hash( $to_hash ) );
123
+			$cookie_hash       = hash_hmac('md5', $to_hash, wp_hash($to_hash));
124 124
 			$cookie_value      = $this->_customer_id . '||' . $this->_session_expiration . '||' . $this->_session_expiring . '||' . $cookie_hash;
125 125
 			$this->_has_cookie = true;
126 126
 
127
-			if ( ! isset( $_COOKIE[ $this->_cookie ] ) || $_COOKIE[ $this->_cookie ] !== $cookie_value ) {
128
-				$this->setcookie( $this->_cookie, $cookie_value, $this->_session_expiration, $this->use_secure_cookie(), true );
127
+			if (!isset($_COOKIE[$this->_cookie]) || $_COOKIE[$this->_cookie] !== $cookie_value) {
128
+				$this->setcookie($this->_cookie, $cookie_value, $this->_session_expiration, $this->use_secure_cookie(), true);
129 129
 			}
130 130
 		}
131 131
 	}
132 132
 
133
-	public function setcookie($name, $value, $expire = 0, $secure = false, $httponly = false){
134
-        if ( ! headers_sent() ) {
135
-            setcookie( $name, $value, $expire, COOKIEPATH ? COOKIEPATH : '/', COOKIE_DOMAIN, $secure, apply_filters( 'wpinv_cookie_httponly', $httponly, $name, $value, $expire, $secure ) );
136
-        } elseif ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
137
-            headers_sent( $file, $line );
138
-            trigger_error( "{$name} cookie cannot be set - headers already sent by {$file} on line {$line}", E_USER_NOTICE ); // @codingStandardsIgnoreLine
133
+	public function setcookie($name, $value, $expire = 0, $secure = false, $httponly = false) {
134
+        if (!headers_sent()) {
135
+            setcookie($name, $value, $expire, COOKIEPATH ? COOKIEPATH : '/', COOKIE_DOMAIN, $secure, apply_filters('wpinv_cookie_httponly', $httponly, $name, $value, $expire, $secure));
136
+        } elseif (defined('WP_DEBUG') && WP_DEBUG) {
137
+            headers_sent($file, $line);
138
+            trigger_error("{$name} cookie cannot be set - headers already sent by {$file} on line {$line}", E_USER_NOTICE); // @codingStandardsIgnoreLine
139 139
         }
140 140
     }
141 141
 
@@ -146,8 +146,8 @@  discard block
 block discarded – undo
146 146
 	 * @return bool
147 147
 	 */
148 148
 	protected function use_secure_cookie() {
149
-        $is_https = false !== strstr( get_option( 'home' ), 'https:' );
150
-		return apply_filters( 'wpinv_session_use_secure_cookie', $is_https && is_ssl() );
149
+        $is_https = false !== strstr(get_option('home'), 'https:');
150
+		return apply_filters('wpinv_session_use_secure_cookie', $is_https && is_ssl());
151 151
 	}
152 152
 
153 153
 	/**
@@ -156,15 +156,15 @@  discard block
 block discarded – undo
156 156
 	 * @return bool
157 157
 	 */
158 158
 	public function has_session() {
159
-		return isset( $_COOKIE[ $this->_cookie ] ) || $this->_has_cookie || is_user_logged_in(); // @codingStandardsIgnoreLine.
159
+		return isset($_COOKIE[$this->_cookie]) || $this->_has_cookie || is_user_logged_in(); // @codingStandardsIgnoreLine.
160 160
 	}
161 161
 
162 162
 	/**
163 163
 	 * Set session expiration.
164 164
 	 */
165 165
 	public function set_session_expiration() {
166
-		$this->_session_expiring   = time() + intval( apply_filters( 'wpinv_session_expiring', 60 * 60 * 47 ) ); // 47 Hours.
167
-		$this->_session_expiration = time() + intval( apply_filters( 'wpinv_session_expiration', 60 * 60 * 48 ) ); // 48 Hours.
166
+		$this->_session_expiring   = time() + intval(apply_filters('wpinv_session_expiring', 60 * 60 * 47)); // 47 Hours.
167
+		$this->_session_expiration = time() + intval(apply_filters('wpinv_session_expiration', 60 * 60 * 48)); // 48 Hours.
168 168
 	}
169 169
 
170 170
 	/**
@@ -174,8 +174,8 @@  discard block
 block discarded – undo
174 174
 	 */
175 175
 	public function generate_customer_id() {
176 176
 		require_once ABSPATH . 'wp-includes/class-phpass.php';
177
-		$hasher      = new PasswordHash( 8, false );
178
-		return md5( $hasher->get_random_bytes( 32 ) );
177
+		$hasher = new PasswordHash(8, false);
178
+		return md5($hasher->get_random_bytes(32));
179 179
 	}
180 180
 
181 181
 	/**
@@ -186,27 +186,27 @@  discard block
 block discarded – undo
186 186
 	 * @return bool|array
187 187
 	 */
188 188
 	public function get_session_cookie() {
189
-		$cookie_value = isset( $_COOKIE[ $this->_cookie ] ) ? wp_unslash( $_COOKIE[ $this->_cookie ] ) : false; // @codingStandardsIgnoreLine.
189
+		$cookie_value = isset($_COOKIE[$this->_cookie]) ? wp_unslash($_COOKIE[$this->_cookie]) : false; // @codingStandardsIgnoreLine.
190 190
 
191
-		if ( empty( $cookie_value ) || ! is_string( $cookie_value ) ) {
191
+		if (empty($cookie_value) || !is_string($cookie_value)) {
192 192
 			return false;
193 193
 		}
194 194
 
195
-		list( $customer_id, $session_expiration, $session_expiring, $cookie_hash ) = explode( '||', $cookie_value );
195
+		list($customer_id, $session_expiration, $session_expiring, $cookie_hash) = explode('||', $cookie_value);
196 196
 
197
-		if ( empty( $customer_id ) ) {
197
+		if (empty($customer_id)) {
198 198
 			return false;
199 199
 		}
200 200
 
201 201
 		// Validate hash.
202 202
 		$to_hash = $customer_id . '|' . $session_expiration;
203
-		$hash    = hash_hmac( 'md5', $to_hash, wp_hash( $to_hash ) );
203
+		$hash    = hash_hmac('md5', $to_hash, wp_hash($to_hash));
204 204
 
205
-		if ( empty( $cookie_hash ) || ! hash_equals( $hash, $cookie_hash ) ) {
205
+		if (empty($cookie_hash) || !hash_equals($hash, $cookie_hash)) {
206 206
 			return false;
207 207
 		}
208 208
 
209
-		return array( $customer_id, $session_expiration, $session_expiring, $cookie_hash );
209
+		return array($customer_id, $session_expiration, $session_expiring, $cookie_hash);
210 210
 	}
211 211
 
212 212
 	/**
@@ -215,15 +215,15 @@  discard block
 block discarded – undo
215 215
 	 * @return array
216 216
 	 */
217 217
 	public function get_session_data() {
218
-		return $this->has_session() ? (array) $this->get_session( $this->_customer_id ) : array();
218
+		return $this->has_session() ? (array) $this->get_session($this->_customer_id) : array();
219 219
 	}
220 220
 
221
-	public function generate_key($customer_id){
222
-        if(!$customer_id){
221
+	public function generate_key($customer_id) {
222
+        if (!$customer_id) {
223 223
             return;
224 224
         }
225 225
 
226
-        return 'wpi_trans_'.$customer_id;
226
+        return 'wpi_trans_' . $customer_id;
227 227
     }
228 228
 
229 229
 	/**
@@ -231,9 +231,9 @@  discard block
 block discarded – undo
231 231
 	 */
232 232
 	public function save_data() {
233 233
 		// Dirty if something changed - prevents saving nothing new.
234
-		if ( $this->_dirty && $this->has_session() ) {
234
+		if ($this->_dirty && $this->has_session()) {
235 235
 
236
-            set_transient( $this->generate_key($this->_customer_id), $this->_data, $this->_session_expiration);
236
+            set_transient($this->generate_key($this->_customer_id), $this->_data, $this->_session_expiration);
237 237
 
238 238
 			$this->_dirty = false;
239 239
 		}
@@ -243,7 +243,7 @@  discard block
 block discarded – undo
243 243
 	 * Destroy all session data.
244 244
 	 */
245 245
 	public function destroy_session() {
246
-		$this->delete_session( $this->_customer_id );
246
+		$this->delete_session($this->_customer_id);
247 247
 		$this->forget_session();
248 248
 	}
249 249
 
@@ -251,7 +251,7 @@  discard block
 block discarded – undo
251 251
 	 * Forget all session data without destroying it.
252 252
 	 */
253 253
 	public function forget_session() {
254
-		$this->setcookie( $this->_cookie, '', time() - YEAR_IN_SECONDS, $this->use_secure_cookie(), true );
254
+		$this->setcookie($this->_cookie, '', time() - YEAR_IN_SECONDS, $this->use_secure_cookie(), true);
255 255
 
256 256
 		wpinv_empty_cart();
257 257
 
@@ -266,10 +266,10 @@  discard block
 block discarded – undo
266 266
 	 * @param int $uid User ID.
267 267
 	 * @return string
268 268
 	 */
269
-	public function nonce_user_logged_out( $uid ) {
269
+	public function nonce_user_logged_out($uid) {
270 270
 
271 271
 		// Check if one of our nonces.
272
-		if ( substr( $uid, 0, 5 ) === 'wpinv' || substr( $uid, 0, 7 ) === 'getpaid' ) {
272
+		if (substr($uid, 0, 5) === 'wpinv' || substr($uid, 0, 7) === 'getpaid') {
273 273
 			return $this->has_session() && $this->_customer_id ? $this->_customer_id : $uid;
274 274
 		}
275 275
 
@@ -283,20 +283,20 @@  discard block
 block discarded – undo
283 283
 	 * @param mixed  $default Default session value.
284 284
 	 * @return string|array
285 285
 	 */
286
-	public function get_session( $customer_id, $default = false ) {
286
+	public function get_session($customer_id, $default = false) {
287 287
 
288
-		if ( defined( 'WP_SETUP_CONFIG' ) ) {
288
+		if (defined('WP_SETUP_CONFIG')) {
289 289
 			return array();
290 290
 		}
291 291
 
292 292
         $key = $this->generate_key($customer_id);
293 293
         $value = get_transient($key);
294 294
 
295
-        if ( !$value ) {
295
+        if (!$value) {
296 296
             $value = $default;
297 297
         }
298 298
 
299
-		return maybe_unserialize( $value );
299
+		return maybe_unserialize($value);
300 300
 	}
301 301
 
302 302
 	/**
@@ -304,7 +304,7 @@  discard block
 block discarded – undo
304 304
 	 *
305 305
 	 * @param int $customer_id Customer ID.
306 306
 	 */
307
-	public function delete_session( $customer_id ) {
307
+	public function delete_session($customer_id) {
308 308
 
309 309
         $key = $this->generate_key($customer_id);
310 310
 
@@ -317,9 +317,9 @@  discard block
 block discarded – undo
317 317
 	 * @param string $customer_id Customer ID.
318 318
 	 * @param int    $timestamp Timestamp to expire the cookie.
319 319
 	 */
320
-	public function update_session_timestamp( $customer_id, $timestamp ) {
320
+	public function update_session_timestamp($customer_id, $timestamp) {
321 321
 
322
-        set_transient( $this->generate_key($customer_id), maybe_serialize( $this->_data ), $timestamp);
322
+        set_transient($this->generate_key($customer_id), maybe_serialize($this->_data), $timestamp);
323 323
 
324 324
 	}
325 325
 }
Please login to merge, or discard this patch.