Support » Plugin: Multisite Shared Menu » This plugin breaks the WP admin bar.

  • Hello,

    I am using this plugin on a Multisite with 300+ subsites. After activating this plugin on a subsite the admin bar acts like I’m on the primary site.

    I’ve had this happen in the past and it was caused by switching blogs in the code and not setting the current blog back when I was done.

    Please let me know if you can fix this issue or I’ll have to hack you plugin.

    Thanks!

    https://wordpress.org/plugins/multisite-shared-menu/

Viewing 2 replies - 1 through 2 (of 2 total)
  • I fixed your plugin. You’ll see my edits below:

    class MasterSharedMenu {
    	protected $loader;
    	protected $plugin_name;
    	protected $version;
    	protected $current_blog_id; //Brad's hack
    
    	public function __construct() {
    		$this->plugin_name = 'multisitesharedmenu';
    		$this->version = '1.2';
    		$this->current_blog_id = get_current_blog_id(); //Brad's hack
    
    		$this->define_admin_hooks();
    		$this->define_public_hooks();
    	}
    
    	private function define_admin_hooks() {
    		$this->add_mfs_menu();
    	}
    
    	private function define_public_hooks() {
    		add_filter( 'pre_wp_nav_menu' , array( $this, 'menuswitch_check'), 10, 2 );
    		add_filter( 'show_admin_bar', array( $this, 'check_restore_blog_menu' ) ); //Brad's hack
    	}
    
    	// Checks if options set to switch menu. If so, switch to the appropriate site and change the menu to load the desired menu....
    	public function menuswitch_check( $a, $menu_object ) {
    		if( !( $mfsSettings = $this->validate_mfs_set() )) {
    			return false;
    		}
    
    		$navigation_affected = $mfsSettings['destinationMenuLocation'];
    
    		if( !is_array( $navigation_affected ) ) {
    			$navigation_affected = array( $navigation_affected ); // backwards-compatibility
    		} 
    
    		// nav menus now stored as an array, loop through each item
    		foreach ( $navigation_affected as $menu) {
    			if (isset( $menu_object )) {
    				if( $menu == $menu_object->theme_location ) {
    					$switchSite = get_blog_details($mfsSettings['sourceSiteID']);
    					switch_to_blog($switchSite->blog_id);
    				}
    			}
    		}
    		return;
    	}
    
    	// Switch back to the current blog/site if plugin settings were used.
    	public function check_restore_blog_menu() {//Brad's hack
    		switch_to_blog($this->current_blog_id); //Brad's hack
    		return true;//Brad's hack
    	}

    I had to make some more changes and forgot to update, here you go:

    <?php
    // allow this plugin to access functions from WP core before they would normally be avalible
    require_once( ABSPATH . 'wp-includes/pluggable.php' ); //Brad's hack
    
    class MasterSharedMenu {
    	protected $loader;
    	protected $plugin_name;
    	protected $version;
    	protected $current_blog_id; //Brad's hack
    
    	public function __construct() {
    		$this->plugin_name = 'multisitesharedmenu';
    		$this->version = '1.2';
    		$this->current_blog_id = get_current_blog_id(); //Brad's hack
    
    		$this->define_admin_hooks();
    		$this->define_public_hooks();
    	}
    
    	private function define_admin_hooks() {
    		$this->add_mfs_menu();
    	}
    
    	private function define_public_hooks() {
    		add_action( 'plugins_loaded', array( $this, 'check_if_user_logged_in' ) ); //Brad's hack
    
    		add_filter( 'pre_wp_nav_menu' , array( $this, 'menuswitch_check'), 10, 2 );
    		add_filter( 'wp_nav_menu_items', array( $this, 'check_restore_blog_menu' ), 10, 2 );
    		if ( $this->check_if_user_logged_in() ) { //Brad's hack
    			add_filter( 'show_admin_bar', array( $this, 'check_restore_blog_menu' ) ); //Brad's hack
    		} //Brad's hack
    	}
    
    	public function check_if_user_logged_in() { //Brad's hack
    		return is_user_logged_in(); //Brad's hack
    	} //Brad's hack
    
    	// Checks if options set to switch menu. If so, switch to the appropriate site and change the menu to load the desired menu....
    	public function menuswitch_check( $a, $menu_object ) {
    		if( ! ( $mfsSettings = $this->validate_mfs_set() ) ) {
    			return false;
    		}
    		
    		$navigation_affected = $mfsSettings['destinationMenuLocation'];
    		
    		if( !is_array( $navigation_affected ) ) {
    			$navigation_affected = array( $navigation_affected ); // backwards-compatibility
    		} 
    		
    		// nav menus now stored as an array, loop through each item
    		foreach ( $navigation_affected as $menu ) {
    			if ( isset( $menu_object ) ) {
    				if( $menu == $menu_object->theme_location ) {
    					$switchSite = get_blog_details( $mfsSettings['sourceSiteID'] );
    					switch_to_blog( $switchSite->blog_id );
    				}
    			}
    		}
    		return;
    	}
    	
    	// Switch back to the current blog/site if plugin settings were used.
    	public function check_restore_blog_menu( $items = null, $args = null ) { //Brad's hack
    		switch_to_blog( $this->current_blog_id ); //Brad's hack
    		if ( null != $items ) { //Brad's hack
    			return $items;
    		} //Brad's hack
    		else { //Brad's hack
    			return true; //Brad's hack
    		} //Brad's hack
    	}
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘This plugin breaks the WP admin bar.’ is closed to new replies.