Skip to content

Commit ab04bb8

Browse files
authored
Scheduler: Don't federate Deletes of unfederated posts. (#1528)
* Scheduler: Don't federate Deletes of unfederated posts. * Add changelog
1 parent f304033 commit ab04bb8

File tree

3 files changed

+48
-22
lines changed

3 files changed

+48
-22
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: changed
3+
4+
No longer federates `Delete` activities for posts that were not federated.

includes/scheduler/class-post.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
namespace Activitypub\Scheduler;
99

1010
use function Activitypub\add_to_outbox;
11+
use function Activitypub\get_wp_object_state;
1112
use function Activitypub\is_post_disabled;
1213

1314
/**
@@ -62,7 +63,7 @@ public static function schedule_post_activity( $post_id, $post, $update, $post_b
6263
break;
6364

6465
case 'trash':
65-
$type = 'Delete';
66+
$type = 'federated' === get_wp_object_state( $post ) ? 'Delete' : false;
6667
break;
6768

6869
default:

tests/includes/scheduler/class-test-post.php

Lines changed: 42 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
namespace Activitypub\Tests\Scheduler;
99

10+
use Activitypub\Scheduler\Post;
11+
1012
/**
1113
* Test Post scheduler class.
1214
*
@@ -24,23 +26,23 @@ public function test_transition_attachment_status() {
2426
wp_set_current_user( self::$user_id );
2527

2628
// Create.
27-
$post_id = self::factory()->attachment->create_upload_object( dirname( __DIR__, 2 ) . '/assets/test.jpg' );
28-
$activitpub_id = \add_query_arg( 'p', $post_id, \home_url( '/' ) );
29-
$outbox_item = $this->get_latest_outbox_item( $activitpub_id );
29+
$post_id = self::factory()->attachment->create_upload_object( dirname( __DIR__, 2 ) . '/assets/test.jpg' );
30+
$activitypub_id = \add_query_arg( 'p', $post_id, \home_url( '/' ) );
31+
$outbox_item = $this->get_latest_outbox_item( $activitypub_id );
3032

3133
$this->assertNotNull( $outbox_item );
3234
$this->assertSame( 'Create', \get_post_meta( $outbox_item->ID, '_activitypub_activity_type', true ) );
3335

3436
// Update.
3537
self::factory()->attachment->update_object( $post_id, array( 'post_title' => 'Updated title' ) );
3638

37-
$outbox_item = $this->get_latest_outbox_item( $activitpub_id );
39+
$outbox_item = $this->get_latest_outbox_item( $activitypub_id );
3840
$this->assertSame( 'Update', \get_post_meta( $outbox_item->ID, '_activitypub_activity_type', true ) );
3941

4042
// Delete.
4143
\wp_delete_attachment( $post_id, true );
4244

43-
$outbox_item = $this->get_latest_outbox_item( $activitpub_id );
45+
$outbox_item = $this->get_latest_outbox_item( $activitypub_id );
4446
$this->assertSame( 'Delete', \get_post_meta( $outbox_item->ID, '_activitypub_activity_type', true ) );
4547

4648
remove_post_type_support( 'attachment', 'activitypub' );
@@ -52,12 +54,31 @@ public function test_transition_attachment_status() {
5254
* @covers ::schedule_post_activity
5355
*/
5456
public function test_schedule_post_activity_regular_post() {
55-
$post_id = self::factory()->post->create( array( 'post_author' => self::$user_id ) );
56-
$activitpub_id = \add_query_arg( 'p', $post_id, \home_url( '/' ) );
57+
$post_id = self::factory()->post->create( array( 'post_author' => self::$user_id ) );
58+
$activitypub_id = \add_query_arg( 'p', $post_id, \home_url( '/' ) );
5759

58-
$post = $this->get_latest_outbox_item( $activitpub_id );
60+
$post = $this->get_latest_outbox_item( $activitypub_id );
5961
$id = \get_post_meta( $post->ID, '_activitypub_object_id', true );
60-
$this->assertSame( $activitpub_id, $id );
62+
$this->assertSame( $activitypub_id, $id );
63+
64+
\wp_delete_post( $post_id, true );
65+
}
66+
67+
/**
68+
* Test post activity scheduling for regular posts.
69+
*
70+
* @covers ::schedule_post_activity
71+
*/
72+
public function test_not_schedule_delete_activity_unfederated_post() {
73+
\remove_action( 'wp_after_insert_post', array( Post::class, 'schedule_post_activity' ), 33 );
74+
$post_id = self::factory()->post->create( array( 'post_author' => self::$user_id ) );
75+
$activitypub_id = \add_query_arg( 'p', $post_id, \home_url( '/' ) );
76+
\add_action( 'wp_after_insert_post', array( Post::class, 'schedule_post_activity' ), 33, 4 );
77+
78+
// Trash the post.
79+
\wp_delete_post( $post_id );
80+
81+
$this->assertNull( $this->get_latest_outbox_item( $activitypub_id ) );
6182

6283
\wp_delete_post( $post_id, true );
6384
}
@@ -69,17 +90,17 @@ public function test_schedule_post_activity_regular_post() {
6990
* @covers ::schedule_post_activity
7091
*/
7192
public function test_activity_type_on_publish() {
72-
$post_id = self::factory()->post->create(
93+
$post_id = self::factory()->post->create(
7394
array(
7495
'post_author' => self::$user_id,
7596
'post_status' => 'draft',
7697
)
7798
);
78-
$activitpub_id = \add_query_arg( 'p', $post_id, \home_url( '/' ) );
99+
$activitypub_id = \add_query_arg( 'p', $post_id, \home_url( '/' ) );
79100

80101
\wp_publish_post( $post_id );
81102

82-
$post = $this->get_latest_outbox_item( $activitpub_id );
103+
$post = $this->get_latest_outbox_item( $activitypub_id );
83104
$type = \get_post_meta( $post->ID, '_activitypub_activity_type', true );
84105
$this->assertSame( 'Create', $type );
85106

@@ -93,8 +114,8 @@ public function test_activity_type_on_publish() {
93114
*/
94115
public function test_schedule_post_activity_bulk_edit() {
95116
wp_set_current_user( self::$user_id );
96-
$post_id = self::factory()->post->create( array( 'post_author' => self::$user_id ) );
97-
$activitpub_id = \add_query_arg( 'p', $post_id, \home_url( '/' ) );
117+
$post_id = self::factory()->post->create( array( 'post_author' => self::$user_id ) );
118+
$activitypub_id = \add_query_arg( 'p', $post_id, \home_url( '/' ) );
98119

99120
// Test bulk edit that should bail (no author or status change).
100121
$_REQUEST['bulk_edit'] = 1;
@@ -104,7 +125,7 @@ public function test_schedule_post_activity_bulk_edit() {
104125

105126
bulk_edit_posts( $_REQUEST ); // phpcs:ignore WordPress.Security.NonceVerification
106127

107-
$outbox_item = $this->get_latest_outbox_item( $activitpub_id );
128+
$outbox_item = $this->get_latest_outbox_item( $activitypub_id );
108129
$this->assertNotSame( 'Update', \get_post_meta( $outbox_item->ID, '_activitypub_activity_type', true ) );
109130

110131
// Test bulk edit with author change (should not bail).
@@ -116,7 +137,7 @@ public function test_schedule_post_activity_bulk_edit() {
116137

117138
bulk_edit_posts( $_REQUEST ); // phpcs:ignore WordPress.Security.NonceVerification
118139

119-
$outbox_item = $this->get_latest_outbox_item( $activitpub_id );
140+
$outbox_item = $this->get_latest_outbox_item( $activitypub_id );
120141
$this->assertNotNull( $outbox_item );
121142

122143
$this->assertSame( 'Update', \get_post_meta( $outbox_item->ID, '_activitypub_activity_type', true ) );
@@ -126,7 +147,7 @@ public function test_schedule_post_activity_bulk_edit() {
126147

127148
bulk_edit_posts( $_REQUEST ); // phpcs:ignore WordPress.Security.NonceVerification
128149

129-
$outbox_item = $this->get_latest_outbox_item( $activitpub_id );
150+
$outbox_item = $this->get_latest_outbox_item( $activitypub_id );
130151
$this->assertNotNull( $outbox_item );
131152
$this->assertSame( 'Delete', \get_post_meta( $outbox_item->ID, '_activitypub_activity_type', true ) );
132153

@@ -138,7 +159,7 @@ public function test_schedule_post_activity_bulk_edit() {
138159
/**
139160
* Data provider for no activity tests.
140161
*
141-
* @return array[] Test parameters.
162+
* @return array[][] Test parameters.
142163
*/
143164
public function no_activity_post_provider() {
144165
return array(
@@ -166,10 +187,10 @@ public function no_activity_post_provider() {
166187
* @param array $args Post data for creating the test post.
167188
*/
168189
public function test_no_activity_scheduled( $args ) {
169-
$post_id = self::factory()->post->create( $args );
170-
$activitpub_id = \add_query_arg( 'p', $post_id, \home_url( '/' ) );
190+
$post_id = self::factory()->post->create( $args );
191+
$activitypub_id = \add_query_arg( 'p', $post_id, \home_url( '/' ) );
171192

172-
$this->assertNull( $this->get_latest_outbox_item( $activitpub_id ) );
193+
$this->assertNull( $this->get_latest_outbox_item( $activitypub_id ) );
173194

174195
\wp_delete_post( $post_id, true );
175196
}

0 commit comments

Comments
 (0)