teste <?php /** * Plugin Name: Admin Bar - Add items to each site menu * Plugin URI: https://brasofilo.com * Description: Based on the WordPress Stackexchange answer: https://wordpress.stackexchange.com/a/56246/12615 * Version: 1.2.1 * Author: Rodolfo Buaiz * Author URI: https://brasofilo.com * Last Updated: 2025-04-12 * */ WPSE_55724_My_Sites_Extra_Items::init(); /** * The links to create on the single sites and the network menus */ class WPSE_55724_Config { public static function singlesite() { return [ 'plg' => [ 'title' => __('Plugins'), 'basename' => false, 'url' => 'plugins.php', ], 'mtt' => [ 'title' => __('Admin Tweaks'), 'basename' => 'many-tips-together/many-tips-together.php', 'url' => 'options-general.php?page=options-general.php_admin_tweaks', ], 'snp' => [ 'title' => __('Snippets'), 'basename' => 'code-snippets/code-snippets.php', 'url' => 'admin.php?page=snippets&orderby=name&order=asc', ], 'acf' => [ 'title' => __('ACF'), 'basename' => 'advanced-custom-fields/acf.php', 'url' => 'edit.php?post_type=acf-field-group&ga=true', 'blogs' => ['2'] /* Site IDs that gets special ACF menu */ ], 'igsc' => [ 'title' => __('IG Scanner'), 'basename' => 'ig-scanner/ig-scanner.php', 'url' => 'tools.php?page=ig-scanner-page', ], ]; } public static function network() { return [ 'snippets' => [ 'title' => __('Snippets'), 'basename' => 'code-snippets/code-snippets.php', 'url' => 'admin.php?page=snippets&orderby=name&order=asc' ], 'wordfence' => [ 'title' => __('Wordfence'), 'basename' => 'wordfence/wordfence.php', 'url' => 'admin.php?page=Wordfence' ], 'backwpup' => [ 'title' => __('BackWPup'), 'basename' => 'backwpup/backwpup.php', 'url' => 'admin.php?page=backwpup' ], ]; } } /** * Class WPSE_55724_My_Sites_Extra_Items * * Adds custom items to the WordPress admin bar for multisite installations, * providing quick access to plugins and tools across the network. */ class WPSE_55724_My_Sites_Extra_Items { /** @var null|WPSE_55724_My_Sites_Extra_Items Singleton instance */ private static $instance = null; /** @var array Plugin basenames for active checks */ private $singlesite_plugins; /** @var array Network plugins to add to admin bar */ private $network_plugins; /** * Gets the singleton instance * * @return WPSE_55724_My_Sites_Extra_Items */ public static function instance() { if (is_null(self::$instance)) { self::$instance = new self(); } return self::$instance; } /** * Constructor - sets up plugin configurations */ private function __construct() { $this->singlesite_plugins = WPSE_55724_Config::singlesite(); $this->network_plugins = WPSE_55724_Config::network(); } /** * Initializes the plugin */ public static function init() { if (is_multisite()) { add_action('plugins_loaded', [self::instance(), 'setup']); } } /** * Sets up admin bar modifications if admin bar is showing */ public function setup() { if (is_admin_bar_showing()) { add_action('admin_bar_menu', [$this, 'admin_bar_menu'], 9999); } } /** * Adds all custom items to the admin bar * * @param WP_Admin_Bar $adminbar The admin bar object */ public function admin_bar_menu($adminbar) { $this->add_network_admin_items($adminbar); $this->add_singlesite_admin_items($adminbar); } /** * Adds network admin items to the admin bar * * @param WP_Admin_Bar $adminbar The admin bar object */ private function add_network_admin_items($adminbar) { foreach ($this->network_plugins as $slug => $plugin) { if ($this->is_plugin_active($plugin['basename'])) { $adminbar->add_menu([ 'parent' => 'network-admin', 'id' => "network-admin-$slug", 'title' => $plugin['title'], 'href' => network_admin_url($plugin['url']), ]); } } } /** * Processes items for each site in the network * * @param WP_Admin_Bar $adminbar The admin bar object */ private function add_singlesite_admin_items($adminbar) { foreach ((array) $adminbar->user->blogs as $blog) { switch_to_blog($blog->userblog_id); $this->modify_singlesite_menu($adminbar, $blog); restore_current_blog(); } } /** * Modifies the admin bar menu for a specific blog * * @param WP_Admin_Bar $adminbar The admin bar object * @param object $blog The blog object */ private function modify_singlesite_menu($adminbar, $blog) { $menu_id = 'blog-' . $blog->userblog_id; $this->remove_default_items($adminbar, $menu_id); $this->add_custom_menu_items($adminbar, $menu_id, $blog); } /** * Removes default items from the blog menu * * @param WP_Admin_Bar $adminbar The admin bar object * @param string $menu_id The parent menu ID */ private function remove_default_items($adminbar, $menu_id) { $adminbar->remove_menu($menu_id . '-n'); // New Post $adminbar->remove_menu($menu_id . '-c'); // Comments } /** * Adds custom menu items for a blog if user has permissions * * @param WP_Admin_Bar $adminbar The admin bar object * @param string $menu_id The parent menu ID * @param object $blog The blog object */ private function add_custom_menu_items($adminbar, $menu_id, $blog) { if (!current_user_can('manage_options')) { return; } foreach ($this->singlesite_plugins as $slug => $plugin) { if (!$plugin['basename'] || $this->is_plugin_active($plugin['basename'])) { $doit = true; if (isset($plugin['blogs']) && !in_array($blog->userblog_id, $plugin['blogs'])) { $doit = false; } if ($doit) { $adminbar->add_menu([ 'parent' => $menu_id, 'id' => "$menu_id-$slug", 'title' => $plugin['title'], 'href' => admin_url($plugin['url']), ]); } } } } /** * Checks if a plugin is active either site-wide or network-wide * * @param string $plugin Plugin basename * @return bool True if plugin is active */ private function is_plugin_active($plugin) { $sites = get_option('active_plugins', []); $network = get_site_option('active_sitewide_plugins', []); return in_array($plugin, $sites) || isset($network[$plugin]); } }Copy