Skip to content

Commit 3166870

Browse files
authored
Introduce user_can_activitypub (#1529)
Replaces is_user_disabled()
1 parent 44a322e commit 3166870

File tree

11 files changed

+71
-69
lines changed

11 files changed

+71
-69
lines changed

includes/class-comment.php

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -125,13 +125,7 @@ public static function are_comments_allowed( $comment ) {
125125
$current_user = Actors::BLOG_USER_ID;
126126
}
127127

128-
$is_user_disabled = is_user_disabled( $current_user );
129-
130-
if ( $is_user_disabled ) {
131-
return false;
132-
}
133-
134-
return true;
128+
return user_can_activitypub( $current_user );
135129
}
136130

137131
/**
@@ -235,10 +229,8 @@ public static function should_be_federated( $comment ) {
235229
$user_id = Actors::BLOG_USER_ID;
236230
}
237231

238-
$is_user_disabled = is_user_disabled( $user_id );
239-
240-
// User is disabled for federation.
241-
if ( $is_user_disabled ) {
232+
// User is not allowed to federate comments.
233+
if ( ! user_can_activitypub( $user_id ) ) {
242234
return false;
243235
}
244236

includes/class-migration.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -761,11 +761,11 @@ private static function add_to_outbox( $comment, $activity_type, $user_id, $visi
761761
}
762762

763763
// If the user is disabled, fall back to the blog user when available.
764-
if ( is_user_disabled( $user_id ) ) {
765-
if ( is_user_disabled( Actors::BLOG_USER_ID ) ) {
766-
return;
767-
} else {
764+
if ( ! user_can_activitypub( $user_id ) ) {
765+
if ( user_can_activitypub( Actors::BLOG_USER_ID ) ) {
768766
$user_id = Actors::BLOG_USER_ID;
767+
} else {
768+
return;
769769
}
770770
}
771771

includes/collection/class-actors.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
use function Activitypub\normalize_url;
1818
use function Activitypub\normalize_host;
1919
use function Activitypub\url_to_authorid;
20-
use function Activitypub\is_user_disabled;
2120
use function Activitypub\is_user_type_disabled;
21+
use function Activitypub\user_can_activitypub;
2222

2323
/**
2424
* Actors collection.
@@ -50,7 +50,7 @@ public static function get_by_id( $user_id ) {
5050
$user_id = (int) $user_id;
5151
}
5252

53-
if ( is_user_disabled( $user_id ) ) {
53+
if ( ! user_can_activitypub( $user_id ) ) {
5454
return new WP_Error(
5555
'activitypub_user_not_found',
5656
\__( 'Actor not found', 'activitypub' ),

includes/functions.php

Lines changed: 44 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -327,57 +327,67 @@ function is_post_disabled( $post ) {
327327
}
328328

329329
/**
330-
* This function checks if a user is disabled for ActivityPub.
330+
* This function checks if a user is enabled for ActivityPub.
331331
*
332332
* @param int $user_id The user ID.
333-
*
334-
* @return boolean True if the user is disabled, false otherwise.
333+
* @return boolean True if the user is enabled, false otherwise.
335334
*/
336-
function is_user_disabled( $user_id ) {
337-
$disabled = false;
338-
335+
function user_can_activitypub( $user_id ) {
339336
switch ( $user_id ) {
340-
// if the user is the application user, it's always enabled.
341-
case \Activitypub\Collection\Actors::APPLICATION_USER_ID:
342-
$disabled = false;
337+
case Actors::APPLICATION_USER_ID:
338+
$enabled = true; // Application user is always enabled.
343339
break;
344-
// if the user is the blog user, it's only enabled in single-user mode.
345-
case \Activitypub\Collection\Actors::BLOG_USER_ID:
346-
if ( is_user_type_disabled( 'blog' ) ) {
347-
$disabled = true;
348-
break;
349-
}
350340

351-
$disabled = false;
341+
case Actors::BLOG_USER_ID:
342+
$enabled = ! is_user_type_disabled( 'blog' );
352343
break;
353-
// if the user is any other user, it's enabled if it can publish posts.
344+
354345
default:
355346
if ( ! \get_user_by( 'id', $user_id ) ) {
356-
$disabled = true;
347+
$enabled = false;
357348
break;
358349
}
359350

360351
if ( is_user_type_disabled( 'user' ) ) {
361-
$disabled = true;
362-
break;
363-
}
364-
365-
if ( ! \user_can( $user_id, 'activitypub' ) ) {
366-
$disabled = true;
352+
$enabled = false;
367353
break;
368354
}
369355

370-
$disabled = false;
371-
break;
356+
$enabled = \user_can( $user_id, 'activitypub' );
372357
}
373358

374359
/**
375360
* Allow plugins to disable users for ActivityPub.
376361
*
362+
* @deprecated unreleased Use the `activitypub_user_can_activitypub` filter instead.
363+
*
377364
* @param boolean $disabled True if the user is disabled, false otherwise.
378-
* @param int $user_id The User-ID.
365+
* @param int $user_id The user ID.
379366
*/
380-
return apply_filters( 'activitypub_is_user_disabled', $disabled, $user_id );
367+
$enabled = ! \apply_filters_deprecated( 'activitypub_is_user_disabled', array( ! $enabled, $user_id ), 'unreleased', 'activitypub_user_can_activitypub' );
368+
369+
/**
370+
* Allow plugins to enable/disable users for ActivityPub.
371+
*
372+
* @param boolean $enabled True if the user is enabled, false otherwise.
373+
* @param int $user_id The user ID.
374+
*/
375+
return apply_filters( 'activitypub_user_can_activitypub', $enabled, $user_id );
376+
}
377+
378+
/**
379+
* This function checks if a user is disabled for ActivityPub.
380+
*
381+
* @deprecated unreleased Use the `user_can_activitypub` function instead.
382+
*
383+
* @param int $user_id The user ID.
384+
*
385+
* @return boolean True if the user is disabled, false otherwise.
386+
*/
387+
function is_user_disabled( $user_id ) {
388+
_deprecated_function( __FUNCTION__, 'unreleased', 'user_can_activitypub' );
389+
390+
return ! user_can_activitypub( $user_id );
381391
}
382392

383393
/**
@@ -615,7 +625,7 @@ function get_active_users( $duration = 1 ) {
615625
}
616626

617627
// If blog user is disabled.
618-
if ( is_user_disabled( Actors::BLOG_USER_ID ) ) {
628+
if ( ! user_can_activitypub( Actors::BLOG_USER_ID ) ) {
619629
return (int) $count;
620630
}
621631

@@ -647,7 +657,7 @@ function get_total_users() {
647657
}
648658

649659
// If blog user is disabled.
650-
if ( is_user_disabled( Actors::BLOG_USER_ID ) ) {
660+
if ( ! user_can_activitypub( Actors::BLOG_USER_ID ) ) {
651661
return (int) $users;
652662
}
653663

@@ -1485,11 +1495,11 @@ function add_to_outbox( $data, $activity_type = null, $user_id = 0, $content_vis
14851495
}
14861496

14871497
// If the user is disabled, fall back to the blog user when available.
1488-
if ( is_user_disabled( $user_id ) ) {
1489-
if ( is_user_disabled( Actors::BLOG_USER_ID ) ) {
1490-
return false;
1491-
} else {
1498+
if ( ! user_can_activitypub( $user_id ) ) {
1499+
if ( user_can_activitypub( Actors::BLOG_USER_ID ) ) {
14921500
$user_id = Actors::BLOG_USER_ID;
1501+
} else {
1502+
return false;
14931503
}
14941504
}
14951505

includes/model/class-user.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313
use Activitypub\Collection\Extra_Fields;
1414

1515
use function Activitypub\is_blog_public;
16-
use function Activitypub\is_user_disabled;
1716
use function Activitypub\get_rest_url_by_path;
1817
use function Activitypub\get_attribution_domains;
18+
use function Activitypub\user_can_activitypub;
1919

2020
/**
2121
* User class.
@@ -88,7 +88,7 @@ public function get_type() {
8888
* @return WP_Error|User The User object or WP_Error if user not found.
8989
*/
9090
public static function from_wp_user( $user_id ) {
91-
if ( is_user_disabled( $user_id ) ) {
91+
if ( ! user_can_activitypub( $user_id ) ) {
9292
return new WP_Error(
9393
'activitypub_user_not_found',
9494
\__( 'User not found', 'activitypub' ),

includes/transformer/class-factory.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
use Activitypub\Http;
1212
use Activitypub\Comment as Comment_Helper;
1313

14-
use function Activitypub\is_user_disabled;
1514
use function Activitypub\is_post_disabled;
15+
use function Activitypub\user_can_activitypub;
1616

1717
/**
1818
* Transformer Factory.
@@ -100,7 +100,7 @@ public static function get_transformer( $data ) {
100100
}
101101
break;
102102
case 'WP_User':
103-
if ( ! is_user_disabled( $data->ID ) ) {
103+
if ( user_can_activitypub( $data->ID ) ) {
104104
return new User( $data );
105105
}
106106
break;

includes/wp-admin/class-admin.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
use Activitypub\Collection\Extra_Fields;
1313
use function Activitypub\count_followers;
1414
use function Activitypub\get_content_visibility;
15-
use function Activitypub\is_user_disabled;
1615
use function Activitypub\is_user_type_disabled;
1716
use function Activitypub\site_supports_blocks;
17+
use function Activitypub\user_can_activitypub;
1818
use function Activitypub\was_comment_received;
1919

2020
/**
@@ -49,7 +49,7 @@ public static function init() {
4949
\add_filter( 'bulk_actions-users', array( self::class, 'user_bulk_options' ) );
5050
\add_filter( 'handle_bulk_actions-users', array( self::class, 'handle_bulk_request' ), 10, 3 );
5151

52-
if ( ! is_user_disabled( get_current_user_id() ) ) {
52+
if ( user_can_activitypub( \get_current_user_id() ) ) {
5353
\add_action( 'show_user_profile', array( self::class, 'add_profile' ) );
5454
}
5555

@@ -112,7 +112,7 @@ private static function show_admin_notice( $admin_notice, $level ) {
112112
*/
113113
public static function followers_list_page() {
114114
// User has to be able to publish posts.
115-
if ( ! is_user_disabled( get_current_user_id() ) ) {
115+
if ( user_can_activitypub( \get_current_user_id() ) ) {
116116
\load_template( ACTIVITYPUB_PLUGIN_DIR . 'templates/user-followers-list.php' );
117117
}
118118
}
@@ -510,7 +510,7 @@ public static function handle_bulk_request( $sendback, $action, $users ) {
510510
public static function dashboard_glance_items( $items ) {
511511
\add_filter( 'number_format_i18n', '\Activitypub\custom_large_numbers', 10, 3 );
512512

513-
if ( ! is_user_disabled( get_current_user_id() ) ) {
513+
if ( user_can_activitypub( \get_current_user_id() ) ) {
514514
$follower_count = sprintf(
515515
// translators: %s: number of followers.
516516
_n(

includes/wp-admin/class-health-check.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
use Activitypub\Collection\Actors;
1313
use Activitypub\Sanitize;
1414

15-
use function Activitypub\is_user_disabled;
15+
use function Activitypub\user_can_activitypub;
1616

1717
/**
1818
* ActivityPub Health_Check Class.
@@ -37,7 +37,7 @@ public static function init() {
3737
* @return array The filtered test array.
3838
*/
3939
public static function add_tests( $tests ) {
40-
if ( ! is_user_disabled( get_current_user_id() ) ) {
40+
if ( user_can_activitypub( \get_current_user_id() ) ) {
4141
$tests['direct']['activitypub_test_author_url'] = array(
4242
'label' => \__( 'Author URL test', 'activitypub' ),
4343
'test' => array( self::class, 'test_author_url' ),

includes/wp-admin/class-menu.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
namespace Activitypub\WP_Admin;
99

10-
use function Activitypub\is_user_disabled;
10+
use function Activitypub\user_can_activitypub;
1111

1212
/**
1313
* ActivityPub Menu Class.
@@ -29,7 +29,7 @@ public static function admin_menu() {
2929
\add_action( 'load-' . $settings_page, array( Settings::class, 'add_settings_help_tab' ) );
3030

3131
// User has to be able to publish posts.
32-
if ( ! is_user_disabled( get_current_user_id() ) ) {
32+
if ( user_can_activitypub( \get_current_user_id() ) ) {
3333
$followers_list_page = \add_users_page(
3434
\__( 'Followers ⁂', 'activitypub' ),
3535
\__( 'Followers ⁂', 'activitypub' ),

includes/wp-admin/class-settings.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
use Activitypub\Collection\Actors;
1111
use Activitypub\Model\Blog;
1212
use Activitypub\Sanitize;
13-
use function Activitypub\is_user_disabled;
13+
use function Activitypub\user_can_activitypub;
1414

1515
/**
1616
* ActivityPub Settings Class.
@@ -281,7 +281,7 @@ public static function settings_page() {
281281
);
282282
}
283283

284-
if ( ! is_user_disabled( Actors::BLOG_USER_ID ) ) {
284+
if ( user_can_activitypub( Actors::BLOG_USER_ID ) ) {
285285
$settings_tabs['blog-profile'] = array(
286286
'label' => __( 'Blog Profile', 'activitypub' ),
287287
'template' => ACTIVITYPUB_PLUGIN_DIR . 'templates/blog-settings.php',
@@ -343,7 +343,7 @@ public static function add_settings_help_tab() {
343343
),
344344
);
345345

346-
if ( ! is_user_disabled( \get_current_user_id() ) ) {
346+
if ( user_can_activitypub( \get_current_user_id() ) ) {
347347
$webfinger = Actors::get_by_id( \get_current_user_id() )->get_webfinger();
348348
} else {
349349
$webfinger = ( new Blog() )->get_webfinger();

0 commit comments

Comments
 (0)