From 4b9dbbef06e6a78cee324f1e1841809c3fe1e5c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Menrath?= Date: Fri, 5 Jul 2024 11:31:46 +0200 Subject: [PATCH] WIP on transformers --- .../activitypub/transformer/class-event.php | 50 ++++++++ .../transformer/class-gatherpress.php | 2 +- .../activitypub/transformer/class-tribe.php | 113 +++++------------- includes/class-admin-notices.php | 2 +- includes/class-setup.php | 2 +- 5 files changed, 83 insertions(+), 86 deletions(-) diff --git a/includes/activitypub/transformer/class-event.php b/includes/activitypub/transformer/class-event.php index 7a3aca6..e1ff34d 100644 --- a/includes/activitypub/transformer/class-event.php +++ b/includes/activitypub/transformer/class-event.php @@ -8,9 +8,12 @@ namespace Activitypub_Event_Extensions\Activitypub\Transformer; +use Activitypub\Activity\Extended_Object\Event as Event_Object; use Activitypub\Model\Blog; use Activitypub\Transformer\Post; +use function Activitypub\get_rest_url_by_path; + /** * Base transformer for WordPress event post types to ActivityPub events. */ @@ -26,4 +29,51 @@ class Event extends Post { $blog = new Blog(); return $blog->get_id(); } + + /** + * Returns the ActivityStreams 2.0 Object-Type for an Event. + * + * @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-event + * + * @return string The Event Object-Type. + */ + protected function get_object_type() { + return 'Event'; + } + + /** + * Generic function that converts an WP-Event object to an ActivityPub-Event object. + * + * @return Event_Object + */ + public function to_object() { + $activitypub_object = new Event_Object(); + $activitypub_object = $this->transform_object_properties( $activitypub_object ); + + $published = \strtotime( $this->wp_object->post_date_gmt ); + + $activitypub_object->set_published( \gmdate( 'Y-m-d\TH:i:s\Z', $published ) ); + + $updated = \strtotime( $this->wp_object->post_modified_gmt ); + + if ( $updated > $published ) { + $activitypub_object->set_updated( \gmdate( 'Y-m-d\TH:i:s\Z', $updated ) ); + } + + $activitypub_object->set_content_map( + array( + $this->get_locale() => $this->get_content(), + ) + ); + $path = sprintf( 'actors/%d/followers', intval( $this->wp_object->post_author ) ); + + $activitypub_object->set_to( + array( + 'https://www.w3.org/ns/activitystreams#Public', + get_rest_url_by_path( $path ), + ) + ); + + return $activitypub_object; + } } diff --git a/includes/activitypub/transformer/class-gatherpress.php b/includes/activitypub/transformer/class-gatherpress.php index a86dde1..e0c0b80 100644 --- a/includes/activitypub/transformer/class-gatherpress.php +++ b/includes/activitypub/transformer/class-gatherpress.php @@ -8,7 +8,7 @@ use Activitypub\Transformer\Post; use Activitypub\Model\Blog_user; -use Activitypub\Activity\Extended_Object\Event; +use Activitypub\Activity\Extended_Object\Event as Event_Object; use Activitypub\Activity\Extended_Object\Place; use GatherPress\Core\Event as GatherPress_Event; diff --git a/includes/activitypub/transformer/class-tribe.php b/includes/activitypub/transformer/class-tribe.php index 3727e99..12b2657 100644 --- a/includes/activitypub/transformer/class-tribe.php +++ b/includes/activitypub/transformer/class-tribe.php @@ -1,5 +1,4 @@ tribe_event = tribe_get_event( $wp_post->ID ); // } - /** - * Get widget name. - * - * Retrieve oEmbed widget name. - * - * @since 1.0.0 - * @access public - * @return string Widget name. - */ - public function get_name() { - - return 'activitypub-event-transformers/tribe'; - } - - /** - * Get widget title. - * - * Retrieve Transformer title. - * - * @since 1.0.0 - * @access public - * @return string Widget title. - */ - public function get_label() { - - return 'The Events Calendar'; - } - - /** - * Returns the ActivityStreams 2.0 Object-Type for an Event. - * - * @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-event - * - * @return string The Event Object-Type. - */ - protected function get_object_type() { - - return 'Event'; - } - - /** - * Get supported post types. - * - * Retrieve the list of supported WordPress post types this transformer widget can handle. - * - * @since 1.0.0 - * @access public - * @return array Widget categories. - */ - public static function get_supported_post_types() { - - return array( 'tribe_events' ); - } - /** * Get tribe category of wp_post * * @return string|null tribe category if it exists */ public function get_tribe_category() { - // todo make it possible that one event can have multiple categories? - - // using cat_slugs isn't 100% nice way to do this, don't know if it's a good idea + // TODO: make it possible that one event can have multiple categories? + // Using cat_slugs isn't the best way to do this, don't know if it's a good idea. $categories = tribe_get_event_cat_slugs( $this->wp_object->ID ); if ( count( $categories ) === 0 ) { @@ -123,7 +69,7 @@ class Tribe extends Event_Transformer { return 'CANCELLED'; } if ( 'postponed' === $this->tribe_event->event_status ) { - return 'CANCELLED'; // this will be reflected in the cancelled reason + return 'CANCELLED'; // This will be reflected in the cancelled reason. } if ( '' === $this->tribe_event->event_status ) { return 'CONFIRMED'; @@ -142,12 +88,12 @@ class Tribe extends Event_Transformer { protected function get_content() { $content = parent::get_content(); - // todo remove link at the end of the content + // TODO: remove link at the end of the content. - // todo add organizer - // $this->tribe_event->organizers[0] + // TODO: add organizer + // $this->tribe_event->organizers[0]. - // todo add Canclled reason in the content (maybe at the end) + // TODO: do add Cancelled reason in the content (maybe at the end). return $content; } @@ -155,26 +101,27 @@ class Tribe extends Event_Transformer { /** * Get the event location. * - * @param int $post_id The WordPress post ID. * @returns array The Place. */ public function get_event_location() { /* - 'post_title' => 'testvenue', - 'post_name' => 'testvenue', - 'guid' => 'http://localhost/venue/testvenue/', - 'post_type' => 'tribe_venue', - 'address' => 'testaddr', - 'country' => 'Austria', - 'city' => 'testcity', - 'state_province' => 'testprovince', - 'state' => '', - 'province' => 'testprovince', - 'zip' => '8000', - 'phone' => '+4312343', - 'permalink' => 'http://localhost/venue/testvenue/', - 'directions_link' => 'https://maps.google.com/maps?f=q&source=s_q&hl=en&geocode=&q=testaddr+testcity+testprovince+8000+Austria', - 'website' => 'https://test.at', + This is how the Tribe event looks like: + TODO: Remove this comment. + 'post_title' => 'testvenue', + 'post_name' => 'testvenue', + 'guid' => 'http://localhost/venue/testvenue/', + 'post_type' => 'tribe_venue', + 'address' => 'testaddr', + 'country' => 'Austria', + 'city' => 'testcity', + 'state_province' => 'testprovince', + 'state' => '', + 'province' => 'testprovince', + 'zip' => '8000', + 'phone' => '+4312343', + 'permalink' => 'http://localhost/venue/testvenue/', + 'directions_link' => 'https://maps.google.com/maps?f=q&source=s_q&hl=en&geocode=&q=testaddr+testcity+testprovince+8000+Austria', + 'website' => 'https://test.at', */ $venue = $this->tribe_event->venues[0]; return ( new Place() ) @@ -184,6 +131,6 @@ class Tribe extends Event_Transformer { $venue->address . "\n" . $venue->zip . ', ' . $venue->city . "\n" . $venue->country - ); // todo add checks that everything exists here (lol) + ); // TODO: add checks that everything exists here. } } diff --git a/includes/class-admin-notices.php b/includes/class-admin-notices.php index 6749fa8..5867458 100644 --- a/includes/class-admin-notices.php +++ b/includes/class-admin-notices.php @@ -53,7 +53,7 @@ class Admin_Notices { // Get the current page. $screen = get_current_screen(); // Check if we are on a edit page for the event, or on the settings page of the event plugin. - $is_event_plugins_edit_page = 'edit' === $screen->base && $this->event_plugin['post_type'] === $screen->event_post_type; + $is_event_plugins_edit_page = 'edit' === $screen->base && $this->event_plugin['post_type'] === $screen->post_type; $is_event_plugins_settings_page = $this->event_plugin['settings_page_id'] === $screen->id; if ( $is_event_plugins_edit_page || $is_event_plugins_settings_page ) { diff --git a/includes/class-setup.php b/includes/class-setup.php index 4f4303c..56e6a27 100644 --- a/includes/class-setup.php +++ b/includes/class-setup.php @@ -40,7 +40,7 @@ class Setup { 'the_events_calendar' => array( 'plugin_file' => 'the-events-calendar/the-events-calendar.php', 'post_type' => 'tribe_events', - 'transformer_class' => 'Tribe_Events', + 'transformer_class' => 'Tribe', 'settings_page_id' => 'tribe_general', ), 'vsel' => array(