| 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 |
\add_filter( |
| 26 |
'activitypub_validate_object', |
| 27 |
array( self::class, 'validate_object' ), |
| 28 |
10, |
| 29 |
3 |
| 30 |
); |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Handles "Create" requests |
| 35 |
* |
| 36 |
* @param array $array The activity-object |
| 37 |
* @param int $user_id The id of the local blog-user |
| 38 |
* @param Activitypub\Activity $object The activity object |
| 39 |
* |
| 40 |
* @return void |
| 41 |
*/ |
| 42 |
public static function handle_create( $array, $user_id, $object = null ) { |
| 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( $state ) ) { |
| 61 |
$reaction = \get_comment( $state ); |
| 62 |
} |
| 63 |
|
| 64 |
\do_action( 'activitypub_handled_create', $array, $user_id, $state, $reaction ); |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Validate the object |
| 69 |
* |
| 70 |
* @param bool $valid The validation state |
| 71 |
* @param string $param The object parameter |
| 72 |
* @param \WP_REST_Request $request The request object |
| 73 |
* @param array $array The activity-object |
| 74 |
* |
| 75 |
* @return bool The validation state: true if valid, false if not |
| 76 |
*/ |
| 77 |
public static function validate_object( $valid, $param, $request ) { |
| 78 |
$json_params = $request->get_json_params(); |
| 79 |
|
| 80 |
if ( |
| 81 |
'Create' !== $json_params['type'] || |
| 82 |
is_wp_error( $request ) |
| 83 |
) { |
| 84 |
return $valid; |
| 85 |
} |
| 86 |
|
| 87 |
$object = $json_params['object']; |
| 88 |
$required = array( |
| 89 |
'id', |
| 90 |
'inReplyTo', |
| 91 |
'content', |
| 92 |
); |
| 93 |
|
| 94 |
if ( array_intersect( $required, array_keys( $object ) ) !== $required ) { |
| 95 |
return false; |
| 96 |
} |
| 97 |
|
| 98 |
return $valid; |
| 99 |
} |
| 100 |
} |
| 101 |
|