2012-09-07 13:31:57 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Handle data for the current customers session.
|
2012-09-12 13:45:42 +00:00
|
|
|
* Implements the WC_Session abstract class
|
2012-09-07 13:31:57 +00:00
|
|
|
*
|
2012-09-12 13:45:42 +00:00
|
|
|
* @class WC_Session_Transients
|
2012-09-07 13:31:57 +00:00
|
|
|
* @version 1.7
|
|
|
|
* @package WooCommerce/Classes
|
|
|
|
* @author WooThemes
|
|
|
|
*/
|
2012-10-15 10:57:58 +00:00
|
|
|
|
|
|
|
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
|
|
|
|
|
2012-09-12 14:21:13 +00:00
|
|
|
class WC_Session_Transients extends WC_Session {
|
2012-09-07 13:31:57 +00:00
|
|
|
|
|
|
|
/** customer_id */
|
|
|
|
private $_customer_id;
|
|
|
|
|
|
|
|
/** cookie name */
|
|
|
|
private $_cookie;
|
|
|
|
|
|
|
|
/**
|
2012-09-12 14:21:13 +00:00
|
|
|
* Constructor for the session class.
|
2012-09-07 13:31:57 +00:00
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
2012-09-07 17:26:13 +00:00
|
|
|
public function __construct() {
|
2012-09-12 14:21:13 +00:00
|
|
|
parent::__construct();
|
2012-09-07 13:31:57 +00:00
|
|
|
|
|
|
|
$this->_cookie = 'wc_session_cookie_' . COOKIEHASH;
|
|
|
|
$this->_customer_id = $this->get_customer_id();
|
2012-09-07 13:52:12 +00:00
|
|
|
$this->_data = maybe_unserialize( get_transient( 'wc_session_' . $this->_customer_id ) );
|
2012-09-07 13:31:57 +00:00
|
|
|
|
2012-09-12 14:21:13 +00:00
|
|
|
if ( false === $this->_data )
|
2012-09-07 13:31:57 +00:00
|
|
|
$this->_data = array();
|
2012-09-07 17:26:13 +00:00
|
|
|
}
|
2012-09-07 13:31:57 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* get_customer_id function.
|
|
|
|
*
|
2012-09-12 13:45:42 +00:00
|
|
|
* @access private
|
2012-09-07 13:31:57 +00:00
|
|
|
* @return mixed
|
|
|
|
*/
|
2012-09-12 13:45:42 +00:00
|
|
|
private function get_customer_id() {
|
2012-09-21 15:55:09 +00:00
|
|
|
if ( $customer_id = $this->get_session_cookie() ) {
|
2012-09-07 13:31:57 +00:00
|
|
|
return $customer_id;
|
2012-09-21 15:55:09 +00:00
|
|
|
} elseif ( is_user_logged_in() ) {
|
|
|
|
return get_current_user_id();
|
2012-09-07 13:31:57 +00:00
|
|
|
} else {
|
|
|
|
return $this->create_customer_id();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get_session_cookie function.
|
|
|
|
*
|
2012-09-12 13:45:42 +00:00
|
|
|
* @access private
|
2012-09-07 13:31:57 +00:00
|
|
|
* @return mixed
|
|
|
|
*/
|
2012-09-12 13:45:42 +00:00
|
|
|
private function get_session_cookie() {
|
2012-09-07 13:31:57 +00:00
|
|
|
if ( ! isset( $_COOKIE[ $this->_cookie ] ) )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
list( $customer_id, $expires, $hash ) = explode( '|', $_COOKIE[ $this->_cookie ] );
|
|
|
|
|
|
|
|
// Validate hash
|
|
|
|
$data = $customer_id . $expires;
|
|
|
|
$rehash = hash_hmac( 'md5', $data, wp_hash( $data ) );
|
|
|
|
|
|
|
|
if ( $hash != $rehash )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return $customer_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a unqiue customer ID and store it in a cookie, along with its hashed value and expirey date. Stored for 48hours.
|
|
|
|
*
|
2012-09-12 13:45:42 +00:00
|
|
|
* @access private
|
2012-09-07 13:31:57 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2012-09-12 13:45:42 +00:00
|
|
|
private function create_customer_id() {
|
2012-09-07 17:26:13 +00:00
|
|
|
$customer_id = wp_generate_password( 32 ); // Ensure this and the transient is < 45 chars. wc_session_ leaves 34.
|
2012-09-07 13:31:57 +00:00
|
|
|
$expires = time() + 172800;
|
|
|
|
$data = $customer_id . $expires;
|
|
|
|
$hash = hash_hmac( 'md5', $data, wp_hash( $data ) );
|
|
|
|
$value = $customer_id . '|' . $expires . '|' . $hash;
|
|
|
|
|
|
|
|
setcookie( $this->_cookie, $value, $expires, COOKIEPATH, COOKIE_DOMAIN, false, true );
|
|
|
|
|
|
|
|
return $customer_id;
|
|
|
|
}
|
2012-09-07 17:26:13 +00:00
|
|
|
|
2012-09-07 13:31:57 +00:00
|
|
|
/**
|
|
|
|
* save_data function.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function save_data() {
|
|
|
|
// Set cart data for 48 hours
|
|
|
|
set_transient( 'wc_session_' . $this->_customer_id, $this->_data, 172800 );
|
|
|
|
}
|
|
|
|
}
|