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-comment.php

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

121 lines 3.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Comment scheduler class file.
4 *
5 * @package Activitypub
6 */
7
8 namespace Activitypub\Scheduler;
9
10 use Activitypub\Comment as Comment_Utils;
11
12 use function Activitypub\add_to_outbox;
13 use function Activitypub\should_comment_be_federated;
14
15 /**
16 * Post scheduler class.
17 */
18 class Comment {
19 /**
20 * Initialize the class, registering WordPress hooks.
21 */
22 public static function init() {
23 if ( ACTIVITYPUB_DISABLE_OUTGOING_INTERACTIONS ) {
24 return;
25 }
26
27 // Comment transitions.
28 \add_action( 'transition_comment_status', array( self::class, 'schedule_comment_activity' ), 20, 3 );
29 \add_action( 'wp_insert_comment', array( self::class, 'schedule_comment_activity_on_insert' ), 10, 2 );
30 \add_action( 'delete_comment', array( self::class, 'schedule_comment_delete_activity' ), 10, 2 );
31 }
32
33 /**
34 * Schedule Comment Activities.
35 *
36 * @see transition_comment_status()
37 *
38 * @param string $new_status New comment status.
39 * @param string $old_status Old comment status.
40 * @param \WP_Comment $comment Comment object.
41 */
42 public static function schedule_comment_activity( $new_status, $old_status, $comment ) {
43 if ( defined( 'WP_IMPORTING' ) && WP_IMPORTING ) {
44 return;
45 }
46
47 $comment = get_comment( $comment );
48
49 // Federate only comments that are written by a registered user.
50 if ( ! $comment || ! $comment->user_id ) {
51 return;
52 }
53
54 /*
55 * Check against supported comment types.
56 * Only federate registered ActivityPub comment types and standard WordPress comments.
57 */
58 $allowed_types = Comment_Utils::get_comment_type_slugs();
59 $allowed_types[] = 'comment'; // Add core WordPress comment types.
60
61 // Check if comment type is in allowed list.
62 if ( ! in_array( $comment->comment_type, $allowed_types, true ) ) {
63 return;
64 }
65
66 $type = false;
67
68 if (
69 'approved' === $new_status &&
70 'approved' !== $old_status
71 ) {
72 $type = 'Create';
73 } elseif ( 'approved' === $new_status ) {
74 $type = 'Update';
75 \update_comment_meta( $comment->comment_ID, 'activitypub_comment_modified', time(), true );
76 } elseif (
77 'trash' === $new_status ||
78 ( 'delete' === $new_status && '' === $old_status ) || // Went through schedule_comment_delete_activity().
79 'spam' === $new_status
80 ) {
81 $type = 'Delete';
82 }
83
84 if ( empty( $type ) ) {
85 return;
86 }
87
88 // Check if comment should be federated or not.
89 if ( ! should_comment_be_federated( $comment ) ) {
90 return;
91 }
92
93 add_to_outbox( $comment, $type, $comment->user_id );
94 }
95
96 /**
97 * Schedule Comment Activities on insert.
98 *
99 * @param int $comment_id Comment ID.
100 * @param \WP_Comment $comment Comment object.
101 */
102 public static function schedule_comment_activity_on_insert( $comment_id, $comment ) {
103 if ( 1 === (int) $comment->comment_approved ) {
104 self::schedule_comment_activity( 'approved', '', $comment );
105 }
106 }
107
108 /**
109 * Schedule Delete activity when a comment is permanently deleted.
110 *
111 * @param int $comment_id Comment ID.
112 * @param \WP_Comment $comment Comment object.
113 */
114 public static function schedule_comment_delete_activity( $comment_id, $comment ) {
115 // Only send Delete activities for comments that were previously federated.
116 if ( Comment_Utils::was_sent( $comment ) ) {
117 self::schedule_comment_activity( 'delete', '', $comment );
118 }
119 }
120 }
121