| 1 |
<?php |
| 2 |
/** |
| 3 |
* Comment 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\set_wp_object_state; |
| 12 |
use function Activitypub\should_comment_be_federated; |
| 13 |
|
| 14 |
/** |
| 15 |
* Post scheduler class. |
| 16 |
*/ |
| 17 |
class Comment { |
| 18 |
/** |
| 19 |
* Initialize the class, registering WordPress hooks. |
| 20 |
*/ |
| 21 |
public static function init() { |
| 22 |
if ( ACTIVITYPUB_DISABLE_OUTGOING_INTERACTIONS ) { |
| 23 |
return; |
| 24 |
} |
| 25 |
|
| 26 |
// Comment transitions. |
| 27 |
\add_action( 'transition_comment_status', array( self::class, 'schedule_comment_activity' ), 20, 3 ); |
| 28 |
\add_action( 'wp_insert_comment', array( self::class, 'schedule_comment_activity_on_insert' ), 10, 2 ); |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* Schedule Comment Activities. |
| 33 |
* |
| 34 |
* @see transition_comment_status() |
| 35 |
* |
| 36 |
* @param string $new_status New comment status. |
| 37 |
* @param string $old_status Old comment status. |
| 38 |
* @param \WP_Comment $comment Comment object. |
| 39 |
*/ |
| 40 |
public static function schedule_comment_activity( $new_status, $old_status, $comment ) { |
| 41 |
if ( defined( 'WP_IMPORTING' ) && WP_IMPORTING ) { |
| 42 |
return; |
| 43 |
} |
| 44 |
|
| 45 |
$comment = get_comment( $comment ); |
| 46 |
|
| 47 |
// Federate only comments that are written by a registered user. |
| 48 |
if ( ! $comment || ! $comment->user_id ) { |
| 49 |
return; |
| 50 |
} |
| 51 |
|
| 52 |
$type = false; |
| 53 |
|
| 54 |
if ( |
| 55 |
'approved' === $new_status && |
| 56 |
'approved' !== $old_status |
| 57 |
) { |
| 58 |
$type = 'Create'; |
| 59 |
} elseif ( 'approved' === $new_status ) { |
| 60 |
$type = 'Update'; |
| 61 |
\update_comment_meta( $comment->comment_ID, 'activitypub_comment_modified', time(), true ); |
| 62 |
} elseif ( |
| 63 |
'trash' === $new_status || |
| 64 |
'spam' === $new_status |
| 65 |
) { |
| 66 |
$type = 'Delete'; |
| 67 |
} |
| 68 |
|
| 69 |
if ( empty( $type ) ) { |
| 70 |
return; |
| 71 |
} |
| 72 |
|
| 73 |
// Check if comment should be federated or not. |
| 74 |
if ( ! should_comment_be_federated( $comment ) ) { |
| 75 |
return; |
| 76 |
} |
| 77 |
|
| 78 |
add_to_outbox( $comment, $type, $comment->user_id ); |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Schedule Comment Activities on insert. |
| 83 |
* |
| 84 |
* @param int $comment_id Comment ID. |
| 85 |
* @param \WP_Comment $comment Comment object. |
| 86 |
*/ |
| 87 |
public static function schedule_comment_activity_on_insert( $comment_id, $comment ) { |
| 88 |
if ( 1 === (int) $comment->comment_approved ) { |
| 89 |
self::schedule_comment_activity( 'approved', '', $comment ); |
| 90 |
} |
| 91 |
} |
| 92 |
} |
| 93 |
|