Skip to content

Commit 29db902

Browse files
authored
Refactor actor lookup order for HTTP URIs (#2327)
1 parent d58baeb commit 29db902

File tree

4 files changed

+97
-7
lines changed

4 files changed

+97
-7
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: fixed
3+
4+
Improved actor resolution by prioritizing blog actor detection before remote actor checks and refining home page URL handling.

includes/class-router.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ public static function init() {
2525
\add_filter( 'redirect_canonical', array( self::class, 'redirect_canonical' ), 10, 2 );
2626
\add_filter( 'redirect_canonical', array( self::class, 'no_trailing_redirect' ), 10, 2 );
2727
\add_filter( 'query_vars', array( self::class, 'add_query_vars' ) );
28+
29+
\add_action( 'parse_query', array( self::class, 'fix_is_home_check' ) );
2830
}
2931

3032
/**
@@ -281,4 +283,22 @@ public static function add_query_vars( $vars ) {
281283

282284
return $vars;
283285
}
286+
287+
/**
288+
* Optimize home page query for ActivityPub requests.
289+
*
290+
* Skip the database query entirely for ActivityPub requests on the home page
291+
* since we only need to return the blog actor, not posts.
292+
*
293+
* @param \WP_Query $wp_query The WP_Query instance.
294+
*/
295+
public static function fix_is_home_check( $wp_query ) {
296+
if (
297+
$wp_query->get( 'actor' ) ||
298+
$wp_query->get( 'stamp' ) ||
299+
$wp_query->get( 'c' )
300+
) {
301+
$wp_query->is_home = false;
302+
}
303+
}
284304
}

includes/collection/class-actors.php

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -222,13 +222,6 @@ public static function get_id_by_resource( $uri ) {
222222
// Check for http(s) URIs.
223223
case 'http':
224224
case 'https':
225-
// Check locally stored remote Actor.
226-
$post = Remote_Actors::get_by_uri( $uri );
227-
228-
if ( ! \is_wp_error( $post ) ) {
229-
return $post->ID;
230-
}
231-
232225
// Check for http(s)://blog.example.com/@username.
233226
$resource_path = \wp_parse_url( $uri, PHP_URL_PATH );
234227

tests/phpunit/tests/includes/class-test-query.php

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,4 +518,77 @@ public function test_maybe_get_stamp_invalid_author() {
518518
// Clean up.
519519
\wp_delete_post( $post_id, true );
520520
}
521+
522+
/**
523+
* Test get_activitypub_object method for home page in Actor mode.
524+
*
525+
* @covers ::get_activitypub_object
526+
*/
527+
public function test_home_page_actor_mode() {
528+
\update_option( 'activitypub_actor_mode', ACTIVITYPUB_ACTOR_MODE );
529+
530+
$actor_queries = array();
531+
532+
// Track database queries using the 'query' filter.
533+
$query_filter = function ( $query ) use ( &$actor_queries ) {
534+
if ( strpos( $query, 'ap_actor' ) !== false ) {
535+
$actor_queries[] = $query;
536+
}
537+
return $query;
538+
};
539+
540+
add_filter( 'query', $query_filter );
541+
542+
Query::get_instance()->__destruct();
543+
$this->go_to( home_url( '/' ) );
544+
$object = Query::get_instance()->get_activitypub_object();
545+
546+
remove_filter( 'query', $query_filter );
547+
548+
$message = 'Should not query Remote_Actors table for home page.';
549+
if ( ! empty( $actor_queries ) ) {
550+
$message .= ' Found queries: ' . wp_json_encode( $actor_queries );
551+
}
552+
553+
$this->assertNull( $object, 'Home page should return null, because the Blog user is disabled.' );
554+
$this->assertEmpty( $actor_queries, $message );
555+
556+
\delete_option( 'activitypub_actor_mode' );
557+
}
558+
559+
/**
560+
* Test get_activitypub_object method for home page in Actor and Blog mode.
561+
*
562+
* @covers ::get_activitypub_object
563+
*/
564+
public function test_home_page_actor_and_blog_mode() {
565+
\update_option( 'activitypub_actor_mode', ACTIVITYPUB_ACTOR_AND_BLOG_MODE );
566+
$actor_queries = array();
567+
568+
// Track database queries using the 'query' filter.
569+
$query_filter = function ( $query ) use ( &$actor_queries ) {
570+
if ( strpos( $query, 'ap_actor' ) !== false ) {
571+
$actor_queries[] = $query;
572+
}
573+
return $query;
574+
};
575+
576+
\add_filter( 'query', $query_filter );
577+
578+
Query::get_instance()->__destruct();
579+
$this->go_to( home_url( '/' ) );
580+
$object = Query::get_instance()->get_activitypub_object();
581+
582+
\remove_filter( 'query', $query_filter );
583+
584+
$message = 'Should not query Remote_Actors table for home page.';
585+
if ( ! empty( $actor_queries ) ) {
586+
$message .= ' Found queries: ' . wp_json_encode( $actor_queries );
587+
}
588+
589+
$this->assertNotNull( $object, 'Home page should return an object' );
590+
$this->assertEmpty( $actor_queries, $message );
591+
592+
\delete_option( 'activitypub_actor_mode' );
593+
}
521594
}

0 commit comments

Comments
 (0)