Skip to content

Commit 6914228

Browse files
authored
Improve outbox scheduling of (custom) posts (#1359)
* Improve outbox scheduling of (custom) posts * Fix tests * no longer needed * Update changelog
1 parent 57b135c commit 6914228

File tree

5 files changed

+43
-66
lines changed

5 files changed

+43
-66
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## [Unreleased]
9+
10+
### Changed
11+
12+
* Use a later hook for Posts to get published to the Outbox, to get sure all `post_meta`s and `taxonomy`s are set stored properly.
13+
814
## [5.3.0] - 2025-02-25
915

1016
### Added

includes/scheduler/class-post.php

Lines changed: 31 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -22,52 +22,23 @@ class Post {
2222
*/
2323
public static function init() {
2424
// Post transitions.
25-
\add_action( 'transition_post_status', array( self::class, 'schedule_post_activity' ), 33, 3 );
25+
\add_action( 'wp_after_insert_post', array( self::class, 'schedule_post_activity' ), 33, 4 );
2626

2727
// Attachment transitions.
2828
\add_action( 'add_attachment', array( self::class, 'transition_attachment_status' ) );
2929
\add_action( 'edit_attachment', array( self::class, 'transition_attachment_status' ) );
3030
\add_action( 'delete_attachment', array( self::class, 'transition_attachment_status' ) );
31-
32-
// Get all post types that support ActivityPub.
33-
$post_types = \get_post_types_by_support( 'activitypub' );
34-
35-
foreach ( $post_types as $post_type ) {
36-
\add_filter( "rest_pre_insert_{$post_type}", array( self::class, 'rest_insert' ), 10, 2 );
37-
}
3831
}
3932

4033
/**
41-
* Schedules Activities for attachment transitions.
34+
* Handle post updates and determine the appropriate Activity type.
4235
*
43-
* @param int $post_id Attachment ID.
36+
* @param int $post_id Post ID.
37+
* @param \WP_Post $post Post object.
38+
* @param bool $update Whether this is an existing post being updated.
39+
* @param \WP_Post $post_before Post object before the update.
4440
*/
45-
public static function transition_attachment_status( $post_id ) {
46-
if ( ! \post_type_supports( 'attachment', 'activitypub' ) ) {
47-
return;
48-
}
49-
50-
switch ( current_action() ) {
51-
case 'add_attachment':
52-
self::schedule_post_activity( 'publish', '', get_post( $post_id ) );
53-
break;
54-
case 'edit_attachment':
55-
self::schedule_post_activity( 'publish', 'publish', get_post( $post_id ) );
56-
break;
57-
case 'delete_attachment':
58-
self::schedule_post_activity( 'trash', '', get_post( $post_id ) );
59-
break;
60-
}
61-
}
62-
63-
/**
64-
* Schedule Activities.
65-
*
66-
* @param string $new_status New post status.
67-
* @param string $old_status Old post status.
68-
* @param \WP_Post $post Post object.
69-
*/
70-
public static function schedule_post_activity( $new_status, $old_status, $post ) {
41+
public static function schedule_post_activity( $post_id, $post, $update, $post_before ) {
7142
if ( defined( 'WP_IMPORTING' ) && WP_IMPORTING ) {
7243
return;
7344
}
@@ -81,9 +52,12 @@ public static function schedule_post_activity( $new_status, $old_status, $post )
8152
return;
8253
}
8354

55+
$new_status = get_post_status( $post );
56+
$old_status = $post_before ? get_post_status( $post_before ) : null;
57+
8458
switch ( $new_status ) {
8559
case 'publish':
86-
$type = ( 'publish' === $old_status ) ? 'Update' : 'Create';
60+
$type = $update ? 'Update' : 'Create';
8761
break;
8862

8963
case 'draft':
@@ -108,37 +82,30 @@ public static function schedule_post_activity( $new_status, $old_status, $post )
10882
}
10983

11084
/**
111-
* Filter the post data before it is inserted via the REST API.
112-
*
113-
* Posts being inserted via the REST API have a different order of operations than in wp_insert_post().
114-
* This filter updates post meta before the post is inserted into the database, so that the
115-
* information is available by the time @see Outbox::add() runs.
116-
*
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.
85+
* Schedules Activities for attachment transitions.
11986
*
120-
* @return \stdClass The prepared post.
87+
* @param int $post_id Attachment ID.
12188
*/
122-
public static function rest_insert( $post, $request ) {
123-
$metas = $request->get_param( 'meta' );
124-
125-
if ( empty( $post->ID ) || ! $metas || ! is_array( $metas ) ) {
126-
return $post;
89+
public static function transition_attachment_status( $post_id ) {
90+
if ( ! \post_type_supports( 'attachment', 'activitypub' ) ) {
91+
return;
12792
}
12893

129-
foreach ( $metas as $meta_key => $meta_value ) {
130-
if (
131-
\str_starts_with( $meta_key, 'activitypub_' ) ||
132-
\str_starts_with( $meta_key, '_activitypub_' )
133-
) {
134-
if ( $meta_value ) {
135-
\update_post_meta( $post->ID, $meta_key, $meta_value );
136-
} else {
137-
\delete_post_meta( $post->ID, $meta_key );
138-
}
139-
}
140-
}
94+
$post = \get_post( $post_id );
14195

142-
return $post;
96+
switch ( current_action() ) {
97+
case 'add_attachment':
98+
// Add the post to the outbox.
99+
add_to_outbox( $post, 'Create', $post->post_author );
100+
break;
101+
case 'edit_attachment':
102+
// Update the post to the outbox.
103+
add_to_outbox( $post, 'Update', $post->post_author );
104+
break;
105+
case 'delete_attachment':
106+
// Delete the post from the outbox.
107+
add_to_outbox( $post, 'Delete', $post->post_author );
108+
break;
109+
}
143110
}
144111
}

readme.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,10 @@ For reasons of data protection, it is not possible to see the followers of other
129129

130130
== Changelog ==
131131

132+
= Unreleased =
133+
134+
* Changed: Use a later hook for Posts to get published to the Outbox, to get sure all `post_meta`s and `taxonomy`s are set stored properly.
135+
132136
= 5.3.0 =
133137

134138
* Added: A fallback `Note` for `Article` objects to improve previews on services that don't support Articles yet.

tests/includes/class-test-migration.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class Test_Migration extends ActivityPub_TestCase_Cache_HTTP {
2929
* Set up the test.
3030
*/
3131
public static function set_up_before_class() {
32-
\remove_action( 'transition_post_status', array( \Activitypub\Scheduler\Post::class, 'schedule_post_activity' ), 33 );
32+
\remove_action( 'wp_after_insert_post', array( \Activitypub\Scheduler\Post::class, 'schedule_post_activity' ), 33 );
3333
\remove_action( 'transition_comment_status', array( \Activitypub\Scheduler\Comment::class, 'schedule_comment_activity' ), 20 );
3434
\remove_action( 'wp_insert_comment', array( \Activitypub\Scheduler\Comment::class, 'schedule_comment_activity_on_insert' ) );
3535

tests/includes/class-test-shortcodes.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class Test_Shortcodes extends \WP_UnitTestCase {
3030
public function set_up() {
3131
parent::set_up();
3232

33-
remove_action( 'transition_post_status', array( Post::class, 'schedule_post_activity' ), 33 );
33+
remove_action( 'wp_after_insert_post', array( Post::class, 'schedule_post_activity' ), 33 );
3434

3535
Shortcodes::register();
3636

0 commit comments

Comments
 (0)