Support » Plugin: WP Session Manager » Nested arrays don't properly the update session's "dirty" flag

  • I’m working on code that calls WP_Session::get_instance(), then puts an array in the session for storing its own values. Even though I’m creating the new value as an array, Recursive_ArrayAccess::offsetSet() converts it internally to a new Recursive_ArrayAccess object with its own container & dirty flag.

    $this->session = WP_Session::get_instance();
    if ( ! isset( $this->session['mysite'] ) ) {
    	$this->session['mysite'] = array(
    		'value1'          => 1,
    		'value2' => new stdClass(),
    		'value3' => false,
    	);
    }
    WP_Session Object
    (
        [session_id:protected] => 17b0d116dea72d34abdcf0990cb52cbc
        [expires:protected] => 1390696131
        [exp_variant:protected] => 1390695771
        [container:protected] => Array
            (
                [mysite] => Recursive_ArrayAccess Object
                    (
                        [container:protected] => Array
                            (
                                [value1] => 1
                                [value2] => stdClass Object
                                [value3] => false
                            )
    
                        [dirty:protected] => 1
                    )
    
            )
    
        [dirty:protected] => 1
    )

    After the session is saved with the nested contents, further changes to the nested array don’t update the parent’s dirty flag so the updates don’t get saved. I worked around it by modifying WP_Session slightly, to have it check the dirty flag recursively: https://gist.github.com/daveross/8625999

    Might be more straightforward to have Recursive_ArrayAccess keep a reference to its parent & update the parents’ dirty flags in Recursive_ArrayAccess::offsetSet() but I didn’t want to take the time to architect that.

    http://wordpress.org/plugins/wp-session-manager/

Viewing 1 replies (of 1 total)
  • I ran into the same issue. Here is how I solved it.

    In class-wp-session.php, add this method to the class WP_Session

    public function isDirty()
    {
        $returnValue = false;
        foreach($this->container as $key=>$value) {
          $returnValue = $returnValue || $value->dirty;
        }
        return $returnValue;
    }

    Then in the method write_data, change the
    if ($this->dirty)
    to
    if ($this->isDirty())

    It needs some work but I’m only using WP Session Manager for a single task at the moment so it work.

    Cheers!
    =C=

Viewing 1 replies (of 1 total)
  • The topic ‘Nested arrays don't properly the update session's "dirty" flag’ is closed to new replies.