Skip to content

Commit 8907507

Browse files
authored
Improve Following class documentation and method efficiency (#2086)
1 parent 4cc26df commit 8907507

File tree

4 files changed

+25
-17
lines changed

4 files changed

+25
-17
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+
Improve Following class documentation and optimize count methods for better performance

includes/collection/class-followers.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ public static function get_followers_with_count( $user_id, $number = -1, $page =
232232
* @return int The number of Followers
233233
*/
234234
public static function count_followers( $user_id ) {
235-
return self::get_followers_with_count( $user_id )['total'];
235+
return self::get_followers_with_count( $user_id, 1 )['total'];
236236
}
237237

238238
/**

includes/collection/class-following.php

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
namespace Activitypub\Collection;
99

1010
use Activitypub\Activity\Activity;
11-
use Activitypub\Http;
1211

1312
use function Activitypub\add_to_outbox;
1413

@@ -54,14 +53,14 @@ class Following {
5453
/**
5554
* Follow a user.
5655
*
57-
* Please do not use this method directly, use `Activitypub\follow` instead.
56+
* Please do not use this method directly, use `\Activitypub\follow` instead.
5857
*
59-
* @see Activitypub\follow
58+
* @see \Activitypub\follow
6059
*
6160
* @param \WP_Post|int $post The ID of the remote Actor.
6261
* @param int $user_id The ID of the WordPress User.
6362
*
64-
* @return \WP_Post|\WP_Error The ID of the Actor or a WP_Error.
63+
* @return int|false|\WP_Post|\WP_Error The Outbox ID or false on failure, the Actor post or a WP_Error.
6564
*/
6665
public static function follow( $post, $user_id ) {
6766
$post = \get_post( $post );
@@ -146,9 +145,9 @@ public static function reject( $post, $user_id ) {
146145
/**
147146
* Remove a follow request.
148147
*
149-
* Please do not use this method directly, use `Activitypub\unfollow` instead.
148+
* Please do not use this method directly, use `\Activitypub\unfollow` instead.
150149
*
151-
* @see Activitypub\unfollow
150+
* @see \Activitypub\unfollow
152151
*
153152
* @param \WP_Post|int $post The ID of the remote Actor.
154153
* @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
264263
* @return int The total number of followings.
265264
*/
266265
public static function count_following( $user_id ) {
267-
return self::get_following_with_count( $user_id, -1, null, array() )['total'];
266+
return self::get_following_with_count( $user_id, 1 )['total'];
268267
}
269268

270269
/**
@@ -330,7 +329,7 @@ public static function get_pending( $user_id, $number = -1, $page = null, $args
330329
* @return int The total number of pending followings.
331330
*/
332331
public static function count_pending( $user_id ) {
333-
return self::get_pending_with_count( $user_id, -1, null, array() )['total'];
332+
return self::get_pending_with_count( $user_id, 1 )['total'];
334333
}
335334

336335
/**
@@ -341,7 +340,12 @@ public static function count_pending( $user_id ) {
341340
* @param int $page Page number.
342341
* @param array $args The WP_Query arguments.
343342
*
344-
* @return \WP_Post[] List of `Following` objects.
343+
* @return array {
344+
* Data about the followings.
345+
*
346+
* @type \WP_Post[] $followers List of `Follower` objects.
347+
* @type int $total Total number of followers.
348+
* }
345349
*/
346350
public static function get_all_with_count( $user_id, $number = -1, $page = null, $args = array() ) {
347351
$defaults = array(
@@ -380,7 +384,7 @@ public static function get_all_with_count( $user_id, $number = -1, $page = null,
380384
* @return \WP_Post[] List of `Following` objects.
381385
*/
382386
public static function get_all( $user_id ) {
383-
return self::get_all_with_count( $user_id, -1, null, array() )['following'];
387+
return self::get_all_with_count( $user_id )['following'];
384388
}
385389

386390
/**
@@ -391,7 +395,7 @@ public static function get_all( $user_id ) {
391395
* @return int The total number of all followings.
392396
*/
393397
public static function count_all( $user_id ) {
394-
return self::get_all_with_count( $user_id, -1, null, array() )['total'];
398+
return self::get_all_with_count( $user_id, 1 )['total'];
395399
}
396400

397401
/**
@@ -403,9 +407,9 @@ public static function count_all( $user_id ) {
403407
*/
404408
public static function count( $user_id ) {
405409
return array(
406-
self::ALL => self::get_all_with_count( $user_id, -1, null, array() )['total'],
407-
self::ACCEPTED => self::get_following_with_count( $user_id, -1, null, array() )['total'],
408-
self::PENDING => self::get_pending_with_count( $user_id, -1, null, array() )['total'],
410+
self::ALL => self::get_all_with_count( $user_id, 1 )['total'],
411+
self::ACCEPTED => self::get_following_with_count( $user_id, 1 )['total'],
412+
self::PENDING => self::get_pending_with_count( $user_id, 1 )['total'],
409413
);
410414
}
411415

includes/functions.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1551,7 +1551,7 @@ function add_to_outbox( $data, $activity_type = null, $user_id = 0, $content_vis
15511551
* @param string|int $remote_actor The Actor URL, WebFinger Resource or Post-ID of the remote Actor.
15521552
* @param int $user_id The ID of the WordPress User.
15531553
*
1554-
* @return \WP_Post|\WP_Error The ID of the Outbox item or a WP_Error.
1554+
* @return int|false|\WP_Post|\WP_Error The Outbox ID or false on failure, the Actor post or a WP_Error.
15551555
*/
15561556
function follow( $remote_actor, $user_id ) {
15571557
if ( \is_numeric( $remote_actor ) ) {
@@ -1581,7 +1581,7 @@ function follow( $remote_actor, $user_id ) {
15811581
* @param string|int $remote_actor The Actor URL, WebFinger Resource or Post-ID of the remote Actor.
15821582
* @param int $user_id The ID of the WordPress User.
15831583
*
1584-
* @return \WP_Post|\WP_Error The ID of the Outbox item or a WP_Error.
1584+
* @return \WP_Post|\WP_Error The Actor post or a WP_Error.
15851585
*/
15861586
function unfollow( $remote_actor, $user_id ) {
15871587
if ( \is_numeric( $remote_actor ) ) {

0 commit comments

Comments
 (0)