Skip to content

Commit 9823112

Browse files
authored
Follow(ing|ers) Tables: Use CPT-ID instead of Actor-URI (#1946)
1 parent a778099 commit 9823112

File tree

5 files changed

+86
-39
lines changed

5 files changed

+86
-39
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Significance: minor
2+
Type: changed
3+
4+
Table actions are now faster by using the Custom Post Type ID instead of the remote user URI, thanks to the unified Actor Model.

includes/collection/class-followers.php

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -67,30 +67,51 @@ public static function add_follower( $user_id, $actor ) {
6767
/**
6868
* Remove a Follower.
6969
*
70-
* @param int $user_id The ID of the WordPress User.
71-
* @param string $actor The Actor URL.
70+
* @param int $post_id The ID of the remote Actor.
71+
* @param int $user_id The ID of the WordPress User.
7272
*
7373
* @return bool True on success, false on failure.
7474
*/
75-
public static function remove_follower( $user_id, $actor ) {
76-
\wp_cache_delete( \sprintf( self::CACHE_KEY_INBOXES, $user_id ), 'activitypub' );
77-
78-
$remote_actor = self::get_follower( $user_id, $actor );
75+
public static function remove( $post_id, $user_id ) {
76+
$remote_actor = \get_post( $post_id );
7977

80-
if ( \is_wp_error( $remote_actor ) ) {
78+
if ( ! $remote_actor ) {
8179
return false;
8280
}
8381

82+
\wp_cache_delete( \sprintf( self::CACHE_KEY_INBOXES, $user_id ), 'activitypub' );
83+
8484
/**
8585
* Fires before a Follower is removed.
8686
*
8787
* @param \WP_Post $remote_actor The remote Actor object.
8888
* @param int $user_id The ID of the WordPress User.
89-
* @param string $actor The Actor URL.
9089
*/
91-
\do_action( 'activitypub_followers_pre_remove_follower', $remote_actor, $user_id, $actor );
90+
\do_action( 'activitypub_followers_pre_remove_follower', $remote_actor, $user_id );
91+
92+
return \delete_post_meta( $post_id, self::FOLLOWER_META_KEY, $user_id );
93+
}
94+
95+
/**
96+
* Remove a Follower.
97+
*
98+
* @deprecated Use Activitypub\Collection\Followers::remove instead.
99+
*
100+
* @param int $user_id The ID of the WordPress User.
101+
* @param string $actor The Actor URL.
102+
*
103+
* @return bool True on success, false on failure.
104+
*/
105+
public static function remove_follower( $user_id, $actor ) {
106+
_deprecated_function( __METHOD__, 'unreleased', 'Activitypub\Collection\Followers::remove' );
107+
108+
$remote_actor = self::get_follower( $user_id, $actor );
109+
110+
if ( \is_wp_error( $remote_actor ) ) {
111+
return false;
112+
}
92113

93-
return \delete_post_meta( $remote_actor->ID, self::FOLLOWER_META_KEY, $user_id );
114+
return self::remove( $remote_actor->ID, $user_id );
94115
}
95116

96117
/**

includes/table/class-followers.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,18 +61,18 @@ public function process_action() {
6161
case 'delete':
6262
// Handle single follower deletion.
6363
if ( isset( $_GET['follower'], $_GET['_wpnonce'] ) ) {
64-
$follower = \esc_url_raw( \wp_unslash( $_GET['follower'] ) );
64+
$follower = \absint( $_GET['follower'] );
6565
$nonce = \sanitize_text_field( \wp_unslash( $_GET['_wpnonce'] ) );
6666

6767
if ( \wp_verify_nonce( $nonce, 'delete-follower_' . $follower ) ) {
68-
Follower_Collection::remove_follower( $this->user_id, $follower );
68+
Follower_Collection::remove( $follower, $this->user_id );
6969

7070
$redirect_args = array(
7171
'updated' => 'true',
7272
'action' => 'deleted',
7373
);
7474

75-
\wp_safe_redirect( \add_query_arg( $redirect_args ) );
75+
\wp_safe_redirect( \add_query_arg( $redirect_args, \remove_query_arg( array( 'follower' ) ) ) );
7676
exit;
7777
}
7878
}
@@ -82,9 +82,9 @@ public function process_action() {
8282
$nonce = \sanitize_text_field( \wp_unslash( $_REQUEST['_wpnonce'] ) );
8383

8484
if ( \wp_verify_nonce( $nonce, 'bulk-' . $this->_args['plural'] ) ) {
85-
$followers = \array_map( 'esc_url_raw', \wp_unslash( $_REQUEST['followers'] ) );
85+
$followers = \array_map( 'absint', \wp_unslash( $_REQUEST['followers'] ) );
8686
foreach ( $followers as $follower ) {
87-
Follower_Collection::remove_follower( $this->user_id, $follower );
87+
Follower_Collection::remove( $follower, $this->user_id );
8888
}
8989

9090
$redirect_args = array(
@@ -93,7 +93,7 @@ public function process_action() {
9393
'count' => \count( $followers ),
9494
);
9595

96-
\wp_safe_redirect( \add_query_arg( $redirect_args ) );
96+
\wp_safe_redirect( \add_query_arg( $redirect_args, \remove_query_arg( array( 'followers' ) ) ) );
9797
exit;
9898
}
9999
}
@@ -284,7 +284,7 @@ public function column_default( $item, $column_name ) {
284284
* @return string
285285
*/
286286
public function column_cb( $item ) {
287-
return \sprintf( '<input type="checkbox" name="followers[]" value="%s" />', \esc_attr( $item['identifier'] ) );
287+
return \sprintf( '<input type="checkbox" name="followers[]" value="%s" />', \esc_attr( $item['id'] ) );
288288
}
289289

290290
/**
@@ -345,10 +345,10 @@ protected function handle_row_actions( $item, $column_name, $primary ) {
345345
\add_query_arg(
346346
array(
347347
'action' => 'delete',
348-
'follower' => $item['identifier'],
348+
'follower' => $item['id'],
349349
)
350350
),
351-
'delete-follower_' . $item['identifier']
351+
'delete-follower_' . $item['id']
352352
),
353353
/* translators: %s: username. */
354354
\esc_attr( \sprintf( \__( 'Delete %s', 'activitypub' ), $item['username'] ) ),

includes/table/class-following.php

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -61,23 +61,18 @@ public function process_action() {
6161
case 'delete':
6262
// Handle single follower deletion.
6363
if ( isset( $_GET['follower'], $_GET['_wpnonce'] ) ) {
64-
$follower = \esc_url_raw( \wp_unslash( $_GET['follower'] ) );
64+
$follower = \absint( $_GET['follower'] );
6565
$nonce = \sanitize_text_field( \wp_unslash( $_GET['_wpnonce'] ) );
6666

6767
if ( \wp_verify_nonce( $nonce, 'delete-follower_' . $follower ) ) {
68-
$actor = Actors::get_remote_by_uri( $follower );
69-
if ( \is_wp_error( $actor ) ) {
70-
break;
71-
}
72-
73-
Following_Collection::unfollow( $actor, $this->user_id );
68+
Following_Collection::unfollow( $follower, $this->user_id );
7469

7570
$redirect_args = array(
7671
'updated' => 'true',
7772
'action' => 'deleted',
7873
);
7974

80-
\wp_safe_redirect( \add_query_arg( $redirect_args ) );
75+
\wp_safe_redirect( \add_query_arg( $redirect_args, \remove_query_arg( array( 'follower' ) ) ) );
8176
exit;
8277
}
8378
}
@@ -87,14 +82,10 @@ public function process_action() {
8782
$nonce = \sanitize_text_field( \wp_unslash( $_REQUEST['_wpnonce'] ) );
8883

8984
if ( \wp_verify_nonce( $nonce, 'bulk-' . $this->_args['plural'] ) ) {
90-
$following = array_map( 'esc_url_raw', \wp_unslash( $_REQUEST['following'] ) );
91-
92-
foreach ( $following as $actor_id ) {
93-
$actor = Actors::get_remote_by_uri( $actor_id );
94-
if ( \is_wp_error( $actor ) ) {
95-
continue;
96-
}
97-
Following_Collection::unfollow( $actor, $this->user_id );
85+
$following = array_map( 'absint', \wp_unslash( $_REQUEST['following'] ) );
86+
87+
foreach ( $following as $post_id ) {
88+
Following_Collection::unfollow( $post_id, $this->user_id );
9889
}
9990

10091
$redirect_args = array(
@@ -103,7 +94,7 @@ public function process_action() {
10394
'count' => \count( $following ),
10495
);
10596

106-
\wp_safe_redirect( \add_query_arg( $redirect_args ) );
97+
\wp_safe_redirect( \add_query_arg( $redirect_args, \remove_query_arg( array( 'following' ) ) ) );
10798
exit;
10899
}
109100
}
@@ -343,7 +334,7 @@ public function column_default( $item, $column_name ) {
343334
* @return string
344335
*/
345336
public function column_cb( $item ) {
346-
return \sprintf( '<input type="checkbox" name="following[]" value="%s" />', \esc_attr( $item['identifier'] ) );
337+
return \sprintf( '<input type="checkbox" name="following[]" value="%s" />', \esc_attr( $item['id'] ) );
347338
}
348339

349340
/**
@@ -428,10 +419,10 @@ protected function handle_row_actions( $item, $column_name, $primary ) {
428419
\add_query_arg(
429420
array(
430421
'action' => 'delete',
431-
'follower' => $item['identifier'],
422+
'follower' => $item['id'],
432423
)
433424
),
434-
'delete-follower_' . $item['identifier']
425+
'delete-follower_' . $item['id']
435426
),
436427
/* translators: %s: username. */
437428
\esc_attr( \sprintf( \__( 'Unfollow %s', 'activitypub' ), $item['username'] ) ),

tests/includes/collection/class-test-followers.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ public function test_delete_follower() {
227227
$follower2 = Followers::get_follower( 2, 'https://example.com/author/jon' );
228228
$this->assertEquals( 'https://example.com/author/jon', $follower2->guid );
229229

230+
$this->setExpectedDeprecated( 'Activitypub\Collection\Followers::remove_follower' );
230231
Followers::remove_follower( 1, 'https://example.com/author/jon' );
231232

232233
$follower = Followers::get_follower( 1, 'https://example.com/author/jon' );
@@ -239,6 +240,36 @@ public function test_delete_follower() {
239240
$this->assertEquals( 1, count( $followers ) );
240241
}
241242

243+
/**
244+
* Tests remove_follower.
245+
*
246+
* @covers ::remove
247+
*/
248+
public function test_remove() {
249+
$followers = array(
250+
'https://example.com/author/jon',
251+
'https://example.org/author/doe',
252+
);
253+
254+
foreach ( $followers as $follower ) {
255+
Followers::add_follower( 1, $follower );
256+
}
257+
258+
$follower = Followers::get_follower( 1, 'https://example.com/author/jon' );
259+
$this->assertEquals( 'https://example.com/author/jon', $follower->guid );
260+
261+
$followers = Followers::get_followers( 1 );
262+
$this->assertEquals( 2, count( $followers ) );
263+
264+
Followers::remove( $followers[0]->ID, 1 );
265+
266+
$follower = Followers::get_follower( 1, $followers[0]->guid );
267+
$this->assertWPError( $follower );
268+
269+
$followers = Followers::get_followers( 1 );
270+
$this->assertEquals( 1, count( $followers ) );
271+
}
272+
242273
/**
243274
* Tests get_outdated_followers.
244275
*

0 commit comments

Comments
 (0)