PluginProbe
ActivityPub / 7.8.4
ActivityPub v7.8.4
9.3.1 9.3.0 9.2.2 9.2.1 9.2.0 9.1.0 9.0.2 9.0.1 9.0.0 8.3.0 8.2.1 8.2.0 8.1.1 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.2.0 1.3.0 2.0.0 2.0.1 2.1.0 2.1.1 All 160 releases
activitypub / includes / scheduler / class-post.php

class-post.php in ActivityPub 7.8.4, at includes/scheduler/class-post.php

127 lines 3.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Post scheduler class file.
4 *
5 * @package Activitypub
6 */
7
8 namespace Activitypub\Scheduler;
9
10 use function Activitypub\add_to_outbox;
11 use function Activitypub\get_wp_object_state;
12 use function Activitypub\is_post_disabled;
13
14 /**
15 * Post scheduler class.
16 */
17 class Post {
18 /**
19 * Initialize the class, registering WordPress hooks.
20 */
21 public static function init() {
22 // Post transitions.
23 \add_action( 'wp_after_insert_post', array( self::class, 'schedule_post_activity' ), 33, 4 );
24
25 // Attachment transitions.
26 \add_action( 'add_attachment', array( self::class, 'transition_attachment_status' ) );
27 \add_action( 'edit_attachment', array( self::class, 'transition_attachment_status' ) );
28 \add_action( 'delete_attachment', array( self::class, 'transition_attachment_status' ) );
29 }
30
31 /**
32 * Handle post updates and determine the appropriate Activity type.
33 *
34 * @param int $post_id Post ID.
35 * @param \WP_Post $post Post object.
36 * @param bool $update Whether this is an existing post being updated.
37 * @param \WP_Post $post_before Post object before the update.
38 */
39 public static function schedule_post_activity( $post_id, $post, $update, $post_before ) {
40 if ( defined( 'WP_IMPORTING' ) && WP_IMPORTING ) {
41 return;
42 }
43
44 if ( is_post_disabled( $post ) ) {
45 return;
46 }
47
48 // Bail on bulk edits, unless post author or post status changed.
49 if ( isset( $_REQUEST['bulk_edit'] ) && ( ! isset( $_REQUEST['post_author'] ) || -1 === (int) $_REQUEST['post_author'] ) && -1 === (int) $_REQUEST['_status'] ) { // phpcs:ignore WordPress
50 return;
51 }
52
53 $new_status = get_post_status( $post );
54 $old_status = $post_before ? get_post_status( $post_before ) : null;
55
56 switch ( $new_status ) {
57 case 'publish':
58 if ( $update ) {
59 $type = ( 'publish' === $old_status ) ? 'Update' : 'Create';
60 } else {
61 $type = 'Create';
62 }
63 break;
64
65 case 'draft':
66 $type = ( 'publish' === $old_status ) ? 'Update' : false;
67 break;
68
69 case 'trash':
70 $type = 'federated' === get_wp_object_state( $post ) ? 'Delete' : false;
71 break;
72
73 default:
74 $type = false;
75 }
76
77 // Do not send Activities if `$type` is not set or unknown.
78 if ( empty( $type ) ) {
79 return;
80 }
81
82 // If the post was not federated before but is an Update activity, it should be a Create activity.
83 if ( get_wp_object_state( $post ) !== 'federated' && 'Update' === $type ) {
84 $type = 'Create';
85 }
86
87 // Add the post to the outbox.
88 add_to_outbox( $post, $type, $post->post_author );
89 }
90
91 /**
92 * Schedules Activities for attachment transitions.
93 *
94 * @param int $post_id Attachment ID.
95 */
96 public static function transition_attachment_status( $post_id ) {
97 if ( \defined( 'WP_IMPORTING' ) && WP_IMPORTING ) {
98 return;
99 }
100
101 if ( ! \post_type_supports( 'attachment', 'activitypub' ) ) {
102 return;
103 }
104
105 if ( is_post_disabled( $post_id ) ) {
106 return;
107 }
108
109 $post = \get_post( $post_id );
110
111 switch ( \current_action() ) {
112 case 'add_attachment':
113 // Add the post to the outbox.
114 add_to_outbox( $post, 'Create', $post->post_author );
115 break;
116 case 'edit_attachment':
117 // Update the post to the outbox.
118 add_to_outbox( $post, 'Update', $post->post_author );
119 break;
120 case 'delete_attachment':
121 // Delete the post from the outbox.
122 add_to_outbox( $post, 'Delete', $post->post_author );
123 break;
124 }
125 }
126 }
127