llms-abstract-session-data.php 4.06 KB
Newer Older
cyrille's avatar
cyrille committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
<?php
/**
 * Base session data class
 *
 * @package LifterLMS/Abstracts/Classes
 *
 * @since 4.0.0
 * @version 4.0.0
 */

defined( 'ABSPATH' ) || exit;

/**
 * LLMS_Abstract_Session
 *
 * @since 4.0.0
 */
abstract class LLMS_Abstract_Session_Data {

	/**
	 * Session data
	 *
	 * @var array
	 */
	protected $data = array();

	/**
	 * Session ID
	 *
	 * The session ID is the logged-in user ID
	 * or a unique ID for an anonymous user.
	 *
	 * @var string
	 */
	protected $id = '';

	/**
	 * Determines if there's session data to be saved.
	 *
	 * If `true` no data needs to be saved, if `false` data
	 * needs to be saved.
	 *
	 * @var boolean
	 */
	protected $is_clean = true;

	/**
	 * Generate a session key for the current user/visitor.
	 *
	 * A logged-in user will use their WP_User ID while logged-out
	 * users will be assigned a random string.
	 *
	 * @since 4.0.0
	 *
	 * @return string
	 */
	protected function generate_id() {

		// Use the current user id if the user is logged in.
		if ( is_user_logged_in() ) {
			return strval( get_current_user_id() );
		}

		// Generate a random id.
		require_once ABSPATH . 'wp-includes/class-phpass.php';
		$hasher = new PasswordHash( 8, false );
		return md5( $hasher->get_random_bytes( 32 ) );

	}

	/**
	 * Retrieve session ID.
	 *
	 * @since 1.0.0
	 * @since 4.0.0 Moved from `LLMS_Sessions`, automatically generates an ID if it doesn't exist.
	 *
	 * @return string Session ID.
	 */
	public function get_id() {
		if ( empty( $this->id ) ) {
			$this->id = $this->generate_id();
		}
		return $this->id;
	}

	/**
	 * Retrieve a session variable.
	 *
	 * @since 1.0.0
	 * @since 3.37.7 Added the `$default` parameter that represents the default value
	 *               to return if the session variable requested doesn't exist.
	 * @since 4.0.0 Moved from `LLMS_Session`.
	 *
	 * @param string $key     The key of the session variable.
	 * @param mixed  $default Optional. The default value to return if no session variable is found with the provided key. Default `false`.
	 * @return mixed
	 */
	public function get( $key, $default = false ) {

		$key = sanitize_key( $key );
		return isset( $this->data[ $key ] ) ? maybe_unserialize( $this->data[ $key ] ) : $default;

	}

	/**
	 * Set a session variable.
	 *
	 * @since 1.0.0
	 * @since 4.0.0 Moved from `LLMS_Session`.
	 *
	 * @param string $key   The key of the session variable.
	 * @param mixed  $value The value of the session variable.
	 * @return mixed
	 */
	public function set( $key, $value ) {

		/**
		 * Using `isset()` allows us to explicitly save a value of `false`
		 * since the `get()` method will return the default value `false` making it look
		 * as if the value hasn't changed (when it actually has).
		 */
		if ( ! isset( $this->$key ) || $value !== $this->get( $key ) ) {
			$this->data[ sanitize_key( $key ) ] = maybe_serialize( $value );
			$this->is_clean                     = false;
		}

		return $this->get( $key );

	}

	/**
	 * Magic get
	 *
	 * @since 1.0.0
	 * @since 4.0.0 Moved from `LLMS_Session`.
	 *
	 * @param string $key The key of the session variable.
	 * @return mixed
	 */
	public function __get( $key ) {
		return $this->get( $key );
	}

	/**
	 * Magic set
	 *
	 * @since 1.0.0
	 * @since 4.0.0 Moved from `LLMS_Session`.
	 *
	 * @param string $key   The key of the session variable.
	 * @param string $value The value of the session variable.
	 * @return void
	 */
	public function __set( $key, $value ) {
		$this->set( $key, $value );
	}

	/**
	 * Magic isset
	 *
	 * @since 1.0.0
	 * @since 4.0.0 Use `sanitize_key()` (like other methods in this class) instead of `sanitize_title()`.
	 *
	 * @param string $key The key of the session variable.
	 * @return bool
	 */
	public function __isset( $key ) {
		return isset( $this->data[ sanitize_key( $key ) ] );
	}

	/**
	 * Magic unset
	 *
	 * @since 1.0.0
	 * @since 4.0.0 Use `sanitize_key()` when removing session var.
	 *
	 * @param string $key The key of the session variable.
	 * @return void
	 */
	public function __unset( $key ) {
		if ( isset( $this->$key ) ) {
			unset( $this->data[ sanitize_key( $key ) ] );
			$this->is_clean = false;
		}
	}

}