Skip to content

Commit 2310f0d

Browse files
apermolloc
andauthored
Fix broken latest posts page links (#581)
* fix(MslsBlog): add is_home() for posts pages When a static page is set as WordPress "Posts page", is_home() returns true but is_front_page() returns false. The language switcher now detects this case and falls back to the target blog's page_for_posts when pages are not explicitly linked via MSLS. Closes #97 * fix(MslsOptions): prevent blog slug corruption The greedy str_replace in check_for_blog_slug() matched /blog as substring of /blogg/, producing truncated URLs like /sv/g/ instead of /sv/blogg/. Replace with boundary-aware prefix stripping that only removes the slug when followed by / or EOL. * refactor(MslsBlog): rename $is_home to $is_front_page Rename misleading variable and apply permalink hook to the posts-page fallback for consistent filtering. * fix(MslsOptions): only re-add slug when stripped The is_main_site re-addition of the blog slug was unconditional, corrupting URLs where the slug was not stripped (e.g. /blogg/ becoming /blog/blogg/). * test(MslsBlog): drop explicit apply_filters mock Brain\Monkey handles apply_filters by default. The explicit mock was inconsistent with other tests and unnecessarily fragile. --------- Co-authored-by: Dennis Ploetner <re@lloc.de>
1 parent 5d3e43d commit 2310f0d

File tree

4 files changed

+63
-6
lines changed

4 files changed

+63
-6
lines changed

includes/MslsBlog.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,12 +134,18 @@ public function get_url( $options ) {
134134
protected function get_permalink( OptionsInterface $options ) {
135135
$url = null;
136136

137-
$is_home = is_front_page();
137+
$is_front_page = is_front_page();
138+
$is_posts_page = ! $is_front_page && is_home();
138139

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

141-
if ( $is_home || $options->has_value( $this->get_language() ) ) {
142+
if ( $is_front_page || $options->has_value( $this->get_language() ) ) {
142143
$url = apply_filters( self::MSLS_GET_PERMALINK_HOOK, $options->get_permalink( $this->get_language() ), $this );
144+
} elseif ( $is_posts_page ) {
145+
$page_for_posts = (int) get_option( 'page_for_posts' );
146+
if ( $page_for_posts > 0 ) {
147+
$url = apply_filters( self::MSLS_GET_PERMALINK_HOOK, (string) get_permalink( $page_for_posts ), $this );
148+
}
143149
}
144150

145151
restore_current_blog();

includes/MslsOptions.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -416,8 +416,15 @@ public static function check_for_blog_slug( $url, $options ) {
416416
if ( $permalink_structure ) {
417417
list( $needle, ) = explode( '/%', $permalink_structure, 2 );
418418

419-
$url = str_replace( $needle, '', $url );
420-
if ( is_main_site() && $options->with_front ) {
419+
$stripped = false;
420+
if ( '' !== $needle && str_starts_with( $url, $needle ) ) {
421+
$rest = substr( $url, strlen( $needle ) );
422+
if ( '' === $rest || '/' === $rest[0] ) {
423+
$url = '' === $rest ? '/' : $rest;
424+
$stripped = true;
425+
}
426+
}
427+
if ( is_main_site() && $options->with_front && $stripped ) {
421428
$url = "{$needle}{$url}";
422429
}
423430
}

tests/phpunit/TestMslsBlog.php

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ public function test_get_frontpage(): void {
5252
$collection->shouldReceive( 'get_current_blog_id' )->andReturn( 2 );
5353

5454
Functions\expect( 'msls_blog_collection' )->once()->andReturn( $collection );
55-
Functions\expect( 'is_front_page' )->once()->andReturn( true );
55+
Functions\expect( 'is_front_page' )->atLeast()->once()->andReturn( true );
56+
Functions\expect( 'is_home' )->zeroOrMoreTimes()->andReturn( false );
5657
Functions\expect( 'switch_to_blog' )->once();
5758
Functions\expect( 'restore_current_blog' )->once();
5859

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

7273
Functions\expect( 'msls_blog_collection' )->once()->andReturn( $collection );
73-
Functions\expect( 'is_front_page' )->once()->andReturn( false );
74+
Functions\expect( 'is_front_page' )->atLeast()->once()->andReturn( false );
75+
Functions\expect( 'is_home' )->atLeast()->once()->andReturn( false );
76+
Functions\expect( 'switch_to_blog' )->once();
77+
Functions\expect( 'restore_current_blog' )->once();
78+
79+
$this->assertEquals( $url, $this->MslsBlogFactory()->get_url( $option ) );
80+
}
81+
82+
public function test_get_posts_page(): void {
83+
$url = 'https://msls.co/sv/blogg/';
84+
85+
$option = \Mockery::mock( MslsOptions::class );
86+
$option->shouldReceive( 'has_value' )->once()->andReturn( false );
87+
88+
$collection = \Mockery::mock( MslsBlogCollection::class );
89+
$collection->shouldReceive( 'get_current_blog_id' )->andReturn( 2 );
90+
91+
Functions\expect( 'msls_blog_collection' )->once()->andReturn( $collection );
92+
Functions\expect( 'is_front_page' )->atLeast()->once()->andReturn( false );
93+
Functions\expect( 'is_home' )->atLeast()->once()->andReturn( true );
94+
Functions\expect( 'switch_to_blog' )->once();
95+
Functions\expect( 'restore_current_blog' )->once();
96+
Functions\expect( 'get_option' )->atLeast()->once()->andReturn( 42 );
97+
Functions\expect( 'get_permalink' )->once()->with( 42 )->andReturn( $url );
98+
99+
$this->assertEquals( $url, $this->MslsBlogFactory()->get_url( $option ) );
100+
}
101+
102+
public function test_get_posts_page_with_translation(): void {
103+
$url = 'https://msls.co/sv/blogg/';
104+
105+
$option = \Mockery::mock( MslsOptions::class );
106+
$option->shouldReceive( 'get_permalink' )->once()->andReturn( $url );
107+
$option->shouldReceive( 'has_value' )->once()->andReturn( true );
108+
109+
$collection = \Mockery::mock( MslsBlogCollection::class );
110+
$collection->shouldReceive( 'get_current_blog_id' )->andReturn( 2 );
111+
112+
Functions\expect( 'msls_blog_collection' )->once()->andReturn( $collection );
113+
Functions\expect( 'is_front_page' )->atLeast()->once()->andReturn( false );
114+
Functions\expect( 'is_home' )->atLeast()->once()->andReturn( true );
74115
Functions\expect( 'switch_to_blog' )->once();
75116
Functions\expect( 'restore_current_blog' )->once();
76117

tests/phpunit/TestMslsOptions.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,9 @@ public static function provide_data_for_slug_check(): array {
204204
array( 'https://msls.co/blog/2024/05/test', 'https://msls.co/blog/2024/05/test', true, true, true, '/blog/%year%/%monthnum%/%postname%/', true ),
205205
array( 'https://msls.co/blog/test', 'https://msls.co/blog/test', true, true, true, '/%postname%/', true ),
206206
array( 'https://msls.co/blog/test', 'https://msls.co/blog/test', true, true, true, '/blog/%postname%/', true ),
207+
array( 'https://msls.co/blogg/', 'https://msls.co/blogg/', true, true, true, '/blog/%postname%/', false ),
208+
array( 'https://msls.co/blogg/', 'https://msls.co/blogg/', true, true, true, '/blog/%postname%/', true ),
209+
array( 'https://msls.co/blog/', 'https://msls.co/', true, true, true, '/blog/%postname%/', false ),
207210
);
208211
}
209212

0 commit comments

Comments
 (0)