From 6191dafe5b932b0ebd74bf72d9a61f9f2c93fea2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Menrath?= Date: Tue, 27 Aug 2024 21:13:37 +0200 Subject: [PATCH] some cleanup and code linting --- activitypub-event-extensions.php | 14 +- .../activitypub/transformer/class-event.php | 8 +- .../transformer/class-events-manager.php | 81 +++------ .../transformer/class-gatherpress.php | 33 +--- .../transformer/class-the-events-calendar.php | 170 ++++++++++++++++++ .../activitypub/transformer/class-tribe.php | 136 -------------- ...s-vs-event.php => class-vs-event-list.php} | 24 +-- includes/class-settings.php | 0 8 files changed, 218 insertions(+), 248 deletions(-) create mode 100644 includes/activitypub/transformer/class-the-events-calendar.php delete mode 100644 includes/activitypub/transformer/class-tribe.php rename includes/activitypub/transformer/{class-vs-event.php => class-vs-event-list.php} (91%) delete mode 100644 includes/class-settings.php diff --git a/activitypub-event-extensions.php b/activitypub-event-extensions.php index da13f29..a8638f7 100644 --- a/activitypub-event-extensions.php +++ b/activitypub-event-extensions.php @@ -32,11 +32,6 @@ Activitypub_Event_Extensions\Autoloader::register(); // Initialize the plugin. Activitypub_Event_Extensions\Setup::get_instance(); - - - - - // For local development purposes: TODO. Remove everything after here. /** @@ -53,9 +48,11 @@ add_filter( 'http_request_host_is_external', 'custom_http_request_host_is_extern * * TODO: Remove this for release. * + * @param bool $is_external Whether the request is external. + * * @todo This filter is temporary code needed to do local testing. */ -function custom_http_request_host_is_external( $is_external, $host, $url ) { +function custom_http_request_host_is_external( $is_external ) { $is_external = true; return $is_external; @@ -70,6 +67,9 @@ function custom_http_request_host_is_external( $is_external, $host, $url ) { */ add_filter( 'https_ssl_verify', 'dont_verify_local_dev_https', 10, 3 ); -function dont_verify_local_dev_https( $url ) { +/** + * TODO: remove it. + */ +function dont_verify_local_dev_https() { return false; } diff --git a/includes/activitypub/transformer/class-event.php b/includes/activitypub/transformer/class-event.php index 7c71022..d7951f8 100644 --- a/includes/activitypub/transformer/class-event.php +++ b/includes/activitypub/transformer/class-event.php @@ -43,9 +43,13 @@ class Event extends Post { /** * Format a human readable HTML summary. + * + * @param string $summary_text The base string to be formatted. + * + * @return string */ - protected function format_html_summary($summary_text) { - + protected function format_html_summary( $summary_text ): string { + return $summary_text; } /** diff --git a/includes/activitypub/transformer/class-events-manager.php b/includes/activitypub/transformer/class-events-manager.php index 6234b6a..476fe2c 100644 --- a/includes/activitypub/transformer/class-events-manager.php +++ b/includes/activitypub/transformer/class-events-manager.php @@ -127,14 +127,14 @@ class Events_Manager extends Event_Transformer { /** * Get the end time from the events metadata. */ - protected function get_end_time() { + public function get_end_time() { return null; } /** * Get the end time from the events metadata. */ - protected function get_start_time() { + public function get_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; @@ -155,7 +155,7 @@ class Events_Manager extends Event_Transformer { * * @return int */ - protected function get_maximum_attendee_capacity() { + public function get_maximum_attendee_capacity() { return $this->em_event->event_spaces; } @@ -164,7 +164,7 @@ class Events_Manager extends Event_Transformer { * * @todo decide whether to include pending bookings or not! */ - protected function get_remaining_attendee_capacity() { + public function get_remaining_attendee_capacity() { $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; @@ -175,12 +175,15 @@ class Events_Manager extends Event_Transformer { * * @return int */ - protected function get_participant_count() { + public function get_participant_count() { $em_bookings = $this->em_event->get_bookings()->get_bookings(); return count( $em_bookings->bookings ); } - protected function get_summary() { + /** + * Hardcoded function for generating a summary. + */ + public function get_summary() { if ( $this->em_event->post_excerpt ) { $excerpt = $this->em_event->post_excerpt; } else { @@ -194,17 +197,17 @@ class Events_Manager extends Event_Transformer { return $summary; } - // protected function get_join_mode() { - // return 'free'; - // } - - private function get_event_link_attachment() { + /** + * Get the event link as an ActivityPub Link object, but as an associative array. + * + * @return array + */ + private function get_event_link_attachment(): array { $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', - // 'name' => $event_link_text, + 'name' => $event_link_text ? $event_link_text : 'Website', 'href' => \esc_url( $event_link_url ), 'mediaType' => 'text/html', ); @@ -230,54 +233,9 @@ class Events_Manager extends Event_Transformer { } /** - * This function tries to map VS-Event categories to Mobilizon event categories. - * - * @return string $category + * Compose the events tags. */ - protected function get_category() { - $categories = $this->em_event->get_categories()->terms; - - if ( empty( $categories ) ) { - return 'MEETING'; - } - - // Prepare an array to store all category information for comparison. - $category_info = array(); - - // Extract relevant category information (name, slug, description) from the categories array. - foreach ( $categories as $category ) { - $category_info[] = strtolower( $category->name ); - $category_info[] = strtolower( $category->slug ); - $category_info[] = strtolower( $category->description ); - } - - // Convert mobilizon categories to lowercase for case-insensitive comparison. - $mobilizon_categories = array_map( 'strtolower', Event::DEFAULT_EVENT_CATEGORIES ); - - // Initialize variables to track the best match. - $best_mobilizon_category_match = ''; - $best_match_length = 0; - - // Check for the best match. - foreach ( $mobilizon_categories as $mobilizon_category ) { - foreach ( $category_info as $category ) { - foreach ( explode( '_', $mobilizon_category ) as $mobilizon_category_slice ) { - if ( stripos( $category, $mobilizon_category_slice ) !== false ) { - // Check if the current match is longer than the previous best match. - $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; - } - } - } - } - } - - return ( '' != $best_mobilizon_category_match ) ? strtoupper( $best_mobilizon_category_match ) : 'MEETING'; - } - - protected function get_tag() { + public function get_tag() { // The parent tag function also fetches the mentions. $tags = parent::get_tag(); @@ -296,6 +254,9 @@ class Events_Manager extends Event_Transformer { return $tags; } + /** + * Get the events title/name. + */ protected function get_name() { return $this->em_event->event_name; } diff --git a/includes/activitypub/transformer/class-gatherpress.php b/includes/activitypub/transformer/class-gatherpress.php index e0c0b80..077c600 100644 --- a/includes/activitypub/transformer/class-gatherpress.php +++ b/includes/activitypub/transformer/class-gatherpress.php @@ -6,8 +6,8 @@ * @license AGPL-3.0-or-later */ -use Activitypub\Transformer\Post; -use Activitypub\Model\Blog_user; +use Activitypub_Event_Extensions\Activitypub\Transformer\Event; +use Activitypub\Model\Blog; use Activitypub\Activity\Extended_Object\Event as Event_Object; use Activitypub\Activity\Extended_Object\Place; use GatherPress\Core\Event as GatherPress_Event; @@ -23,7 +23,7 @@ if ( ! defined( 'ABSPATH' ) ) { * * @since 1.0.0 */ -class GatherPress extends Post { +class GatherPress extends Event { /** * The target ActivityPub Event object of the transformer. @@ -35,7 +35,7 @@ class GatherPress extends Post { /** * The current GatherPress Event object. * - * @var Event + * @var GatherPress_Event */ protected $gp_event; @@ -56,7 +56,6 @@ class GatherPress extends Post { * @return string Widget name. */ public function get_transformer_name() { - return 'gatherpress/gp-event'; } @@ -70,7 +69,6 @@ class GatherPress extends Post { * @return string Widget title. */ public function get_transformer_label() { - return 'GatherPress Event'; } @@ -84,7 +82,6 @@ class GatherPress extends Post { * @return array Widget categories. */ public static function get_supported_post_types() { - return array( GatherPress_Event::POST_TYPE ); } @@ -96,7 +93,6 @@ class GatherPress extends Post { * @return string The Event Object-Type. */ protected function get_type() { - return 'Event'; } @@ -106,7 +102,6 @@ class GatherPress extends Post { * @return array The Place. */ public function get_location() { - $address = $this->gp_venue['full_address']; $place = new Place(); $place->set_type( 'Place' ); @@ -119,7 +114,6 @@ class GatherPress extends Post { * 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' ); } @@ -127,7 +121,6 @@ class GatherPress extends Post { * 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' ); } @@ -151,7 +144,6 @@ class GatherPress 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'; @@ -164,16 +156,6 @@ class GatherPress extends Post { return $attachments; } - /** - * TODO: - * - * @return string $category - */ - protected function get_category() { - - return 'MEETING'; - } - /** * Returns the User-URL of the Author of the Post. * @@ -182,8 +164,7 @@ class GatherPress extends Post { * @return string The User-URL. */ protected function get_attributed_to() { - - $user = new Blog_User(); + $user = new Blog(); return $user->get_url(); } @@ -196,7 +177,6 @@ class GatherPress 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 ) ) { @@ -219,8 +199,7 @@ class GatherPress extends Post { * @return Activitypub\Activity\Event */ public function to_object() { - - $this->ap_object = new Event(); + $this->ap_object = new Event_Object(); $this->gp_event = new GatherPress_Event( $this->wp_object->ID ); $this->gp_venue = $this->gp_event->get_venue_information(); diff --git a/includes/activitypub/transformer/class-the-events-calendar.php b/includes/activitypub/transformer/class-the-events-calendar.php new file mode 100644 index 0000000..acfa499 --- /dev/null +++ b/includes/activitypub/transformer/class-the-events-calendar.php @@ -0,0 +1,170 @@ +tribe_event = \tribe_get_event( $wp_object ); + } + + /** + * 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 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 ) { + return null; + } + + return $categories[0]; + } + + /** + * Get status of the tribe event + * + * @return string status of the event + */ + public function get_tribe_status() { + + if ( 'canceled' === $this->tribe_event->event_status ) { + return 'CANCELLED'; + } + if ( 'postponed' === $this->tribe_event->event_status ) { + return 'CANCELLED'; // This will be reflected in the cancelled reason. + } + if ( '' === $this->tribe_event->event_status ) { + return 'CONFIRMED'; + } + + return new WP_Error( 'invalid event_status value', __( 'invalid event_status', 'activitypub' ), array( 'status' => 404 ) ); + } + + /** + * Extract the join mode. + * + * If the ticket sale is active set it to restricted. + * + * @return string + */ + public function get_join_mode() { + return empty( $this->tribe_event->tickets ) ? 'free' : 'restricted'; + } + + /** + * Check if the comments are enabled for the current event. + */ + public function get_comments_enabled(): bool { + return ( 'open' === $this->tribe_event->comment_status ) ? true : false; + } + + /** + * Check if the event is an online event. + */ + public function get_is_online(): bool { + return false; + } + + /** + * Returns the content for the ActivityPub Item with + * + * The content will be generated based on the user settings. + * + * @return string The content. + */ + protected function get_content() { + + $content = parent::get_content(); + // TODO: remove link at the end of the content. + + // TODO: add organizer + // $this->tribe_event->organizers[0]. + + // TODO: do add Cancelled reason in the content (maybe at the end). + + return $content; + } + + /** + * Get the event location. + * + * @return Place|array The place/venue if one is set. + */ + public function get_location(): Place|null { + if ( empty( $this->wp_object->venues ) || ! empty( $this->wp_object->venues[0] ) ) { + return null; + } + // We currently only support a single venue. + $event_venue = $this->wp_object->venues[0]; + + $address = array( + 'addressCountry' => $event_venue->country, + 'addressLocality' => $event_venue->city, + 'addressRegion' => $event_venue->province, + 'postalCode' => $event_venue->zip, + 'streetAddress' => $event_venue->address, + 'type' => 'PostalAddress', + ); + + $location = new Place(); + $location->set_address( $address ); + $location->set_id( $event_venue->permalink ); + $location->set_name( $event_venue->post_name ); + + return $location; + } + + /** + * Extend the default event transformers to_object function. + * + * This is the heart of the ActivityPub transformer. + * + * @return Event_Object + */ + public function to_object() { + $activitypub_object = parent::to_object(); + + return $activitypub_object; + } +} diff --git a/includes/activitypub/transformer/class-tribe.php b/includes/activitypub/transformer/class-tribe.php deleted file mode 100644 index 12b2657..0000000 --- a/includes/activitypub/transformer/class-tribe.php +++ /dev/null @@ -1,136 +0,0 @@ -tribe_event = tribe_get_event( $wp_post->ID ); - // } - - /** - * 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 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 ) { - return null; - } - - return $categories[0]; - } - - /** - * Get status of the tribe event - * - * @return string status of the event - */ - public function get_tribe_status() { - - if ( 'canceled' === $this->tribe_event->event_status ) { - return 'CANCELLED'; - } - if ( 'postponed' === $this->tribe_event->event_status ) { - return 'CANCELLED'; // This will be reflected in the cancelled reason. - } - if ( '' === $this->tribe_event->event_status ) { - return 'CONFIRMED'; - } - - return new WP_Error( 'invalid event_status value', __( 'invalid event_status', 'activitypub' ), array( 'status' => 404 ) ); - } - - /** - * Returns the content for the ActivityPub Item with - * - * The content will be generated based on the user settings. - * - * @return string The content. - */ - protected function get_content() { - - $content = parent::get_content(); - // TODO: remove link at the end of the content. - - // TODO: add organizer - // $this->tribe_event->organizers[0]. - - // TODO: do add Cancelled reason in the content (maybe at the end). - - return $content; - } - - /** - * Get the event location. - * - * @returns array The Place. - */ - public function get_event_location() { - /* - 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() ) - ->set_type( 'Place' ) - ->set_name( $venue->post_name ) - ->set_address( - $venue->address . "\n" . - $venue->zip . ', ' . $venue->city . "\n" . - $venue->country - ); // TODO: add checks that everything exists here. - } -} diff --git a/includes/activitypub/transformer/class-vs-event.php b/includes/activitypub/transformer/class-vs-event-list.php similarity index 91% rename from includes/activitypub/transformer/class-vs-event.php rename to includes/activitypub/transformer/class-vs-event-list.php index a48e596..a518824 100644 --- a/includes/activitypub/transformer/class-vs-event.php +++ b/includes/activitypub/transformer/class-vs-event-list.php @@ -9,7 +9,6 @@ namespace Activitypub_Event_Extensions\Activitypub\Transformer; use Activitypub_Event_Extensions\Activitypub\Transformer\Event as Event_Transformer; -use Activitypub\Model\Blog; use Activitypub\Activity\Extended_Object\Event; use Activitypub\Activity\Extended_Object\Place; @@ -21,14 +20,16 @@ if ( ! defined( 'ABSPATH' ) ) { } /** - * ActivityPub Transformer for VS Event + * ActivityPub Transformer for VS Event. + * + * This transformer tries a different principle: The setters are chainable. * * @since 1.0.0 */ -class VS_Event extends Event_Transformer { +class VS_Event_List extends Event_Transformer { /** - * The target transformet ActivityPub Event object. + * The target transformer ActivityPub Event object. * * @var Event */ @@ -154,15 +155,6 @@ class VS_Event extends Event_Transformer { return $attachments; } - /** - * This function tries to map VS-Event categories to Mobilizon event categories. - * - * @return string $category - */ - protected function get_category() { - return 'MEETING'; - } - /** * Create a custom summary. * @@ -205,7 +197,7 @@ class VS_Event extends Event_Transformer { $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 ), true ) ) { $this->ap_object->$setter_function( $this->$getter_function() ); } else { $this->ap_object->$setter_function( $value ); @@ -220,7 +212,7 @@ class VS_Event extends Event_Transformer { * @param string $method The method name. * @param string $params The method params. * - * @return void + * @return void|this */ public function __call( $method, $params ) { @@ -230,7 +222,7 @@ class VS_Event extends Event_Transformer { return $this->set( $var, $params[0] ); } - // when do we need: call_user_func( array( $activitypub_object, $setter ), $value ); + // TODO: When do we need: call_user_func( array( $activitypub_object, $setter ), $value ). return $this; } diff --git a/includes/class-settings.php b/includes/class-settings.php deleted file mode 100644 index e69de29..0000000