From 4a4a06de378f44dcdd9836506d0aca55ef62febb Mon Sep 17 00:00:00 2001 From: Matthias Pfefferle Date: Wed, 26 Apr 2023 17:22:44 +0200 Subject: [PATCH] get_follower requires user_id check --- includes/collection/class-followers.php | 23 +++++++++++++++---- tests/test-class-db-activitypub-followers.php | 17 ++++++++++++++ 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/includes/collection/class-followers.php b/includes/collection/class-followers.php index 9669398..98971b9 100644 --- a/includes/collection/class-followers.php +++ b/includes/collection/class-followers.php @@ -183,12 +183,27 @@ class Followers { * * @param string $actor The Actor URL * - * @return \Activitypub\Model\Follower The Follower object + * @return \Activitypub\Model\Follower The Follower object */ - public static function get_follower( $actor ) { - $term = get_term_by( 'name', $actor, self::TAXONOMY ); + public static function get_follower( $user_id, $actor ) { + $terms = new WP_Term_Query( + array( + 'name' => $actor, + 'taxonomy' => self::TAXONOMY, + 'hide_empty' => false, + 'object_ids' => $user_id, + 'number' => 1, + ) + ); - return new Follower( $term->name ); + $term = $terms->get_terms(); + + if ( is_array( $term ) && ! empty( $term ) ) { + $term = reset( $term ); + return new Follower( $term->name ); + } + + return null; } /** diff --git a/tests/test-class-db-activitypub-followers.php b/tests/test-class-db-activitypub-followers.php index 0591178..97318d4 100644 --- a/tests/test-class-db-activitypub-followers.php +++ b/tests/test-class-db-activitypub-followers.php @@ -50,6 +50,23 @@ class Test_Db_Activitypub_Followers extends WP_UnitTestCase { $this->assertContains( $follower, $db_followers ); } + public function test_get_follower() { + $followers = array( 'https://example.com/author/jon' ); + + $pre_http_request = new MockAction(); + add_filter( 'pre_http_request', array( $pre_http_request, 'filter' ), 10, 3 ); + + foreach ( $followers as $follower ) { + \Activitypub\Collection\Followers::add_follower( 1, $follower ); + } + + $follower = \Activitypub\Collection\Followers::get_follower( 1, 'https://example.com/author/jon' ); + $this->assertEquals( 'https://example.com/author/jon', $follower->get_actor() ); + + $follower = \Activitypub\Collection\Followers::get_follower( 1, 'http://sally.example.org' ); + $this->assertNull( $follower ); + } + public static function http_request_host_is_external( $in, $host ) { if ( in_array( $host, array( 'example.com', 'example.org' ), true ) ) { return true;