Skip to content

Commit 84a4d71

Browse files
authored
Merge pull request #322 from lloc/refactoring-version-2-8
MslsBlock and MslsShortCode introduced
2 parents b99dab3 + ab6c6e4 commit 84a4d71

8 files changed

+165
-137
lines changed

includes/MslsBlock.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace lloc\Msls;
4+
5+
class MslsBlock {
6+
7+
8+
protected MslsOptions $options;
9+
10+
public function __construct( MslsOptions $options ) {
11+
$this->options = $options;
12+
}
13+
14+
/**
15+
* @codeCoverageIgnore
16+
*/
17+
public static function init(): void {
18+
$obj = new self( msls_options() );
19+
20+
if ( function_exists( 'register_block_type' ) ) {
21+
$obj->register_block();
22+
}
23+
}
24+
25+
/**
26+
* Register block and shortcode.
27+
*/
28+
public function register_block(): bool {
29+
if ( $this->options->is_excluded() ) {
30+
return false;
31+
}
32+
33+
register_block_type( MslsPlugin::plugin_dir_path( 'js/msls-widget-block' ) );
34+
35+
return true;
36+
}
37+
}

includes/MslsPlugin.php

Lines changed: 8 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -43,20 +43,17 @@ public static function init() {
4343
register_activation_hook( self::file(), array( $obj, 'activate' ) );
4444

4545
if ( function_exists( 'is_multisite' ) && is_multisite() ) {
46-
add_filter( 'msls_get_output', array( __CLASS__, 'get_output' ) );
47-
add_action( 'init', array( MslsAdminBar::class, 'init' ) );
48-
49-
add_action( 'widgets_init', array( $obj, 'init_widget' ) );
50-
add_filter( 'the_content', array( $obj, 'content_filter' ) );
46+
add_action( 'admin_enqueue_scripts', array( $obj, 'custom_enqueue' ) );
47+
add_action( 'wp_enqueue_scripts', array( $obj, 'custom_enqueue' ) );
5148

49+
add_action( 'init', array( MslsAdminBar::class, 'init' ) );
50+
add_action( 'init', array( MslsBlock::class, 'init' ) );
51+
add_action( 'init', array( MslsShortCode::class, 'init' ) );
52+
add_action( 'widgets_init', array( MslsWidget::class, 'init' ) );
5253
add_action( 'wp_head', array( __CLASS__, 'print_alternate_links' ) );
5354

54-
if ( function_exists( 'register_block_type' ) ) {
55-
add_action( 'init', array( $obj, 'block_init' ) );
56-
}
57-
58-
add_action( 'admin_enqueue_scripts', array( $obj, 'custom_enqueue' ) );
59-
add_action( 'wp_enqueue_scripts', array( $obj, 'custom_enqueue' ) );
55+
add_filter( 'msls_get_output', array( __CLASS__, 'get_output' ) );
56+
add_filter( 'the_content', array( $obj, 'content_filter' ) );
6057

6158
\lloc\Msls\ContentImport\Service::instance()->register();
6259

@@ -191,22 +188,6 @@ public function filter_string( $pref = '<p id="msls">', $post = '</p>' ) {
191188
return ! empty( $output ) ? $pref . $output . $post : '';
192189
}
193190

194-
/**
195-
* Register block and shortcode.
196-
*
197-
* @return bool
198-
*/
199-
public function block_init() {
200-
if ( ! $this->options->is_excluded() ) {
201-
register_block_type( self::plugin_dir_path( 'js/msls-widget-block' ) );
202-
add_shortcode( 'sc_msls_widget', array( $this, 'block_render' ) );
203-
204-
return true;
205-
}
206-
207-
return false;
208-
}
209-
210191
/**
211192
* Loads styles and some js if needed
212193
*
@@ -279,41 +260,6 @@ public static function path(): string {
279260
return defined( 'MSLS_PLUGIN_PATH' ) ? constant( 'MSLS_PLUGIN_PATH' ) : '';
280261
}
281262

282-
/**
283-
* Register widget
284-
*
285-
* The widget will only be registered if the current blog is not
286-
* excluded in the configuration of the plugin.
287-
*
288-
* @return boolean
289-
*/
290-
public function init_widget() {
291-
if ( ! $this->options->is_excluded() ) {
292-
register_widget( MslsWidget::class );
293-
294-
return true;
295-
}
296-
297-
return false;
298-
}
299-
300-
/**
301-
* Render widget output
302-
*
303-
* @return string
304-
*/
305-
public function block_render() {
306-
if ( ! $this->init_widget() ) {
307-
return '';
308-
}
309-
310-
ob_start();
311-
the_widget( MslsWidget::class );
312-
$output = ob_get_clean();
313-
314-
return $output;
315-
}
316-
317263
/**
318264
* Load textdomain
319265
*

includes/MslsShortCode.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace lloc\Msls;
4+
5+
class MslsShortCode {
6+
7+
8+
protected MslsOptions $options;
9+
10+
public function __construct( MslsOptions $options ) {
11+
$this->options = $options;
12+
}
13+
14+
/**
15+
* @codeCoverageIgnore
16+
*/
17+
public static function init(): void {
18+
$obj = new self( msls_options() );
19+
add_shortcode( 'sc_msls_widget', array( $obj, 'block_render' ) );
20+
}
21+
22+
/**
23+
* Renders output using the widget's output
24+
*
25+
* @return string|false
26+
*/
27+
public function block_render() {
28+
if ( $this->options->is_excluded() ) {
29+
return '';
30+
}
31+
32+
ob_start();
33+
the_widget( MslsWidget::class );
34+
$output = ob_get_clean();
35+
36+
return $output;
37+
}
38+
}

includes/MslsWidget.php

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22
/**
33
* MslsWidget
4+
*
45
* @author Dennis Ploetner <re@lloc.de>
56
* @since 0.9.8
67
*/
@@ -9,20 +10,32 @@
910

1011
/**
1112
* The standard widget of the Multisite Language Switcher
13+
*
1214
* @package Msls
1315
*/
1416
class MslsWidget extends \WP_Widget {
1517

1618
public $id_base = 'mslswidget';
1719

1820
/**
19-
* Constructor
21+
* @codeCoverageIgnore
2022
*/
2123
public function __construct() {
22-
$name = apply_filters( 'msls_widget_title',
23-
__( 'Multisite Language Switcher', 'multisite-language-switcher' ) );
24+
$name = apply_filters(
25+
'msls_widget_title',
26+
__( 'Multisite Language Switcher', 'multisite-language-switcher' )
27+
);
2428

25-
parent::__construct( $this->id_base, $name, [ 'show_instance_in_rest' => true ] );
29+
parent::__construct( $this->id_base, $name, array( 'show_instance_in_rest' => true ) );
30+
}
31+
32+
/**
33+
* @codeCoverageIgnore
34+
*/
35+
public function init(): void {
36+
if ( ! msls_options()->is_excluded() ) {
37+
register_widget( self::class );
38+
}
2639
}
2740

2841
/**
@@ -34,12 +47,12 @@ public function __construct() {
3447
* @user MslsOutput
3548
*/
3649
public function widget( $args, $instance ) {
37-
$default = [
50+
$default = array(
3851
'before_widget' => '',
3952
'after_widget' => '',
4053
'before_title' => '',
4154
'after_title' => '',
42-
];
55+
);
4356

4457
$args = wp_parse_args( $args, $default );
4558

@@ -51,7 +64,7 @@ public function widget( $args, $instance ) {
5164

5265
$content = MslsOutput::init()->__toString();
5366
if ( '' === $content ) {
54-
$text = __( 'No available translations found', 'multisite-language-switcher' );
67+
$text = __( 'No available translations found', 'multisite-language-switcher' );
5568
$content = apply_filters( 'msls_widget_alternative_content', $text );
5669
}
5770

@@ -92,5 +105,4 @@ public function form( $instance ) {
92105
( isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : '' )
93106
);
94107
}
95-
96108
}

tests/phpunit/TestMslsAdminBar.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,9 @@
22

33
namespace lloc\MslsTests;
44

5-
use Brain\Monkey\Functions;
65
use lloc\Msls\MslsAdminBar;
76
use lloc\Msls\MslsBlog;
87
use lloc\Msls\MslsBlogCollection;
9-
use lloc\Msls\MslsLink;
108
use lloc\Msls\MslsOptions;
119

1210
class TestMslsAdminBar extends MslsUnitTestCase {

tests/phpunit/TestMslsBlock.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php declare( strict_types=1 );
2+
3+
namespace lloc\MslsTests;
4+
5+
use Brain\Monkey\Functions;
6+
use lloc\Msls\MslsBlock;
7+
use lloc\Msls\MslsOptions;
8+
9+
class TestMslsBlock extends MslsUnitTestCase {
10+
11+
public function test_register_block_excluded_true(): void {
12+
$options = \Mockery::mock( MslsOptions::class );
13+
$options->shouldReceive( 'is_excluded' )->andReturn( true );
14+
15+
$this->assertFalse( ( new MslsBlock( $options ) )->register_block() );
16+
}
17+
18+
19+
public function test_register_block_excluded_false(): void {
20+
$options = \Mockery::mock( MslsOptions::class );
21+
$options->shouldReceive( 'is_excluded' )->andReturn( false );
22+
23+
Functions\expect( 'register_block_type' )->once();
24+
Functions\expect( 'plugin_dir_path' )->once();
25+
26+
$this->assertTrue( ( new MslsBlock( $options ) )->register_block() );
27+
}
28+
}

tests/phpunit/TestMslsPlugin.php

Lines changed: 1 addition & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -48,26 +48,6 @@ function test_admin_menu_admin_bar_not_showing(): void {
4848
$this->assertFalse( $test->custom_enqueue() );
4949
}
5050

51-
function test_init_widget_not_excluded(): void {
52-
Functions\expect( 'register_widget' )->once();
53-
54-
$options = \Mockery::mock( MslsOptions::class );
55-
$options->shouldReceive( 'is_excluded' )->andReturnFalse();
56-
57-
$test = new MslsPlugin( $options );
58-
59-
$this->assertTrue( $test->init_widget() );
60-
}
61-
62-
function test_init_widget_excluded(): void {
63-
$options = \Mockery::mock( MslsOptions::class );
64-
$options->shouldReceive( 'is_excluded' )->andReturnTrue();
65-
66-
$test = new MslsPlugin( $options );
67-
68-
$this->assertFalse( $test->init_widget() );
69-
}
70-
7151
/**
7252
* Verify the static init_i18n_support-method
7353
*/
@@ -175,6 +155,7 @@ protected function provide_content_filter_data(): array {
175155
array( 'Test', 'Test', true, true, true ),
176156
);
177157
}
158+
178159
/**
179160
* @dataProvider provide_content_filter_data
180161
*/
@@ -190,51 +171,6 @@ public function test_content_filter_empty( string $content, string $expected, bo
190171
$this->assertEquals( $expected, $test->content_filter( $content ) );
191172
}
192173

193-
public function test_block_init_excluded() {
194-
$options = \Mockery::mock( MslsOptions::class );
195-
$options->shouldReceive( 'is_excluded' )->once()->andReturn( true );
196-
197-
$test = new MslsPlugin( $options );
198-
199-
$this->assertFalse( $test->block_init() );
200-
}
201-
202-
public function test_block_init_not_excluded() {
203-
Functions\expect( 'register_block_type' )->once();
204-
Functions\expect( 'add_shortcode' )->once();
205-
Functions\expect( 'plugin_dir_path' )->once();
206-
207-
$options = \Mockery::mock( MslsOptions::class );
208-
$options->shouldReceive( 'is_excluded' )->once()->andReturn( false );
209-
210-
$test = new MslsPlugin( $options );
211-
212-
$this->assertTrue( $test->block_init() );
213-
}
214-
215-
public function test_block_render(): void {
216-
$expected = '<div id="msls-widget"></div>';
217-
218-
Functions\expect( 'register_widget' )->once();
219-
Functions\when( 'the_widget' )->justEcho( $expected );
220-
221-
$options = \Mockery::mock( MslsOptions::class );
222-
$options->shouldReceive( 'is_excluded' )->once()->andReturn( false );
223-
224-
$test = new MslsPlugin( $options );
225-
226-
$this->assertEquals( $expected, $test->block_render() );
227-
}
228-
229-
public function test_block_render_exclude(): void {
230-
$options = \Mockery::mock( MslsOptions::class );
231-
$options->shouldReceive( 'is_excluded' )->once()->andReturn( true );
232-
233-
$test = new MslsPlugin( $options );
234-
235-
$this->assertEquals( '', $test->block_render() );
236-
}
237-
238174
public function test_activate(): void {
239175
Functions\expect( 'register_uninstall_hook' )->once();
240176

0 commit comments

Comments
 (0)