Skip to content

Commit 2b4aebb

Browse files
authored
Merge pull request #320 from lloc/refactoring-version-2-8
Refactoring version 2 8
2 parents b36c8ee + c3d45cc commit 2b4aebb

File tree

5 files changed

+142
-58
lines changed

5 files changed

+142
-58
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"test": "vendor/bin/phpunit",
3535
"coverage": "php -d xdebug.mode=coverage vendor/bin/phpunit --coverage-html tests/coverage",
3636
"analyze": "vendor/bin/phpstan analyze",
37+
"playwright": "npx playwright test",
3738
"php74": "phpcs -p ./*.php includes/ --standard=vendor/phpcompatibility/php-compatibility/PHPCompatibility --runtime-set testVersion 7.4",
3839
"php81": "phpcs -p ./*.php includes/ --standard=vendor/phpcompatibility/php-compatibility/PHPCompatibility --runtime-set testVersion 8.1",
3940
"php82": "phpcs -p ./*.php includes/ --standard=vendor/phpcompatibility/php-compatibility/PHPCompatibility --runtime-set testVersion 8.2",

includes/MslsAdminBar.php

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
3+
namespace lloc\Msls;
4+
5+
class MslsAdminBar {
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+
}
15+
16+
/**
17+
* @codeCoverageIgnore
18+
*/
19+
public function init(): void {
20+
$obj = new MslsAdminBar( msls_options(), msls_blog_collection() );
21+
22+
if ( is_admin_bar_showing() ) {
23+
add_action( 'admin_bar_menu', array( $obj, 'update_admin_bar' ), 999 );
24+
}
25+
}
26+
27+
/**
28+
* Callback that updates the admin bar with the blog information
29+
*/
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+
}
36+
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;
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 );
75+
}
76+
}

includes/MslsPlugin.php

Lines changed: 2 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public static function init() {
4444

4545
if ( function_exists( 'is_multisite' ) && is_multisite() ) {
4646
add_filter( 'msls_get_output', array( __CLASS__, 'get_output' ) );
47+
add_action( 'init', array( MslsAdminBar::class, 'init' ) );
4748

4849
add_action( 'widgets_init', array( $obj, 'init_widget' ) );
4950
add_filter( 'the_content', array( $obj, 'content_filter' ) );
@@ -54,8 +55,6 @@ public static function init() {
5455
add_action( 'init', array( $obj, 'block_init' ) );
5556
}
5657

57-
add_action( 'init', array( __CLASS__, 'admin_bar_init' ) );
58-
5958
add_action( 'admin_enqueue_scripts', array( $obj, 'custom_enqueue' ) );
6059
add_action( 'wp_enqueue_scripts', array( $obj, 'custom_enqueue' ) );
6160

@@ -123,50 +122,6 @@ public static function get_output() {
123122
return $obj;
124123
}
125124

126-
/**
127-
* @return bool
128-
*/
129-
public static function admin_bar_init() {
130-
if ( is_admin_bar_showing() ) {
131-
add_action( 'admin_bar_menu', array( __CLASS__, 'update_adminbar' ), 999 );
132-
133-
return true;
134-
}
135-
136-
return false;
137-
}
138-
139-
/**
140-
* @param $wp_admin_bar
141-
*
142-
* @return void
143-
*/
144-
public static function update_adminbar( \WP_Admin_Bar $wp_admin_bar ): void {
145-
$icon_type = msls_options()->get_icon_type();
146-
147-
$blog_collection = msls_blog_collection();
148-
foreach ( $blog_collection->get_plugin_active_blogs() as $blog ) {
149-
$title = $blog->get_blavatar() . $blog->get_title( $icon_type );
150-
151-
$wp_admin_bar->add_node(
152-
array(
153-
'id' => 'blog-' . $blog->userblog_id,
154-
'title' => $title,
155-
)
156-
);
157-
}
158-
159-
$blog = $blog_collection->get_current_blog();
160-
if ( is_object( $blog ) && method_exists( $blog, 'get_title' ) ) {
161-
$wp_admin_bar->add_node(
162-
array(
163-
'id' => 'site-name',
164-
'title' => $blog->get_title( $icon_type ),
165-
)
166-
);
167-
}
168-
}
169-
170125
/**
171126
* Callback for action wp_head
172127
*/
@@ -190,7 +145,7 @@ public function content_filter( string $content ) {
190145
}
191146

192147
/**
193-
* Create filterstring for msls_content_filter()
148+
* Create filter-string for msls_content_filter()
194149
*
195150
* @param string $pref
196151
* @param string $post

tests/phpunit/TestMslsAdminBar.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php declare( strict_types=1 );
2+
3+
namespace lloc\MslsTests;
4+
5+
use Brain\Monkey\Functions;
6+
use lloc\Msls\MslsAdminBar;
7+
use lloc\Msls\MslsBlog;
8+
use lloc\Msls\MslsBlogCollection;
9+
use lloc\Msls\MslsLink;
10+
use lloc\Msls\MslsOptions;
11+
12+
class TestMslsAdminBar extends MslsUnitTestCase {
13+
14+
15+
public function setUp(): void {
16+
parent::setUp();
17+
18+
$options = \Mockery::mock( MslsOptions::class );
19+
$options->shouldReceive( 'get_icon_type' )->andReturn( 'label' );
20+
21+
$blog_a = \Mockery::mock( MslsBlog::class );
22+
$blog_a->userblog_id = 1;
23+
$blog_a->shouldReceive( 'get_title' )->andReturn( 'Blog A' );
24+
$blog_a->shouldReceive( 'get_blavatar' )->andReturn( '<div>Blavatar</div>' );
25+
26+
$blog_b = \Mockery::mock( MslsBlog::class );
27+
$blog_b->userblog_id = 2;
28+
$blog_b->shouldReceive( 'get_title' )->andReturn( 'Blog B' );
29+
$blog_b->shouldReceive( 'get_blavatar' )->andReturn( '<div>Blavatar</div>' );
30+
31+
$blog_c = null;
32+
33+
$collection = \Mockery::mock( MslsBlogCollection::class );
34+
$collection->shouldReceive( 'get_current_blog' )->andReturn( $blog_a );
35+
$collection->shouldReceive( 'get_plugin_active_blogs' )->andReturn( array( $blog_a, $blog_b, $blog_c ) );
36+
37+
$this->test = new MslsAdminBar( $options, $collection );
38+
}
39+
40+
public function test_add_node_false(): void {
41+
$wp_admin_bar = \Mockery::mock( \WP_Admin_Bar::class );
42+
$wp_admin_bar->shouldReceive( 'get_node' )->once()->andReturnNull();
43+
44+
$this->assertFalse( $this->test->add_node( $wp_admin_bar, 'node-id', 'title' ) );
45+
}
46+
47+
public function test_add_node_true(): void {
48+
$wp_admin_bar = \Mockery::mock( \WP_Admin_Bar::class );
49+
$wp_admin_bar->shouldReceive( 'get_node' )->once()->andReturn( (object) array() );
50+
$wp_admin_bar->shouldReceive( 'add_node' )->once();
51+
52+
$this->assertTrue( $this->test->add_node( $wp_admin_bar, 'node-id', 'title' ) );
53+
}
54+
55+
public function test_update_admin_bar(): void {
56+
$wp_admin_bar = \Mockery::mock( \WP_Admin_Bar::class );
57+
$wp_admin_bar->shouldReceive( 'get_node' )->times( 3 )->andReturn( (object) array() );
58+
$wp_admin_bar->shouldReceive( 'add_node' )->times( 3 );
59+
60+
$this->expectOutputString( '' );
61+
$this->test->update_admin_bar( $wp_admin_bar );
62+
}
63+
}

tests/phpunit/TestMslsPlugin.php

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -242,15 +242,4 @@ public function test_activate(): void {
242242

243243
$this->expectOutputString( '' );
244244
}
245-
246-
public function test_admin_bar_init_true(): void {
247-
Functions\expect( 'is_admin_bar_showing' )->once()->andReturnTrue();
248-
249-
$this->assertTrue( MslsPlugin::admin_bar_init() );
250-
}
251-
public function test_admin_bar_init_false(): void {
252-
Functions\expect( 'is_admin_bar_showing' )->once()->andReturn( false );
253-
254-
$this->assertFalse( MslsPlugin::admin_bar_init() );
255-
}
256245
}

0 commit comments

Comments
 (0)