File tree Expand file tree Collapse file tree 5 files changed +46
-5
lines changed
Expand file tree Collapse file tree 5 files changed +46
-5
lines changed Original file line number Diff line number Diff line change 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.
Original file line number Diff line number Diff 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 */
13071309function 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 ;
Original file line number Diff line number Diff line change 1111use Activitypub \Comment as Comment_Helper ;
1212use Activitypub \Http ;
1313
14+ use function Activitypub \get_user_id ;
1415use function Activitypub \is_post_disabled ;
1516use 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 ;
Original file line number Diff line number Diff 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 /**
Original file line number Diff line number Diff line change 1313use Activitypub \Transformer \Factory ;
1414use Activitypub \Transformer \Json ;
1515use 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
You can’t perform that action at this time.
0 commit comments