diff --git a/includes/activitypub/transformer/class-event.php b/includes/activitypub/transformer/class-event.php index 4dc96e1..33a36d0 100644 --- a/includes/activitypub/transformer/class-event.php +++ b/includes/activitypub/transformer/class-event.php @@ -8,10 +8,12 @@ namespace Activitypub_Event_Extensions\Activitypub\Transformer; -use Activitypub\Activity\Extended_Object\Event as Event_Object; -use Activitypub\Transformer\Post; +use DateTime; -use function Activitypub\get_rest_url_by_path; +use Activitypub\Activity\Extended_Object\Event as Event_Object; +use Activitypub\Activity\Extended_Object\Place; + +use Activitypub\Transformer\Post; /** * Base transformer for WordPress event post types to ActivityPub events. @@ -19,7 +21,7 @@ use function Activitypub\get_rest_url_by_path; * Everything that transforming several WordPress post types that represent events * have in common, as well as sane defaults for events should be defined here. */ -class Event extends Post { +abstract class Event extends Post { /** * The WordPress event taxonomy. @@ -118,6 +120,141 @@ class Event extends Post { } } + /** + * Retrieves the excerpt text (may be HTML). Used for constructing the summary. + * + * @return ?string + */ + protected function extract_excerpt(): ?string { + if ( $this->wp_object->excerpt ) { + return $this->wp_object->post_excerpt; + } else { + return null; + } + } + + /** + * Get the start time. + * + * This is mandatory and must be implemented in the final event transformer class. + */ + abstract protected function get_start_time(): string; + + /** + * Get the end time. + * + * This is not mandatory and therefore just return null by default. + */ + protected function get_end_time(): ?string { + return null; + } + + /** + * Get a default for the location. + * + * This should be overridden in the actual event transformer. + */ + protected function get_location(): ?Place { + return null; + } + + /** + * Compose a human readable formatted start time. + */ + protected function format_time( $is_start_time = true ) { + $time = $is_start_time ? $this->get_start_time() : $this->get_end_time(); + if ( is_null( $time ) ) { + return ''; + } + $start_datetime = new DateTime( $time ); + $start_timestamp = $start_datetime->getTimestamp(); + $datetime_format = get_option( 'date_format' ) . ' ' . get_option( 'time_format' ); + return wp_date( $datetime_format, $start_timestamp ); + } + + /** + * Format a human readable address. + */ + protected function format_address(): string { + $location = $this->get_location(); + if ( is_null( $location ) ) { + return ''; + } + $address = $location->get_address(); + if ( ! $address ) { + return $location->get_name(); + } + if ( is_string( $address ) ) { + return $address; + } + if ( ! is_array( $address ) ) { + return ''; + } + return isset( $address['locality'] ) ? $address['locality'] : ''; + } + + /** + * Format the category using the translation. + */ + protected function format_category(): string { + require_once ACTIVITYPUB_EVENT_EXTENSIONS_PLUGIN_DIR . '/includes/event-categories.php'; + $category = $this->get_category(); + if ( array_key_exists( $category, ACTIVITYPUB_EVENT_EXTENSIONS_EVENT_CATEGORIES ) ) { + return ACTIVITYPUB_EVENT_EXTENSIONS_EVENT_CATEGORIES[ $category ]; + } else { + return ACTIVITYPUB_EVENT_EXTENSIONS_EVENT_CATEGORIES['MEETING']; + } + } + + /** + * 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(): ?string { + $excerpt = $this->extract_excerpt(); + // BeforeFirstRelease: decide whether this should be a admin setting. + $fallback_to_content = true; + if ( is_null( $excerpt ) && $fallback_to_content ) { + $excerpt = $this->get_content(); + } + $category = $this->format_category(); + $start_time = $this->format_time(); + $end_time = $this->format_time( false ); + $address = $this->format_address(); + + $formatted_items = array(); + if ( ! empty( $category ) ) { + $formatted_items[] = "šŸ·ļø $category"; + } + + if ( ! empty( $start_time ) ) { + $formatted_items[] = "šŸ—“ļø . {$start_time}"; + } + + if ( ! empty( $end_time ) ) { + $formatted_items[] = "ā³ . {$end_time}"; + } + + if ( ! empty( $address ) ) { + $formatted_items[] = "šŸ“ {$address}"; + } + // Compose the summary based on the number of meta items. + if ( count( $formatted_items ) > 1 ) { + $summary = ''; + } elseif ( 1 === count( $formatted_items ) ) { + $summary = $formatted_items[0]; // Just the one item without