Skip to content

MslsContentFilter refatored from MslsPlugin #328

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions MultisiteLanguageSwitcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
76 changes: 76 additions & 0 deletions includes/MslsContentFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

namespace lloc\Msls;

class MslsContentFilter {


protected MslsOptions $options;

public function __construct( MslsOptions $options ) {
$this->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 = '<p id="msls">', $post = '</p>' ) {
$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 : '';
}
}
66 changes: 1 addition & 65 deletions includes/MslsPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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 = '<p id="msls">', $post = '</p>' ) {
$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
*
Expand Down
76 changes: 76 additions & 0 deletions tests/phpunit/TestMslsContentFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php declare( strict_types=1 );

namespace lloc\MslsTests;

use Brain\Monkey\Functions;
use lloc\Msls\MslsBlog;
use lloc\Msls\MslsBlogCollection;
use lloc\Msls\MslsContentFilter;
use lloc\Msls\MslsOptions;

use lloc\Msls\MslsPlugin;

class TestMslsContentFilter extends MslsUnitTestCase {

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 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 = '<p>Test>/p>';
$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>';
$this->assertEquals( $expected, $test->content_filter( $content ) );
}
}
25 changes: 0 additions & 25 deletions tests/phpunit/TestMslsPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
Loading