Skip to content

Commit b3f936d

Browse files
authored
Improve user ID fallback and transformer logic (#2246)
1 parent 359c801 commit b3f936d

File tree

5 files changed

+46
-5
lines changed

5 files changed

+46
-5
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: fixed
3+
4+
Posts now only fall back to the blog user when blog mode is enabled and no valid author exists, ensuring content negotiation only runs if an Actor is available.

includes/functions.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1300,12 +1300,24 @@ function get_content_warning( $post_id ) {
13001300
/**
13011301
* Get the ActivityPub ID of a User by the WordPress User ID.
13021302
*
1303+
* Fall back to blog user if in blog mode or if user is not found.
1304+
*
13031305
* @param int $id The WordPress User ID.
13041306
*
1305-
* @return string The ActivityPub ID (a URL) of the User.
1307+
* @return string|false The ActivityPub ID (a URL) of the User or false if not found.
13061308
*/
13071309
function get_user_id( $id ) {
1308-
$user = Actors::get_by_id( $id );
1310+
$mode = \get_option( 'activitypub_actor_mode', 'default' );
1311+
1312+
if ( ACTIVITYPUB_BLOG_MODE === $mode ) {
1313+
$user = Actors::get_by_id( Actors::BLOG_USER_ID );
1314+
} else {
1315+
$user = Actors::get_by_id( $id );
1316+
1317+
if ( \is_wp_error( $user ) ) {
1318+
$user = Actors::get_by_id( Actors::BLOG_USER_ID );
1319+
}
1320+
}
13091321

13101322
if ( \is_wp_error( $user ) ) {
13111323
return false;

includes/transformer/class-factory.php

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

14+
use function Activitypub\get_user_id;
1415
use function Activitypub\is_post_disabled;
1516
use function Activitypub\user_can_activitypub;
1617

@@ -90,7 +91,7 @@ public static function get_transformer( $data ) {
9091
case 'WP_Post':
9192
if ( 'attachment' === $data->post_type && ! is_post_disabled( $data ) ) {
9293
return new Attachment( $data );
93-
} elseif ( ! is_post_disabled( $data ) ) {
94+
} elseif ( ! is_post_disabled( $data ) && get_user_id( $data->post_author ) ) {
9495
return new Post( $data );
9596
}
9697
break;

tests/includes/class-test-functions.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -712,6 +712,17 @@ public function test_get_user_id() {
712712
$user->add_cap( 'activitypub' );
713713

714714
$this->assertIsString( \Activitypub\get_user_id( $user->ID ) );
715+
716+
\add_option( 'activitypub_actor_mode', ACTIVITYPUB_ACTOR_MODE );
717+
718+
$this->assertIsString( \Activitypub\get_user_id( $user->ID ) );
719+
720+
$user->remove_cap( 'activitypub' );
721+
\update_option( 'activitypub_actor_mode', ACTIVITYPUB_BLOG_MODE );
722+
$this->assertIsString( \Activitypub\get_user_id( $user->ID ) );
723+
724+
\update_option( 'activitypub_actor_mode', ACTIVITYPUB_ACTOR_MODE );
725+
$this->assertFalse( \Activitypub\get_user_id( $user->ID ) );
715726
}
716727

717728
/**

tests/includes/transformer/class-test-factory.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,13 @@
1313
use Activitypub\Transformer\Factory;
1414
use Activitypub\Transformer\Json;
1515
use Activitypub\Transformer\Post;
16-
use WP_UnitTestCase;
1716

1817
/**
1918
* Test class for Transformer Factory.
2019
*
2120
* @coversDefaultClass \Activitypub\Transformer\Factory
2221
*/
23-
class Test_Factory extends WP_UnitTestCase {
22+
class Test_Factory extends \WP_UnitTestCase {
2423
/**
2524
* Test post ID.
2625
*
@@ -113,6 +112,20 @@ public function test_get_transformer_post() {
113112
$post = get_post( self::$post_id );
114113
$transformer = Factory::get_transformer( $post );
115114

115+
$this->assertInstanceOf( \WP_Error::class, $transformer );
116+
117+
\add_option( 'activitypub_actor_mode', ACTIVITYPUB_ACTOR_AND_BLOG_MODE );
118+
119+
$post = get_post( self::$post_id );
120+
$transformer = Factory::get_transformer( $post );
121+
122+
$this->assertInstanceOf( Post::class, $transformer );
123+
124+
\add_option( 'activitypub_actor_mode', ACTIVITYPUB_ACTOR_MODE );
125+
126+
$post = get_post( self::$post_id );
127+
$transformer = Factory::get_transformer( $post );
128+
116129
$this->assertInstanceOf( Post::class, $transformer );
117130
}
118131

0 commit comments

Comments
 (0)