PluginProbe
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) / 1.3.2
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) v1.3.2
1.8.12.3 1.8.12.2 1.8.12.1 1.8.12 1.8.11.3 1.8.11.2 1.8.11.1 1.8.11 1.6.6 1.6.60 1.6.7 1.6.8 1.6.9 1.7.0 1.7.0.1 1.7.0.11 1.7.0.12 1.7.0.14 1.7.0.2 1.7.0.3 1.7.0.5 1.7.0.6 1.7.0.7 1.7.0.9 1.8.0 All 210 releases
charitable / includes / public / class-charitable-session.php

class-charitable-session.php in Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) 1.3.2, at includes/public/class-charitable-session.php

258 lines 5.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Charitable Session class.
4 *
5 * The responsibility of this class is to manager the user sessions.
6 *
7 * @package Charitable
8 * @subpackage Charitable/Charitable Session
9 * @copyright Copyright (c) 2015, Eric Daams
10 * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
11 * @since 1.0.0
12 */
13 if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
14
15 if ( ! class_exists( 'Charitable_Session' ) ) :
16
17 /**
18 * Charitable_Session
19 *
20 * @since 1.0.0
21 */
22 class Charitable_Session {
23
24 /**
25 * The single instance of this class.
26 *
27 * @var Charitable_Session|null
28 * @access private
29 * @static
30 */
31 private static $instance = null;
32
33 /**
34 * Holds our session data
35 *
36 * @var array
37 * @access private
38 * @since 1.0.0
39 */
40 private $session;
41
42 /**
43 * Returns and/or create the single instance of this class.
44 *
45 * @return Charitable_Session
46 * @access public
47 * @since 1.2.0
48 */
49 public static function get_instance() {
50 if ( is_null( self::$instance ) ) {
51 self::$instance = new Charitable_Session();
52 }
53
54 return self::$instance;
55 }
56
57 /**
58 * Instantiate session object. Private constructor.
59 *
60 * @access private
61 * @since 1.0.0
62 */
63 private function __construct() {
64 if ( ! defined( 'WP_SESSION_COOKIE' ) )
65 define( 'WP_SESSION_COOKIE', 'charitable_session' );
66
67 if ( ! class_exists( 'Recursive_ArrayAccess' ) ) {
68 require_once( charitable()->get_path( 'includes' ) . 'libraries/wp-session/class-recursive-arrayaccess.php' );
69 }
70
71 if ( ! class_exists( 'WP_Session' ) ) {
72 require_once( charitable()->get_path( 'includes' ) . 'libraries/wp-session/class-wp-session.php' );
73 require_once( charitable()->get_path( 'includes' ) . 'libraries/wp-session/wp-session.php' );
74 }
75
76 /* Set the expiration length & variant of the session */
77 add_filter( 'wp_session_expiration', array( $this, 'set_session_length' ), 99999 );
78 add_filter( 'wp_session_expiration_variant', array( $this, 'set_session_expiration_variant_length' ), 99999 );
79
80 $this->session = WP_Session::get_instance();
81 }
82
83 /**
84 * Returns the session ID.
85 *
86 * @return string Session ID
87 * @access public
88 * @since 1.0.0
89 */
90 public function get_session_id() {
91 return $this->session->session_id;
92 }
93
94 /**
95 * Return a session variable.
96 *
97 * @param string $key
98 * @return mixed Session variable
99 * @access public
100 * @since 1.0.0
101 */
102 public function get( $key ) {
103 $key = sanitize_key( $key );
104 return isset( $this->session[ $key ] ) ? maybe_unserialize( $this->session[ $key ] ) : false;
105 }
106
107 /**
108 * Set a session variable.
109 *
110 * @param string $key
111 * @param mixed $value
112 * @return mixed The session variable value.
113 * @access public
114 * @since 1.0.0
115 */
116 public function set( $key, $value ) {
117 $key = sanitize_key( $key );
118
119 if ( is_array( $value ) ) {
120 $this->session[ $key ] = serialize( $value );
121 } else {
122 $this->session[ $key ] = $value;
123 }
124
125 return $this->session[ $key ];
126 }
127
128 /**
129 * Set the length of the cookie session to 24 hours.
130 *
131 * @return void
132 * @access public
133 * @since 1.0.0
134 */
135 public function set_session_length() {
136 if ( ! defined( 'DAY_IN_SECONDS' ) ) {
137 define( 'DAY_IN_SECONDS', 86400 );
138 }
139
140 return DAY_IN_SECONDS;
141 }
142
143 /**
144 * Set the cookie expiration variant time to 23 hours.
145 *
146 * @return void
147 * @access public
148 * @since 1.0.0
149 */
150 public function set_session_expiration_variant_length() {
151 if ( ! defined( 'HOUR_IN_SECONDS' ) ) {
152 define( 'HOUR_IN_SECONDS', 3600 );
153 }
154
155 return HOUR_IN_SECONDS * 23;
156 }
157
158 /**
159 * Add a donation to a campaign to the session.
160 *
161 * @param int $campaign_id
162 * @param int $amount
163 * @return void
164 * @access public
165 * @since 1.0.0
166 */
167 public function add_donation( $campaign_id, $amount ) {
168 $donations = $this->get( 'donations' );
169
170 $campaign_donation = isset( $donations[ $campaign_id ] ) ? $donations[ $campaign_id ] : array();
171 $campaign_donation[ 'amount' ] = floatval( $amount );
172
173 $donations[ $campaign_id ] = $campaign_donation;
174
175 $this->set( 'donations', $donations );
176 }
177
178 /**
179 * Remove a donation from the session.
180 *
181 * @return void
182 * @access public
183 * @since 1.0.0
184 */
185 public function remove_donation( $campaign_id ) {
186 $donations = $this->get( 'donations' );
187
188 if ( isset( $donations[ $campaign_id ] ) ) {
189 unset( $donations[ $campaign_id ] );
190 }
191
192 $this->set( 'donations', $donations );
193 }
194
195 /**
196 * Return the donation in session for a campaign.
197 *
198 * @param int $campaign_id
199 * @return false|array
200 * @access public
201 * @since 1.0.0
202 */
203 public function get_donation_by_campaign( $campaign_id ) {
204 $donations = $this->get( 'donations' );
205 return isset( $donations[ $campaign_id ] ) ? $donations[ $campaign_id ] : false;
206 }
207
208 /**
209 * Store the donation key in the session, to ensure the user can access their receipt.
210 *
211 * @param string $donation_key
212 * @return void
213 * @access public
214 * @since 1.1.2
215 */
216 public function add_donation_key( $donation_key ) {
217 $keys = $this->get( 'donation-keys' );
218
219 if ( ! $keys ) {
220 $keys = array();
221 }
222
223 $keys[] = $donation_key;
224
225 $this->set( 'donation-keys', $keys );
226 }
227
228 /**
229 * Checks whether the donation key is stored in the session.
230 *
231 * @param string $donation_key
232 * @return boolean
233 * @access public
234 * @since 1.0.0
235 */
236 public function has_donation_key( $donation_key ) {
237 $keys = $this->get( 'donation-keys' );
238
239 if ( ! $keys ) {
240 return false;
241 }
242
243 return in_array( $donation_key, $keys );
244 }
245
246 /**
247 * Add the all notices to the session.
248 *
249 * @return void
250 * @access public
251 * @since 1.0.0
252 */
253 public function add_notices() {
254 $this->set( 'notices', charitable_get_notices()->get_notices() );
255 }
256 }
257
258 endif;