|
4 | 4 |
|
5 | 5 | class MslsAdminBar {
|
6 | 6 |
|
| 7 | + protected string $icon_type; |
| 8 | + |
| 9 | + protected MslsBlogCollection $blog_collection; |
| 10 | + |
| 11 | + public function __construct( MslsOptions $options, MslsBlogCollection $blog_collection ) { |
| 12 | + $this->icon_type = $options->get_icon_type(); |
| 13 | + $this->blog_collection = $blog_collection; |
| 14 | + } |
7 | 15 |
|
8 | 16 | /**
|
9 | 17 | * @codeCoverageIgnore
|
10 | 18 | */
|
11 | 19 | public function init(): void {
|
| 20 | + $obj = new MslsAdminBar( msls_options(), msls_blog_collection() ); |
| 21 | + |
12 | 22 | if ( is_admin_bar_showing() ) {
|
13 |
| - add_action( 'admin_bar_menu', array( __CLASS__, 'update_admin_bar' ), 999 ); |
| 23 | + add_action( 'admin_bar_menu', array( $obj, 'update_admin_bar' ), 999 ); |
14 | 24 | }
|
15 | 25 | }
|
16 | 26 |
|
17 | 27 | /**
|
18 |
| - * @param $wp_admin_bar |
19 |
| - * |
20 |
| - * @return void |
| 28 | + * Callback that updates the admin bar with the blog information |
21 | 29 | */
|
22 |
| - public static function update_admin_bar( \WP_Admin_Bar $wp_admin_bar ): void { |
23 |
| - $icon_type = msls_options()->get_icon_type(); |
24 |
| - |
25 |
| - $blog_collection = msls_blog_collection(); |
26 |
| - foreach ( $blog_collection->get_plugin_active_blogs() as $blog ) { |
27 |
| - $title = $blog->get_blavatar() . $blog->get_title( $icon_type ); |
28 |
| - |
29 |
| - $wp_admin_bar->add_node( |
30 |
| - array( |
31 |
| - 'id' => 'blog-' . $blog->userblog_id, |
32 |
| - 'title' => $title, |
33 |
| - ) |
34 |
| - ); |
| 30 | + public function update_admin_bar( \WP_Admin_Bar $wp_admin_bar ): void { |
| 31 | + foreach ( $this->blog_collection->get_plugin_active_blogs() as $blog ) { |
| 32 | + $title = $this->get_title( $blog, true ); |
| 33 | + |
| 34 | + $title && $this->add_node( $wp_admin_bar, 'blog-' . $blog->userblog_id, $title ); |
35 | 35 | }
|
36 | 36 |
|
37 |
| - $blog = $blog_collection->get_current_blog(); |
38 |
| - if ( is_object( $blog ) && method_exists( $blog, 'get_title' ) ) { |
39 |
| - $wp_admin_bar->add_node( |
40 |
| - array( |
41 |
| - 'id' => 'site-name', |
42 |
| - 'title' => $blog->get_title( $icon_type ), |
43 |
| - ) |
44 |
| - ); |
| 37 | + $blog = $this->blog_collection->get_current_blog(); |
| 38 | + $title = $this->get_title( $blog ); |
| 39 | + |
| 40 | + $title && $this->add_node( $wp_admin_bar, 'site-name', $title ); |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Adds node information to an existing node |
| 45 | + */ |
| 46 | + public function add_node( \WP_Admin_Bar $wp_admin_bar, string $node_id, string $title ): bool { |
| 47 | + $node = $wp_admin_bar->get_node( $node_id ); |
| 48 | + if ( is_null( $node ) ) { |
| 49 | + return false; |
45 | 50 | }
|
| 51 | + |
| 52 | + $wp_admin_bar->add_node( |
| 53 | + array( |
| 54 | + 'id' => $node_id, |
| 55 | + 'title' => $title, |
| 56 | + ) |
| 57 | + ); |
| 58 | + |
| 59 | + return true; |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Gets a title with label orflag (depending on the settings) for the blog |
| 64 | + * |
| 65 | + * It uses a blavatar icon as prefix if $blavatar is set to true |
| 66 | + */ |
| 67 | + protected function get_title( ?MslsBlog $blog, bool $blavatar = false ): ?string { |
| 68 | + if ( is_null( $blog ) ) { |
| 69 | + return $blog; |
| 70 | + } |
| 71 | + |
| 72 | + $prefix = $blavatar ? $blog->get_blavatar() : ''; |
| 73 | + |
| 74 | + return $prefix . $blog->get_title( $this->icon_type ); |
46 | 75 | }
|
47 | 76 | }
|
0 commit comments