Wrapper class to use sessions with WordPress
<?php
/**
* EDD Session
*
* This is a wrapper class for WP_Session / PHP $_SESSION and handles the storage of cart items, purchase sessions, etc
*
* @see https://github.com/easydigitaldownloads/Easy-Digital-Downloads/blob/master/includes/class-edd-session.php
* @package EDD
* @subpackage Classes/Session
* @copyright Copyright (c) 2013, Pippin Williamson
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
* @since 1.5
*/
// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) exit;
/**
* EDD_Session Class
*
* @since 1.5
*/
class EDD_Session {
/**
* Holds our session data
*
* @var array
* @access private
* @since 1.5
*/
private $session = array();
/**
* Whether to use PHP $_SESSION or WP_Session
*
* PHP $_SESSION is opt-in only by defining the EDD_USE_PHP_SESSIONS constant
*
* @var bool
* @access private
* @since 1.5,1
*/
private $use_php_sessions = false;
/**
* Get things started
*
* Defines our WP_Session constants, includes the necessary libraries and
* retrieves the WP Session instance
*
* @access public
* @since 1.5
* @return void
*/
public function __construct() {
$this->use_php_sessions = defined( 'EDD_USE_PHP_SESSIONS' ) && EDD_USE_PHP_SESSIONS;
if( $this->use_php_sessions ) {
// Use PHP SESSION (must be enabled via the EDD_USE_PHP_SESSIONS constant)
if( ! session_id() )
add_action( 'init', 'session_start', -1 );
} else {
// Use WP_Session (default)
if ( ! defined( 'WP_SESSION_COOKIE' ) )
define( 'WP_SESSION_COOKIE', 'wordpress_wp_session' );
if ( ! class_exists( 'Recursive_ArrayAccess' ) )
require_once EDD_PLUGIN_DIR . 'includes/libraries/class-recursive-arrayaccess.php';
if ( ! class_exists( 'WP_Session' ) ) {
require_once EDD_PLUGIN_DIR . 'includes/libraries/class-wp-session.php';
require_once EDD_PLUGIN_DIR . 'includes/libraries/wp-session.php';
}
}
if ( empty( $this->session ) && ! $this->use_php_sessions )
add_action( 'plugins_loaded', array( $this, 'init' ) );
else
add_action( 'init', array( $this, 'init' ) );
}
/**
* Setup the WP_Session instance
*
* @access public
* @since 1.5
* @return void
*/
public function init() {
if( $this->use_php_sessions )
$this->session = isset( $_SESSION['edd'] ) && is_array( $_SESSION['edd'] ) ? $_SESSION['edd'] : array();
else
$this->session = WP_Session::get_instance();
return $this->session;
}
/**
* Retrieve session ID
*
* @access public
* @since 1.6
* @return string Session ID
*/
public function get_id() {
return $this->session->session_id;
}
/**
* Retrieve a session variable
*
* @access public
* @since 1.5
* @param string $key Session key
* @return string Session variable
*/
public function get( $key ) {
$key = sanitize_key( $key );
return isset( $this->session[ $key ] ) ? maybe_unserialize( $this->session[ $key ] ) : false;
}
/**
* Set a session variable
*
* @access public
* @since 1.5
* @param string $key Session key
* @param string $variable Session variable
* @return array Session variable
*/
public function set( $key, $value ) {
$key = sanitize_key( $key );
if ( is_array( $value ) )
$this->session[ $key ] = serialize( $value );
else
$this->session[ $key ] = $value;
if( $this->use_php_sessions )
$_SESSION['edd'] = $this->session;
return $this->session[ $key ];
}
}
When I want to store some session data with the wrapper class, such as when a customer adds an item to the shopping cart, I simply do this:
```php
$cart = array(
array(
'item_id' => 43,
'item_name' => 'My Product Name'
),
array(
'item_id' => 22,
'item_name' => 'My Second Product Name'
)
);
EDD()->session->set( 'edd_cart', $cart );
```
And when I want to retrieve the session data:
```php
EDD()->session->get( 'edd_cart' );
```