PluginProbe
ActivityPub / 5.7.0
ActivityPub v5.7.0
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 5.7.0, at includes/scheduler/class-post.php

114 lines 3.0 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'] ) && -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 $type = ( 'publish' === $old_status ) ? 'Update' : 'Create';
59 break;
60
61 case 'draft':
62 $type = ( 'publish' === $old_status ) ? 'Update' : false;
63 break;
64
65 case 'trash':
66 $type = 'federated' === get_wp_object_state( $post ) ? 'Delete' : false;
67 break;
68
69 default:
70 $type = false;
71 }
72
73 // Do not send Activities if `$type` is not set or unknown.
74 if ( empty( $type ) ) {
75 return;
76 }
77
78 // Add the post to the outbox.
79 add_to_outbox( $post, $type, $post->post_author );
80 }
81
82 /**
83 * Schedules Activities for attachment transitions.
84 *
85 * @param int $post_id Attachment ID.
86 */
87 public static function transition_attachment_status( $post_id ) {
88 if ( \defined( 'WP_IMPORTING' ) && WP_IMPORTING ) {
89 return;
90 }
91
92 if ( ! \post_type_supports( 'attachment', 'activitypub' ) ) {
93 return;
94 }
95
96 $post = \get_post( $post_id );
97
98 switch ( \current_action() ) {
99 case 'add_attachment':
100 // Add the post to the outbox.
101 add_to_outbox( $post, 'Create', $post->post_author );
102 break;
103 case 'edit_attachment':
104 // Update the post to the outbox.
105 add_to_outbox( $post, 'Update', $post->post_author );
106 break;
107 case 'delete_attachment':
108 // Delete the post from the outbox.
109 add_to_outbox( $post, 'Delete', $post->post_author );
110 break;
111 }
112 }
113 }
114