| @@ -2,12 +2,12 @@ | ||
| 2 | 2 | /** |
| 3 | 3 | * Responsible for managing user sessions. |
| 4 | 4 | * |
| 5 | 5 | * @package Charitable/Classes/Charitable Session |
| 6 | - * @copyright Copyright (c) 2018, Eric Daams | |
| 6 | + * @copyright Copyright (c) 2023, WP Charitable LLC | |
| 7 | 7 | * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License |
| 8 | 8 | * @since 1.0.0 |
| 9 | - * @version 1.5.4 | |
| 9 | + * @version 1.8.9.1 | |
| 10 | 10 | */ |
| 11 | 11 | |
| 12 | 12 | // Exit if accessed directly. |
| 13 | 13 | if ( ! defined( 'ABSPATH' ) ) { |
| @@ -32,8 +32,17 @@ | ||
| 32 | 32 | */ |
| 33 | 33 | private $session; |
| 34 | 34 | |
| 35 | 35 | /** |
| 36 | + * Whether to disable the cookie. | |
| 37 | + * | |
| 38 | + * @since 1.6.27 | |
| 39 | + * | |
| 40 | + * @var boolean | |
| 41 | + */ | |
| 42 | + private $disable_cookie; | |
| 43 | + | |
| 44 | + /** | |
| 36 | 45 | * Instantiate session object. |
| 37 | 46 | * |
| 38 | 47 | * @since 1.0.0 |
| 39 | 48 | * @since 1.5.4 Access changed to public. |
| @@ -38,14 +47,23 @@ | ||
| 38 | 47 | * @since 1.0.0 |
| 39 | 48 | * @since 1.5.4 Access changed to public. |
| 40 | 49 | */ |
| 41 | 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 | + | |
| 42 | 60 | if ( ! $this->should_start_session() ) { |
| 43 | 61 | return; |
| 44 | 62 | } |
| 45 | 63 | |
| 46 | 64 | if ( ! defined( 'WP_SESSION_COOKIE' ) ) { |
| 47 | - define( 'WP_SESSION_COOKIE', 'charitable_session' ); | |
| 65 | + define( 'WP_SESSION_COOKIE', 'charitable_session' ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedConstantFound -- This is a bundled library constant. Changing it would break existing functionality. | |
| 48 | 66 | } |
| 49 | 67 | |
| 50 | 68 | if ( ! class_exists( 'Recursive_ArrayAccess' ) ) { |
| 51 | 69 | require_once( charitable()->get_path( 'includes' ) . 'libraries/wp-session/class-recursive-arrayaccess.php' ); |
| @@ -81,9 +99,9 @@ | ||
| 81 | 99 | public function init() { |
| 82 | 100 | $this->session = WP_Session::get_instance(); |
| 83 | 101 | |
| 84 | 102 | /* We're missing a Session ID, so we'll need to queue up our session scripts. */ |
| 85 | - if ( ! $this->has_session_id() ) { | |
| 103 | + if ( ! $this->has_session_id() || $this->disable_cookie ) { | |
| 86 | 104 | add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ), 1 ); |
| 87 | 105 | } |
| 88 | 106 | |
| 89 | 107 | return $this->session; |
| @@ -96,21 +114,17 @@ | ||
| 96 | 114 | * |
| 97 | 115 | * @return void |
| 98 | 116 | */ |
| 99 | 117 | 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 | 118 | |
| 119 | + $min = charitable_get_min_suffix(); | |
| 120 | + $version = charitable()->get_version(); | |
| 121 | + | |
| 108 | 122 | $assets_dir = charitable()->get_path( 'assets', false ); |
| 109 | 123 | |
| 110 | 124 | wp_register_script( |
| 111 | 125 | 'js-cookie', |
| 112 | - $assets_dir . 'js/libraries/js-cookie' . $suffix . '.js', | |
| 126 | + $assets_dir . 'js/libraries/js-cookie' . $min . '.js', | |
| 113 | 127 | array(), |
| 114 | 128 | '2.1.4', |
| 115 | 129 | false |
| 116 | 130 | ); |
| @@ -116,25 +130,30 @@ | ||
| 116 | 130 | ); |
| 117 | 131 | |
| 118 | 132 | wp_enqueue_script( |
| 119 | 133 | 'charitable-sessions', |
| 120 | - $assets_dir . 'js/charitable-session'. $suffix . '.js', | |
| 134 | + $assets_dir . 'js/charitable-session' . $min . '.js', | |
| 121 | 135 | array( 'js-cookie' ), |
| 122 | 136 | $version, |
| 123 | 137 | false |
| 124 | 138 | ); |
| 125 | 139 | |
| 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 | - ) ); | |
| 140 | + wp_localize_script( | |
| 141 | + 'charitable-sessions', | |
| 142 | + 'CHARITABLE_SESSION', | |
| 143 | + array( | |
| 144 | + 'ajaxurl' => admin_url( 'admin-ajax.php' ), | |
| 145 | + 'id' => $this->get_session_id(), | |
| 146 | + 'cookie_name' => WP_SESSION_COOKIE, | |
| 147 | + 'expiration' => $this->set_session_length(), | |
| 148 | + 'expiration_variant' => $this->set_session_expiration_variant_length(), | |
| 149 | + 'secure' => (bool) apply_filters( 'wp_session_cookie_secure', false ), // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- This is a bundled library hook. Changing it would break existing functionality. | |
| 150 | + 'cookie_path' => COOKIEPATH, | |
| 151 | + 'cookie_domain' => COOKIE_DOMAIN, | |
| 152 | + 'generated_id' => WP_Session_Utils::generate_id(), | |
| 153 | + 'disable_cookie' => $this->disable_cookie, | |
| 154 | + ) | |
| 155 | + ); | |
| 137 | 156 | } |
| 138 | 157 | |
| 139 | 158 | /** |
| 140 | 159 | * Returns and/or create the single instance of this class. |
| @@ -251,19 +270,33 @@ | ||
| 251 | 270 | /** |
| 252 | 271 | * Add a donation to a campaign to the session. |
| 253 | 272 | * |
| 254 | 273 | * @since 1.0.0 |
| 274 | + * @since 1.6.25 Added $period parameter. | |
| 255 | 275 | * |
| 256 | - * @param int $campaign_id Campaign ID. | |
| 257 | - * @param int $amount Donation amount. | |
| 276 | + * @param int $campaign_id Campaign ID. | |
| 277 | + * @param int $amount Donation amount. | |
| 278 | + * @param string $period Set the donation period. Defaults to once. | |
| 258 | 279 | * @return void |
| 259 | 280 | */ |
| 260 | - public function add_donation( $campaign_id, $amount ) { | |
| 281 | + public function add_donation( $campaign_id, $amount, $period = 'once' ) { | |
| 261 | 282 | $donations = $this->get( 'donations' ); |
| 262 | 283 | |
| 263 | - $campaign_donation = isset( $donations[ $campaign_id ] ) ? $donations[ $campaign_id ] : array(); | |
| 264 | - $campaign_donation['amount'] = floatval( $amount ); | |
| 284 | + /* | |
| 285 | + * get() returns false when the key is not in the session, which is the normal state for the | |
| 286 | + * first donation of a fresh session. Assigning $donations[ $campaign_id ] to that false | |
| 287 | + * relied on PHP silently converting it to an array - deprecated in PHP 8.1 and a fatal | |
| 288 | + * TypeError in PHP 9, on the donation path. add_donation_key() below already guards exactly | |
| 289 | + * this way; this method was simply missing it. | |
| 290 | + */ | |
| 291 | + if ( ! is_array( $donations ) ) { | |
| 292 | + $donations = array(); | |
| 293 | + } | |
| 265 | 294 | |
| 295 | + $campaign_donation = isset( $donations[ $campaign_id ] ) ? $donations[ $campaign_id ] : array(); | |
| 296 | + $campaign_donation['amount'] = floatval( charitable_get_currency_helper()->cast_to_decimal_format( $amount ) ); | |
| 297 | + $campaign_donation['donation_period'] = $period; | |
| 298 | + | |
| 266 | 299 | $donations[ $campaign_id ] = $campaign_donation; |
| 267 | 300 | |
| 268 | 301 | $this->set( 'donations', $donations ); |
| 269 | 302 | } |
| @@ -377,9 +410,13 @@ | ||
| 377 | 410 | * |
| 378 | 411 | * @return string Session ID |
| 379 | 412 | */ |
| 380 | 413 | public function get_session_id() { |
| 381 | - return $this->session->session_id; | |
| 414 | + if ( isset( $this->session->session_id ) ) { | |
| 415 | + return $this->session->session_id; | |
| 416 | + } else { | |
| 417 | + return false; | |
| 418 | + } | |
| 382 | 419 | } |
| 383 | 420 | |
| 384 | 421 | /** |
| 385 | 422 | * Determines if we should start sessions |
| @@ -391,14 +428,13 @@ | ||
| 391 | 428 | public function should_start_session() { |
| 392 | 429 | $start_session = true; |
| 393 | 430 | |
| 394 | 431 | if ( ! empty( $_SERVER['REQUEST_URI'] ) ) { |
| 395 | - | |
| 396 | - $blacklist = $this->get_blacklist(); | |
| 397 | - $uri = ltrim( $_SERVER['REQUEST_URI'], '/' ); | |
| 432 | + $blocklist = $this->get_blocklist(); | |
| 433 | + $uri = ltrim( $_SERVER['REQUEST_URI'], '/' ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized | |
| 398 | 434 | $uri = untrailingslashit( $uri ); |
| 399 | 435 | |
| 400 | - if ( in_array( $uri, $blacklist ) ) { | |
| 436 | + if ( in_array( $uri, $blocklist ) ) { | |
| 401 | 437 | $start_session = false; |
| 402 | 438 | } |
| 403 | 439 | |
| 404 | 440 | if ( false !== strpos( $uri, 'feed=' ) ) { |
| @@ -416,43 +452,56 @@ | ||
| 416 | 452 | return apply_filters( 'charitable_start_session', $start_session ); |
| 417 | 453 | } |
| 418 | 454 | |
| 419 | 455 | /** |
| 420 | - * Retrieve the URI blacklist | |
| 456 | + * Retrieve the URI blocklist. | |
| 421 | 457 | * |
| 422 | - * These are the URIs where we never start sessions | |
| 458 | + * These are the URIs where we never start sessions. | |
| 423 | 459 | * |
| 424 | 460 | * @since 1.4.17 |
| 425 | 461 | * |
| 426 | 462 | * @return array |
| 427 | 463 | */ |
| 428 | - public function get_blacklist() { | |
| 464 | + public function get_blocklist() { | |
| 429 | 465 | /** |
| 430 | - * Filter the blacklist of URIs where sessions should not be started. | |
| 466 | + * Filter the blocklist of URIs where sessions should not be started. | |
| 431 | 467 | * |
| 432 | 468 | * @since 1.4.17 |
| 433 | 469 | * |
| 434 | - * @param string[] $blacklist Array of URIs. | |
| 470 | + * @param string[] $blocklist Array of URIs. | |
| 435 | 471 | */ |
| 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 | - ) ); | |
| 472 | + $blocklist = apply_filters( | |
| 473 | + 'charitable_session_start_uri_blocklist', | |
| 474 | + array( | |
| 475 | + 'feed', | |
| 476 | + 'feed/rss', | |
| 477 | + 'feed/rss2', | |
| 478 | + 'feed/rdf', | |
| 479 | + 'feed/atom', | |
| 480 | + 'comments/feed', | |
| 481 | + ) | |
| 482 | + ); | |
| 444 | 483 | |
| 484 | + /** | |
| 485 | + * Deprecated filter retained for backwards compatibility. | |
| 486 | + * | |
| 487 | + * @since 1.4.17 | |
| 488 | + * @since 1.6.42 Deprecated in favour of charitable_session_start_uri_blocklist | |
| 489 | + * | |
| 490 | + * @param string[] $blocklist Array of URIs. | |
| 491 | + */ | |
| 492 | + $blocklist = apply_filters( 'charitable_session_start_uri_blacklist', $blocklist ); | |
| 493 | + | |
| 445 | 494 | /* Look to see if WordPress is in a sub folder or this is a network site that uses sub folders */ |
| 446 | 495 | $folder = str_replace( network_home_url(), '', get_site_url() ); |
| 447 | 496 | |
| 448 | 497 | if ( ! empty( $folder ) ) { |
| 449 | - foreach ( $blacklist as $path ) { | |
| 450 | - $blacklist[] = $folder . '/' . $path; | |
| 498 | + foreach ( $blocklist as $path ) { | |
| 499 | + $blocklist[] = $folder . '/' . $path; | |
| 451 | 500 | } |
| 452 | 501 | } |
| 453 | 502 | |
| 454 | - return $blacklist; | |
| 503 | + return $blocklist; | |
| 455 | 504 | } |
| 456 | 505 | } |
| 457 | 506 | |
| 458 | 507 | endif; |