Plugin menu addition in multisite

Answer remade and now working as per the Question specifications.

To add a menu item to each site of the network if an specific plugin is active in that site, I’m using a Must Use plugin. It only runs if is_multisite() and if is_admin_bar_showing().

In this example, the plugin is instantiated with the following values that check for the plugin Analytics360º and remove all default menu items:

  • Menu item title: Analytics360
  • Plugin to check for: analytics360/analytics360.php
  • Menu item URL: index.php?page=analytics360.php
  • Remove defaults: array( 'newpost', 'comments', 'visit', 'dashboard' )

plugin in action

plugin in action

<?php
/**
 * Plugin Name: Modify My Sites admin menu
 * Plugin URI: http://wordpress.stackexchange.com/q/55724/12615
 * Description: Add an admin menu link to specific plugin, if it is active in the site. Can remove default items as well.
 * Author: Rodolfo Buaiz
 * Author URI: http://wordpress.stackexchange.com/users/12615/brasofilo
 * License: GPLv3
 *  
 * Class based in http://wordpress.stackexchange.com/a/77236/12615
 **/


WPSE_55724_My_Sites_Extra_Items::init( 
    'Analytics360', // Menu item title
    'analytics360/analytics360.php', // Plugin to check for
    'index.php?page=analytics360.php', // Menu item URL
    array( 'newpost', 'comments', 'visit', 'dashboard' ) // Remove defaults
);

class WPSE_55724_My_Sites_Extra_Items
{
    private static $ins = null;

    private static $title = null;
    private static $plugin_file = null;
    private static $plugin_page = null;
    private static $remove_defaults = null;

    public static function instance()
    {
        is_null( self::$ins ) && self::$ins = new self;
        return self::$ins;
    }

    public static function init( $title, $plugin_file, $plugin_page, $remove_defaults )
    {
        self::$title = $title;
        self::$plugin_file = $plugin_file;
        self::$plugin_page = $plugin_page;
        self::$remove_defaults = $remove_defaults;

        if( is_multisite() )
            add_action( 'plugins_loaded', array( self::instance(), '_setup' ) );
    }

    public function _setup()
    {
        if( is_admin_bar_showing() )
            add_action( 'admin_bar_menu', array( $this, 'add_items' ), 9999 );          
    }

    /**
     * The global var is passed by reference in a do_action_ref_array
     */
    public function add_items( $wp_admin_bar ) 
    {
        foreach ( (array) $wp_admin_bar->user->blogs as $blog ) 
        {
            switch_to_blog( $blog->userblog_id );

            // Current menu ID
            $menu_id  = 'blog-' . $blog->userblog_id;

            // Add URL to current-site/wp-admin/plugins.php
            $wp_admin_bar->add_menu( array(
                'parent' => $menu_id,
                'id'     => $menu_id . '-p',
                'title'  => __( 'Plugins' ),
                'href'   => admin_url( 'plugins.php' ),
            ) );

            // Remove default menu items
            $this->remove_items( 
                &$wp_admin_bar, 
                self::$remove_defaults,
                $menu_id
            );

            // Add custom menu for chosen Plugin it active in current site or network activated 
            if( current_user_can( 'manage_options' ) && ( $this->is_plugin_active( self::$plugin_file ) || $this->is_plugin_active_for_network( self::$plugin_file ) ) )
            {
                $wp_admin_bar->add_menu( array(
                    'parent' => $menu_id,
                    'id'     => $menu_id . '-my-p',
                    'title'  => self::$title,
                    'href'   => admin_url( self::$plugin_page ),
                ) );
            }

            restore_current_blog();
        }
    }   

    /**
     * 
     */
    private function remove_items( $wp_admin_bar, $items, $menu_id )
    {
        if( in_array( 'newpost', $items ) )
            $wp_admin_bar->remove_menu( $menu_id . '-n' ); // New Post

        if( in_array( 'comments', $items ) )
            $wp_admin_bar->remove_menu( $menu_id . '-c' ); // Comments

        if( in_array( 'visit', $items ) )
            $wp_admin_bar->remove_menu( $menu_id . '-v' ); // Visit Site

        if( in_array( 'dashboard', $items ) )
            $wp_admin_bar->remove_menu( $menu_id . '-d' ); // Dashboard
    }


    /**
     * WP function uses is_plugin_active_for_network 
     * which doesn't work in frontend
     */
    private function is_plugin_active( $plugin )
    {
        return in_array( $plugin, (array) get_option( 'active_plugins', array() ) );
    }

    private function is_plugin_active_for_network( $plugin )
    {
        $plugins = get_site_option( 'active_sitewide_plugins');
        if ( isset($plugins[$plugin]) )
            return true;
        return false;
    }
}

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)