Easy Property Listings 3.5.15 Code Reference
  • Package
  • Class
  • Tree
  • Todo
  • Hook Reference

Packages

  • EPL
    • Admin
      • Actions
      • Classes
        • ContactsTable
        • ReportsGraph
        • Welcome
      • Contacts
      • ContactsActions
      • ContactsFunctions
      • Elements
      • Functions
      • Help
      • HelpSingle
      • Menus
      • MenusAddons
      • MenusExtensions
      • MenusLicenses
      • Plugins
      • Reports
      • ReportsGraphing
      • User
    • Assets
      • ScriptsStyles
      • SVG
    • Classes
      • AuthorLoader
      • AuthorMeta
      • Contact
      • Cron
      • CustomPostType
      • Forms
      • License
      • ListingAdvanced
      • ListingElements
      • MetaboxesCustomFields
      • Pagination
      • PropertyMeta
      • RenderFields
      • RestAPI
      • Search
      • Session
      • Updater
    • Compatibility
      • Extensions
      • Functions
      • Shortcodes
    • Functions
      • Actions
      • ConditionalTags
      • ErrorTracking
      • Formatting
      • Front
      • Global
      • Install
      • Pagination
      • Settings
      • Templates
    • Hooks
      • EnergyCertificate
      • ExternalLinks
      • FloorPlan
      • Map
      • ReadMore
      • WebLink
    • Meta
      • InitCustomFields
      • Meta
    • PostTypes
      • Business
      • Commercial
      • CommercialLand
      • Contact
      • Functions
      • Land
      • Property
      • Rental
      • Rural
    • Shortcode
      • CommercialListingSearch
      • ContactForm
      • Listing
      • ListingAdvanced
      • ListingAuction
      • ListingCategory
      • ListingFeature
      • ListingLocation
      • ListingMetaDoc
      • ListingOpen
      • ListingResults
      • ListingSearch
      • Map
    • Taxonomy
      • BusinessCategories
      • ContactTag
      • Features
      • Location
    • Templates
      • Themes
        • iThemes
        • iThemesBuilder
    • Widget
      • Admin
        • Dashboard
      • Classes
        • Author
        • ContactCapture
        • Gallery
        • Listing
        • Search
      • Functions
  • None
  • WordPress
    • Session

Classes

  • EPL_Advanced_Shortcode_Listing
  • EPL_Author
  • EPL_Author_Loader
  • EPL_Author_Meta
  • EPL_Contact
  • EPL_Contact_Reports_Table
  • EPL_CPT
  • EPL_Cron
  • EPL_FORM_BUILDER
  • EPL_Graph
  • EPL_License
  • EPL_Listing_Elements
  • EPL_METABOX
  • EPL_Pagination_Call
  • EPL_Property_Meta
  • EPL_Render_Fields
  • EPL_Rest_API
  • EPL_SEARCH
  • EPL_Search_Fields
  • EPL_Session
  • EPL_SL_Plugin_Updater
  • EPL_Welcome
  • EPL_Widget_Author
  • EPL_Widget_Contact_Capture
  • EPL_Widget_Property_Gallery
  • EPL_Widget_Property_Search
  • EPL_Widget_Recent_Property

Functions

  • EPL
  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 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 
<?php
/**
 * EPL Session
 *
 * This is a wrapper class for WP_Session / PHP $_SESSION
 *
 * @package     EPL
 * @subpackage  Classes/Session
 * @copyright   Copyright (c) 2020, Merv Barrett
 * @license     http://opensource.org/licenses/gpl-2.0.php GNU Public License
 * @since       3.4.15
 */

// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

/**
 * EPL_Session Class
 *
 * @since 3.4.15
 */
class EPL_Session {

    /**
     * Holds our session data
     *
     * @var array
     * @access private
     * @since 3.4.15
     */
    private $session;


    /**
     * Whether to use PHP $_SESSION or WP_Session
     *
     * @var bool
     * @access private
     * @since 3.4.15
     */
    private $use_php_sessions = false;

    /**
     * Session index prefix
     *
     * @var string
     * @access private
     * @since 3.4.15
     */
    private $prefix = '';


    /**
     * Get things started
     *
     * Defines our WP_Session constants, includes the necessary libraries and
     * retrieves the WP Session instance
     *
     * @since 3.4.15
     */
    public function __construct() {

        $this->use_php_sessions = $this->use_php_sessions();

        if ( $this->use_php_sessions ) {

            if ( is_multisite() ) {

                $this->prefix = '_' . get_current_blog_id();

            }

            // Use PHP SESSION (must be enabled via the EPL_USE_PHP_SESSIONS constant)
            add_action( 'init', array( $this, 'maybe_start_session' ), -2 );

        } else {

            if ( ! $this->should_start_session() ) {
                return;
            }

            // Use WP_Session (default)
            if ( ! defined( 'WP_SESSION_COOKIE' ) ) {
                define( 'WP_SESSION_COOKIE', 'epl_wp_session' );
            }

            if ( ! class_exists( 'Recursive_ArrayAccess' ) ) {

                require_once EPL_PATH_LIB . 'includes/libraries/class-recursive-arrayaccess.php';
            }

            if ( ! class_exists( 'WP_Session' ) ) {
                require_once EPL_PATH_LIB . 'includes/libraries/class-wp-session.php';
                require_once EPL_PATH_LIB . 'includes/libraries/wp-session.php';
            }

            add_filter( 'wp_session_expiration_variant', array( $this, 'set_expiration_variant_time' ), 99999 );
            add_filter( 'wp_session_expiration', array( $this, 'set_expiration_time' ), 99999 );

        }

        if ( empty( $this->session ) && ! $this->use_php_sessions ) {
            add_action( 'plugins_loaded', array( $this, 'init' ), -1 );
        } else {
            add_action( 'init', array( $this, 'init' ), -1 );
        }

    }

    /**
     * Setup the WP_Session instance
     *
     * @since 3.4.15
     * @return void
     */
    public function init() {

        if ( $this->use_php_sessions ) {
            $this->session = isset( $_SESSION[ 'epl' . $this->prefix ] ) && is_array( $_SESSION[ 'epl' . $this->prefix ] ) ? $_SESSION[ 'epl' . $this->prefix ] : array();
        } else {
            $this->session = WP_Session::get_instance();
        }

        return $this->session;
    }


    /**
     * Retrieve session ID
     *
     * @since 1.6
     * @return string Session ID
     */
    public function get_id() {
        return $this->session->session_id;
    }


    /**
     * Retrieve a session variable
     *
     * @since 3.4.15
     * @param string $key Session key
     * @return mixed Session variable
     */
    public function get( $key ) {

        $key    = sanitize_key( $key );
        $return = false;

        if ( isset( $this->session[ $key ] ) && ! empty( $this->session[ $key ] ) ) {

            preg_match( '/[oO]\s*:\s*\d+\s*:\s*"\s*(?!(?i)(stdClass))/', $this->session[ $key ], $matches );
            if ( ! empty( $matches ) ) {
                $this->set( $key, null );
                return false;
            }

            if ( is_numeric( $this->session[ $key ] ) ) {
                $return = $this->session[ $key ];
            } else {

                $maybe_json = json_decode( $this->session[ $key ] );

                // Since json_last_error is PHP 5.3+, we have to rely on a `null` value for failing to parse JSON.
                if ( is_null( $maybe_json ) ) {
                    $is_serialized = is_serialized( $this->session[ $key ] );
                    if ( $is_serialized ) {
                        $value = @unserialize( $this->session[ $key ] );
                        $this->set( $key, (array) $value );
                        $return = $value;
                    } else {
                        $return = $this->session[ $key ];
                    }
                } else {
                    $return = json_decode( $this->session[ $key ], true );
                }
            }
        }

        return $return;
    }

    /**
     * Set a session variable
     *
     * @since 3.4.15
     *
     * @param string           $key Session key
     * @param int|string|array $value Session variable
     * @return mixed Session variable
     */
    public function set( $key, $value ) {

        $key = sanitize_key( $key );

        if ( is_array( $value ) ) {
            $this->session[ $key ] = wp_json_encode( $value );
        } else {
            $this->session[ $key ] = esc_attr( $value );
        }

        if ( $this->use_php_sessions ) {

            $_SESSION[ 'epl' . $this->prefix ] = $this->session;
        }

        return $this->session[ $key ];
    }

    /**
     * Force the cookie expiration variant time to 23 hours
     *
     * @since 3.4.15
     * @param int $exp Default expiration (1 hour)
     * @return int
     */
    public function set_expiration_variant_time( $exp ) {
        return ( 30 * 60 * 23 );
    }

    /**
     * Force the cookie expiration time to 24 hours
     *
     * @since 3.4.15
     * @param int $exp Default expiration (1 hour)
     * @return int Cookie expiration time
     */
    public function set_expiration_time( $exp ) {
        return ( 30 * 60 * 24 );
    }

    /**
     * Starts a new session if one hasn't started yet.
     *
     * @return boolean
     * Checks to see if the server supports PHP sessions
     * or if the EPL_USE_PHP_SESSIONS constant is defined
     *
     * @since 3.4.15
     * @author Daniel J Griffiths
     * @return boolean $ret True if we are using PHP sessions, false otherwise
     */
    public function use_php_sessions() {

        $ret = false;

        // If the database variable is already set, no need to run autodetection
        $epl_use_php_sessions = (bool) get_option( 'epl_use_php_sessions' );

        if ( ! $epl_use_php_sessions ) {

            // Attempt to detect if the server supports PHP sessions
            if ( function_exists( 'session_start' ) ) {

                $this->set( 'epl_use_php_sessions', 1 );

                if ( $this->get( 'epl_use_php_sessions' ) ) {

                    $ret = true;

                    // Set the database option
                    update_option( 'epl_use_php_sessions', true );

                }
            }
        } else {
            $ret = $epl_use_php_sessions;
        }

        // Enable or disable PHP Sessions based on the EPL_USE_PHP_SESSIONS constant
        if ( defined( 'EPL_USE_PHP_SESSIONS' ) && EPL_USE_PHP_SESSIONS ) {
            $ret = true;
        } elseif ( defined( 'EPL_USE_PHP_SESSIONS' ) && ! EPL_USE_PHP_SESSIONS ) {
            $ret = false;
        }

        return (bool) apply_filters( 'epl_use_php_sessions', $ret );
    }

    /**
     * Determines if we should start sessions
     *
     * @since  2.5.11
     * @return bool
     */
    public function should_start_session() {

        $start_session = true;

        if ( ! empty( $_SERVER['REQUEST_URI'] ) ) {

            $blacklist = $this->get_blacklist();
            $uri       = ltrim( $_SERVER['REQUEST_URI'], '/' );
            $uri       = untrailingslashit( $uri );

            if ( in_array( $uri, $blacklist ) ) {
                $start_session = false;
            }

            if ( false !== strpos( $uri, 'feed=' ) ) {
                $start_session = false;
            }

            if ( false !== strpos( $uri, 'wp_scrape_key' ) ) {
                // Starting sessions while saving the file editor can break the save process, so don't start
                $start_session = false;
            }
        }

        return apply_filters( 'epl_start_session', $start_session );

    }

    /**
     * Retrieve the URI blacklist
     *
     * These are the URIs where we never start sessions
     *
     * @since  2.5.11
     * @return array
     */
    public function get_blacklist() {

        $blacklist = apply_filters(
            'epl_session_start_uri_blacklist', array(
                'feed',
                'feed/rss',
                'feed/rss2',
                'feed/rdf',
                'feed/atom',
                'comments/feed',
            )
        );

        // Look to see if WordPress is in a sub folder or this is a network site that uses sub folders
        $folder = str_replace( network_home_url(), '', get_site_url() );

        if ( ! empty( $folder ) ) {
            foreach ( $blacklist as $path ) {
                $blacklist[] = $folder . '/' . $path;
            }
        }

        return $blacklist;
    }

    /**
     * Starts a new session if one hasn't started yet.
     */
    public function maybe_start_session() {

        if ( ! $this->should_start_session() ) {
            return;
        }

        if ( ! session_id() && ! headers_sent() ) {
            session_start();
        }
    }

}
Easy Property Listings 3.5.15 Code Reference API documentation generated by ApiGen