| 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 |
$comment_type = $comment->comment_type; |
| 59 |
if ( '' === $comment_type ) { |
| 60 |
// Be backwards compatible with comments that have an empty type by treating them as standard comments. |
| 61 |
$comment_type = 'comment'; |
| 62 |
} |
| 63 |
|
| 64 |
$allowed_types = Comment_Utils::get_comment_type_slugs(); |
| 65 |
$allowed_types[] = 'comment'; // Add core WordPress comment types. |
| 66 |
|
| 67 |
// Check if comment type is in allowed list. |
| 68 |
if ( ! in_array( $comment_type, $allowed_types, true ) ) { |
| 69 |
return; |
| 70 |
} |
| 71 |
|
| 72 |
$type = false; |
| 73 |
|
| 74 |
if ( |
| 75 |
'approved' === $new_status && |
| 76 |
'approved' !== $old_status |
| 77 |
) { |
| 78 |
$type = 'Create'; |
| 79 |
} elseif ( 'approved' === $new_status ) { |
| 80 |
$type = 'Update'; |
| 81 |
\update_comment_meta( $comment->comment_ID, 'activitypub_comment_modified', time(), true ); |
| 82 |
} elseif ( |
| 83 |
'trash' === $new_status || |
| 84 |
( 'delete' === $new_status && '' === $old_status ) || // Went through schedule_comment_delete_activity(). |
| 85 |
'spam' === $new_status |
| 86 |
) { |
| 87 |
$type = 'Delete'; |
| 88 |
} |
| 89 |
|
| 90 |
if ( empty( $type ) ) { |
| 91 |
return; |
| 92 |
} |
| 93 |
|
| 94 |
// Check if comment should be federated or not. |
| 95 |
if ( ! should_comment_be_federated( $comment ) ) { |
| 96 |
return; |
| 97 |
} |
| 98 |
|
| 99 |
add_to_outbox( $comment, $type, $comment->user_id ); |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Schedule Comment Activities on insert. |
| 104 |
* |
| 105 |
* @param int $comment_id Comment ID. |
| 106 |
* @param \WP_Comment $comment Comment object. |
| 107 |
*/ |
| 108 |
public static function schedule_comment_activity_on_insert( $comment_id, $comment ) { |
| 109 |
if ( 1 === (int) $comment->comment_approved ) { |
| 110 |
self::schedule_comment_activity( 'approved', '', $comment ); |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Schedule Delete activity when a comment is permanently deleted. |
| 116 |
* |
| 117 |
* @param int $comment_id Comment ID. |
| 118 |
* @param \WP_Comment $comment Comment object. |
| 119 |
*/ |
| 120 |
public static function schedule_comment_delete_activity( $comment_id, $comment ) { |
| 121 |
// Only send Delete activities for comments that were previously federated. |
| 122 |
if ( Comment_Utils::was_sent( $comment ) ) { |
| 123 |
self::schedule_comment_activity( 'delete', '', $comment ); |
| 124 |
} |
| 125 |
} |
| 126 |
} |
| 127 |
|