Fixed coding standards
This commit is contained in:
parent
b27635409c
commit
1889c66061
|
@ -9,11 +9,11 @@ if ( ! defined( 'ABSPATH' ) ) {
|
|||
*
|
||||
* From 2.5 this uses a custom table for session storage. Based on https://github.com/kloon/woocommerce-large-sessions.
|
||||
*
|
||||
* @class WC_Session_Handler
|
||||
* @version 2.5.0
|
||||
* @package WooCommerce/Classes
|
||||
* @category Class
|
||||
* @author WooThemes
|
||||
* @class WC_Session_Handler
|
||||
* @version 2.5.0
|
||||
* @package WooCommerce/Classes
|
||||
* @category Class
|
||||
* @author WooThemes
|
||||
*/
|
||||
class WC_Session_Handler extends WC_Session {
|
||||
|
||||
|
@ -33,7 +33,7 @@ class WC_Session_Handler extends WC_Session {
|
|||
private $_table;
|
||||
|
||||
/**
|
||||
* Constructor for the session class.
|
||||
* Constructor for the session class
|
||||
*/
|
||||
public function __construct() {
|
||||
global $wpdb;
|
||||
|
@ -60,57 +60,58 @@ class WC_Session_Handler extends WC_Session {
|
|||
|
||||
$this->_data = $this->get_session_data();
|
||||
|
||||
// Actions
|
||||
add_action( 'woocommerce_set_cart_cookies', array( $this, 'set_customer_session_cookie' ), 10 );
|
||||
add_action( 'woocommerce_cleanup_sessions', array( $this, 'cleanup_sessions' ), 10 );
|
||||
add_action( 'shutdown', array( $this, 'save_data' ), 20 );
|
||||
add_action( 'wp_logout', array( $this, 'destroy_session' ) );
|
||||
if ( ! is_user_logged_in() ) {
|
||||
add_action( 'woocommerce_thankyou', array( $this, 'destroy_session' ) );
|
||||
add_filter( 'nonce_user_logged_out', array( $this, 'nonce_user_logged_out' ) );
|
||||
}
|
||||
}
|
||||
// Actions
|
||||
add_action( 'woocommerce_set_cart_cookies', array( $this, 'set_customer_session_cookie' ), 10 );
|
||||
add_action( 'woocommerce_cleanup_sessions', array( $this, 'cleanup_sessions' ), 10 );
|
||||
add_action( 'shutdown', array( $this, 'save_data' ), 20 );
|
||||
add_action( 'wp_logout', array( $this, 'destroy_session' ) );
|
||||
if ( ! is_user_logged_in() ) {
|
||||
add_action( 'woocommerce_thankyou', array( $this, 'destroy_session' ) );
|
||||
add_filter( 'nonce_user_logged_out', array( $this, 'nonce_user_logged_out' ) );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the session cookie on-demand (usually after adding an item to the cart).
|
||||
*
|
||||
* Since the cookie name (as of 2.1) is prepended with wp, cache systems like batcache will not cache pages when set.
|
||||
*
|
||||
* Warning: Cookies will only be set if this is called before the headers are sent.
|
||||
*/
|
||||
public function set_customer_session_cookie( $set ) {
|
||||
if ( $set ) {
|
||||
// Set/renew our cookie
|
||||
/**
|
||||
* Sets the session cookie on-demand (usually after adding an item to the cart)
|
||||
*
|
||||
* Since the cookie name (as of 2.1) is prepended with wp, cache systems like batcache will not cache pages when set
|
||||
*
|
||||
* Warning: Cookies will only be set if this is called before the headers are sent
|
||||
*/
|
||||
public function set_customer_session_cookie( $set ) {
|
||||
if ( $set ) {
|
||||
// Set/renew our cookie
|
||||
$to_hash = $this->_customer_id . $this->_session_expiration;
|
||||
$cookie_hash = hash_hmac( 'md5', $to_hash, wp_hash( $to_hash ) );
|
||||
$cookie_value = $this->_customer_id . '||' . $this->_session_expiration . '||' . $this->_session_expiring . '||' . $cookie_hash;
|
||||
$this->_has_cookie = true;
|
||||
|
||||
// Set the cookie
|
||||
wc_setcookie( $this->_cookie, $cookie_value, $this->_session_expiration, apply_filters( 'wc_session_use_secure_cookie', false ) );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return true if the current user has an active session, i.e. a cookie to retrieve values
|
||||
* @return boolean
|
||||
*/
|
||||
public function has_session() {
|
||||
return isset( $_COOKIE[ $this->_cookie ] ) || $this->_has_cookie || is_user_logged_in();
|
||||
}
|
||||
|
||||
/**
|
||||
* set_session_expiration function.
|
||||
*/
|
||||
public function set_session_expiration() {
|
||||
$this->_session_expiring = time() + intval( apply_filters( 'wc_session_expiring', 60 * 60 * 47 ) ); // 47 Hours
|
||||
$this->_session_expiration = time() + intval( apply_filters( 'wc_session_expiration', 60 * 60 * 48 ) ); // 48 Hours
|
||||
}
|
||||
// Set the cookie
|
||||
wc_setcookie( $this->_cookie, $cookie_value, $this->_session_expiration, apply_filters( 'wc_session_use_secure_cookie', false ) );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a unique customer ID for guests, or return user ID if logged in.
|
||||
* Return true if the current user has an active session, i.e. a cookie to retrieve values
|
||||
*
|
||||
* Uses Portable PHP password hashing framework to generate a unique cryptographically strong ID.
|
||||
* @return bool
|
||||
*/
|
||||
public function has_session() {
|
||||
return isset( $_COOKIE[ $this->_cookie ] ) || $this->_has_cookie || is_user_logged_in();
|
||||
}
|
||||
|
||||
/**
|
||||
* Set session expiration.
|
||||
*/
|
||||
public function set_session_expiration() {
|
||||
$this->_session_expiring = time() + intval( apply_filters( 'wc_session_expiring', 60 * 60 * 47 ) ); // 47 Hours
|
||||
$this->_session_expiration = time() + intval( apply_filters( 'wc_session_expiration', 60 * 60 * 48 ) ); // 48 Hours
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a unique customer ID for guests, or return user ID if logged in
|
||||
*
|
||||
* Uses Portable PHP password hashing framework to generate a unique cryptographically strong ID
|
||||
*
|
||||
* @return int|string
|
||||
*/
|
||||
|
@ -125,7 +126,7 @@ class WC_Session_Handler extends WC_Session {
|
|||
}
|
||||
|
||||
/**
|
||||
* get_session_cookie function.
|
||||
* Get session cookie
|
||||
*
|
||||
* @return bool|array
|
||||
*/
|
||||
|
@ -148,7 +149,7 @@ class WC_Session_Handler extends WC_Session {
|
|||
}
|
||||
|
||||
/**
|
||||
* get_session_data function.
|
||||
* Get session data
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
|
@ -157,7 +158,8 @@ class WC_Session_Handler extends WC_Session {
|
|||
}
|
||||
|
||||
/**
|
||||
* Gets a cache prefix. This is used in session names so the entire cache can be invalidated with 1 function call.
|
||||
* Gets a cache prefix. This is used in session names so the entire cache can be invalidated with 1 function call
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function get_cache_prefix() {
|
||||
|
@ -170,12 +172,12 @@ class WC_Session_Handler extends WC_Session {
|
|||
return 'wc_session_' . $prefix_num . '_';
|
||||
}
|
||||
|
||||
/**
|
||||
* save_data function.
|
||||
*/
|
||||
public function save_data() {
|
||||
// Dirty if something changed - prevents saving nothing new
|
||||
if ( $this->_dirty && $this->has_session() ) {
|
||||
/**
|
||||
* Save data
|
||||
*/
|
||||
public function save_data() {
|
||||
// Dirty if something changed - prevents saving nothing new
|
||||
if ( $this->_dirty && $this->has_session() ) {
|
||||
global $wpdb;
|
||||
|
||||
$session_id = $wpdb->get_var( $wpdb->prepare( "SELECT session_id FROM $this->_table WHERE session_key = %s;", $this->_customer_id ) );
|
||||
|
@ -215,15 +217,15 @@ class WC_Session_Handler extends WC_Session {
|
|||
// Set cache
|
||||
wp_cache_set( $this->get_cache_prefix() . $this->_customer_id, $this->_data, WC_SESSION_CACHE_GROUP, $this->_session_expiration - time() );
|
||||
|
||||
// Mark session clean after saving
|
||||
$this->_dirty = false;
|
||||
}
|
||||
}
|
||||
// Mark session clean after saving
|
||||
$this->_dirty = false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Destroy all session data
|
||||
*/
|
||||
public function destroy_session() {
|
||||
/**
|
||||
* Destroy all session data
|
||||
*/
|
||||
public function destroy_session() {
|
||||
// Clear cookie
|
||||
wc_setcookie( $this->_cookie, '', time() - YEAR_IN_SECONDS, apply_filters( 'wc_session_use_secure_cookie', false ) );
|
||||
|
||||
|
@ -239,15 +241,16 @@ class WC_Session_Handler extends WC_Session {
|
|||
}
|
||||
|
||||
/**
|
||||
* When a user is logged out, ensure they have a unique nonce by using the customer/session ID.
|
||||
* When a user is logged out, ensure they have a unique nonce by using the customer/session ID
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function nonce_user_logged_out( $uid ) {
|
||||
return $this->has_session() && $this->_customer_id ? $this->_customer_id : $uid;
|
||||
}
|
||||
|
||||
/**
|
||||
* cleanup_sessions function.
|
||||
/**
|
||||
* Cleanup sessions
|
||||
*/
|
||||
public function cleanup_sessions() {
|
||||
global $wpdb;
|
||||
|
@ -264,11 +267,12 @@ class WC_Session_Handler extends WC_Session {
|
|||
|
||||
/**
|
||||
* Returns the session
|
||||
*
|
||||
* @param string $customer_id
|
||||
* @param mixed $default
|
||||
* @return string|array
|
||||
*/
|
||||
function get_session( $customer_id, $default = false ) {
|
||||
public function get_session( $customer_id, $default = false ) {
|
||||
global $wpdb;
|
||||
|
||||
if ( defined( 'WP_SETUP_CONFIG' ) ) {
|
||||
|
@ -293,9 +297,10 @@ class WC_Session_Handler extends WC_Session {
|
|||
|
||||
/**
|
||||
* Delete the session from the cache and database
|
||||
* @param int $customer_id
|
||||
*
|
||||
* @param int $customer_id
|
||||
*/
|
||||
function delete_session( $customer_id ) {
|
||||
public function delete_session( $customer_id ) {
|
||||
global $wpdb;
|
||||
|
||||
wp_cache_delete( $this->get_cache_prefix() . $customer_id, WC_SESSION_CACHE_GROUP );
|
||||
|
@ -310,11 +315,13 @@ class WC_Session_Handler extends WC_Session {
|
|||
|
||||
/**
|
||||
* Update the session expiry timestamp
|
||||
* @param string $customer_id
|
||||
* @param int $timestamp
|
||||
*
|
||||
* @param string $customer_id
|
||||
* @param int $timestamp
|
||||
*/
|
||||
public function update_session_timestamp( $customer_id, $timestamp ) {
|
||||
global $wpdb;
|
||||
|
||||
$wpdb->update(
|
||||
$this->_table,
|
||||
array(
|
||||
|
|
Loading…
Reference in New Issue