Skip to content

Commit 5bc7506

Browse files
authored
Merge pull request #328 from lloc/refactoring-version-2-8
MslsContentFilter refatored from MslsPlugin
2 parents 299c351 + 9928386 commit 5bc7506

File tree

5 files changed

+158
-90
lines changed

5 files changed

+158
-90
lines changed

MultisiteLanguageSwitcher.php

+5
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,11 @@ function get_the_msls( $attr ): string {
7676
* @uses get_the_msls
7777
*
7878
* @param string[] $arr
79+
public static function init(): void
80+
{
81+
add_filter( 'the_content', array( $obj, 'content_filter' ) );
82+
83+
}
7984
*/
8085
function the_msls( array $arr = array() ): void {
8186
echo get_the_msls( $arr );

includes/MslsContentFilter.php

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
3+
namespace lloc\Msls;
4+
5+
class MslsContentFilter {
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+
add_filter( 'the_content', array( $obj, 'content_filter' ) );
21+
}
22+
23+
/**
24+
* Filter for the_content()
25+
*
26+
* @param string $content
27+
*
28+
* @return string
29+
*/
30+
public function content_filter( string $content ) {
31+
if ( ! is_front_page() && is_singular() && $this->options->is_content_filter() ) {
32+
$content .= $this->filter_string();
33+
}
34+
35+
return $content;
36+
}
37+
38+
/**
39+
* Create filter-string for msls_content_filter()
40+
*
41+
* @param string $pref
42+
* @param string $post
43+
*
44+
* @return string
45+
*/
46+
public function filter_string( $pref = '<p id="msls">', $post = '</p>' ) {
47+
$obj = MslsOutput::init();
48+
$links = $obj->get( 1, true, true );
49+
50+
/* translators: %s: list of languages */
51+
$output = __( 'This post is also available in %s.', 'multisite-language-switcher' );
52+
53+
if ( has_filter( 'msls_filter_string' ) ) {
54+
/**
55+
* Overrides the string for the output of the translation hint
56+
*
57+
* @param string $output
58+
* @param array $links
59+
*
60+
* @since 1.0
61+
*/
62+
$output = apply_filters( 'msls_filter_string', $output, $links );
63+
} elseif ( count( $links ) > 1 ) {
64+
$last = array_pop( $links );
65+
66+
/* translators: %1$s: list of languages separated by a comma, %2$s: last language */
67+
$format = __( '%1$s and %2$s', 'multisite-language-switcher' );
68+
69+
$output = sprintf( $output, sprintf( $format, implode( ', ', $links ), $last ) );
70+
} elseif ( 1 == count( $links ) ) {
71+
$output = sprintf( $output, $links[0] );
72+
}
73+
74+
return ! empty( $output ) ? $pref . $output . $post : '';
75+
}
76+
}

includes/MslsPlugin.php

+1-65
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,11 @@ public static function init() {
4949
add_action( 'init', array( MslsAdminBar::class, 'init' ) );
5050
add_action( 'init', array( MslsBlock::class, 'init' ) );
5151
add_action( 'init', array( MslsShortCode::class, 'init' ) );
52+
add_action( 'init', array( MslsContentFilter::class, 'init' ) );
5253
add_action( 'widgets_init', array( MslsWidget::class, 'init' ) );
5354
add_action( 'wp_head', array( __CLASS__, 'print_alternate_links' ) );
5455

5556
add_filter( 'msls_get_output', array( __CLASS__, 'get_output' ) );
56-
add_filter( 'the_content', array( $obj, 'content_filter' ) );
5757

5858
\lloc\Msls\ContentImport\Service::instance()->register();
5959

@@ -127,70 +127,6 @@ public static function print_alternate_links() {
127127
echo self::get_output()->get_alternate_links(), PHP_EOL;
128128
}
129129

130-
/**
131-
* Filter for the_content()
132-
*
133-
* @param string $content
134-
*
135-
* @return string
136-
*/
137-
public function content_filter( string $content ) {
138-
if ( ! is_front_page() && is_singular() && $this->options->is_content_filter() ) {
139-
$content .= $this->filter_string();
140-
}
141-
142-
return $content;
143-
}
144-
145-
/**
146-
* Create filter-string for msls_content_filter()
147-
*
148-
* @param string $pref
149-
* @param string $post
150-
*
151-
* @return string
152-
*/
153-
public function filter_string( $pref = '<p id="msls">', $post = '</p>' ) {
154-
$obj = MslsOutput::init();
155-
$links = $obj->get( 1, true, true );
156-
157-
/* translators: %s: list of languages */
158-
$output = __( 'This post is also available in %s.', 'multisite-language-switcher' );
159-
160-
if ( has_filter( 'msls_filter_string' ) ) {
161-
/**
162-
* Overrides the string for the output of the translation hint
163-
*
164-
* @param string $output
165-
* @param array $links
166-
*
167-
* @since 1.0
168-
*/
169-
$output = apply_filters( 'msls_filter_string', $output, $links );
170-
} elseif ( count( $links ) > 1 ) {
171-
$last = array_pop( $links );
172-
173-
/* translators: %1$s: list of languages separated by a comma, %2$s: last language */
174-
$format = __( '%1$s and %2$s', 'multisite-language-switcher' );
175-
176-
$output = sprintf(
177-
$output,
178-
sprintf(
179-
$format,
180-
implode( ', ', $links ),
181-
$last
182-
)
183-
);
184-
} elseif ( 1 == count( $links ) ) {
185-
$output = sprintf(
186-
$output,
187-
$links[0]
188-
);
189-
}
190-
191-
return ! empty( $output ) ? $pref . $output . $post : '';
192-
}
193-
194130
/**
195131
* Loads styles and some js if needed
196132
*
+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php declare( strict_types=1 );
2+
3+
namespace lloc\MslsTests;
4+
5+
use Brain\Monkey\Functions;
6+
use lloc\Msls\MslsBlog;
7+
use lloc\Msls\MslsBlogCollection;
8+
use lloc\Msls\MslsContentFilter;
9+
use lloc\Msls\MslsOptions;
10+
11+
use lloc\Msls\MslsPlugin;
12+
13+
class TestMslsContentFilter extends MslsUnitTestCase {
14+
15+
protected function provide_content_filter_data(): array {
16+
return array(
17+
array( 'Test', 'Test', true, false, false ),
18+
array( 'Test', 'Test', false, false, false ),
19+
array( 'Test', 'Test', false, true, false ),
20+
array( 'Test', 'Test', false, false, true ),
21+
array( 'Test', 'Test', true, true, true ),
22+
);
23+
}
24+
25+
/**
26+
* @dataProvider provide_content_filter_data
27+
*/
28+
public function test_content_filter_empty( string $content, string $expected, bool $is_front_page, bool $is_singular, bool $is_content_filter ) {
29+
Functions\when( 'is_front_page' )->justReturn( $is_front_page );
30+
Functions\when( 'is_singular' )->justReturn( $is_singular );
31+
32+
$options = \Mockery::mock( MslsOptions::class );
33+
$options->shouldReceive( 'is_content_filter' )->andReturn( $is_content_filter );
34+
35+
$test = new MslsContentFilter( $options );
36+
37+
$this->assertEquals( $expected, $test->content_filter( $content ) );
38+
}
39+
40+
public function test_content_filter_one_link() {
41+
$blog = \Mockery::mock( MslsBlog::class );
42+
$blog->shouldReceive( 'get_language' )->once()->andReturn( 'de_DE' );
43+
$blog->shouldReceive( 'get_description' )->once()->andReturn( 'Deutsch' );
44+
45+
$collection = \Mockery::mock( MslsBlogCollection::class );
46+
$collection->shouldReceive( 'get_filtered' )->once()->andReturn( array( $blog ) );
47+
$collection->shouldReceive( 'is_current_blog' )->once()->andReturn( true );
48+
49+
$options = \Mockery::mock( MslsOptions::class );
50+
$options->shouldReceive( 'is_content_filter' )->andReturn( true );
51+
$options->shouldReceive( 'get_flag_url' )->once()->andReturn( 'https://lloc.de/wp-content/plugins/msls/flags/de.png' );
52+
53+
Functions\expect( 'is_front_page' )->twice()->andReturn( false );
54+
Functions\expect( 'is_admin' )->once()->andReturn( false );
55+
Functions\expect( 'is_search' )->once()->andReturn( false );
56+
Functions\expect( 'is_404' )->once()->andReturn( false );
57+
Functions\expect( 'is_category' )->once()->andReturn( false );
58+
Functions\expect( 'is_tag' )->once()->andReturn( false );
59+
Functions\expect( 'is_tax' )->once()->andReturn( false );
60+
Functions\expect( 'is_date' )->once()->andReturn( false );
61+
Functions\expect( 'is_author' )->once()->andReturn( false );
62+
Functions\expect( 'is_post_type_archive' )->once()->andReturn( false );
63+
Functions\expect( 'get_queried_object_id' )->once()->andReturn( 42 );
64+
Functions\expect( 'get_option' )->once()->andReturn( array() );
65+
Functions\expect( 'is_singular' )->once()->andReturn( true );
66+
Functions\expect( 'msls_options' )->once()->andReturn( $options );
67+
Functions\expect( 'msls_blog_collection' )->once()->andReturn( $collection );
68+
Functions\expect( 'get_permalink' )->once()->andReturn( 'https://msls.co/testpage/' );
69+
70+
$test = new MslsContentFilter( $options );
71+
72+
$content = '<p>Test>/p>';
73+
$expected = '<p>Test>/p><p id="msls">This post is also available in <a href="https://msls.co/testpage/" title="Deutsch" class="current_language">Deutsch</a>.</p>';
74+
$this->assertEquals( $expected, $test->content_filter( $content ) );
75+
}
76+
}

tests/phpunit/TestMslsPlugin.php

-25
Original file line numberDiff line numberDiff line change
@@ -146,31 +146,6 @@ public function test_print_alternate_links() {
146146
MslsPlugin::print_alternate_links();
147147
}
148148

149-
protected function provide_content_filter_data(): array {
150-
return array(
151-
array( 'Test', 'Test', true, false, false ),
152-
array( 'Test', 'Test', false, false, false ),
153-
array( 'Test', 'Test', false, true, false ),
154-
array( 'Test', 'Test', false, false, true ),
155-
array( 'Test', 'Test', true, true, true ),
156-
);
157-
}
158-
159-
/**
160-
* @dataProvider provide_content_filter_data
161-
*/
162-
public function test_content_filter_empty( string $content, string $expected, bool $is_front_page, bool $is_singular, bool $is_content_filter ) {
163-
Functions\when( 'is_front_page' )->justReturn( $is_front_page );
164-
Functions\when( 'is_singular' )->justReturn( $is_singular );
165-
166-
$options = \Mockery::mock( MslsOptions::class );
167-
$options->shouldReceive( 'is_content_filter' )->andReturn( $is_content_filter );
168-
169-
$test = new MslsPlugin( $options );
170-
171-
$this->assertEquals( $expected, $test->content_filter( $content ) );
172-
}
173-
174149
public function test_activate(): void {
175150
Functions\expect( 'register_uninstall_hook' )->once();
176151

0 commit comments

Comments
 (0)