1 <?php
2 /**
3 * EDD Session
4 *
5 * This is a wrapper calss for WP_Session and handles the storage of cart items, purchase sessions, etc
6 *
7 * @package EDD
8 * @subpackage Classes/Session
9 * @copyright Copyright (c) 2013, Pippin Williamson
10 * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
11 * @since 1.5
12 */
13
14 // Exit if accessed directly
15 if ( ! defined( 'ABSPATH' ) ) exit;
16
17 /**
18 * EDD_Session Class
19 *
20 * @since 1.5
21 */
22 class EDD_Session {
23 /**
24 * Holds our session data
25 *
26 * @var array
27 * @access private
28 * @since 1.5
29 */
30 private $session = array();
31
32 /**
33 * Get things started
34 *
35 * Defines our WP_Session constants, includes the necessary libraries and
36 * retrieves the WP Session instance
37 *
38 * @access public
39 * @since 1.5
40 * @return void
41 */
42 public function __construct() {
43 if ( ! defined( 'WP_SESSION_COOKIE' ) )
44 define( 'WP_SESSION_COOKIE', 'wordpress_wp_session' );
45
46 if ( ! class_exists( 'Recursive_ArrayAccess' ) )
47 require_once EDD_PLUGIN_DIR . 'includes/libraries/class-recursive-arrayaccess.php';
48
49 if ( ! class_exists( 'WP_Session' ) ) {
50 require_once EDD_PLUGIN_DIR . 'includes/libraries/class-wp-session.php';
51 require_once EDD_PLUGIN_DIR . 'includes/libraries/wp-session.php';
52 }
53
54 if ( empty( $this->session ) )
55 add_action( 'plugins_loaded', array( $this, 'init' ) );
56 else
57 add_action( 'init', array( $this, 'init' ) );
58 }
59
60 /**
61 * Setup the WP_Session instance
62 *
63 * @access public
64 * @since 1.5
65 * @return void
66 */
67 public function init() {
68 $this->session = WP_Session::get_instance();
69 return $this->session;
70 }
71
72 /**
73 * Retrieve a session variable
74 *
75 * @access public
76 * @since 1.5
77 * @param string $key Session key
78 * @return string Session variable
79 */
80 public function get( $key ) {
81 $key = sanitize_key( $key );
82 return isset( $this->session[ $key ] ) ? maybe_unserialize( $this->session[ $key ] ) : false;
83 }
84
85 /**
86 * Set a session variable
87 *
88 * @access public
89 * @since 1.5
90 * @param string $key Session key
91 * @param string $variable Session variable
92 * @return array Session variable
93 */
94 public function set( $key, $value ) {
95 $key = sanitize_key( $key );
96
97 if ( is_array( $value ) )
98 $this->session[ $key ] = serialize( $value );
99 else
100 $this->session[ $key ] = $value;
101
102 return $this->session[ $key ];
103 }
104 }