PluginProbe
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) / 1.6.9
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) v1.6.9
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.6.9, at includes/public/class-charitable-session.php

459 lines 10.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Responsible for managing user sessions.
4 *
5 * @package Charitable/Classes/Charitable Session
6 * @copyright Copyright (c) 2018, Eric Daams
7 * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
8 * @since 1.0.0
9 * @version 1.5.4
10 */
11
12 // Exit if accessed directly.
13 if ( ! defined( 'ABSPATH' ) ) {
14 exit;
15 }
16
17 if ( ! class_exists( 'Charitable_Session' ) ) :
18
19 /**
20 * Charitable_Session
21 *
22 * @since 1.0.0
23 */
24 class Charitable_Session {
25
26 /**
27 * Holds our session data
28 *
29 * @since 1.0.0
30 *
31 * @var WP_Session
32 */
33 private $session;
34
35 /**
36 * Instantiate session object.
37 *
38 * @since 1.0.0
39 * @since 1.5.4 Access changed to public.
40 */
41 public function __construct() {
42 if ( ! $this->should_start_session() ) {
43 return;
44 }
45
46 if ( ! defined( 'WP_SESSION_COOKIE' ) ) {
47 define( 'WP_SESSION_COOKIE', 'charitable_session' );
48 }
49
50 if ( ! class_exists( 'Recursive_ArrayAccess' ) ) {
51 require_once( charitable()->get_path( 'includes' ) . 'libraries/wp-session/class-recursive-arrayaccess.php' );
52 }
53
54 if ( ! class_exists( 'WP_Session_Utils' ) ) {
55 require_once( charitable()->get_path( 'includes' ) . 'libraries/wp-session/class-wp-session-utils.php' );
56 }
57
58 if ( defined( 'WP_CLI' ) && WP_CLI ) {
59 require_once( charitable()->get_path( 'includes' ) . 'libraries/wp-session/wp-cli.php' );
60 }
61
62 if ( ! class_exists( 'WP_Session' ) ) {
63 require_once( charitable()->get_path( 'includes' ) . 'libraries/wp-session/class-wp-session.php' );
64 require_once( charitable()->get_path( 'includes' ) . 'libraries/wp-session/wp-session.php' );
65 }
66
67 /* Set the expiration length & variant of the session */
68 add_filter( 'wp_session_expiration', array( $this, 'set_session_length' ), 99999 );
69 add_filter( 'wp_session_expiration_variant', array( $this, 'set_session_expiration_variant_length' ), 99999 );
70
71 $this->init();
72 }
73
74 /**
75 * Create the session.
76 *
77 * @since 1.4.17
78 *
79 * @return WP_Session
80 */
81 public function init() {
82 $this->session = WP_Session::get_instance();
83
84 /* We're missing a Session ID, so we'll need to queue up our session scripts. */
85 if ( ! $this->has_session_id() ) {
86 add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ), 1 );
87 }
88
89 return $this->session;
90 }
91
92 /**
93 * Set up scripts to create & manage cookies client-side.
94 *
95 * @since 1.5.0
96 *
97 * @return void
98 */
99 public function enqueue_scripts() {
100 if ( ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ) {
101 $suffix = '';
102 $version = '';
103 } else {
104 $suffix = '.min';
105 $version = charitable()->get_version();
106 }
107
108 $assets_dir = charitable()->get_path( 'assets', false );
109
110 wp_register_script(
111 'js-cookie',
112 $assets_dir . 'js/libraries/js-cookie' . $suffix . '.js',
113 array(),
114 '2.1.4',
115 false
116 );
117
118 wp_enqueue_script(
119 'charitable-sessions',
120 $assets_dir . 'js/charitable-session'. $suffix . '.js',
121 array( 'js-cookie' ),
122 $version,
123 false
124 );
125
126 wp_localize_script( 'charitable-sessions', 'CHARITABLE_SESSION', array(
127 'ajaxurl' => admin_url( 'admin-ajax.php' ),
128 'id' => $this->get_session_id(),
129 'cookie_name' => WP_SESSION_COOKIE,
130 'expiration' => $this->set_session_length(),
131 'expiration_variant' => $this->set_session_expiration_variant_length(),
132 'secure' => (bool) apply_filters( 'wp_session_cookie_secure', false ),
133 'cookie_path' => COOKIEPATH,
134 'cookie_domain' => COOKIE_DOMAIN,
135 'generated_id' => WP_Session_Utils::generate_id(),
136 ) );
137 }
138
139 /**
140 * Returns and/or create the single instance of this class.
141 *
142 * @since 1.2.0
143 *
144 * @return Charitable_Session
145 */
146 public static function get_instance() {
147 return charitable()->registry()->get( 'session' );
148 }
149
150 /**
151 * Checks whether the current request has a session ID.
152 *
153 * @since 1.5.0
154 *
155 * @return boolean
156 */
157 public function has_session_id() {
158 return ! empty( $this->session->session_id );
159 }
160
161 /**
162 * Return a session variable.
163 *
164 * @since 1.0.0
165 *
166 * @param string $key Session variable key.
167 * @return mixed Session variable
168 */
169 public function get( $key ) {
170 $key = sanitize_key( $key );
171 return isset( $this->session[ $key ] ) ? maybe_unserialize( $this->session[ $key ] ) : false;
172 }
173
174 /**
175 * Set a session variable.
176 *
177 * @since 1.0.0
178 *
179 * @param string $key Session variable key.
180 * @param mixed $value The value of the session variable.
181 * @return mixed The session variable value.
182 */
183 public function set( $key, $value ) {
184 $key = sanitize_key( $key );
185
186 if ( is_array( $value ) ) {
187 $this->session[ $key ] = serialize( $value );
188 } else {
189 $this->session[ $key ] = $value;
190 }
191
192 return $this->session[ $key ];
193 }
194
195 /**
196 * Remove a session variable.
197 *
198 * @since 1.5.0
199 *
200 * @param string $key Session variable key.
201 * @return mixed The session variable value.
202 */
203 public function remove( $key ) {
204 unset( $this->session[ $key ] );
205 }
206
207 /**
208 * Set the length of the cookie session to 24 hours.
209 *
210 * @since 1.0.0
211 *
212 * @return int
213 */
214 public function set_session_length() {
215 if ( ! defined( 'DAY_IN_SECONDS' ) ) {
216 define( 'DAY_IN_SECONDS', 86400 );
217 }
218
219 /**
220 * Filter the length of the session.
221 *
222 * @since 1.0.0
223 *
224 * @param int $seconds Defaults to 86400 — one day.
225 */
226 return apply_filters( 'charitable_session_length', DAY_IN_SECONDS );
227 }
228
229 /**
230 * Set the cookie expiration variant time to 23 hours.
231 *
232 * @since 1.0.0
233 *
234 * @return int
235 */
236 public function set_session_expiration_variant_length() {
237 if ( ! defined( 'HOUR_IN_SECONDS' ) ) {
238 define( 'HOUR_IN_SECONDS', 3600 );
239 }
240
241 /**
242 * Filter the variation length of the session.
243 *
244 * @since 1.0.0
245 *
246 * @param int $seconds Defaults to 82800 — 23 hours.
247 */
248 return apply_filters( 'charitable_session_expiration_variant_length', HOUR_IN_SECONDS * 23 );
249 }
250
251 /**
252 * Add a donation to a campaign to the session.
253 *
254 * @since 1.0.0
255 *
256 * @param int $campaign_id Campaign ID.
257 * @param int $amount Donation amount.
258 * @return void
259 */
260 public function add_donation( $campaign_id, $amount ) {
261 $donations = $this->get( 'donations' );
262
263 $campaign_donation = isset( $donations[ $campaign_id ] ) ? $donations[ $campaign_id ] : array();
264 $campaign_donation['amount'] = floatval( $amount );
265
266 $donations[ $campaign_id ] = $campaign_donation;
267
268 $this->set( 'donations', $donations );
269 }
270
271 /**
272 * Remove a donation from the session.
273 *
274 * @since 1.0.0
275 *
276 * @param int $campaign_id Campaign ID.
277 * @return void
278 */
279 public function remove_donation( $campaign_id ) {
280 $donations = $this->get( 'donations' );
281
282 if ( isset( $donations[ $campaign_id ] ) ) {
283 unset( $donations[ $campaign_id ] );
284 }
285
286 $this->set( 'donations', $donations );
287 }
288
289 /**
290 * Return the donation in session for a campaign.
291 *
292 * @since 1.0.0
293 *
294 * @param int $campaign_id The campaign ID.
295 * @return false|array
296 */
297 public function get_donation_by_campaign( $campaign_id ) {
298 $donations = $this->get( 'donations' );
299 return isset( $donations[ $campaign_id ] ) ? $donations[ $campaign_id ] : false;
300 }
301
302 /**
303 * Store the donation key in the session, to ensure the user can access their receipt.
304 *
305 * @since 1.1.2
306 *
307 * @param string $donation_key The transaction key for the donation.
308 * @return void
309 */
310 public function add_donation_key( $donation_key ) {
311 $keys = $this->get( 'donation-keys' );
312
313 if ( ! $keys ) {
314 $keys = array();
315 }
316
317 $keys[] = $donation_key;
318
319 $this->set( 'donation-keys', $keys );
320 }
321
322 /**
323 * Checks whether the donation key is stored in the session.
324 *
325 * @since 1.0.0
326 *
327 * @param string $donation_key The transaction key for the donation.
328 * @return boolean
329 */
330 public function has_donation_key( $donation_key ) {
331 $keys = $this->get( 'donation-keys' );
332
333 if ( ! $keys ) {
334 return false;
335 }
336
337 return in_array( $donation_key, $keys );
338 }
339
340 /**
341 * Add the all notices to the session.
342 *
343 * @since 1.0.0
344 *
345 * @return void
346 */
347 public function add_notices() {
348 $this->set( 'notices', charitable_get_notices()->get_notices() );
349 }
350
351 /**
352 * Return any notices set in the session.
353 *
354 * @since 1.4.0
355 *
356 * @return array Session variable
357 */
358 public function get_notices() {
359 $notices = $this->get( 'notices' );
360
361 if ( $notices ) {
362 return $notices;
363 }
364
365 return array(
366 'error' => array(),
367 'warning' => array(),
368 'success' => array(),
369 'info' => array(),
370 );
371 }
372
373 /**
374 * Returns the session ID.
375 *
376 * @since 1.3.5
377 *
378 * @return string Session ID
379 */
380 public function get_session_id() {
381 return $this->session->session_id;
382 }
383
384 /**
385 * Determines if we should start sessions
386 *
387 * @since 1.4.17
388 *
389 * @return boolean
390 */
391 public function should_start_session() {
392 $start_session = true;
393
394 if ( ! empty( $_SERVER['REQUEST_URI'] ) ) {
395
396 $blacklist = $this->get_blacklist();
397 $uri = ltrim( $_SERVER['REQUEST_URI'], '/' );
398 $uri = untrailingslashit( $uri );
399
400 if ( in_array( $uri, $blacklist ) ) {
401 $start_session = false;
402 }
403
404 if ( false !== strpos( $uri, 'feed=' ) ) {
405 $start_session = false;
406 }
407 }
408
409 /**
410 * Filter whether the session should be started for the current request.
411 *
412 * @since 1.4.17
413 *
414 * @param boolean $start_session Whether to start the session.
415 */
416 return apply_filters( 'charitable_start_session', $start_session );
417 }
418
419 /**
420 * Retrieve the URI blacklist
421 *
422 * These are the URIs where we never start sessions
423 *
424 * @since 1.4.17
425 *
426 * @return array
427 */
428 public function get_blacklist() {
429 /**
430 * Filter the blacklist of URIs where sessions should not be started.
431 *
432 * @since 1.4.17
433 *
434 * @param string[] $blacklist Array of URIs.
435 */
436 $blacklist = apply_filters( 'charitable_session_start_uri_blacklist', array(
437 'feed',
438 'feed/rss',
439 'feed/rss2',
440 'feed/rdf',
441 'feed/atom',
442 'comments/feed',
443 ) );
444
445 /* Look to see if WordPress is in a sub folder or this is a network site that uses sub folders */
446 $folder = str_replace( network_home_url(), '', get_site_url() );
447
448 if ( ! empty( $folder ) ) {
449 foreach ( $blacklist as $path ) {
450 $blacklist[] = $folder . '/' . $path;
451 }
452 }
453
454 return $blacklist;
455 }
456 }
457
458 endif;
459