@@ -44,6 +44,105 @@ public function __construct() {
44
44
'ajax ' => false ,
45
45
)
46
46
);
47
+
48
+ \add_action ( 'load- ' . get_current_screen ()->id , array ( $ this , 'process_action ' ), 20 );
49
+ \add_action ( 'admin_notices ' , array ( $ this , 'process_admin_notices ' ) );
50
+ }
51
+
52
+ /**
53
+ * Process action.
54
+ */
55
+ public function process_action () {
56
+ if ( ! \current_user_can ( 'edit_user ' , $ this ->user_id ) ) {
57
+ return ;
58
+ }
59
+
60
+ switch ( $ this ->current_action () ) {
61
+ case 'delete ' :
62
+ // Handle single follower deletion.
63
+ if ( isset ( $ _GET ['follower ' ], $ _GET ['_wpnonce ' ] ) ) {
64
+ $ follower = \esc_url_raw ( \wp_unslash ( $ _GET ['follower ' ] ) );
65
+ $ nonce = \sanitize_text_field ( \wp_unslash ( $ _GET ['_wpnonce ' ] ) );
66
+
67
+ 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 );
74
+
75
+ $ redirect_args = array (
76
+ 'updated ' => 'true ' ,
77
+ 'action ' => 'deleted ' ,
78
+ );
79
+
80
+ \wp_safe_redirect ( \add_query_arg ( $ redirect_args ) );
81
+ exit ;
82
+ }
83
+ }
84
+
85
+ // Handle bulk actions.
86
+ if ( isset ( $ _REQUEST ['following ' ], $ _REQUEST ['_wpnonce ' ] ) ) {
87
+ $ nonce = \sanitize_text_field ( \wp_unslash ( $ _REQUEST ['_wpnonce ' ] ) );
88
+
89
+ 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 );
98
+ }
99
+
100
+ $ redirect_args = array (
101
+ 'updated ' => 'true ' ,
102
+ 'action ' => 'all_deleted ' ,
103
+ 'count ' => \count ( $ following ),
104
+ );
105
+
106
+ \wp_safe_redirect ( \add_query_arg ( $ redirect_args ) );
107
+ exit ;
108
+ }
109
+ }
110
+ break ;
111
+
112
+ default :
113
+ break ;
114
+ }
115
+ }
116
+
117
+ /**
118
+ * Process admin notices based on query parameters.
119
+ */
120
+ public function process_admin_notices () {
121
+ if ( isset ( $ _REQUEST ['updated ' ] ) && 'true ' === $ _REQUEST ['updated ' ] && ! empty ( $ _REQUEST ['action ' ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
122
+ $ message = '' ;
123
+ switch ( $ _REQUEST ['action ' ] ) { // phpcs:ignore WordPress.Security.NonceVerification
124
+ case 'deleted ' :
125
+ $ message = \__ ( 'Account unfollowed. ' , 'activitypub ' );
126
+ break ;
127
+ case 'all_deleted ' :
128
+ $ count = \absint ( $ _REQUEST ['count ' ] ?? 0 ); // phpcs:ignore WordPress.Security.NonceVerification
129
+ /* translators: %d: Number of accounts unfollowed. */
130
+ $ message = \_n ( '%d account unfollowed. ' , '%d accounts unfollowed. ' , $ count , 'activitypub ' );
131
+ $ message = \sprintf ( $ message , \number_format_i18n ( $ count ) );
132
+ break ;
133
+ }
134
+
135
+ if ( ! empty ( $ message ) ) {
136
+ \wp_admin_notice (
137
+ $ message ,
138
+ array (
139
+ 'type ' => 'success ' ,
140
+ 'dismissible ' => true ,
141
+ 'id ' => 'message ' ,
142
+ )
143
+ );
144
+ }
145
+ }
47
146
}
48
147
49
148
/**
@@ -77,14 +176,10 @@ public function get_sortable_columns() {
77
176
* Prepare items.
78
177
*/
79
178
public function prepare_items () {
80
- $ status = Following_Collection::ALL ;
81
-
82
- $ this ->process_action ();
83
-
179
+ $ status = Following_Collection::ALL ;
84
180
$ page_num = $ this ->get_pagenum ();
85
181
$ per_page = $ this ->get_items_per_page ( 'activitypub_following_per_page ' );
86
-
87
- $ args = array ();
182
+ $ args = array ();
88
183
89
184
// phpcs:disable WordPress.Security.NonceVerification.Recommended
90
185
if ( isset ( $ _GET ['orderby ' ] ) ) {
@@ -292,36 +387,6 @@ public function column_modified( $item ) {
292
387
);
293
388
}
294
389
295
- /**
296
- * Process action.
297
- */
298
- public function process_action () {
299
- if ( ! isset ( $ _REQUEST ['following ' ], $ _REQUEST ['_wpnonce ' ] ) ) {
300
- return ;
301
- }
302
-
303
- $ nonce = \sanitize_text_field ( \wp_unslash ( $ _REQUEST ['_wpnonce ' ] ) );
304
- if ( ! \wp_verify_nonce ( $ nonce , 'bulk- ' . $ this ->_args ['plural ' ] ) ) {
305
- return ;
306
- }
307
-
308
- if ( ! \current_user_can ( 'edit_user ' , $ this ->user_id ) ) {
309
- return ;
310
- }
311
-
312
- if ( $ this ->current_action () === 'delete ' ) {
313
- $ following = array_map ( 'esc_url_raw ' , \wp_unslash ( $ _REQUEST ['following ' ] ) );
314
-
315
- foreach ( $ following as $ actor_id ) {
316
- $ actor = Actors::get_remote_by_uri ( $ actor_id );
317
- if ( \is_wp_error ( $ actor ) ) {
318
- continue ;
319
- }
320
- Following_Collection::unfollow ( $ actor , $ this ->user_id );
321
- }
322
- }
323
- }
324
-
325
390
/**
326
391
* Message to be displayed when there are no followings.
327
392
*/
@@ -342,4 +407,38 @@ public function single_row( $item ) {
342
407
$ this ->single_row_columns ( $ item );
343
408
\printf ( "</tr> \n" );
344
409
}
410
+
411
+ /**
412
+ * Handles the row actions for each following item.
413
+ *
414
+ * @param array $item The current following item.
415
+ * @param string $column_name The current column name.
416
+ * @param string $primary The primary column name.
417
+ * @return string HTML for the row actions.
418
+ */
419
+ protected function handle_row_actions ( $ item , $ column_name , $ primary ) {
420
+ if ( $ column_name !== $ primary ) {
421
+ return '' ;
422
+ }
423
+
424
+ $ actions = array (
425
+ 'unfollow ' => sprintf (
426
+ '<a href="%s" aria-label="%s">%s</a> ' ,
427
+ \wp_nonce_url (
428
+ \add_query_arg (
429
+ array (
430
+ 'action ' => 'delete ' ,
431
+ 'follower ' => $ item ['identifier ' ],
432
+ )
433
+ ),
434
+ 'delete-follower_ ' . $ item ['identifier ' ]
435
+ ),
436
+ /* translators: %s: username. */
437
+ \esc_attr ( \sprintf ( \__ ( 'Unfollow %s ' , 'activitypub ' ), $ item ['username ' ] ) ),
438
+ \esc_html__ ( 'Unfollow ' , 'activitypub ' )
439
+ ),
440
+ );
441
+
442
+ return $ this ->row_actions ( $ actions );
443
+ }
345
444
}
0 commit comments