Skip to content

Commit 423b651

Browse files
authored
Outbox: Skip most bulk editing (#1341)
* Outbox: Skip most bulk editing Props @mattwiebe. * Changelog * Use correct request key for new post status * Use bulk edit function for tests
1 parent 007877c commit 423b651

File tree

4 files changed

+58
-3
lines changed

4 files changed

+58
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1717
### Changed
1818

1919
* Outbox now precesses the first batch of followers right away to avoid delays in processing new Activities.
20+
* Post bulk edits no longer create Outbox items, unless author or post status change.
2021

2122
### Fixed
2223

includes/scheduler/class-post.php

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

88
namespace Activitypub\Scheduler;
99

10-
use Activitypub\Scheduler;
1110
use Activitypub\Collection\Outbox;
1211

1312
use function Activitypub\add_to_outbox;
@@ -77,6 +76,11 @@ public static function schedule_post_activity( $new_status, $old_status, $post )
7776
return;
7877
}
7978

79+
// Bail on bulk edits, unless post author or post status changed.
80+
if ( isset( $_REQUEST['bulk_edit'] ) && -1 === (int) $_REQUEST['post_author'] && -1 === (int) $_REQUEST['_status'] ) { // phpcs:ignore WordPress
81+
return;
82+
}
83+
8084
switch ( $new_status ) {
8185
case 'publish':
8286
$type = ( 'publish' === $old_status ) ? 'Update' : 'Create';
@@ -110,8 +114,8 @@ public static function schedule_post_activity( $new_status, $old_status, $post )
110114
* This filter updates post meta before the post is inserted into the database, so that the
111115
* information is available by the time @see Outbox::add() runs.
112116
*
113-
* @param \stdClass $post An object representing a single post prepared for inserting or updating the database.
114-
* @param \WP_REST_Request $request The request object.
117+
* @param \stdClass $post An object representing a single post prepared for inserting or updating the database.
118+
* @param \WP_REST_Request $request The request object.
115119
*
116120
* @return \stdClass The prepared post.
117121
*/

readme.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ For reasons of data protection, it is not possible to see the followers of other
137137
* Added: Setting to adjust the number of days Outbox items are kept before being purged.
138138
* Added: Show metadata in the New Follower E-Mail.
139139
* Changed: Outbox now precesses the first batch of followers right away to avoid delays in processing new Activities.
140+
* Changed: Post bulk edits no longer create Outbox items, unless author or post status change.
140141
* Fixed: The Outbox purging routine no longer is limited to deleting 5 items at a time.
141142
* Fixed an issue where the outbox could not send object types other than `Base_Object` (introduced in 5.0.0).
142143
* Fixed: Ellipses now display correctly in notification emails for Likes and Reposts.

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

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,55 @@ public function test_schedule_post_activity_regular_post() {
6262
\wp_delete_post( $post_id, true );
6363
}
6464

65+
/**
66+
* Test post activity scheduling during bulk edits.
67+
*
68+
* @covers ::schedule_post_activity
69+
*/
70+
public function test_schedule_post_activity_bulk_edit() {
71+
wp_set_current_user( self::$user_id );
72+
$post_id = self::factory()->post->create( array( 'post_author' => self::$user_id ) );
73+
$activitpub_id = \add_query_arg( 'p', $post_id, \home_url( '/' ) );
74+
75+
// Test bulk edit that should bail (no author or status change).
76+
$_REQUEST['bulk_edit'] = 1;
77+
$_REQUEST['post_author'] = -1;
78+
$_REQUEST['_status'] = -1;
79+
$_REQUEST['post'] = array( $post_id );
80+
81+
bulk_edit_posts( $_REQUEST ); // phpcs:ignore WordPress.Security.NonceVerification
82+
83+
$outbox_item = $this->get_latest_outbox_item( $activitpub_id );
84+
$this->assertNotSame( 'Update', \get_post_meta( $outbox_item->ID, '_activitypub_activity_type', true ) );
85+
86+
// Test bulk edit with author change (should not bail).
87+
$new_user_id = self::factory()->user->create( array( 'role' => 'editor' ) );
88+
get_userdata( $new_user_id )->add_cap( 'activitypub' );
89+
wp_set_current_user( $new_user_id );
90+
91+
$_REQUEST['post_author'] = $new_user_id;
92+
93+
bulk_edit_posts( $_REQUEST ); // phpcs:ignore WordPress.Security.NonceVerification
94+
95+
$outbox_item = $this->get_latest_outbox_item( $activitpub_id );
96+
$this->assertNotNull( $outbox_item );
97+
98+
$this->assertSame( 'Update', \get_post_meta( $outbox_item->ID, '_activitypub_activity_type', true ) );
99+
100+
// Test bulk edit with status change (should not bail).
101+
$_REQUEST['_status'] = 'trash';
102+
103+
bulk_edit_posts( $_REQUEST ); // phpcs:ignore WordPress.Security.NonceVerification
104+
105+
$outbox_item = $this->get_latest_outbox_item( $activitpub_id );
106+
$this->assertNotNull( $outbox_item );
107+
$this->assertSame( 'Delete', \get_post_meta( $outbox_item->ID, '_activitypub_activity_type', true ) );
108+
109+
// Clean up.
110+
unset( $_REQUEST['bulk_edit'], $_REQUEST['post_author'], $_REQUEST['post_status'] );
111+
\wp_delete_post( $post_id, true );
112+
}
113+
65114
/**
66115
* Data provider for no activity tests.
67116
*

0 commit comments

Comments
 (0)