WordPress-Coding-Standards
WordPress-Coding-Standards copied to clipboard
switch_to_blog() - flag for missing subsequent restore_current_blog()
Describe the solution you'd like
Sometimes difficult to determine logic bugs occur when switch_to_blog()
is called without a subsequent restoration. It would be worth flagging to the user that restore_current_blog()
should be called after (if not already) after switch_to_blog()
.
This should be reported:
function switch_blog() {
$id = get_current_blog_id();
if ( $id === 2 ) {
return;
}
switch_to_blog( 2 );
}
This should not be reported:
function switch_blog() {
$id = get_current_blog_id();
if ( $id === 2 ) {
return;
}
switch_to_blog( 2 );
restore_current_blog();
}
Additional context (optional)
https://github.com/Automattic/VIP-Coding-Standards/issues/651