diff --git a/MultisiteLanguageSwitcher.php b/MultisiteLanguageSwitcher.php index ecb941be..69c63b8c 100644 --- a/MultisiteLanguageSwitcher.php +++ b/MultisiteLanguageSwitcher.php @@ -76,6 +76,11 @@ function get_the_msls( $attr ): string { * @uses get_the_msls * * @param string[] $arr + public static function init(): void + { + add_filter( 'the_content', array( $obj, 'content_filter' ) ); + + } */ function the_msls( array $arr = array() ): void { echo get_the_msls( $arr ); diff --git a/includes/MslsContentFilter.php b/includes/MslsContentFilter.php new file mode 100644 index 00000000..e91bf78f --- /dev/null +++ b/includes/MslsContentFilter.php @@ -0,0 +1,76 @@ +options = $options; + } + + /** + * @codeCoverageIgnore + */ + public static function init(): void { + $obj = new self( msls_options() ); + + add_filter( 'the_content', array( $obj, 'content_filter' ) ); + } + + /** + * Filter for the_content() + * + * @param string $content + * + * @return string + */ + public function content_filter( string $content ) { + if ( ! is_front_page() && is_singular() && $this->options->is_content_filter() ) { + $content .= $this->filter_string(); + } + + return $content; + } + + /** + * Create filter-string for msls_content_filter() + * + * @param string $pref + * @param string $post + * + * @return string + */ + public function filter_string( $pref = '
', $post = '
' ) { + $obj = MslsOutput::init(); + $links = $obj->get( 1, true, true ); + + /* translators: %s: list of languages */ + $output = __( 'This post is also available in %s.', 'multisite-language-switcher' ); + + if ( has_filter( 'msls_filter_string' ) ) { + /** + * Overrides the string for the output of the translation hint + * + * @param string $output + * @param array $links + * + * @since 1.0 + */ + $output = apply_filters( 'msls_filter_string', $output, $links ); + } elseif ( count( $links ) > 1 ) { + $last = array_pop( $links ); + + /* translators: %1$s: list of languages separated by a comma, %2$s: last language */ + $format = __( '%1$s and %2$s', 'multisite-language-switcher' ); + + $output = sprintf( $output, sprintf( $format, implode( ', ', $links ), $last ) ); + } elseif ( 1 == count( $links ) ) { + $output = sprintf( $output, $links[0] ); + } + + return ! empty( $output ) ? $pref . $output . $post : ''; + } +} diff --git a/includes/MslsPlugin.php b/includes/MslsPlugin.php index c5366ae8..497ac1e4 100644 --- a/includes/MslsPlugin.php +++ b/includes/MslsPlugin.php @@ -49,11 +49,11 @@ public static function init() { add_action( 'init', array( MslsAdminBar::class, 'init' ) ); add_action( 'init', array( MslsBlock::class, 'init' ) ); add_action( 'init', array( MslsShortCode::class, 'init' ) ); + add_action( 'init', array( MslsContentFilter::class, 'init' ) ); add_action( 'widgets_init', array( MslsWidget::class, 'init' ) ); add_action( 'wp_head', array( __CLASS__, 'print_alternate_links' ) ); add_filter( 'msls_get_output', array( __CLASS__, 'get_output' ) ); - add_filter( 'the_content', array( $obj, 'content_filter' ) ); \lloc\Msls\ContentImport\Service::instance()->register(); @@ -127,70 +127,6 @@ public static function print_alternate_links() { echo self::get_output()->get_alternate_links(), PHP_EOL; } - /** - * Filter for the_content() - * - * @param string $content - * - * @return string - */ - public function content_filter( string $content ) { - if ( ! is_front_page() && is_singular() && $this->options->is_content_filter() ) { - $content .= $this->filter_string(); - } - - return $content; - } - - /** - * Create filter-string for msls_content_filter() - * - * @param string $pref - * @param string $post - * - * @return string - */ - public function filter_string( $pref = '', $post = '
' ) { - $obj = MslsOutput::init(); - $links = $obj->get( 1, true, true ); - - /* translators: %s: list of languages */ - $output = __( 'This post is also available in %s.', 'multisite-language-switcher' ); - - if ( has_filter( 'msls_filter_string' ) ) { - /** - * Overrides the string for the output of the translation hint - * - * @param string $output - * @param array $links - * - * @since 1.0 - */ - $output = apply_filters( 'msls_filter_string', $output, $links ); - } elseif ( count( $links ) > 1 ) { - $last = array_pop( $links ); - - /* translators: %1$s: list of languages separated by a comma, %2$s: last language */ - $format = __( '%1$s and %2$s', 'multisite-language-switcher' ); - - $output = sprintf( - $output, - sprintf( - $format, - implode( ', ', $links ), - $last - ) - ); - } elseif ( 1 == count( $links ) ) { - $output = sprintf( - $output, - $links[0] - ); - } - - return ! empty( $output ) ? $pref . $output . $post : ''; - } - /** * Loads styles and some js if needed * diff --git a/tests/phpunit/TestMslsContentFilter.php b/tests/phpunit/TestMslsContentFilter.php new file mode 100644 index 00000000..6c1ac81a --- /dev/null +++ b/tests/phpunit/TestMslsContentFilter.php @@ -0,0 +1,76 @@ +justReturn( $is_front_page ); + Functions\when( 'is_singular' )->justReturn( $is_singular ); + + $options = \Mockery::mock( MslsOptions::class ); + $options->shouldReceive( 'is_content_filter' )->andReturn( $is_content_filter ); + + $test = new MslsContentFilter( $options ); + + $this->assertEquals( $expected, $test->content_filter( $content ) ); + } + + public function test_content_filter_one_link() { + $blog = \Mockery::mock( MslsBlog::class ); + $blog->shouldReceive( 'get_language' )->once()->andReturn( 'de_DE' ); + $blog->shouldReceive( 'get_description' )->once()->andReturn( 'Deutsch' ); + + $collection = \Mockery::mock( MslsBlogCollection::class ); + $collection->shouldReceive( 'get_filtered' )->once()->andReturn( array( $blog ) ); + $collection->shouldReceive( 'is_current_blog' )->once()->andReturn( true ); + + $options = \Mockery::mock( MslsOptions::class ); + $options->shouldReceive( 'is_content_filter' )->andReturn( true ); + $options->shouldReceive( 'get_flag_url' )->once()->andReturn( 'https://lloc.de/wp-content/plugins/msls/flags/de.png' ); + + Functions\expect( 'is_front_page' )->twice()->andReturn( false ); + Functions\expect( 'is_admin' )->once()->andReturn( false ); + Functions\expect( 'is_search' )->once()->andReturn( false ); + Functions\expect( 'is_404' )->once()->andReturn( false ); + Functions\expect( 'is_category' )->once()->andReturn( false ); + Functions\expect( 'is_tag' )->once()->andReturn( false ); + Functions\expect( 'is_tax' )->once()->andReturn( false ); + Functions\expect( 'is_date' )->once()->andReturn( false ); + Functions\expect( 'is_author' )->once()->andReturn( false ); + Functions\expect( 'is_post_type_archive' )->once()->andReturn( false ); + Functions\expect( 'get_queried_object_id' )->once()->andReturn( 42 ); + Functions\expect( 'get_option' )->once()->andReturn( array() ); + Functions\expect( 'is_singular' )->once()->andReturn( true ); + Functions\expect( 'msls_options' )->once()->andReturn( $options ); + Functions\expect( 'msls_blog_collection' )->once()->andReturn( $collection ); + Functions\expect( 'get_permalink' )->once()->andReturn( 'https://msls.co/testpage/' ); + + $test = new MslsContentFilter( $options ); + + $content = 'Test>/p>'; + $expected = '
Test>/p>
This post is also available in Deutsch.
'; + $this->assertEquals( $expected, $test->content_filter( $content ) ); + } +} diff --git a/tests/phpunit/TestMslsPlugin.php b/tests/phpunit/TestMslsPlugin.php index 786d229a..34c767cb 100644 --- a/tests/phpunit/TestMslsPlugin.php +++ b/tests/phpunit/TestMslsPlugin.php @@ -146,31 +146,6 @@ public function test_print_alternate_links() { MslsPlugin::print_alternate_links(); } - protected function provide_content_filter_data(): array { - return array( - array( 'Test', 'Test', true, false, false ), - array( 'Test', 'Test', false, false, false ), - array( 'Test', 'Test', false, true, false ), - array( 'Test', 'Test', false, false, true ), - array( 'Test', 'Test', true, true, true ), - ); - } - - /** - * @dataProvider provide_content_filter_data - */ - public function test_content_filter_empty( string $content, string $expected, bool $is_front_page, bool $is_singular, bool $is_content_filter ) { - Functions\when( 'is_front_page' )->justReturn( $is_front_page ); - Functions\when( 'is_singular' )->justReturn( $is_singular ); - - $options = \Mockery::mock( MslsOptions::class ); - $options->shouldReceive( 'is_content_filter' )->andReturn( $is_content_filter ); - - $test = new MslsPlugin( $options ); - - $this->assertEquals( $expected, $test->content_filter( $content ) ); - } - public function test_activate(): void { Functions\expect( 'register_uninstall_hook' )->once();