-
-
Save doiftrue/d46b1a5a7f5fd5c6cd1992fc99407db1 to your computer and use it in GitHub Desktop.
[wpkama embed] https://wp-kama.com/2489 Multisite_Admin_Bar
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Сutomize multisite admin bar. | |
* Moves default nested multisite menu to main toolbar menu. | |
*/ | |
class Multisite_Admin_Bar { | |
public static function init(): void { | |
add_action( 'admin_bar_menu', [ __CLASS__, 'cutomize_toolbar' ], 31 ); // 31 - after `wp_admin_bar_site_menu()` | |
add_action( 'wp_head', [ __CLASS__, 'styles' ] ); | |
add_action( 'admin_head', [ __CLASS__, 'styles' ] ); | |
add_action( 'admin_bar_menu', [ __CLASS__, 'add_switch_node' ], 30 ); | |
} | |
public static function styles(): void { | |
?> | |
<style id="cutomize_multisite_admin_bar"> | |
#wp-admin-bar-network-admin .ab-icon:before{ color:#797c7d; } /* the color of the main icon */ | |
#wpadminbar .hl-current-blog{ background-color: rgba(255 255 255 / 0.15); } | |
</style> | |
<?php | |
} | |
public static function cutomize_toolbar( \WP_Admin_Bar $toolbar ): void { | |
// for multisite only | |
if( ! $toolbar->get_node( 'site-name' ) ){ | |
return; | |
} | |
// remove Multisite Language Switcher hook | |
remove_action( 'admin_bar_menu', [ \lloc\Msls\MslsPlugin::class, 'update_adminbar' ], 999 ); | |
// NOTE: the closure needed to access `WP_Admin_Bar->nodes` private property | |
$callback = function(){ | |
/** @var \WP_Admin_Bar $this */ | |
/// remove nodes | |
unset( | |
$this->nodes['site-name'], // default current site node | |
$this->nodes['my-sites-super-admin'], | |
$this->nodes['my-sites'], | |
$this->nodes['my-sites-list'] | |
); | |
/// network-admin | |
$net_admin = & $this->nodes['network-admin']; | |
$net_admin->parent = ''; | |
$net_admin->title = '<span class="ab-icon dashicons-admin-multisite"></span><span class="ab-label">Network</span>'; | |
/// blogs | |
foreach( $this->nodes as $key => $node ){ | |
preg_match( '/blog-(\d+)(?:-(\w+))?/', $node->id, $mm ); | |
$blog_id = (int) ( $mm[1] ?? 0 ); | |
$type = $mm[2] ?? ''; | |
if( $blog_id ){ | |
// main: blog-1, blog-2 | |
if( ! $type ){ | |
// title | |
preg_match( '~^<.*>~', $node->title, $mm ); | |
$lang = langdata()->blog_id_lang( $blog_id ); | |
$title_icon_html = $mm[0] ?? ''; | |
$flag_img = sprintf( '<img src="%s" alt="%s">', langdata()->flag_url( $lang ), $lang ); | |
$node->title = $title_icon_html . $flag_img . ' ' . strtoupper( $lang ); | |
$node->href = is_admin() ? get_home_url( $blog_id ) : $node->href; | |
$node->parent = ''; | |
$node->meta['class'] = ( get_current_blog_id() === $blog_id ) ? 'hl-current-blog' : ''; | |
} | |
// comments: blog-1-c, blog-2-c | |
if( 'c' === $type ){ | |
unset( $this->nodes[ $key ] ); | |
} | |
} | |
} | |
}; | |
$callback->call( $toolbar ); | |
} | |
public static function add_switch_node( \WP_Admin_Bar $toolbar ): void { | |
global $pagenow; | |
$url = $_SERVER['REQUEST_URI']; | |
$url = str_starts_with( $url, '/ru' ) ? preg_replace( '~^/ru~', '', $url ) : "/ru$url"; | |
if( 'post.php' === $pagenow && ( $post_id = (int) ( $_GET['post'] ?? 0 ) ) ){ | |
$other_post_id = is_ru() ? get_ru_en_post_id( $post_id ) : get_en_ru_post_id( $post_id ); | |
$url = $other_post_id | |
? str_replace( "post=$post_id", "post=$other_post_id", $url ) | |
: ''; | |
} | |
$toolbar->add_menu( [ | |
'id' => 'switch_langs', | |
'title' => '<span class="ab-icon dashicons-controls-repeat"></span>', | |
'href' => $url, | |
'meta' => [ | |
'title' => 'Switch Curent Page to Opposite Lang', | |
'onclick' => 'alert("aaaaaaaaa")', | |
], | |
] ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment