@@ -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}
0 commit comments