| 1 |
<?php |
| 2 |
namespace Activitypub\Handler; |
| 3 |
|
| 4 |
use WP_Error; |
| 5 |
use Activitypub\Collection\Interactions; |
| 6 |
|
| 7 |
use function Activitypub\is_activity_public; |
| 8 |
use function Activitypub\object_id_to_comment; |
| 9 |
|
| 10 |
/** |
| 11 |
* Handle Create requests |
| 12 |
*/ |
| 13 |
class Create { |
| 14 |
/** |
| 15 |
* Initialize the class, registering WordPress hooks |
| 16 |
*/ |
| 17 |
public static function init() { |
| 18 |
\add_action( |
| 19 |
'activitypub_inbox_create', |
| 20 |
array( self::class, 'handle_create' ), |
| 21 |
10, |
| 22 |
3 |
| 23 |
); |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* Handles "Create" requests |
| 28 |
* |
| 29 |
* @param array $array The activity-object |
| 30 |
* @param int $user_id The id of the local blog-user |
| 31 |
* @param Activitypub\Activity $object The activity object |
| 32 |
* |
| 33 |
* @return void |
| 34 |
*/ |
| 35 |
public static function handle_create( $array, $user_id, $object = null ) { |
| 36 |
if ( |
| 37 |
! isset( $array['object'] ) || |
| 38 |
! isset( $array['object']['id'] ) |
| 39 |
) { |
| 40 |
return; |
| 41 |
} |
| 42 |
|
| 43 |
// check if Activity is public or not |
| 44 |
if ( ! is_activity_public( $array ) ) { |
| 45 |
// @todo maybe send email |
| 46 |
return; |
| 47 |
} |
| 48 |
|
| 49 |
$check_dupe = object_id_to_comment( $array['object']['id'] ); |
| 50 |
|
| 51 |
// if comment exists, call update action |
| 52 |
if ( $check_dupe ) { |
| 53 |
\do_action( 'activitypub_inbox_update', $array, $user_id, $object ); |
| 54 |
return; |
| 55 |
} |
| 56 |
|
| 57 |
$state = Interactions::add_comment( $array ); |
| 58 |
$reaction = null; |
| 59 |
|
| 60 |
if ( $state && ! \is_wp_error( $reaction ) ) { |
| 61 |
$reaction = \get_comment( $state ); |
| 62 |
} |
| 63 |
|
| 64 |
\do_action( 'activitypub_handled_create', $array, $user_id, $state, $reaction ); |
| 65 |
} |
| 66 |
} |
| 67 |
|