Skip to content

Commit b91b6bd

Browse files
authored
@-name rewrites: don't redirect to slashed version (#1447)
1 parent 28f0666 commit b91b6bd

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Significance: patch
2+
Type: changed
3+
4+
Don't redirect @-name URLs to trailing slashed versions

includes/class-activitypub.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public static function init() {
2626
\add_filter( 'template_include', array( self::class, 'render_activitypub_template' ), 99 );
2727
\add_action( 'template_redirect', array( self::class, 'template_redirect' ) );
2828
\add_filter( 'redirect_canonical', array( self::class, 'redirect_canonical' ), 10, 2 );
29+
\add_filter( 'redirect_canonical', array( self::class, 'no_trailing_redirect' ), 10, 2 );
2930
\add_filter( 'query_vars', array( self::class, 'add_query_vars' ) );
3031
\add_filter( 'pre_get_avatar_data', array( self::class, 'pre_get_avatar_data' ), 11, 2 );
3132

@@ -209,6 +210,22 @@ function () use ( $id ) {
209210
);
210211
}
211212

213+
/**
214+
* Remove trailing slash from ActivityPub @username requests.
215+
*
216+
* @param string $redirect_url The URL to redirect to.
217+
* @param string $requested_url The requested URL.
218+
*
219+
* @return string $redirect_url The possibly-unslashed redirect URL.
220+
*/
221+
public static function no_trailing_redirect( $redirect_url, $requested_url ) {
222+
if ( get_query_var( 'actor' ) ) {
223+
return $requested_url;
224+
}
225+
226+
return $redirect_url;
227+
}
228+
212229
/**
213230
* Add support for `p` and `author` query vars.
214231
*

tests/includes/class-test-activitypub.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,4 +219,30 @@ public function test_custom_post_type_with_support_returns_200() {
219219
unset( $_SERVER['HTTP_ACCEPT'] );
220220
_unregister_post_type( 'test_cpt_supported' );
221221
}
222+
223+
/**
224+
* Test no_trailing_redirect method.
225+
*
226+
* @covers ::no_trailing_redirect
227+
*/
228+
public function test_no_trailing_redirect() {
229+
// Test case 1: When actor query var is set, it should return the requested URL.
230+
set_query_var( 'actor', 'testuser' );
231+
$requested_url = 'https://example.org/@testuser';
232+
$redirect_url = 'https://example.org/@testuser/';
233+
234+
$result = \Activitypub\Activitypub::no_trailing_redirect( $redirect_url, $requested_url );
235+
$this->assertEquals( $requested_url, $result, 'Should return requested URL when actor query var is set.' );
236+
237+
// Test case 2: When actor query var is not set, it should return the redirect URL.
238+
set_query_var( 'actor', '' );
239+
$requested_url = 'https://example.org/some-page';
240+
$redirect_url = 'https://example.org/some-page/';
241+
242+
$result = \Activitypub\Activitypub::no_trailing_redirect( $redirect_url, $requested_url );
243+
$this->assertEquals( $redirect_url, $result, 'Should return redirect URL when actor query var is not set.' );
244+
245+
// Clean up.
246+
set_query_var( 'actor', null );
247+
}
222248
}

0 commit comments

Comments
 (0)