| 1 |
<?php |
| 2 |
/** |
| 3 |
* Announce handler file. |
| 4 |
* |
| 5 |
* @package Activitypub |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace Activitypub\Handler; |
| 9 |
|
| 10 |
use Activitypub\Http; |
| 11 |
use Activitypub\Comment; |
| 12 |
use Activitypub\Collection\Interactions; |
| 13 |
|
| 14 |
use function Activitypub\object_to_uri; |
| 15 |
use function Activitypub\is_activity_public; |
| 16 |
|
| 17 |
/** |
| 18 |
* Handle Create requests. |
| 19 |
*/ |
| 20 |
class Announce { |
| 21 |
/** |
| 22 |
* Initialize the class, registering WordPress hooks. |
| 23 |
*/ |
| 24 |
public static function init() { |
| 25 |
\add_action( |
| 26 |
'activitypub_inbox_announce', |
| 27 |
array( self::class, 'handle_announce' ), |
| 28 |
10, |
| 29 |
3 |
| 30 |
); |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Handles "Announce" requests. |
| 35 |
* |
| 36 |
* @param array $announcement The activity-object. |
| 37 |
* @param int $user_id The id of the local blog-user. |
| 38 |
* @param \Activitypub\Activity\Activity $activity The activity object. |
| 39 |
*/ |
| 40 |
public static function handle_announce( $announcement, $user_id, $activity = null ) { |
| 41 |
// Check if Activity is public or not. |
| 42 |
if ( ! is_activity_public( $announcement ) ) { |
| 43 |
// @todo maybe send email |
| 44 |
return; |
| 45 |
} |
| 46 |
|
| 47 |
self::maybe_save_announce( $announcement, $user_id ); |
| 48 |
|
| 49 |
if ( is_string( $announcement['object'] ) ) { |
| 50 |
$object = Http::get_remote_object( $announcement['object'] ); |
| 51 |
} else { |
| 52 |
$object = $announcement['object']; |
| 53 |
} |
| 54 |
|
| 55 |
if ( ! $object || is_wp_error( $object ) ) { |
| 56 |
return; |
| 57 |
} |
| 58 |
|
| 59 |
if ( ! isset( $object['type'] ) ) { |
| 60 |
return; |
| 61 |
} |
| 62 |
|
| 63 |
$type = \strtolower( $object['type'] ); |
| 64 |
|
| 65 |
/** |
| 66 |
* Fires after an Announce has been received. |
| 67 |
* |
| 68 |
* @param array $object The object. |
| 69 |
* @param int $user_id The id of the local blog-user. |
| 70 |
* @param array $activity The activity object. |
| 71 |
*/ |
| 72 |
\do_action( 'activitypub_inbox', $object, $user_id, $type, $activity ); |
| 73 |
|
| 74 |
/** |
| 75 |
* Fires after an Announce of a specific type has been received. |
| 76 |
* |
| 77 |
* @param array $object The object. |
| 78 |
* @param int $user_id The id of the local blog-user. |
| 79 |
* @param array $activity The activity object. |
| 80 |
*/ |
| 81 |
\do_action( "activitypub_inbox_{$type}", $object, $user_id, $activity ); |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Try to save the Announce. |
| 86 |
* |
| 87 |
* @param array $activity The activity-object. |
| 88 |
* @param int $user_id The id of the local blog-user. |
| 89 |
*/ |
| 90 |
public static function maybe_save_announce( $activity, $user_id ) { |
| 91 |
$url = object_to_uri( $activity['object'] ); |
| 92 |
|
| 93 |
if ( empty( $url ) ) { |
| 94 |
return; |
| 95 |
} |
| 96 |
|
| 97 |
$exists = Comment::object_id_to_comment( esc_url_raw( $url ) ); |
| 98 |
if ( $exists ) { |
| 99 |
return; |
| 100 |
} |
| 101 |
|
| 102 |
$state = Interactions::add_reaction( $activity ); |
| 103 |
$reaction = null; |
| 104 |
|
| 105 |
if ( $state && ! is_wp_error( $state ) ) { |
| 106 |
$reaction = get_comment( $state ); |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Fires after an Announce has been saved. |
| 111 |
* |
| 112 |
* @param array $activity The activity-object. |
| 113 |
* @param int $user_id The id of the local blog-user. |
| 114 |
* @param mixed $state The state of the reaction. |
| 115 |
* @param mixed $reaction The reaction. |
| 116 |
*/ |
| 117 |
do_action( 'activitypub_handled_announce', $activity, $user_id, $state, $reaction ); |
| 118 |
} |
| 119 |
} |
| 120 |
|