2024-12-08 21:57:53 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Create handler file.
|
|
|
|
*
|
|
|
|
* @package Event_Bridge_For_ActivityPub
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Event_Bridge_For_ActivityPub\ActivityPub\Handler;
|
|
|
|
|
|
|
|
use Activitypub\Collection\Actors;
|
2024-12-15 09:52:20 +01:00
|
|
|
use DateTime;
|
|
|
|
use DateTimeZone;
|
2024-12-14 14:46:00 +01:00
|
|
|
use Event_Bridge_For_ActivityPub\ActivityPub\Collection\Event_Sources;
|
2024-12-10 19:34:15 +01:00
|
|
|
use Event_Bridge_For_ActivityPub\Setup;
|
|
|
|
|
|
|
|
use function Activitypub\is_activity_public;
|
2024-12-08 21:57:53 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle Create requests.
|
|
|
|
*/
|
|
|
|
class Create {
|
|
|
|
/**
|
|
|
|
* Initialize the class, registering WordPress hooks.
|
|
|
|
*/
|
|
|
|
public static function init() {
|
|
|
|
\add_action(
|
|
|
|
'activitypub_inbox_create',
|
2024-12-10 22:43:13 +01:00
|
|
|
array( self::class, 'handle_create' ),
|
|
|
|
15,
|
|
|
|
2
|
|
|
|
);
|
|
|
|
\add_filter(
|
|
|
|
'activitypub_validate_object',
|
|
|
|
array( self::class, 'validate_object' ),
|
|
|
|
12,
|
|
|
|
3
|
2024-12-08 21:57:53 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle "Create" requests.
|
|
|
|
*
|
2024-12-10 19:34:15 +01:00
|
|
|
* @param array $activity The activity-object.
|
|
|
|
* @param int $user_id The id of the local blog-user.
|
2024-12-08 21:57:53 +01:00
|
|
|
*/
|
2024-12-10 19:34:15 +01:00
|
|
|
public static function handle_create( $activity, $user_id ) {
|
2024-12-11 23:09:12 +01:00
|
|
|
// We only process activities that are target to the application user.
|
2024-12-10 19:34:15 +01:00
|
|
|
if ( Actors::APPLICATION_USER_ID !== $user_id ) {
|
2024-12-08 21:57:53 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-12-14 14:46:00 +01:00
|
|
|
if ( ! self::actor_is_event_source( $activity['actor'] ) ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-12-10 19:34:15 +01:00
|
|
|
// Check if Activity is public or not.
|
|
|
|
if ( ! is_activity_public( $activity ) ) {
|
|
|
|
return;
|
|
|
|
}
|
2024-12-08 21:57:53 +01:00
|
|
|
|
2024-12-10 19:34:15 +01:00
|
|
|
// Check if an object is set.
|
2024-12-10 22:43:13 +01:00
|
|
|
if ( ! isset( $activity['object']['type'] ) || 'Event' !== $activity['object']['type'] ) {
|
2024-12-08 21:57:53 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-12-15 09:52:20 +01:00
|
|
|
if ( self::is_time_passed( $activity['object']['startTime'] ) ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-12-10 19:34:15 +01:00
|
|
|
$transmogrifier_class = Setup::get_transmogrifier();
|
|
|
|
|
|
|
|
if ( ! $transmogrifier_class ) {
|
2024-12-08 21:57:53 +01:00
|
|
|
return;
|
|
|
|
}
|
2024-12-10 19:34:15 +01:00
|
|
|
|
|
|
|
$transmogrifier = new $transmogrifier_class( $activity['object'] );
|
|
|
|
$transmogrifier->create();
|
2024-12-08 21:57:53 +01:00
|
|
|
}
|
2024-12-10 22:43:13 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 ( isset( $json_params['object']['type'] ) && 'Event' === $json_params['object']['type'] ) {
|
|
|
|
$valid = true;
|
2024-12-11 23:09:12 +01:00
|
|
|
} else {
|
|
|
|
return $valid;
|
2024-12-10 22:43:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( empty( $json_params['type'] ) ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-12-14 14:46:00 +01:00
|
|
|
if ( empty( $json_params['actor'] ) ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! in_array( $json_params['type'], array( 'Create', 'Update', 'Delete', 'Announce' ), true ) || is_wp_error( $request ) ) {
|
2024-12-10 22:43:13 +01:00
|
|
|
return $valid;
|
|
|
|
}
|
|
|
|
|
|
|
|
$object = $json_params['object'];
|
|
|
|
|
|
|
|
if ( ! is_array( $object ) ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$required = array(
|
|
|
|
'id',
|
2024-12-14 14:46:00 +01:00
|
|
|
'startTime',
|
|
|
|
'name',
|
2024-12-10 22:43:13 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
if ( array_intersect( $required, array_keys( $object ) ) !== $required ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $valid;
|
|
|
|
}
|
2024-12-11 23:09:12 +01:00
|
|
|
|
2024-12-15 09:52:20 +01:00
|
|
|
/**
|
|
|
|
* Check if a given DateTime is already passed.
|
|
|
|
*
|
|
|
|
* @param string $time_string The ActivityPub like time string.
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
private static function is_time_passed( $time_string ) {
|
|
|
|
// Create a DateTime object from the ActivityPub time string.
|
|
|
|
$time = new DateTime( $time_string, new DateTimeZone( 'UTC' ) );
|
|
|
|
|
|
|
|
// Get the current time in UTC.
|
|
|
|
$current_time = new DateTime( 'now', new DateTimeZone( 'UTC' ) );
|
|
|
|
|
|
|
|
// Compare the event time with the current time.
|
|
|
|
return $time < $current_time;
|
|
|
|
}
|
|
|
|
|
2024-12-11 23:09:12 +01:00
|
|
|
/**
|
2024-12-14 14:46:00 +01:00
|
|
|
* Check if an ActivityPub actor is an event source.
|
2024-12-11 23:09:12 +01:00
|
|
|
*
|
2024-12-14 14:46:00 +01:00
|
|
|
* @param string $actor_id The actor ID.
|
|
|
|
* @return bool
|
2024-12-11 23:09:12 +01:00
|
|
|
*/
|
2024-12-14 14:46:00 +01:00
|
|
|
public static function actor_is_event_source( $actor_id ) {
|
|
|
|
$event_sources = Event_Sources::get_event_sources();
|
|
|
|
foreach ( $event_sources as $event_source ) {
|
|
|
|
if ( $actor_id === $event_source->get_id() ) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
2024-12-11 23:09:12 +01:00
|
|
|
}
|
2024-12-08 21:57:53 +01:00
|
|
|
}
|