WordPress.org

Plugin Directory

Changeset 635249


Ignore:
Timestamp:
12/07/12 06:30:27 (17 months ago)
Author:
ericmann
Message:

Update to version 1.1.

Location:
wp-session-manager
Files:
7 added
4 edited

Legend:

Unmodified
Added
Removed
  • wp-session-manager/trunk/class-wp-session.php

    r633588 r635249  
    33 * WordPress session managment. 
    44 * 
    5  * Standardizes WordPress session data and uses either database transients or in-memory caching 
     5 * Standardizes WordPress session data using database-backed options for storage. 
    66 * for storing user session information. 
    77 * 
     
    1717 * @since   3.6.0 
    1818 */ 
    19 class WP_Session implements ArrayAccess, Iterator, Countable { 
    20     /** 
    21      * Internal data collection. 
    22      * 
    23      * @var array 
    24      */ 
    25     private $container; 
    26  
     19final class WP_Session extends Recursive_ArrayAccess implements Iterator, Countable { 
    2720    /** 
    2821     * ID of the current session. 
     
    3326 
    3427    /** 
    35      * Time in seconds until session data expired. 
     28     * Unix timestamp when session expires. 
    3629     * 
    3730     * @var int 
    3831     */ 
    39     private $cache_expire; 
     32    private $expires; 
     33 
     34    /** 
     35     * Unix timestamp indicating when the expiration time needs to be reset. 
     36     * 
     37     * @var int 
     38     */ 
     39    private $exp_variant; 
    4040 
    4141    /** 
     
    6969     * @uses apply_filters Calls `wp_session_expiration` to determine how long until sessions expire. 
    7070     */ 
    71     private function __construct() { 
     71    protected function __construct() { 
    7272        if ( isset( $_COOKIE[WP_SESSION_COOKIE] ) ) { 
    73             $this->session_id = stripslashes( $_COOKIE[WP_SESSION_COOKIE] ); 
     73            $cookie = stripslashes( $_COOKIE[WP_SESSION_COOKIE] ); 
     74            $cookie_crumbs = explode( '||', $cookie ); 
     75 
     76            $this->session_id = $cookie_crumbs[0]; 
     77            $this->expires = $cookie_crumbs[1]; 
     78            $this->exp_variant = $cookie_crumbs[2]; 
     79 
     80            // Update the session expiration if we're past the variant time 
     81            if ( time() > $this->exp_variant ) { 
     82                $this->set_expiration(); 
     83                update_option( "_wp_session_expires_{$this->session_id}", $this->expires ); 
     84            } 
    7485        } else { 
    7586            $this->session_id = $this->generate_id(); 
     87            $this->set_expiration(); 
    7688        } 
    7789 
    7890        $this->read_data(); 
    7991 
    80         $this->cache_expire = intval( apply_filters( 'wp_session_expiration', 24 * 60 ) ); 
    81  
    82         setcookie( WP_SESSION_COOKIE, $this->session_id, time() + $this->cache_expire, COOKIEPATH, COOKIE_DOMAIN ); 
     92        setcookie( WP_SESSION_COOKIE, $this->session_id . '||' . $this->expires . '||' . $this->exp_variant , $this->expires, COOKIEPATH, COOKIE_DOMAIN ); 
     93    } 
     94 
     95    /** 
     96     * Set both the expiration time and the expiration variant. 
     97     * 
     98     * If the current time is below the variant, we don't update the session's expiration time. If it's 
     99     * greater than the variant, then we update the expiration time in the database.  This prevents 
     100     * writing to the database on every page load for active sessions and only updates the expiration 
     101     * time if we're nearing when the session actually expires. 
     102     * 
     103     * By default, the expiration time is set to 30 minutes. 
     104     * By default, the expiration variant is set to 24 minutes. 
     105     * 
     106     * As a result, the session expiration time - at a maximum - will only be written to the database once 
     107     * every 24 minutes.  After 30 minutes, the session will have been expired. No cookie will be sent by 
     108     * the browser, and the old session will be queued for deletion by the garbage collector. 
     109     * 
     110     * @uses apply_filters Calls `wp_session_expiration_variant` to get the max update window for session data. 
     111     * @uses apply_filters Calls `wp_session_expiration` to get the standard expiration time for sessions. 
     112     */ 
     113    private function set_expiration() { 
     114        $this->exp_variant = time() + intval( apply_filters( 'wp_session_expiration_variant', 24 * 60 ) ); 
     115        $this->expires = time() + intval( apply_filters( 'wp_session_expiration', 30 * 60 ) ); 
    83116    } 
    84117 
     
    103136     */ 
    104137    private function read_data() { 
    105         $data = get_transient( "_wp_session_{$this->session_id}" ); 
    106  
    107         if ( ! $data ) { 
    108             $data = array(); 
    109         } 
    110  
    111         $this->container = $data; 
    112  
    113         set_transient( "_wp_session_{$this->session_id}", $data, $this->cache_expire ); 
    114  
    115         return $data; 
     138        $this->container = get_option( "_wp_session_{$this->session_id}", array() ); 
     139 
     140        return $this->container; 
    116141    } 
    117142 
     
    120145     */ 
    121146    public function write_data() { 
    122         set_transient( "_wp_session_{$this->session_id}", $this->container, $this->cache_expire ); 
     147        $option_key = "_wp_session_{$this->session_id}"; 
     148 
     149        // Only write the collection to the DB if it's changed. 
     150        if ( $this->dirty ) { 
     151            if ( false === get_option( $option_key ) ) { 
     152                add_option( "_wp_session_{$this->session_id}", $this->container, '', 'no' ); 
     153            } else { 
     154                update_option( "_wp_session_{$this->session_id}", $this->container ); 
     155            } 
     156        } 
    123157    } 
    124158 
     
    157191    public function regenerate_id( $delete_old = false ) { 
    158192        if ( $delete_old ) { 
    159             delete_transient( "_wp_session_{$this->session_id}" ); 
     193            delete_option( "_wp_session_{$this->session_id}" ); 
    160194        } 
    161195 
    162196        $this->session_id = $this->generate_id(); 
    163197 
    164         setcookie( WP_SESSION_COOKIE, $this->session_id, time() + $this->cache_expire, COOKIEPATH, COOKIE_DOMAIN ); 
     198        setcookie( WP_SESSION_COOKIE, $this->session_id . '||' . $this->expires . '||' . $this->exp_variant , $this->expires, COOKIEPATH, COOKIE_DOMAIN ); 
    165199    } 
    166200 
     
    180214     */ 
    181215    public function cache_expiration() { 
    182         return $this->cache_expire; 
     216        return $this->expires; 
    183217    } 
    184218 
     
    191225 
    192226    /*****************************************************************/ 
    193     /*                   ArrayAccess Implementation                  */ 
    194     /*****************************************************************/ 
    195  
    196     /** 
    197      * Whether a offset exists 
    198      * 
    199      * @link http://php.net/manual/en/arrayaccess.offsetexists.php 
    200      * 
    201      * @param mixed $offset An offset to check for. 
    202      * 
    203      * @return boolean true on success or false on failure. 
    204      */ 
    205     public function offsetExists( $offset ) { 
    206         return isset( $this->container[ $offset ]) ; 
    207     } 
    208  
    209     /** 
    210      * Offset to retrieve 
    211      * 
    212      * @link http://php.net/manual/en/arrayaccess.offsetget.php 
    213      * 
    214      * @param mixed $offset The offset to retrieve. 
    215      * 
    216      * @return mixed Can return all value types. 
    217      */ 
    218     public function offsetGet( $offset ) { 
    219         return isset( $this->container[ $offset ] ) ? $this->container[ $offset ] : null; 
    220     } 
    221  
    222     /** 
    223      * Offset to set 
    224      * 
    225      * @link http://php.net/manual/en/arrayaccess.offsetset.php 
    226      * 
    227      * @param mixed $offset The offset to assign the value to. 
    228      * @param mixed $value  The value to set. 
    229      * 
    230      * @return void 
    231      */ 
    232     public function offsetSet( $offset, $value ) { 
    233         if ( is_null( $offset ) ) { 
    234             $this->container[] = $value; 
    235         } else { 
    236             $this->container[ $offset ] = $value; 
    237         } 
    238     } 
    239  
    240     /** 
    241      * Offset to unset 
    242      * 
    243      * @link http://php.net/manual/en/arrayaccess.offsetunset.php 
    244      * 
    245      * @param mixed $offset The offset to unset. 
    246      * 
    247      * @return void 
    248      */ 
    249     public function offsetUnset( $offset ) { 
    250         unset( $this->container[ $offset ] ); 
    251     } 
    252  
    253     /*****************************************************************/ 
    254227    /*                     Iterator Implementation                   */ 
    255228    /*****************************************************************/ 
  • wp-session-manager/trunk/readme.txt

    r633588 r635249  
    55Requires at least: 3.4.2 
    66Tested up to: 3.5 
    7 Stable tag: 1.0.1 
     7Stable tag: 1.1 
    88License: GPLv2 or later 
    99License URI: http://www.gnu.org/licenses/gpl-2.0.html 
     
    5555== Changelog == 
    5656 
     57= 1.1 = 
     58* Implement Recursive_ArrayAccess to provide multidimensional array support 
     59* Better expiration for session data 
     60* Implement garbage collection to keep the database clean 
     61 
     62= 1.0.2 = 
     63* Switch to object persistence rather than transients 
     64 
    5765= 1.0.1 = 
    5866* Changes implementation to avoid the use of a global variable (still registered for convenience) 
  • wp-session-manager/trunk/wp-session-manager.php

    r633588 r635249  
    44 * Plugin URI: http://jumping-duck.com/wordpress/plugins 
    55 * Description: Prototype session management for WordPress. 
    6  * Version: 1.0.1 
     6 * Version: 1.1 
    77 * Author: Eric Mann 
    88 * Author URI: http://eamann.com 
     
    1414    define( 'WP_SESSION_COOKIE', '_wp_session' ); 
    1515 
     16if ( ! class_exists( 'Recursive_ArrayAccess' ) ) { 
     17    require_once( 'class-recursive-arrayaccess.php' ); 
     18} 
     19 
    1620// Only include the functionality if it's not pre-defined. 
    1721if ( ! class_exists( 'WP_Session' ) ) { 
  • wp-session-manager/trunk/wp-session.php

    r633588 r635249  
    7575function wp_session_start() { 
    7676    $wp_session = WP_Session::get_instance(); 
    77  
    78     $wp_session = WP_Session::get_instance(); 
    7977    do_action( 'wp_session_start' ); 
    8078 
     
    117115} 
    118116add_action( 'shutdown', 'wp_session_write_close' ); 
     117 
     118/** 
     119 * Clean up expired sessions by removing data and their expiration entries from 
     120 * the WordPress options table. 
     121 * 
     122 * This method should never be called directly and should instead be triggered as part 
     123 * of a scheduled task or cron job. 
     124 */ 
     125function wp_session_cleanup() { 
     126    global $wpdb; 
     127 
     128    if ( defined( 'WP_SETUP_CONFIG' ) ) { 
     129        return; 
     130    } 
     131 
     132    if ( ! defined( 'WP_INSTALLING' ) ) { 
     133        $expiration_keys = $wpdb->get_results( "SELECT option_name, option_value FROM $wpdb->options WHERE option_name LIKE '_wp_session_expires_%'" ); 
     134 
     135        foreach( $expiration_keys as $expiration ) { 
     136            // If the session has expired 
     137            if ( time() > intval( $expiration->option_value ) ) { 
     138                // Get the session ID 
     139                $session_id = substr( $expiration->option_name, 20 ); 
     140 
     141                delete_option( $expiration->option_name ); 
     142                delete_option( "_wp_session_{$session_id}" ); 
     143            } 
     144        } 
     145    } 
     146 
     147    // Allow other plugins to hook in to the garbage collection process. 
     148    do_action( 'wp_session_cleanup' ); 
     149} 
     150add_action( 'wp_session_garbage_collection', 'wp_session_cleanup' ); 
     151 
     152/** 
     153 * Register the garbage collector as a twice daily event. 
     154 */ 
     155function wp_session_register_garbage_collection() { 
     156    if ( ! wp_next_scheduled( 'wp_session_garbage_collection' ) ) { 
     157        wp_schedule_event( time(), 'twicedaily', 'wp_session_garbage_collection' ); 
     158    } 
     159} 
     160add_action( 'wp', 'wp_session_register_garbage_collection' ); 
     161?> 
Note: See TracChangeset for help on using the changeset viewer.