<?php
// patch for stupid plugins using switch_to_blog instead of restore_current_blog
add_action( 'switch_blog', function( $new_blog, $prev_blog_id ){
// avoid recursion, because switch_blog action is called by restore_current_blog too
if( empty( $GLOBALS['_wp_switched_stack'] ) ){
return;
}
if( $new_blog === get_current_blog_id() ){
// prepare the stack in order to be emptied by restore_current_blog()
$GLOBALS['_wp_switched_stack'] = array( get_current_blog_id() );
// call what everyone should call: https://codex.wordpress.org/Function_Reference/restore_current_blog
// "When calling switch_to_blog() repeatedly, either call restore_current_blog() each time"
restore_current_blog();
}
}, PHP_INT_MAX, 2 );