Skip to content

Commit 113bdb4

Browse files
authored
Prevent post creation when Reader is deactivated (#2666)
1 parent bfc5210 commit 113bdb4

File tree

3 files changed

+114
-0
lines changed

3 files changed

+114
-0
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+
Prevent post creation when Reader is deactivated.

includes/handler/class-create.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,10 @@ public static function create_interaction( $activity, $user_ids, $activity_objec
104104
* @return \WP_Post|\WP_Error|false The post on success, WP_Error on failure, false if already exists.
105105
*/
106106
public static function create_post( $activity, $user_ids, $activity_object = null ) {
107+
if ( ! \get_option( 'activitypub_create_posts', false ) ) {
108+
return false;
109+
}
110+
107111
$existing_post = Posts::get_by_guid( $activity['object']['id'] );
108112

109113
// If post exists, call update action.

tests/phpunit/tests/includes/handler/class-test-create.php

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,4 +436,110 @@ public function test_handle_create_malformed_object() {
436436
// Should not create objects with malformed data.
437437
$this->assertEquals( count( $objects_before ), count( $objects_after ) );
438438
}
439+
440+
/**
441+
* Test create_post returns false when activitypub_create_posts option is disabled.
442+
*
443+
* @covers ::create_post
444+
*/
445+
public function test_create_post_disabled_by_option() {
446+
// Ensure option is not set.
447+
\delete_option( 'activitypub_create_posts' );
448+
449+
// Mock HTTP request for Remote_Actors::fetch_by_uri.
450+
$mock_callback = function ( $pre, $url_or_object ) {
451+
$url = \Activitypub\object_to_uri( $url_or_object );
452+
if ( 'https://example.com/users/testuser' === $url ) {
453+
return array(
454+
'id' => 'https://example.com/users/testuser',
455+
'type' => 'Person',
456+
'name' => 'Test Actor',
457+
'preferredUsername' => 'testuser',
458+
'url' => 'https://example.com/users/testuser',
459+
'inbox' => 'https://example.com/users/testuser/inbox',
460+
);
461+
}
462+
return $pre;
463+
};
464+
\add_filter( 'activitypub_pre_http_get_remote_object', $mock_callback, 10, 2 );
465+
466+
$activity = array(
467+
'id' => 'https://example.com/activities/create_disabled',
468+
'type' => 'Create',
469+
'actor' => 'https://example.com/users/testuser',
470+
'to' => array( 'https://www.w3.org/ns/activitystreams#Public' ),
471+
'object' => array(
472+
'id' => 'https://example.com/objects/note_disabled',
473+
'type' => 'Note',
474+
'content' => '<p>This should not be created</p>',
475+
'attributedTo' => 'https://example.com/users/testuser',
476+
'published' => '2023-01-01T12:00:00Z',
477+
'to' => array( 'https://www.w3.org/ns/activitystreams#Public' ),
478+
),
479+
);
480+
481+
$result = Create::create_post( $activity, array( $this->user_id ) );
482+
483+
$this->assertFalse( $result );
484+
485+
// Verify no post was created.
486+
$created_object = Posts::get_by_guid( 'https://example.com/objects/note_disabled' );
487+
$this->assertTrue( \is_wp_error( $created_object ) );
488+
489+
\remove_filter( 'activitypub_pre_http_get_remote_object', $mock_callback );
490+
}
491+
492+
/**
493+
* Test create_post works when activitypub_create_posts option is enabled.
494+
*
495+
* @covers ::create_post
496+
*/
497+
public function test_create_post_enabled_by_option() {
498+
// Enable the option.
499+
\update_option( 'activitypub_create_posts', '1' );
500+
501+
// Mock HTTP request for Remote_Actors::fetch_by_uri.
502+
$mock_callback = function ( $pre, $url_or_object ) {
503+
$url = \Activitypub\object_to_uri( $url_or_object );
504+
if ( 'https://example.com/users/testuser2' === $url ) {
505+
return array(
506+
'id' => 'https://example.com/users/testuser2',
507+
'type' => 'Person',
508+
'name' => 'Test Actor 2',
509+
'preferredUsername' => 'testuser2',
510+
'url' => 'https://example.com/users/testuser2',
511+
'inbox' => 'https://example.com/users/testuser2/inbox',
512+
);
513+
}
514+
return $pre;
515+
};
516+
\add_filter( 'activitypub_pre_http_get_remote_object', $mock_callback, 10, 2 );
517+
518+
$activity = array(
519+
'id' => 'https://example.com/activities/create_enabled',
520+
'type' => 'Create',
521+
'actor' => 'https://example.com/users/testuser2',
522+
'to' => array( 'https://www.w3.org/ns/activitystreams#Public' ),
523+
'object' => array(
524+
'id' => 'https://example.com/objects/note_enabled',
525+
'type' => 'Note',
526+
'content' => '<p>This should be created</p>',
527+
'attributedTo' => 'https://example.com/users/testuser2',
528+
'published' => '2023-01-01T12:00:00Z',
529+
'to' => array( 'https://www.w3.org/ns/activitystreams#Public' ),
530+
),
531+
);
532+
533+
$result = Create::create_post( $activity, array( $this->user_id ) );
534+
535+
$this->assertInstanceOf( 'WP_Post', $result );
536+
537+
// Verify post was created.
538+
$created_object = Posts::get_by_guid( 'https://example.com/objects/note_enabled' );
539+
$this->assertNotNull( $created_object );
540+
$this->assertStringContainsString( 'This should be created', $created_object->post_content );
541+
542+
\remove_filter( 'activitypub_pre_http_get_remote_object', $mock_callback );
543+
\delete_option( 'activitypub_create_posts' );
544+
}
439545
}

0 commit comments

Comments
 (0)