2023-12-08 23:23:11 +00:00
|
|
|
<?php
|
2024-10-09 12:44:17 +00:00
|
|
|
/**
|
|
|
|
* Create handler file.
|
|
|
|
*
|
|
|
|
* @package Activitypub
|
|
|
|
*/
|
|
|
|
|
2023-12-08 23:23:11 +00:00
|
|
|
namespace Activitypub\Handler;
|
|
|
|
|
|
|
|
use Activitypub\Collection\Interactions;
|
|
|
|
|
|
|
|
use function Activitypub\is_activity_public;
|
|
|
|
use function Activitypub\object_id_to_comment;
|
|
|
|
|
|
|
|
/**
|
2024-10-09 12:44:17 +00:00
|
|
|
* Handle Create requests.
|
2023-12-08 23:23:11 +00:00
|
|
|
*/
|
|
|
|
class Create {
|
|
|
|
/**
|
2024-10-09 12:44:17 +00:00
|
|
|
* Initialize the class, registering WordPress hooks.
|
2023-12-08 23:23:11 +00:00
|
|
|
*/
|
|
|
|
public static function init() {
|
2024-02-08 12:31:25 +00:00
|
|
|
\add_action(
|
|
|
|
'activitypub_inbox_create',
|
|
|
|
array( self::class, 'handle_create' ),
|
|
|
|
10,
|
|
|
|
3
|
|
|
|
);
|
2024-10-09 12:44:17 +00:00
|
|
|
|
|
|
|
\add_filter(
|
|
|
|
'activitypub_validate_object',
|
|
|
|
array( self::class, 'validate_object' ),
|
|
|
|
10,
|
|
|
|
3
|
|
|
|
);
|
2023-12-08 23:23:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-10-09 12:44:17 +00:00
|
|
|
* Handles "Create" requests.
|
2023-12-08 23:23:11 +00:00
|
|
|
*
|
2024-10-09 12:44:17 +00:00
|
|
|
* @param array $activity The activity-object.
|
|
|
|
* @param int $user_id The id of the local blog-user.
|
|
|
|
* @param \Activitypub\Activity\Activity $activity_object Optional. The activity object. Default null.
|
2023-12-08 23:23:11 +00:00
|
|
|
*/
|
2024-10-09 12:44:17 +00:00
|
|
|
public static function handle_create( $activity, $user_id, $activity_object = null ) {
|
|
|
|
// Check if Activity is public or not.
|
|
|
|
if ( ! is_activity_public( $activity ) ) {
|
|
|
|
// @todo maybe send email.
|
2023-12-08 23:23:11 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-10-09 12:44:17 +00:00
|
|
|
$check_dupe = object_id_to_comment( $activity['object']['id'] );
|
2023-12-08 23:23:11 +00:00
|
|
|
|
2024-10-09 12:44:17 +00:00
|
|
|
// If comment exists, call update action.
|
2023-12-08 23:23:11 +00:00
|
|
|
if ( $check_dupe ) {
|
2024-10-09 12:44:17 +00:00
|
|
|
/**
|
|
|
|
* Fires when a Create activity is received for an existing comment.
|
|
|
|
*
|
|
|
|
* @param array $activity The activity-object.
|
|
|
|
* @param int $user_id The id of the local blog-user.
|
|
|
|
* @param \WP_Comment|\WP_Error $check_dupe The comment object or WP_Error.
|
|
|
|
* @param \Activitypub\Activity\Activity $activity_object The activity object.
|
|
|
|
*/
|
|
|
|
\do_action( 'activitypub_inbox_update', $activity, $user_id, $activity_object );
|
2023-12-08 23:23:11 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-10-09 12:44:17 +00:00
|
|
|
$state = Interactions::add_comment( $activity );
|
2023-12-08 23:23:11 +00:00
|
|
|
$reaction = null;
|
|
|
|
|
2024-07-19 19:46:05 +00:00
|
|
|
if ( $state && ! \is_wp_error( $state ) ) {
|
2023-12-08 23:23:11 +00:00
|
|
|
$reaction = \get_comment( $state );
|
|
|
|
}
|
|
|
|
|
2024-10-09 12:44:17 +00:00
|
|
|
/**
|
|
|
|
* Fires after a Create activity has been handled.
|
|
|
|
*
|
|
|
|
* @param array $activity The activity-object.
|
|
|
|
* @param int $user_id The id of the local blog-user.
|
|
|
|
* @param \WP_Comment|\WP_Error $state The comment object or WP_Error.
|
|
|
|
* @param \WP_Comment|\WP_Error|null $reaction The reaction object or WP_Error.
|
|
|
|
*/
|
|
|
|
\do_action( 'activitypub_handled_create', $activity, $user_id, $state, $reaction );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validate the object.
|
|
|
|
*
|
|
|
|
* @param bool $valid The validation state.
|
|
|
|
* @param string $param The object parameter.
|
|
|
|
* @param \WP_REST_Request $request The request object.
|
|
|
|
*
|
|
|
|
* @return bool The validation state: true if valid, false if not.
|
|
|
|
*/
|
|
|
|
public static function validate_object( $valid, $param, $request ) {
|
|
|
|
$json_params = $request->get_json_params();
|
|
|
|
|
|
|
|
if (
|
|
|
|
'Create' !== $json_params['type'] ||
|
|
|
|
is_wp_error( $request )
|
|
|
|
) {
|
|
|
|
return $valid;
|
|
|
|
}
|
|
|
|
|
|
|
|
$object = $json_params['object'];
|
|
|
|
$required = array(
|
|
|
|
'id',
|
|
|
|
'inReplyTo',
|
|
|
|
'content',
|
|
|
|
);
|
|
|
|
|
|
|
|
if ( array_intersect( $required, array_keys( $object ) ) !== $required ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $valid;
|
2023-12-08 23:23:11 +00:00
|
|
|
}
|
|
|
|
}
|