| 1 |
<?php |
| 2 |
/** |
| 3 |
* Create handler file. |
| 4 |
* |
| 5 |
* @package Activitypub |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace Activitypub\Handler; |
| 9 |
|
| 10 |
use Activitypub\Collection\Interactions; |
| 11 |
|
| 12 |
use function Activitypub\is_self_ping; |
| 13 |
use function Activitypub\is_activity_reply; |
| 14 |
use function Activitypub\is_activity_public; |
| 15 |
use function Activitypub\object_id_to_comment; |
| 16 |
|
| 17 |
/** |
| 18 |
* Handle Create requests. |
| 19 |
*/ |
| 20 |
class Create { |
| 21 |
/** |
| 22 |
* Initialize the class, registering WordPress hooks. |
| 23 |
*/ |
| 24 |
public static function init() { |
| 25 |
\add_action( |
| 26 |
'activitypub_inbox_create', |
| 27 |
array( self::class, 'handle_create' ), |
| 28 |
10, |
| 29 |
3 |
| 30 |
); |
| 31 |
|
| 32 |
\add_filter( |
| 33 |
'activitypub_validate_object', |
| 34 |
array( self::class, 'validate_object' ), |
| 35 |
10, |
| 36 |
3 |
| 37 |
); |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Handles "Create" requests. |
| 42 |
* |
| 43 |
* @param array $activity The activity-object. |
| 44 |
* @param int $user_id The id of the local blog-user. |
| 45 |
* @param \Activitypub\Activity\Activity $activity_object Optional. The activity object. Default null. |
| 46 |
*/ |
| 47 |
public static function handle_create( $activity, $user_id, $activity_object = null ) { |
| 48 |
// Check if Activity is public or not. |
| 49 |
if ( |
| 50 |
! is_activity_public( $activity ) || |
| 51 |
! is_activity_reply( $activity ) |
| 52 |
) { |
| 53 |
return; |
| 54 |
} |
| 55 |
|
| 56 |
$check_dupe = object_id_to_comment( $activity['object']['id'] ); |
| 57 |
|
| 58 |
// If comment exists, call update action. |
| 59 |
if ( $check_dupe ) { |
| 60 |
/** |
| 61 |
* Fires when a Create activity is received for an existing comment. |
| 62 |
* |
| 63 |
* @param array $activity The activity-object. |
| 64 |
* @param int $user_id The id of the local blog-user. |
| 65 |
* @param \Activitypub\Activity\Activity $activity_object The activity object. |
| 66 |
*/ |
| 67 |
\do_action( 'activitypub_inbox_update', $activity, $user_id, $activity_object ); |
| 68 |
return; |
| 69 |
} |
| 70 |
|
| 71 |
if ( is_self_ping( $activity['object']['id'] ) ) { |
| 72 |
return; |
| 73 |
} |
| 74 |
|
| 75 |
$state = Interactions::add_comment( $activity ); |
| 76 |
$reaction = null; |
| 77 |
|
| 78 |
if ( $state && ! \is_wp_error( $state ) ) { |
| 79 |
$reaction = \get_comment( $state ); |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Fires after a Create activity has been handled. |
| 84 |
* |
| 85 |
* @param array $activity The activity-object. |
| 86 |
* @param int $user_id The id of the local blog-user. |
| 87 |
* @param \WP_Comment|\WP_Error $state The comment object or WP_Error. |
| 88 |
* @param \WP_Comment|\WP_Error|null $reaction The reaction object or WP_Error. |
| 89 |
*/ |
| 90 |
\do_action( 'activitypub_handled_create', $activity, $user_id, $state, $reaction ); |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Validate the object. |
| 95 |
* |
| 96 |
* @param bool $valid The validation state. |
| 97 |
* @param string $param The object parameter. |
| 98 |
* @param \WP_REST_Request $request The request object. |
| 99 |
* |
| 100 |
* @return bool The validation state: true if valid, false if not. |
| 101 |
*/ |
| 102 |
public static function validate_object( $valid, $param, $request ) { |
| 103 |
$json_params = $request->get_json_params(); |
| 104 |
|
| 105 |
if ( empty( $json_params['type'] ) ) { |
| 106 |
return false; |
| 107 |
} |
| 108 |
|
| 109 |
if ( |
| 110 |
'Create' !== $json_params['type'] || |
| 111 |
is_wp_error( $request ) |
| 112 |
) { |
| 113 |
return $valid; |
| 114 |
} |
| 115 |
|
| 116 |
$object = $json_params['object']; |
| 117 |
|
| 118 |
if ( ! is_array( $object ) ) { |
| 119 |
return false; |
| 120 |
} |
| 121 |
|
| 122 |
$required = array( |
| 123 |
'id', |
| 124 |
'content', |
| 125 |
); |
| 126 |
|
| 127 |
if ( array_intersect( $required, array_keys( $object ) ) !== $required ) { |
| 128 |
return false; |
| 129 |
} |
| 130 |
|
| 131 |
return $valid; |
| 132 |
} |
| 133 |
} |
| 134 |
|