| 1 |
<?php |
| 2 |
/** |
| 3 |
* Announce handler file. |
| 4 |
* |
| 5 |
* @package Activitypub |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace Activitypub\Handler; |
| 9 |
|
| 10 |
use Activitypub\Collection\Actors; |
| 11 |
use Activitypub\Collection\Interactions; |
| 12 |
use Activitypub\Comment; |
| 13 |
use Activitypub\Http; |
| 14 |
|
| 15 |
use function Activitypub\is_activity; |
| 16 |
use function Activitypub\is_activity_public; |
| 17 |
use function Activitypub\object_to_uri; |
| 18 |
|
| 19 |
/** |
| 20 |
* Handle Create requests. |
| 21 |
*/ |
| 22 |
class Announce { |
| 23 |
/** |
| 24 |
* Initialize the class, registering WordPress hooks. |
| 25 |
*/ |
| 26 |
public static function init() { |
| 27 |
\add_action( 'activitypub_inbox_announce', array( self::class, 'handle_announce' ), 10, 3 ); |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Handles "Announce" requests. |
| 32 |
* |
| 33 |
* @param array $announcement The activity-object. |
| 34 |
* @param int|int[] $user_ids The id(s) of the local blog-user(s). |
| 35 |
* @param \Activitypub\Activity\Activity $activity The activity object. |
| 36 |
*/ |
| 37 |
public static function handle_announce( $announcement, $user_ids, $activity = null ) { |
| 38 |
// Check if Activity is public or not. |
| 39 |
if ( ! is_activity_public( $announcement ) ) { |
| 40 |
// @todo maybe send email |
| 41 |
return; |
| 42 |
} |
| 43 |
|
| 44 |
// Ignore announces from the blog actor. |
| 45 |
if ( Actors::BLOG_USER_ID === Actors::get_id_by_resource( $announcement['actor'] ) ) { |
| 46 |
return; |
| 47 |
} |
| 48 |
|
| 49 |
// Check if reposts are allowed. |
| 50 |
if ( ! Comment::is_comment_type_enabled( 'repost' ) ) { |
| 51 |
return; |
| 52 |
} |
| 53 |
|
| 54 |
self::maybe_save_announce( $announcement, $user_ids ); |
| 55 |
|
| 56 |
if ( is_string( $announcement['object'] ) ) { |
| 57 |
$object = Http::get_remote_object( $announcement['object'] ); |
| 58 |
} else { |
| 59 |
$object = $announcement['object']; |
| 60 |
} |
| 61 |
|
| 62 |
if ( ! $object || is_wp_error( $object ) ) { |
| 63 |
return; |
| 64 |
} |
| 65 |
|
| 66 |
if ( ! is_activity( $object ) ) { |
| 67 |
return; |
| 68 |
} |
| 69 |
|
| 70 |
$type = \strtolower( $object['type'] ); |
| 71 |
|
| 72 |
/** |
| 73 |
* Fires after an Announce has been received. |
| 74 |
* |
| 75 |
* @param array $object The object. |
| 76 |
* @param int[] $user_ids The ids of the local blog-users. |
| 77 |
* @param string $type The type of the activity. |
| 78 |
* @param \Activitypub\Activity\Activity|null $activity The activity object. |
| 79 |
*/ |
| 80 |
\do_action( 'activitypub_inbox', $object, (array) $user_ids, $type, $activity ); |
| 81 |
|
| 82 |
/** |
| 83 |
* Fires after an Announce of a specific type has been received. |
| 84 |
* |
| 85 |
* @param array $object The object. |
| 86 |
* @param int[] $user_ids The ids of the local blog-users. |
| 87 |
* @param \Activitypub\Activity\Activity|null $activity The activity object. |
| 88 |
*/ |
| 89 |
\do_action( "activitypub_inbox_{$type}", $object, (array) $user_ids, $activity ); |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Try to save the Announce. |
| 94 |
* |
| 95 |
* @param array $activity The activity-object. |
| 96 |
* @param int|int[] $user_ids The id of the local blog-user. |
| 97 |
*/ |
| 98 |
public static function maybe_save_announce( $activity, $user_ids ) { |
| 99 |
$url = object_to_uri( $activity ); |
| 100 |
|
| 101 |
if ( empty( $url ) ) { |
| 102 |
return; |
| 103 |
} |
| 104 |
|
| 105 |
$exists = Comment::object_id_to_comment( esc_url_raw( $url ) ); |
| 106 |
if ( $exists ) { |
| 107 |
return; |
| 108 |
} |
| 109 |
|
| 110 |
// If the object is a Create activity, extract the actual object from it. |
| 111 |
if ( isset( $activity['object']['type'] ) && 'Create' === $activity['object']['type'] ) { |
| 112 |
$activity['object'] = object_to_uri( $activity['object']['object'] ); |
| 113 |
} |
| 114 |
|
| 115 |
$success = false; |
| 116 |
$result = Interactions::add_reaction( $activity ); |
| 117 |
|
| 118 |
if ( $result && ! is_wp_error( $result ) ) { |
| 119 |
$success = true; |
| 120 |
$result = get_comment( $result ); |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* Fires after an ActivityPub Announce activity has been handled. |
| 125 |
* |
| 126 |
* @param array $activity The ActivityPub activity data. |
| 127 |
* @param int[] $user_ids The local user IDs. |
| 128 |
* @param bool $success True on success, false otherwise. |
| 129 |
* @param array|string|int|\WP_Error|false $result The WP_Comment object of the created announce/repost comment, or null if creation failed. |
| 130 |
*/ |
| 131 |
\do_action( 'activitypub_handled_announce', $activity, (array) $user_ids, $success, $result ); |
| 132 |
} |
| 133 |
} |
| 134 |
|