2013-01-15 19:51:19 +00:00
|
|
|
<?php
|
2013-02-20 17:14:46 +00:00
|
|
|
|
|
|
|
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
|
|
|
|
|
2013-01-15 19:51:19 +00:00
|
|
|
/**
|
|
|
|
* Handle data for the current customers session.
|
|
|
|
* Implements the WC_Session abstract class
|
|
|
|
*
|
|
|
|
* Long term plan will be, if https://github.com/ericmann/wp-session-manager/ gains traction
|
|
|
|
* in WP core, this will be switched out to use it and maintain backwards compatibility :)
|
|
|
|
*
|
|
|
|
* Partly based on WP SESSION by Eric Mann.
|
|
|
|
*
|
|
|
|
* @class WC_Session_Handler
|
|
|
|
* @version 2.0.0
|
|
|
|
* @package WooCommerce/Classes
|
2013-02-20 17:14:46 +00:00
|
|
|
* @category Class
|
2013-01-15 19:51:19 +00:00
|
|
|
* @author WooThemes
|
|
|
|
*/
|
|
|
|
class WC_Session_Handler extends WC_Session {
|
|
|
|
|
|
|
|
/** cookie name */
|
|
|
|
private $_cookie;
|
|
|
|
|
2013-01-16 11:05:50 +00:00
|
|
|
/** session due to expire timestamp */
|
|
|
|
private $_session_expiring;
|
|
|
|
|
2013-01-15 19:51:19 +00:00
|
|
|
/** session expiration timestamp */
|
|
|
|
private $_session_expiration;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor for the session class.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function __construct() {
|
2013-10-17 14:29:39 +00:00
|
|
|
$this->_cookie = 'wp_woocommerce_session_' . COOKIEHASH;
|
2013-01-15 19:51:19 +00:00
|
|
|
|
|
|
|
if ( $cookie = $this->get_session_cookie() ) {
|
|
|
|
$this->_customer_id = $cookie[0];
|
2013-01-16 11:05:50 +00:00
|
|
|
$this->_session_expiration = $cookie[1];
|
|
|
|
$this->_session_expiring = $cookie[2];
|
2013-01-15 19:51:19 +00:00
|
|
|
|
|
|
|
// Update session if its close to expiring
|
2013-01-16 11:05:50 +00:00
|
|
|
if ( time() > $this->_session_expiring ) {
|
|
|
|
$this->set_session_expiration();
|
2014-04-08 07:35:04 +00:00
|
|
|
$session_expiry_option = '_wc_session_expires_' . $this->_customer_id;
|
|
|
|
// Check if option exists first to avoid auloading cleaned up sessions
|
|
|
|
if ( false === get_option( $session_expiry_option ) ) {
|
|
|
|
add_option( $session_expiry_option, $this->_session_expiration, '', 'no' );
|
|
|
|
} else {
|
|
|
|
update_option( $session_expiry_option, $this->_session_expiration );
|
|
|
|
}
|
2013-01-15 19:51:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
2013-01-16 11:05:50 +00:00
|
|
|
$this->set_session_expiration();
|
2013-01-15 19:51:19 +00:00
|
|
|
$this->_customer_id = $this->generate_customer_id();
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->_data = $this->get_session_data();
|
|
|
|
|
|
|
|
// Actions
|
2013-10-17 14:29:39 +00:00
|
|
|
add_action( 'woocommerce_set_cart_cookies', array( $this, 'set_customer_session_cookie' ), 10 );
|
2013-01-16 09:53:08 +00:00
|
|
|
add_action( 'woocommerce_cleanup_sessions', array( $this, 'cleanup_sessions' ), 10 );
|
2013-01-15 19:51:19 +00:00
|
|
|
add_action( 'shutdown', array( $this, 'save_data' ), 20 );
|
|
|
|
}
|
|
|
|
|
2013-10-17 14:29:39 +00:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
|
|
|
|
// Set the cookie
|
2014-03-19 09:16:26 +00:00
|
|
|
wc_setcookie( $this->_cookie, $cookie_value, $this->_session_expiration, apply_filters( 'wc_session_use_secure_cookie', false ) );
|
2013-10-17 14:29:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-15 19:51:19 +00:00
|
|
|
/**
|
2013-01-16 11:05:50 +00:00
|
|
|
* set_session_expiration function.
|
2013-01-15 19:51:19 +00:00
|
|
|
*
|
2013-09-13 12:39:46 +00:00
|
|
|
* @access public
|
2013-01-15 19:51:19 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2013-09-13 12:39:46 +00:00
|
|
|
public function set_session_expiration() {
|
2013-01-16 11:05:50 +00:00
|
|
|
$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
|
2013-01-15 19:51:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* generate_customer_id function.
|
|
|
|
*
|
2013-09-13 12:39:46 +00:00
|
|
|
* @access public
|
2013-01-15 19:51:19 +00:00
|
|
|
* @return mixed
|
|
|
|
*/
|
2013-09-13 12:39:46 +00:00
|
|
|
public function generate_customer_id() {
|
2013-01-15 19:51:19 +00:00
|
|
|
if ( is_user_logged_in() )
|
|
|
|
return get_current_user_id();
|
|
|
|
else
|
2013-04-05 21:24:51 +00:00
|
|
|
return wp_generate_password( 32, false );
|
2013-01-15 19:51:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get_session_cookie function.
|
|
|
|
*
|
2013-09-13 12:39:46 +00:00
|
|
|
* @access public
|
2013-01-15 19:51:19 +00:00
|
|
|
* @return mixed
|
|
|
|
*/
|
2013-09-13 12:39:46 +00:00
|
|
|
public function get_session_cookie() {
|
2013-03-06 11:59:24 +00:00
|
|
|
if ( empty( $_COOKIE[ $this->_cookie ] ) )
|
2013-01-15 19:51:19 +00:00
|
|
|
return false;
|
|
|
|
|
2013-01-16 11:05:50 +00:00
|
|
|
list( $customer_id, $session_expiration, $session_expiring, $cookie_hash ) = explode( '||', $_COOKIE[ $this->_cookie ] );
|
2013-01-15 19:51:19 +00:00
|
|
|
|
|
|
|
// Validate hash
|
2013-01-16 11:05:50 +00:00
|
|
|
$to_hash = $customer_id . $session_expiration;
|
2013-01-15 19:51:19 +00:00
|
|
|
$hash = hash_hmac( 'md5', $to_hash, wp_hash( $to_hash ) );
|
|
|
|
|
|
|
|
if ( $hash != $cookie_hash )
|
|
|
|
return false;
|
|
|
|
|
2013-01-16 11:05:50 +00:00
|
|
|
return array( $customer_id, $session_expiration, $session_expiring, $cookie_hash );
|
2013-01-15 19:51:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get_session_data function.
|
|
|
|
*
|
2013-09-13 12:39:46 +00:00
|
|
|
* @access public
|
2013-01-15 19:51:19 +00:00
|
|
|
* @return array
|
|
|
|
*/
|
2013-09-13 12:39:46 +00:00
|
|
|
public function get_session_data() {
|
2013-06-14 13:42:24 +00:00
|
|
|
return (array) get_option( '_wc_session_' . $this->_customer_id, array() );
|
2013-01-15 19:51:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* save_data function.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function save_data() {
|
|
|
|
// Dirty if something changed - prevents saving nothing new
|
|
|
|
if ( $this->_dirty ) {
|
2013-01-16 11:05:50 +00:00
|
|
|
|
|
|
|
$session_option = '_wc_session_' . $this->_customer_id;
|
|
|
|
$session_expiry_option = '_wc_session_expires_' . $this->_customer_id;
|
|
|
|
|
|
|
|
if ( false === get_option( $session_option ) ) {
|
2013-04-09 08:40:23 +00:00
|
|
|
add_option( $session_option, $this->_data, '', 'no' );
|
|
|
|
add_option( $session_expiry_option, $this->_session_expiration, '', 'no' );
|
2013-01-15 19:51:19 +00:00
|
|
|
} else {
|
2013-01-16 11:05:50 +00:00
|
|
|
update_option( $session_option, $this->_data );
|
2013-01-15 19:51:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-01-16 09:53:08 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* cleanup_sessions function.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function cleanup_sessions() {
|
|
|
|
global $wpdb;
|
|
|
|
|
2014-01-13 10:45:31 +00:00
|
|
|
if ( ! defined( 'WP_SETUP_CONFIG' ) && ! defined( 'WP_INSTALLING' ) ) {
|
|
|
|
$now = time();
|
|
|
|
$expired_sessions = array();
|
|
|
|
$wc_session_expires = $wpdb->get_results( "SELECT option_name, option_value FROM $wpdb->options WHERE option_name LIKE '_wc_session_expires_%'" );
|
|
|
|
|
|
|
|
foreach ( $wc_session_expires as $wc_session_expire ) {
|
|
|
|
if ( $now > intval( $wc_session_expire->option_value ) ) {
|
|
|
|
$session_id = substr( $wc_session_expire->option_name, 20 );
|
|
|
|
$expired_sessions[] = $wc_session_expire->option_name; // Expires key
|
|
|
|
$expired_sessions[] = "_wc_session_$session_id"; // Session key
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! empty( $expired_sessions ) ) {
|
2014-04-01 13:51:41 +00:00
|
|
|
$expired_sessions_chunked = array_chunk( $expired_sessions, 100 );
|
|
|
|
foreach ( $expired_sessions_chunked as $chunk ) {
|
|
|
|
$option_names = implode( "','", $chunk );
|
|
|
|
$wpdb->query( "DELETE FROM $wpdb->options WHERE option_name IN ('$option_names')" );
|
|
|
|
}
|
2014-01-13 10:45:31 +00:00
|
|
|
}
|
|
|
|
}
|
2013-01-16 09:53:08 +00:00
|
|
|
}
|
2013-01-15 19:51:19 +00:00
|
|
|
}
|