WordPress-Coding-Standards icon indicating copy to clipboard operation
WordPress-Coding-Standards copied to clipboard

switch_to_blog() - flag for missing subsequent restore_current_blog()

Open rebeccahum opened this issue 3 years ago • 0 comments

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

rebeccahum avatar Apr 15 '21 15:04 rebeccahum