diff --git a/includes/activitypub/model/class-event-source.php b/includes/activitypub/model/class-event-source.php index 7344be9..0838980 100644 --- a/includes/activitypub/model/class-event-source.php +++ b/includes/activitypub/model/class-event-source.php @@ -15,6 +15,7 @@ namespace Event_Bridge_For_ActivityPub\ActivityPub\Model; use Activitypub\Activity\Actor; use Event_Bridge_For_ActivityPub\ActivityPub\Collection\Event_Sources; use WP_Error; +use WP_Post; use function Activitypub\sanitize_url; @@ -61,11 +62,24 @@ class Event_Source extends Actor { return array(); } + /** + * Getter for URL attribute. + * + * @return string The URL. + */ + public function get_url() { + if ( $this->url ) { + return $this->url; + } + + return $this->id; + } + /** * Get the WordPress post which stores the Event Source by the ActivityPub actor id of the event source. * * @param string $actor_id The ActivityPub actor ID. - * @return ?int The WordPress post ID if the actor is found, null if not. + * @return ?WP_Post The WordPress post if the actor is found, null if not. */ private static function get_wp_post_by_activitypub_actor_id( $actor_id ) { global $wpdb; @@ -110,8 +124,8 @@ class Event_Source extends Actor { $object->set_id( $post->guid ); $object->set_name( $post->post_title ); $object->set_summary( $post->post_excerpt ); - $object->set_published( gmdate( 'Y-m-d H:i:s', strtotime( $post->post_date ) ) ); - $object->set_updated( gmdate( 'Y-m-d H:i:s', strtotime( $post->post_modified ) ) ); + $object->set_published( gmdate( 'Y-m-d H:i:s', strtotime( $post->post_date_gmt ) ) ); + $object->set_updated( gmdate( 'Y-m-d H:i:s', strtotime( $post->post_modified_gmt ) ) ); $thumbnail_id = get_post_thumbnail_id( $post ); if ( $thumbnail_id ) { $object->set_icon( diff --git a/includes/activitypub/transmogrifier/class-the-events-calendar.php b/includes/activitypub/transmogrifier/class-the-events-calendar.php index 556b727..6205a18 100644 --- a/includes/activitypub/transmogrifier/class-the-events-calendar.php +++ b/includes/activitypub/transmogrifier/class-the-events-calendar.php @@ -11,9 +11,11 @@ namespace Event_Bridge_For_ActivityPub\ActivityPub\Transmogrifier; +use Event_Bridge_For_ActivityPub\ActivityPub\Model\Event_Source; use Tribe__Date_Utils; use function Activitypub\sanitize_url; +use function Activitypub\object_to_uri; // Exit if accessed directly. defined( 'ABSPATH' ) || exit; // @codeCoverageIgnore @@ -103,6 +105,59 @@ class The_Events_Calendar extends Base { return $post_id; } + /** + * Add organizer. + * + * @return int|bool $post_id The organizers post ID. + */ + private function add_organizer() { + // This might likely change, because of FEP-8a8e. + $actor = $this->activitypub_event->get_attributed_to(); + if ( is_null( $actor ) ) { + return false; + } + $actor_id = object_to_uri( $actor ); + + $event_source = Event_Source::get_by_id( $actor_id ); + + // As long as we do not support announces, we expect the attributedTo to be an existing event source. + if ( ! $event_source ) { + return false; + } + + $tribe_organizer = tribe_organizers() + ->set_args( + array( + 'organizer' => $event_source->get_name(), + 'description' => $event_source->get_summary(), + 'post_date_gmt' => $event_source->get_published(), + 'website' => $event_source->get_id(), + 'excerpt' => $event_source->get_summary(), + ), + 'publish', + true // This enables avoid_duplicates which includes exact matches of title, content, excerpt, and website. + )->create(); + + if ( ! $tribe_organizer ) { + return; + } + + // Make a relationship between the event source WP_Post and the organizer WP_Post. + wp_update_post( + array( + 'ID' => $tribe_organizer->ID, + 'post_parent' => $event_source->get__id(), + ) + ); + + // Add the thumbnail of the event source to the organizer. + if ( get_post_thumbnail_id( $event_source ) ) { + set_post_thumbnail( $tribe_organizer, get_post_thumbnail_id( $event_source ) ); + } + + return $tribe_organizer->ID; + } + /** * Save the ActivityPub event object as GatherPress Event. * @@ -118,6 +173,8 @@ class The_Events_Calendar extends Base { $venue_id = $this->add_venue(); + $organizer_id = $this->add_organizer(); + $args = array( 'title' => sanitize_text_field( $this->activitypub_event->get_name() ), 'content' => wp_kses_post( $this->activitypub_event->get_content() ?? '' ), @@ -132,6 +189,11 @@ class The_Events_Calendar extends Base { $args['VenueID'] = $venue_id; } + if ( $organizer_id ) { + $args['organizer'] = $organizer_id; + $args['OrganizerID'] = $organizer_id; + } + $tribe_event = new The_Events_Calendar_Event_Repository(); if ( $post_id ) {