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