diff --git a/includes/activitypub/handler/class-create.php b/includes/activitypub/handler/class-create.php index 411bbb3..d980551 100644 --- a/includes/activitypub/handler/class-create.php +++ b/includes/activitypub/handler/class-create.php @@ -22,7 +22,15 @@ class Create { public static function init() { \add_action( 'activitypub_inbox_create', - array( self::class, 'handle_create' ) + array( self::class, 'handle_create' ), + 15, + 2 + ); + \add_filter( + 'activitypub_validate_object', + array( self::class, 'validate_object' ), + 12, + 3 ); } @@ -44,7 +52,7 @@ class Create { } // Check if an object is set. - if ( ! isset( $activity['object']['type'] ) || 'Event' !== isset( $activity['object']['type'] ) ) { + if ( ! isset( $activity['object']['type'] ) || 'Event' !== $activity['object']['type'] ) { return; } @@ -57,4 +65,48 @@ class Create { $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; + } + + if ( empty( $json_params['type'] ) ) { + return false; + } + + if ( + 'Create' !== $json_params['type'] || + is_wp_error( $request ) + ) { + return $valid; + } + + $object = $json_params['object']; + + if ( ! is_array( $object ) ) { + return false; + } + + $required = array( + 'id', + ); + + if ( array_intersect( $required, array_keys( $object ) ) !== $required ) { + return false; + } + + return $valid; + } } diff --git a/includes/activitypub/transmogrifier/class-gatherpress.php b/includes/activitypub/transmogrifier/class-gatherpress.php index 7db80d8..06c196d 100644 --- a/includes/activitypub/transmogrifier/class-gatherpress.php +++ b/includes/activitypub/transmogrifier/class-gatherpress.php @@ -55,7 +55,7 @@ class GatherPress { /** * Save the ActivityPub event object as GatherPress Event. */ - public function save() { + public function create() { // Insert new GatherPress Event post. $post_id = wp_insert_post( array( diff --git a/includes/class-settings.php b/includes/class-settings.php index 02769ae..ab88721 100644 --- a/includes/class-settings.php +++ b/includes/class-settings.php @@ -144,7 +144,8 @@ class Settings { $valid_options = array(); foreach ( $active_event_plugins as $active_event_plugin ) { if ( $active_event_plugin->supports_event_sources() ) { - $valid_options[] = $active_event_plugin::class; + $full_class = $active_event_plugin::class; + $valid_options[] = substr( $full_class, strrpos( $full_class, '\\' ) + 1 ); } } if ( in_array( $plugin, $valid_options, true ) ) { diff --git a/includes/class-setup.php b/includes/class-setup.php index 87bdb59..b0fd90c 100644 --- a/includes/class-setup.php +++ b/includes/class-setup.php @@ -366,8 +366,10 @@ class Setup { return; } $active_event_plugins = $setup->get_active_event_plugins(); - if ( array_key_exists( $event_plugin, $active_event_plugins ) ) { - return $active_event_plugins[ $event_plugin ]->get_transmogrifier_class(); + foreach ( $active_event_plugins as $active_event_plugin ) { + if ( strrpos( $active_event_plugin::class, $event_plugin ) ) { + return $active_event_plugin::get_transmogrifier_class(); + } } } } diff --git a/includes/integrations/class-event-plugin.php b/includes/integrations/class-event-plugin.php index 6c25b1a..a76e485 100644 --- a/includes/integrations/class-event-plugin.php +++ b/includes/integrations/class-event-plugin.php @@ -100,7 +100,7 @@ abstract class Event_Plugin { * Returns the class used for transmogrifying an Event (ActivityStreams to Event plugin transformation). */ public static function get_transmogrifier_class(): ?string { - if ( ! self::supports_event_sources() ) { + if ( ! static::supports_event_sources() ) { return null; } return str_replace( 'Integrations', 'Activitypub\Transmogrifier', static::class );