Skip to content
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
10 changes: 8 additions & 2 deletions includes/MslsBlog.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,18 @@ public function get_url( $options ) {
protected function get_permalink( OptionsInterface $options ) {
$url = null;

$is_home = is_front_page();
$is_front_page = is_front_page();
$is_posts_page = ! $is_front_page && is_home();

switch_to_blog( $this->obj->userblog_id );

if ( $is_home || $options->has_value( $this->get_language() ) ) {
if ( $is_front_page || $options->has_value( $this->get_language() ) ) {
$url = apply_filters( self::MSLS_GET_PERMALINK_HOOK, $options->get_permalink( $this->get_language() ), $this );
} elseif ( $is_posts_page ) {
$page_for_posts = (int) get_option( 'page_for_posts' );
if ( $page_for_posts > 0 ) {
$url = apply_filters( self::MSLS_GET_PERMALINK_HOOK, (string) get_permalink( $page_for_posts ), $this );
}
}

restore_current_blog();
Expand Down
11 changes: 9 additions & 2 deletions includes/MslsOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -416,8 +416,15 @@ public static function check_for_blog_slug( $url, $options ) {
if ( $permalink_structure ) {
list( $needle, ) = explode( '/%', $permalink_structure, 2 );

$url = str_replace( $needle, '', $url );
if ( is_main_site() && $options->with_front ) {
$stripped = false;
if ( '' !== $needle && str_starts_with( $url, $needle ) ) {
$rest = substr( $url, strlen( $needle ) );
if ( '' === $rest || '/' === $rest[0] ) {
$url = '' === $rest ? '/' : $rest;
$stripped = true;
}
}
if ( is_main_site() && $options->with_front && $stripped ) {
$url = "{$needle}{$url}";
}
}
Expand Down
45 changes: 43 additions & 2 deletions tests/phpunit/TestMslsBlog.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ public function test_get_frontpage(): void {
$collection->shouldReceive( 'get_current_blog_id' )->andReturn( 2 );

Functions\expect( 'msls_blog_collection' )->once()->andReturn( $collection );
Functions\expect( 'is_front_page' )->once()->andReturn( true );
Functions\expect( 'is_front_page' )->atLeast()->once()->andReturn( true );
Functions\expect( 'is_home' )->zeroOrMoreTimes()->andReturn( false );
Functions\expect( 'switch_to_blog' )->once();
Functions\expect( 'restore_current_blog' )->once();

Expand All @@ -70,7 +71,47 @@ public function test_get_url(): void {
$collection->shouldReceive( 'get_current_blog_id' )->andReturn( 2 );

Functions\expect( 'msls_blog_collection' )->once()->andReturn( $collection );
Functions\expect( 'is_front_page' )->once()->andReturn( false );
Functions\expect( 'is_front_page' )->atLeast()->once()->andReturn( false );
Functions\expect( 'is_home' )->atLeast()->once()->andReturn( false );
Functions\expect( 'switch_to_blog' )->once();
Functions\expect( 'restore_current_blog' )->once();

$this->assertEquals( $url, $this->MslsBlogFactory()->get_url( $option ) );
}

public function test_get_posts_page(): void {
$url = 'https://msls.co/sv/blogg/';

$option = \Mockery::mock( MslsOptions::class );
$option->shouldReceive( 'has_value' )->once()->andReturn( false );

$collection = \Mockery::mock( MslsBlogCollection::class );
$collection->shouldReceive( 'get_current_blog_id' )->andReturn( 2 );

Functions\expect( 'msls_blog_collection' )->once()->andReturn( $collection );
Functions\expect( 'is_front_page' )->atLeast()->once()->andReturn( false );
Functions\expect( 'is_home' )->atLeast()->once()->andReturn( true );
Functions\expect( 'switch_to_blog' )->once();
Functions\expect( 'restore_current_blog' )->once();
Functions\expect( 'get_option' )->atLeast()->once()->andReturn( 42 );
Functions\expect( 'get_permalink' )->once()->with( 42 )->andReturn( $url );

$this->assertEquals( $url, $this->MslsBlogFactory()->get_url( $option ) );
}

public function test_get_posts_page_with_translation(): void {
$url = 'https://msls.co/sv/blogg/';

$option = \Mockery::mock( MslsOptions::class );
$option->shouldReceive( 'get_permalink' )->once()->andReturn( $url );
$option->shouldReceive( 'has_value' )->once()->andReturn( true );

$collection = \Mockery::mock( MslsBlogCollection::class );
$collection->shouldReceive( 'get_current_blog_id' )->andReturn( 2 );

Functions\expect( 'msls_blog_collection' )->once()->andReturn( $collection );
Functions\expect( 'is_front_page' )->atLeast()->once()->andReturn( false );
Functions\expect( 'is_home' )->atLeast()->once()->andReturn( true );
Functions\expect( 'switch_to_blog' )->once();
Functions\expect( 'restore_current_blog' )->once();

Expand Down
3 changes: 3 additions & 0 deletions tests/phpunit/TestMslsOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,9 @@ public static function provide_data_for_slug_check(): array {
array( 'https://msls.co/blog/2024/05/test', 'https://msls.co/blog/2024/05/test', true, true, true, '/blog/%year%/%monthnum%/%postname%/', true ),
array( 'https://msls.co/blog/test', 'https://msls.co/blog/test', true, true, true, '/%postname%/', true ),
array( 'https://msls.co/blog/test', 'https://msls.co/blog/test', true, true, true, '/blog/%postname%/', true ),
array( 'https://msls.co/blogg/', 'https://msls.co/blogg/', true, true, true, '/blog/%postname%/', false ),
array( 'https://msls.co/blogg/', 'https://msls.co/blogg/', true, true, true, '/blog/%postname%/', true ),
array( 'https://msls.co/blog/', 'https://msls.co/', true, true, true, '/blog/%postname%/', false ),
);
}

Expand Down
Loading