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.
- The topic ‘Nested arrays don't properly the update session's "dirty" flag’ is closed to new replies.