[wpmu-trac] [WordPress MU Trac] #1072: 'restore_current_blog()' not returning to previous blog after multiple 'switch_to_blog()' function calls
WordPress MU Trac
wpmu-trac at lists.automattic.com
Wed Jul 22 20:25:08 UTC 2009
#1072: 'restore_current_blog()' not returning to previous blog after multiple
'switch_to_blog()' function calls
------------------------+---------------------------------------------------
Reporter: bigdawggi | Owner: somebody
Type: defect | Status: new
Priority: normal | Milestone:
Component: component1 | Version: 2.8.2
Severity: normal | Keywords: switch to blog, restore current blog,
------------------------+---------------------------------------------------
After the switch_to_blog() function is called multiple times (nested), and
when using the restore_current_blog() function call to back out of the
'$switched_stack'; after the first restore_current_blog() function call is
made, the following restore_current_blog() function calls are negated,
therefore leaving the global $blog_id variable set to an unanticipated
$blog_id.
Also, the switch_to_blog() function was appending an item to the
$switched_stack array, but then returning without ever changing the
$switched variable '''if''' the global $blog_id was the same as the id of
the blog you're trying to switch to. This caused the $switched_stack to
become corrupt, by leaving orphaned id's of the blogs you tried to switch
to in the $switched_stack array.
I am attaching the suggested patches in a .diff file of the current wpmu-
functions.php file in WPMU trunk.
== Nesting test code below: ==
{{{
echo '<p>Switch to 10, then 1 (the current blog), then 23, then
restore, then restore, then restore:</p>';
echo '<p>before 10:</p>';
echo '<p>Blog ID:'.$blog_id.'</p>';
print_r($switched_stack);
switch_to_blog(10);
echo '<p>during 10:</p>';
echo '<p>Blog ID:'.$blog_id.'</p>';
print_r($switched_stack);
switch_to_blog(1);
echo '<p>during 1:</p>';
echo '<p>Blog ID:'.$blog_id.'</p>';
print_r($switched_stack);
switch_to_blog(23);
echo '<p>during 23:</p>';
echo '<p>Blog ID:'.$blog_id.'</p>';
print_r($switched_stack);
restore_current_blog();
echo '<p>after 23:</p>';
echo '<p>Blog ID:'.$blog_id.'</p>';
print_r($switched_stack);
restore_current_blog();
echo '<p>after 1:</p>';
echo '<p>Blog ID:'.$blog_id.'</p>';
print_r($switched_stack);
restore_current_blog();
echo '<p>after 10:</p>';
echo '<p>Blog ID:'.$blog_id.'</p>';
print_r($switched_stack);
echo '<hr />';
}}}
== Switch to same blog id test code below: ==
{{{
echo '<p>Switch to 1, then restore:</p>';
echo '<p>before 1:</p>';
echo '<p>Blog ID:'.$blog_id.'</p>';
print_r($switched_stack);
switch_to_blog(1);
echo '<p>during 1:</p>';
echo '<p>Blog ID:'.$blog_id.'</p>';
print_r($switched_stack);
restore_current_blog();
echo '<p>after 1:</p>';
echo '<p>Blog ID:'.$blog_id.'</p>';
print_r($switched_stack);
}}}
--
Ticket URL: <http://trac.mu.wordpress.org/ticket/1072>
WordPress MU Trac <http://mu.wordpress.org/>
WordPress Multiuser
More information about the wpmu-trac
mailing list