wordpress-event-bridge-for-.../includes/activitypub/handler/class-create.php
André Menrath 076e2619f0
Some checks failed
PHP Code Checker / PHP Code Checker (pull_request) Failing after 50s
PHPUnit / PHPUnit – PHP 7.4 (pull_request) Failing after 53s
PHPUnit / PHPUnit – PHP 8.0 (pull_request) Successful in 1m4s
PHPUnit / PHPUnit – PHP 8.1 (pull_request) Successful in 1m2s
PHPUnit / PHPUnit – PHP 8.2 (pull_request) Successful in 1m1s
PHPUnit / PHPUnit – PHP 8.3 (pull_request) Successful in 1m6s
PHPUnit / PHPUnit – PHP 8.4 (pull_request) Successful in 1m5s
wip
2024-12-14 14:46:00 +01:00

138 lines
3 KiB
PHP

<?php
/**
* Create handler file.
*
* @package Event_Bridge_For_ActivityPub
*/
namespace Event_Bridge_For_ActivityPub\ActivityPub\Handler;
use Activitypub\Collection\Actors;
use Event_Bridge_For_ActivityPub\ActivityPub\Collection\Event_Sources;
use Event_Bridge_For_ActivityPub\Setup;
use function Activitypub\is_activity_public;
/**
* Handle Create requests.
*/
class Create {
/**
* Initialize the class, registering WordPress hooks.
*/
public static function init() {
\add_action(
'activitypub_inbox_create',
array( self::class, 'handle_create' ),
15,
2
);
\add_filter(
'activitypub_validate_object',
array( self::class, 'validate_object' ),
12,
3
);
}
/**
* Handle "Create" requests.
*
* @param array $activity The activity-object.
* @param int $user_id The id of the local blog-user.
*/
public static function handle_create( $activity, $user_id ) {
// We only process activities that are target to the application user.
if ( Actors::APPLICATION_USER_ID !== $user_id ) {
return;
}
if ( ! self::actor_is_event_source( $activity['actor'] ) ) {
return;
}
// Check if Activity is public or not.
if ( ! is_activity_public( $activity ) ) {
return;
}
// Check if an object is set.
if ( ! isset( $activity['object']['type'] ) || 'Event' !== $activity['object']['type'] ) {
return;
}
$transmogrifier_class = Setup::get_transmogrifier();
if ( ! $transmogrifier_class ) {
return;
}
$transmogrifier = new $transmogrifier_class( $activity['object'] );
$transmogrifier->create();
}
/**
* 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;
} else {
return $valid;
}
if ( empty( $json_params['type'] ) ) {
return false;
}
if ( empty( $json_params['actor'] ) ) {
return false;
}
if ( ! in_array( $json_params['type'], array( 'Create', 'Update', 'Delete', 'Announce' ), true ) || is_wp_error( $request ) ) {
return $valid;
}
$object = $json_params['object'];
if ( ! is_array( $object ) ) {
return false;
}
$required = array(
'id',
'startTime',
'name',
);
if ( array_intersect( $required, array_keys( $object ) ) !== $required ) {
return false;
}
return $valid;
}
/**
* Check if an ActivityPub actor is an event source.
*
* @param string $actor_id The actor ID.
* @return bool
*/
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;
}
}