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