From ee68cfa1397aa703a0ca27bee33e8afab59af633 Mon Sep 17 00:00:00 2001 From: Konstantin Obenland Date: Thu, 21 Aug 2025 08:55:30 -0500 Subject: [PATCH 1/4] Improve Following class documentation and method efficiency - Remove unused Http import - Fix @see references to use proper namespace format (\Activitypub\follow) - Update return type documentation for follow() method to be more accurate - Optimize count methods to avoid unnecessary data retrieval by using minimal number parameter - Fix return type documentation for get_all_with_count to correctly reference 'following' instead of 'followers' --- includes/collection/class-following.php | 32 ++++++++++++++----------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/includes/collection/class-following.php b/includes/collection/class-following.php index 9fa551483..c0d8b3e56 100644 --- a/includes/collection/class-following.php +++ b/includes/collection/class-following.php @@ -8,7 +8,6 @@ namespace Activitypub\Collection; use Activitypub\Activity\Activity; -use Activitypub\Http; use function Activitypub\add_to_outbox; @@ -54,14 +53,14 @@ class Following { /** * Follow a user. * - * Please do not use this method directly, use `Activitypub\follow` instead. + * Please do not use this method directly, use `\Activitypub\follow` instead. * - * @see Activitypub\follow + * @see \Activitypub\follow * * @param \WP_Post|int $post The ID of the remote Actor. * @param int $user_id The ID of the WordPress User. * - * @return \WP_Post|\WP_Error The ID of the Actor or a WP_Error. + * @return int|false|\WP_Post|\WP_Error The Outbox ID or false on failure, the Actor post or a WP_Error. */ public static function follow( $post, $user_id ) { $post = \get_post( $post ); @@ -146,9 +145,9 @@ public static function reject( $post, $user_id ) { /** * Remove a follow request. * - * Please do not use this method directly, use `Activitypub\unfollow` instead. + * Please do not use this method directly, use `\Activitypub\unfollow` instead. * - * @see Activitypub\unfollow + * @see \Activitypub\unfollow * * @param \WP_Post|int $post The ID of the remote Actor. * @param int $user_id The ID of the WordPress User. @@ -264,7 +263,7 @@ public static function get_following( $user_id, $number = -1, $page = null, $arg * @return int The total number of followings. */ public static function count_following( $user_id ) { - return self::get_following_with_count( $user_id, -1, null, array() )['total']; + return self::get_following_with_count( $user_id, 1 )['total']; } /** @@ -330,7 +329,7 @@ public static function get_pending( $user_id, $number = -1, $page = null, $args * @return int The total number of pending followings. */ public static function count_pending( $user_id ) { - return self::get_pending_with_count( $user_id, -1, null, array() )['total']; + return self::get_pending_with_count( $user_id, 1 )['total']; } /** @@ -341,7 +340,12 @@ public static function count_pending( $user_id ) { * @param int $page Page number. * @param array $args The WP_Query arguments. * - * @return \WP_Post[] List of `Following` objects. + * @return array { + * Data about the followings. + * + * @type \WP_Post[] $followers List of `Follower` objects. + * @type int $total Total number of followers. + * } */ public static function get_all_with_count( $user_id, $number = -1, $page = null, $args = array() ) { $defaults = array( @@ -380,7 +384,7 @@ public static function get_all_with_count( $user_id, $number = -1, $page = null, * @return \WP_Post[] List of `Following` objects. */ public static function get_all( $user_id ) { - return self::get_all_with_count( $user_id, -1, null, array() )['following']; + return self::get_all_with_count( $user_id )['following']; } /** @@ -391,7 +395,7 @@ public static function get_all( $user_id ) { * @return int The total number of all followings. */ public static function count_all( $user_id ) { - return self::get_all_with_count( $user_id, -1, null, array() )['total']; + return self::get_all_with_count( $user_id, 1 )['total']; } /** @@ -403,9 +407,9 @@ public static function count_all( $user_id ) { */ public static function count( $user_id ) { return array( - self::ALL => self::get_all_with_count( $user_id, -1, null, array() )['total'], - self::ACCEPTED => self::get_following_with_count( $user_id, -1, null, array() )['total'], - self::PENDING => self::get_pending_with_count( $user_id, -1, null, array() )['total'], + self::ALL => self::get_all_with_count( $user_id, 1 )['total'], + self::ACCEPTED => self::get_following_with_count( $user_id, 1 )['total'], + self::PENDING => self::get_pending_with_count( $user_id, 1 )['total'], ); } From bf631f705ad0b3226a51bbeedcfd091b3706a69d Mon Sep 17 00:00:00 2001 From: Automattic Bot Date: Thu, 21 Aug 2025 15:56:10 +0200 Subject: [PATCH 2/4] Add changelog --- .github/changelog/2086-from-description | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 .github/changelog/2086-from-description diff --git a/.github/changelog/2086-from-description b/.github/changelog/2086-from-description new file mode 100644 index 000000000..0c3881354 --- /dev/null +++ b/.github/changelog/2086-from-description @@ -0,0 +1,4 @@ +Significance: patch +Type: changed + +Improve Following class documentation and optimize count methods for better performance From e5559566060dc4830c740a20fe98ddb87a61b072 Mon Sep 17 00:00:00 2001 From: Konstantin Obenland Date: Thu, 21 Aug 2025 09:00:35 -0500 Subject: [PATCH 3/4] Update return type annotations for follow and unfollow Adjusted the PHPDoc return types for the follow and unfollow functions to more accurately reflect their possible return values. This improves code documentation and helps with static analysis. --- includes/functions.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/includes/functions.php b/includes/functions.php index fa8319576..0dca2fc1c 100644 --- a/includes/functions.php +++ b/includes/functions.php @@ -1550,7 +1550,7 @@ function add_to_outbox( $data, $activity_type = null, $user_id = 0, $content_vis * @param string|int $remote_actor The Actor URL, WebFinger Resource or Post-ID of the remote Actor. * @param int $user_id The ID of the WordPress User. * - * @return \WP_Post|\WP_Error The ID of the Outbox item or a WP_Error. + * @return int|false|\WP_Post|\WP_Error The Outbox ID or false on failure, the Actor post or a WP_Error. */ function follow( $remote_actor, $user_id ) { if ( \is_numeric( $remote_actor ) ) { @@ -1580,7 +1580,7 @@ function follow( $remote_actor, $user_id ) { * @param string|int $remote_actor The Actor URL, WebFinger Resource or Post-ID of the remote Actor. * @param int $user_id The ID of the WordPress User. * - * @return \WP_Post|\WP_Error The ID of the Outbox item or a WP_Error. + * @return \WP_Post|\WP_Error The Actor post or a WP_Error. */ function unfollow( $remote_actor, $user_id ) { if ( \is_numeric( $remote_actor ) ) { From 62d439a9afd5f53a4aa3c2ac4821b2e88dda35de Mon Sep 17 00:00:00 2001 From: Konstantin Obenland Date: Mon, 25 Aug 2025 10:59:21 -0500 Subject: [PATCH 4/4] Pass limit parameter to get_followers_with_count Updated count_followers to pass a limit of 1 to get_followers_with_count, ensuring the method signature is correctly followed and potentially improving performance. --- includes/collection/class-followers.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/collection/class-followers.php b/includes/collection/class-followers.php index 287587b98..c71fe31a5 100644 --- a/includes/collection/class-followers.php +++ b/includes/collection/class-followers.php @@ -232,7 +232,7 @@ public static function get_followers_with_count( $user_id, $number = -1, $page = * @return int The number of Followers */ public static function count_followers( $user_id ) { - return self::get_followers_with_count( $user_id )['total']; + return self::get_followers_with_count( $user_id, 1 )['total']; } /**