WordPress.org

Plugin Directory

Changeset 633588


Ignore:
Timestamp:
12/03/12 17:53:36 (17 months ago)
Author:
ericmann
Message:

Update to avoid using global $wp_session and to properly implement Iterator and Countable.

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

Legend:

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

    r632899 r633588  
    1717 * @since   3.6.0 
    1818 */ 
    19 class WP_Session implements ArrayAccess { 
     19class WP_Session implements ArrayAccess, Iterator, Countable { 
    2020    /** 
    2121     * Internal data collection. 
     
    7070     */ 
    7171    private function __construct() { 
    72         if ( isset( $_COOKIE['_wp_session'] ) ) { 
    73             $this->session_id = stripslashes( $_COOKIE['_wp_session'] ); 
     72        if ( isset( $_COOKIE[WP_SESSION_COOKIE] ) ) { 
     73            $this->session_id = stripslashes( $_COOKIE[WP_SESSION_COOKIE] ); 
    7474        } else { 
    7575            $this->session_id = $this->generate_id(); 
     
    8080        $this->cache_expire = intval( apply_filters( 'wp_session_expiration', 24 * 60 ) ); 
    8181 
    82         setcookie( '_wp_session', $this->session_id, time() + $this->cache_expire, COOKIEPATH, COOKIE_DOMAIN ); 
     82        setcookie( WP_SESSION_COOKIE, $this->session_id, time() + $this->cache_expire, COOKIEPATH, COOKIE_DOMAIN ); 
    8383    } 
    8484 
     
    162162        $this->session_id = $this->generate_id(); 
    163163 
    164         setcookie( '_wp_session', $this->session_id, time() + $this->cache_expire, COOKIEPATH, COOKIE_DOMAIN ); 
     164        setcookie( WP_SESSION_COOKIE, $this->session_id, time() + $this->cache_expire, COOKIEPATH, COOKIE_DOMAIN ); 
    165165    } 
    166166 
     
    189189        $this->container = array(); 
    190190    } 
     191 
     192    /*****************************************************************/ 
     193    /*                   ArrayAccess Implementation                  */ 
     194    /*****************************************************************/ 
    191195 
    192196    /** 
     
    246250        unset( $this->container[ $offset ] ); 
    247251    } 
     252 
     253    /*****************************************************************/ 
     254    /*                     Iterator Implementation                   */ 
     255    /*****************************************************************/ 
     256 
     257    /** 
     258     * Current position of the array. 
     259     * 
     260     * @link http://php.net/manual/en/iterator.current.php 
     261     * 
     262     * @return mixed 
     263     */ 
     264    public function current() { 
     265        return current( $this->container ); 
     266    } 
     267 
     268    /** 
     269     * Key of the current element. 
     270     * 
     271     * @link http://php.net/manual/en/iterator.key.php 
     272     * 
     273     * @return mixed 
     274     */ 
     275    public function key() { 
     276        return key( $this->container ); 
     277    } 
     278 
     279    /** 
     280     * Move the internal point of the container array to the next item 
     281     * 
     282     * @link http://php.net/manual/en/iterator.next.php 
     283     * 
     284     * @return void 
     285     */ 
     286    public function next() { 
     287        next( $this->container ); 
     288    } 
     289 
     290    /** 
     291     * Rewind the internal point of the container array. 
     292     * 
     293     * @link http://php.net/manual/en/iterator.rewind.php 
     294     * 
     295     * @return void 
     296     */ 
     297    public function rewind() { 
     298        reset( $this->container ); 
     299    } 
     300 
     301    /** 
     302     * Is the current key valid? 
     303     * 
     304     * @link http://php.net/manual/en/iterator.rewind.php 
     305     * 
     306     * @return bool 
     307     */ 
     308    public function valid() { 
     309        return $this->offsetExists( $this->key() ); 
     310    } 
     311 
     312    /*****************************************************************/ 
     313    /*                    Countable Implementation                   */ 
     314    /*****************************************************************/ 
     315 
     316    /** 
     317     * Get the count of elements in the container array. 
     318     * 
     319     * @link http://php.net/manual/en/countable.count.php 
     320     * 
     321     * @return int 
     322     */ 
     323    public function count() { 
     324        return count( $this->container ); 
     325    } 
    248326} 
  • wp-session-manager/trunk/readme.txt

    r632899 r633588  
    55Requires at least: 3.4.2 
    66Tested up to: 3.5 
    7 Stable tag: 1.0 
     7Stable tag: 1.0.1 
    88License: GPLv2 or later 
    99License URI: http://www.gnu.org/licenses/gpl-2.0.html 
     
    28281. Upload the entire `/wp-session-manager` folder to the `/wp-content/plugins/` directory. 
    29291. Activate the plugin through the 'Plugins' menu in WordPress. 
    30 1. Use `global $wp_session;` in your code. 
     301. Use `WP_Session::get_instance()` in your code. 
    3131 
    3232== Frequently Asked Questions == 
     
    3434= How do I add session variables? = 
    3535 
    36 First, make a reference to the global session object.  Then, use it like an associative array, just like `$_SESSION`: 
     36First, make a reference to the WP_Session instance.  Then, use it like an associative array, just like `$_SESSION`: 
    3737 
    38 `global $wp_session; 
     38`$wp_session = WP_Session::get_instance(); 
    3939$wp_session['user_name'] = 'User Name';                            // A string 
    4040$wp_session['user_contact'] = array( 'email' => 'user@name.com' ); // An array 
     
    5555== Changelog == 
    5656 
     57= 1.0.1 = 
     58* Changes implementation to avoid the use of a global variable (still registered for convenience) 
     59 
    5760= 1.0 = 
    5861* First version 
  • wp-session-manager/trunk/wp-session-manager.php

    r632899 r633588  
    44 * Plugin URI: http://jumping-duck.com/wordpress/plugins 
    55 * Description: Prototype session management for WordPress. 
    6  * Version: 1.0 
     6 * Version: 1.0.1 
    77 * Author: Eric Mann 
    88 * Author URI: http://eamann.com 
    99 * License: GPLv2+ 
    1010 */ 
     11 
     12// let users change the session cookie name 
     13if( ! defined( 'WP_SESSION_COOKIE' ) ) 
     14    define( 'WP_SESSION_COOKIE', '_wp_session' ); 
    1115 
    1216// Only include the functionality if it's not pre-defined. 
  • wp-session-manager/trunk/wp-session.php

    r632899 r633588  
    1717 */ 
    1818function wp_session_cache_expire() { 
    19     /** @var $wp_session WP_Session */ 
    20     global $wp_session; 
     19    $wp_session = WP_Session::get_instance(); 
    2120 
    2221    return $wp_session->cache_expiration(); 
     
    3635 */ 
    3736function wp_session_decode( $data ) { 
    38     /** @var $wp_session WP_Session */ 
    39     global $wp_session; 
     37    $wp_session = WP_Session::get_instance(); 
    4038 
    4139    return $wp_session->json_in( $data ); 
     
    4846 */ 
    4947function wp_session_encode() { 
    50     /** @var $wp_session WP_Session */ 
    51     global $wp_session; 
     48    $wp_session = WP_Session::get_instance(); 
    5249 
    5350    return $wp_session->json_out(); 
     
    6259 */ 
    6360function wp_session_regenerate_id( $delete_old_session = false ) { 
    64     /** @var $wp_session WP_Session */ 
    65     global $wp_session; 
     61    $wp_session = WP_Session::get_instance(); 
    6662 
    6763    $wp_session->regenerate_id( $delete_old_session ); 
     
    7874 */ 
    7975function wp_session_start() { 
    80     /** @var $wp_session WP_Session */ 
    81     global $wp_session; 
     76    $wp_session = WP_Session::get_instance(); 
    8277 
    8378    $wp_session = WP_Session::get_instance(); 
     
    9489 */ 
    9590function wp_session_status() { 
    96     /** @var $wp_session WP_Session */ 
    97     global $wp_session; 
     91    $wp_session = WP_Session::get_instance(); 
    9892 
    9993    if ( $wp_session->session_started() ) { 
     
    108102 */ 
    109103function wp_session_unset() { 
    110     /** @var $wp_session WP_Session */ 
    111     global $wp_session; 
     104    $wp_session = WP_Session::get_instance(); 
    112105 
    113106    $wp_session->reset(); 
     
    118111 */ 
    119112function wp_session_write_close() { 
    120     /** @var $wp_session WP_Session */ 
    121     global $wp_session; 
     113    $wp_session = WP_Session::get_instance(); 
    122114 
    123115    $wp_session->write_data(); 
Note: See TracChangeset for help on using the changeset viewer.