From 4fef29a6c2c9b8f701e2b3ab41e3f7afd7eef289 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Menrath?= Date: Tue, 9 Apr 2024 19:20:53 +0200 Subject: [PATCH] stash-2024-04-09 --- activitypub-event-extensions.php | 9 + includes/activitypub/handler/class-join.php | 6 +- .../transformer/class-events-manager.php | 66 +++-- .../transformer/class-gatherpress.php | 251 ++++++++++++++++++ .../activitypub/transformer/class-tribe.php | 22 +- .../transformer/class-vs-event.php | 55 ++-- includes/category-mapper.php | 15 +- 7 files changed, 367 insertions(+), 57 deletions(-) create mode 100644 includes/activitypub/transformer/class-gatherpress.php diff --git a/activitypub-event-extensions.php b/activitypub-event-extensions.php index 4ecbfd8..68bea23 100644 --- a/activitypub-event-extensions.php +++ b/activitypub-event-extensions.php @@ -47,6 +47,15 @@ add_filter( return new \Events_Manager( $wp_object ); } + /** + * Events manager + * @see https://wordpress.org/plugins/events-manager/ + */ + if ( class_exists( 'GatherPress\Core\Event' ) && $wp_object->post_type === 'gp_event' ) { + require_once __DIR__ . '/includes/activitypub/transformer/class-gatherpress.php'; + return new \GatherPress( $wp_object ); + } + // Return the default transformer. return $transformer; diff --git a/includes/activitypub/handler/class-join.php b/includes/activitypub/handler/class-join.php index 8c891f8..20e4d45 100644 --- a/includes/activitypub/handler/class-join.php +++ b/includes/activitypub/handler/class-join.php @@ -1,2 +1,6 @@ em_event->event_location_type; } @@ -94,21 +100,22 @@ class Events_Manager extends Post { * @return array The Place. */ public function get_location() { + if ( 'url' === $this->em_event->event_location_type ) { return null; } - $location = new Place(); + $location = new Place(); $em_location = $this->em_event->get_location(); $location->set_name( $em_location->location_name ); $address = array( - 'type' => 'PostalAddress', - 'addressCountry' => $em_location->location_country, + 'type' => 'PostalAddress', + 'addressCountry' => $em_location->location_country, 'addressLocality' => $em_location->location_town, - 'streetAddress' => $em_location->location_address, - 'name' => $em_location->location_name, + 'streetAddress' => $em_location->location_address, + 'name' => $em_location->location_name, ); if ( $em_location->location_state ) { $address['addressRegion'] = $em_location->location_state; @@ -121,11 +128,11 @@ class Events_Manager extends Post { return $location; } - /** * Get the end time from the events metadata. */ protected function get_end_time() { + return null; } @@ -133,8 +140,9 @@ class Events_Manager extends Post { * Get the end time from the events metadata. */ protected function get_start_time() { - $date_string = $this->em_event->event_start_date; - $time_string = $this->em_event->event_start_time; + + $date_string = $this->em_event->event_start_date; + $time_string = $this->em_event->event_start_time; $timezone_string = $this->em_event->event_timezone; // Create a DateTime object with the given date, time, and timezone @@ -149,6 +157,7 @@ class Events_Manager extends Post { } protected function get_maximum_attendee_capacity() { + return $this->em_event->event_spaces; } @@ -156,46 +165,51 @@ class Events_Manager extends Post { * @todo decide whether to include pending bookings or not! */ protected function get_remaining_attendee_capacity() { - $em_bookings = $this->em_event->get_bookings()->get_bookings(); + + $em_bookings = $this->em_event->get_bookings()->get_bookings(); $remaining_attendee_capacity = $this->em_event->event_spaces - count( $em_bookings->bookings ); return $remaining_attendee_capacity; } protected function get_participant_count() { + $em_bookings = $this->em_event->get_bookings()->get_bookings(); return count( $em_bookings->bookings ); } protected function get_content() { + return $this->wp_object->post_content; } protected function get_summary() { + if ( $this->em_event->post_excerpt ) { $excerpt = $this->em_event->post_excerpt; } else { $excerpt = $this->get_content(); } - $address = $this->em_event->get_location()->location_name; - $start_time = strtotime( $this->get_start_time() ); - $datetime_format = get_option( 'date_format' ) . ' ' . get_option( 'time_format' ); + $address = $this->em_event->get_location()->location_name; + $start_time = strtotime( $this->get_start_time() ); + $datetime_format = get_option( 'date_format' ) . ' ' . get_option( 'time_format' ); $start_time_string = wp_date( $datetime_format, $start_time ); - $summary = "šŸ“ {$address}\nšŸ“… {$start_time_string}\n\n{$excerpt}"; + $summary = "šŸ“ {$address}\nšŸ“… {$start_time_string}\n\n{$excerpt}"; return $summary; } // protected function get_join_mode() { - // return 'free'; + // return 'free'; // } private function get_event_link_attachment() { - $event_link_url = $this->em_event->event_location->data['url']; + + $event_link_url = $this->em_event->event_location->data['url']; $event_link_text = $this->em_event->event_location->data['text']; return array( - 'type' => 'Link', - 'name' => 'Website', + 'type' => 'Link', + 'name' => 'Website', // 'name' => $event_link_text, - 'href' => \esc_url( $event_link_url ), + 'href' => \esc_url( $event_link_url ), 'mediaType' => 'text/html', ); } @@ -216,7 +230,8 @@ class Events_Manager extends Post { if ( 'url' === $this->em_event->event_location_type ) { $attachments[] = $this->get_event_link_attachment(); } - return $attachments; } + return $attachments; + } /** * This function tries to map VS-Event categories to Mobilizon event categories. @@ -224,6 +239,7 @@ class Events_Manager extends Post { * @return string $category */ protected function get_category() { + $categories = $this->em_event->get_categories()->terms; if ( empty( $categories ) ) { @@ -245,7 +261,7 @@ class Events_Manager extends Post { // Initialize variables to track the best match. $best_mobilizon_category_match = ''; - $best_match_length = 0; + $best_match_length = 0; // Check for the best match. foreach ( $mobilizon_categories as $mobilizon_category ) { @@ -256,7 +272,7 @@ class Events_Manager extends Post { $current_match_legnth = strlen( $mobilizon_category_slice ); if ( $current_match_legnth > $best_match_length ) { $best_mobilizon_category_match = $mobilizon_category; - $best_match_length = $current_match_legnth; + $best_match_length = $current_match_legnth; } } } @@ -274,7 +290,7 @@ class Events_Manager extends Post { if ( $post_tags ) { foreach ( $post_tags as $post_tag ) { - $tag = array( + $tag = array( 'type' => 'Hashtag', 'href' => \esc_url( \get_tag_link( $post_tag->term_id ) ), 'name' => esc_hashtag( $post_tag->name ), @@ -286,6 +302,7 @@ class Events_Manager extends Post { } protected function get_name() { + return $this->em_event->event_name; } @@ -295,7 +312,8 @@ class Events_Manager extends Post { * @return Activitypub\Activity\Event */ public function to_object() { - $this->em_event = new EM_Event( $this->wp_object->ID, 'post_id' ); + + $this->em_event = new EM_Event( $this->wp_object->ID, 'post_id' ); $activitypub_object = new Event(); $activitypub_object = $this->transform_object_properties( $activitypub_object ); diff --git a/includes/activitypub/transformer/class-gatherpress.php b/includes/activitypub/transformer/class-gatherpress.php new file mode 100644 index 0000000..c6fd70a --- /dev/null +++ b/includes/activitypub/transformer/class-gatherpress.php @@ -0,0 +1,251 @@ +gp_venue['full_address']; + $place = new Place(); + $place->set_type( 'Place' ); + $place->set_name( $address ); + $place->set_address( $address ); + return $place; + } + + /** + * Get the end time from the event object. + */ + protected function get_end_time() { + + return $this->gp_event->get_datetime_end( 'Y-m-d\TH:i:s\Z' ); + } + + /** + * Get the end time from the event object. + */ + protected function get_start_time() { + + return $this->gp_event->get_datetime_start( 'Y-m-d\TH:i:s\Z' ); + } + + /** + * Get the event link from the events metadata. + */ + private function get_event_link() { + + $event_link = get_post_meta( $this->wp_object->ID, 'event-link', true ); + if ( $event_link ) { + return array( + 'type' => 'Link', + 'name' => 'Website', + 'href' => \esc_url( $event_link ), + 'mediaType' => 'text/html', + ); + } + } + + /** + * Overrides/extends the get_attachments function to also add the event Link. + */ + protected function get_attachment() { + + $attachments = parent::get_attachment(); + if ( count( $attachments ) ) { + $attachments[0]['type'] = 'Document'; + $attachments[0]['name'] = 'Banner'; + } + $event_link = $this->get_event_link(); + if ( $event_link ) { + $attachments[] = $this->get_event_link(); + } + return $attachments; + } + + /** + * TODO: + * + * @return string $category + */ + protected function get_category() { + + return 'MEETING'; + } + + /** + * Returns the User-URL of the Author of the Post. + * + * If `single_user` mode is enabled, the URL of the Blog-User is returned. + * + * @return string The User-URL. + */ + protected function get_attributed_to() { + + $user = new Blog_User(); + return $user->get_url(); + } + + /** + * Create a custom summary. + * + * It contains also the most important meta-information. The summary is often used when the + * ActivityPub object type 'Event' is not supported, e.g. in Mastodon. + * + * @return string $summary The custom event summary. + */ + public function get_summary() { + + if ( $this->wp_object->excerpt ) { + $excerpt = $this->wp_object->post_excerpt; + } elseif ( get_post_meta( $this->wp_object->ID, 'event-summary', true ) ) { + $excerpt = get_post_meta( $this->wp_object->ID, 'event-summary', true ); + } else { + $excerpt = $this->get_content(); + } + + $address = get_post_meta( $this->wp_object->ID, 'event-location', true ); + $start_time = get_post_meta( $this->wp_object->ID, 'event-start-date', true ); + $datetime_format = get_option( 'date_format' ) . ' ' . get_option( 'time_format' ); + $start_time_string = wp_date( $datetime_format, $start_time ); + $summary = "šŸ“ {$address}\nšŸ“… {$start_time_string}\n\n{$excerpt}"; + return $summary; + } + + /** + * Transform the WordPress Object into an ActivityPub Object. + * + * @return Activitypub\Activity\Event + */ + public function to_object() { + + $this->ap_object = new Event(); + $this->gp_event = new GatherPress_Event( $this->wp_object->ID ); + $this->gp_venue = $this->gp_event->get_venue_information(); + + $this->ap_object = parent::to_object(); + + $this->ap_object->set_comments_enabled( 'open' === $this->wp_object->comment_status ? true : false ); + + $this->ap_object->set_external_participation_url( $this->get_url() ); + + $online_event_link = $this->gp_event->maybe_get_online_event_link(); + + if ( $online_event_link ) { + $this->ap_object->set_is_online( true ); + } else { + $this->ap_object->set_is_online( false ); + } + + $this->ap_object->set_status( 'CONFIRMED' ); + + $this->ap_object->set_name( get_the_title( $this->wp_object->ID ) ); + + $this->ap_object->set_actor( get_rest_url_by_path( 'application' ) ); + $this->ap_object->set_to( array( 'https://www.w3.org/ns/activitystreams#Public' ) ); + + $this->ap_object->set_location(); + return $this->ap_object; + } +} diff --git a/includes/activitypub/transformer/class-tribe.php b/includes/activitypub/transformer/class-tribe.php index cd664e2..b67d3ae 100644 --- a/includes/activitypub/transformer/class-tribe.php +++ b/includes/activitypub/transformer/class-tribe.php @@ -1,4 +1,5 @@ tribe_event = tribe_get_event( $wp_post->ID ); + // parent::set_wp_post( $wp_post ); + // $this->tribe_event = tribe_get_event( $wp_post->ID ); // } /** @@ -48,6 +50,7 @@ class Tribe extends Post { * @return string Widget name. */ public function get_name() { + return 'activitypub-event-transformers/tribe'; } @@ -61,6 +64,7 @@ class Tribe extends Post { * @return string Widget title. */ public function get_label() { + return 'The Events Calendar'; } @@ -72,6 +76,7 @@ class Tribe extends Post { * @return string The Event Object-Type. */ protected function get_object_type() { + return 'Event'; } @@ -85,6 +90,7 @@ class Tribe extends Post { * @return array Widget categories. */ public static function get_supported_post_types() { + return array( 'tribe_events' ); } @@ -112,6 +118,7 @@ class Tribe extends Post { * @return string status of the event */ public function get_tribe_status() { + if ( 'canceled' === $this->tribe_event->event_status ) { return 'CANCELLED'; } @@ -133,6 +140,7 @@ class Tribe extends Post { * @return string The content. */ protected function get_content() { + $content = parent::get_content(); // todo remove link at the end of the content diff --git a/includes/activitypub/transformer/class-vs-event.php b/includes/activitypub/transformer/class-vs-event.php index 711ca0f..4eaa23e 100644 --- a/includes/activitypub/transformer/class-vs-event.php +++ b/includes/activitypub/transformer/class-vs-event.php @@ -1,4 +1,5 @@ wp_object->ID, 'event-location', true ); - $place = new Place(); + $place = new Place(); $place->set_type( 'Place' ); $place->set_name( $address ); $place->set_address( $address ); @@ -98,6 +106,7 @@ class VS_Event extends Post { * Get the end time from the events metadata. */ protected function get_end_time() { + $end_time = get_post_meta( $this->wp_object->ID, 'event-date', true ); return \gmdate( 'Y-m-d\TH:i:s\Z', $end_time ); } @@ -106,6 +115,7 @@ class VS_Event extends Post { * Get the end time from the events metadata. */ protected function get_start_time() { + $start_time = get_post_meta( $this->wp_object->ID, 'event-start-date', true ); return \gmdate( 'Y-m-d\TH:i:s\Z', $start_time ); } @@ -114,12 +124,13 @@ class VS_Event extends Post { * Get the event link from the events metadata. */ private function get_event_link() { + $event_link = get_post_meta( $this->wp_object->ID, 'event-link', true ); if ( $event_link ) { return array( - 'type' => 'Link', - 'name' => 'Website', - 'href' => \esc_url( $event_link ), + 'type' => 'Link', + 'name' => 'Website', + 'href' => \esc_url( $event_link ), 'mediaType' => 'text/html', ); } @@ -129,6 +140,7 @@ class VS_Event extends Post { * Overrides/extends the get_attachments function to also add the event Link. */ protected function get_attachment() { + $attachments = parent::get_attachment(); if ( count( $attachments ) ) { $attachments[0]['type'] = 'Document'; @@ -147,6 +159,7 @@ class VS_Event extends Post { * @return string $category */ protected function get_category() { + $post_categories = wp_get_post_terms( $this->wp_object->ID, 'event_cat' ); if ( empty( $post_categories ) ) { @@ -168,7 +181,7 @@ class VS_Event extends Post { // Initialize variables to track the best match. $best_mobilizon_category_match = ''; - $best_match_length = 0; + $best_match_length = 0; // Check for the best match. foreach ( $mobilizon_categories as $mobilizon_category ) { @@ -179,7 +192,7 @@ class VS_Event extends Post { $current_match_legnth = strlen( $mobilizon_category_slice ); if ( $current_match_legnth > $best_match_length ) { $best_mobilizon_category_match = $mobilizon_category; - $best_match_length = $current_match_legnth; + $best_match_length = $current_match_legnth; } } } @@ -191,12 +204,13 @@ class VS_Event extends Post { /** * Returns the User-URL of the Author of the Post. - * - * If `single_user` mode is enabled, the URL of the Blog-User is returned. + * + * If `single_user` mode is enabled, the URL of the Blog-User is returned. * * @return string The User-URL. */ protected function get_attributed_to() { + $user = new Blog_User(); return $user->get_url(); } @@ -210,6 +224,7 @@ class VS_Event extends Post { * @return string $summary The custom event summary. */ public function get_summary() { + if ( $this->wp_object->excerpt ) { $excerpt = $this->wp_object->post_excerpt; } elseif ( get_post_meta( $this->wp_object->ID, 'event-summary', true ) ) { @@ -218,15 +233,14 @@ class VS_Event extends Post { $excerpt = $this->get_content(); } - $address = get_post_meta( $this->wp_object->ID, 'event-location', true ); - $start_time = get_post_meta( $this->wp_object->ID, 'event-start-date', true ); - $datetime_format = get_option( 'date_format' ) . ' ' . get_option( 'time_format' ); + $address = get_post_meta( $this->wp_object->ID, 'event-location', true ); + $start_time = get_post_meta( $this->wp_object->ID, 'event-start-date', true ); + $datetime_format = get_option( 'date_format' ) . ' ' . get_option( 'time_format' ); $start_time_string = wp_date( $datetime_format, $start_time ); - $summary = "šŸ“ {$address}\nšŸ“… {$start_time_string}\n\n{$excerpt}"; + $summary = "šŸ“ {$address}\nšŸ“… {$start_time_string}\n\n{$excerpt}"; return $summary; } - /** * Generic setter. * @@ -236,6 +250,7 @@ class VS_Event extends Post { * @return mixed The value. */ public function set( $key, $value ) { + if ( ! $this->ap_object->has( $key ) ) { return new WP_Error( 'invalid_key', __( 'Invalid key', 'activitypub' ), array( 'status' => 404 ) ); } @@ -243,7 +258,7 @@ class VS_Event extends Post { $setter_function = 'set_' . $key; $getter_function = 'get_' . $key; - if ( in_array( $getter_function, get_class_methods( $this ) ) ) { + if ( in_array( $getter_function, get_class_methods( $this ) ) ) { $this->ap_object->$setter_function( $this->$getter_function() ); } else { $this->ap_object->$setter_function( $value ); @@ -261,6 +276,7 @@ class VS_Event extends Post { * @return void */ public function __call( $method, $params ) { + $var = \strtolower( \substr( $method, 4 ) ); if ( \strncasecmp( $method, 'set', 3 ) === 0 ) { @@ -278,10 +294,11 @@ class VS_Event extends Post { * @return Activitypub\Activity\Event */ public function to_object() { + $this->ap_object = new Event(); - + $this - ->set_content() + ->set_content() ->set_content_map() ->set_attributed_to() ->set_published() @@ -289,10 +306,10 @@ class VS_Event extends Post { ->set_end_time() ->set_type() ->set_category() - ->set_attachment() - ->set_comments_enabled( true ) + ->set_attachment() + ->set_comments_enabled( true ) ->set_external_participation_url( $this->get_url() ) - ->set_status( 'CONFIRMED' ) + ->set_status( 'CONFIRMED' ) ->set_name( get_the_title( $this->wp_object->ID ) ) ->set_is_online( false ) ->set_in_language( $this->get_locale() ) diff --git a/includes/category-mapper.php b/includes/category-mapper.php index f4d9ae5..3ba9f75 100644 --- a/includes/category-mapper.php +++ b/includes/category-mapper.php @@ -1,4 +1,5 @@ $best_match_length ) { $best_mobilizon_category_match = $mobilizon_category; - $best_match_length = $current_match_legnth; + $best_match_length = $current_match_legnth; } } } @@ -60,5 +63,5 @@ class Category_Mapper { } return ( '' != $best_mobilizon_category_match ) ? strtoupper( $best_mobilizon_category_match ) : 'MEETING'; - } + } }