WIP: Add Event Sources Logic (ActivityPub follows) #86

Draft
linos wants to merge 36 commits from event_sources into main
5 changed files with 62 additions and 7 deletions
Showing only changes of commit 16762b2b31 - Show all commits

View file

@ -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;
}
}

View file

@ -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(

View file

@ -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 ) ) {

View file

@ -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();
}
}
}
}

View file

@ -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 );