wordpress-activitypub-event.../includes/activitypub/transformer/class-events-manager.php

326 lines
8.4 KiB
PHP
Raw Permalink Normal View History

<?php
2024-04-09 19:20:53 +02:00
/**
* ActivityPub Transformer for the plugin Very Simple Event List.
*
* @package activity-event-transformers
* @license AGPL-3.0-or-later
*/
2024-01-05 14:01:30 +01:00
use EM_Event;
2024-01-18 15:27:16 +01:00
use Activitypub\Activity\Extended_Object\Event;
use Activitypub\Activity\Extended_Object\Place;
use Activitypub\Transformer\Post;
2024-01-05 16:24:31 +01:00
use function Activitypub\esc_hashtag;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
/**
* ActivityPub Transformer for events from the WordPress plugin 'Events Manager'
2024-01-05 21:18:34 +01:00
*
* @see https://wordpress.org/plugins/events-manager/
*
* @since 1.0.0
*/
class Events_Manager extends BaseEvent {
2024-04-09 19:20:53 +02:00
2024-01-05 14:01:30 +01:00
/**
* Holds the EM_Event object.
2024-01-05 21:18:34 +01:00
*
2024-01-05 14:01:30 +01:00
* @var EM_Event
*/
protected $em_event;
/**
* Get transformer name.
*
* Retrieve the transformers name.
*
* @since 1.0.0
* @access public
* @return string Widget name.
*/
public function get_transformer_name() {
2024-04-09 19:20:53 +02:00
return 'activitypub-event-transformers/events-manager';
}
/**
* Get transformer title.
*
* Retrieve the transformers label.
*
* @since 1.0.0
* @access public
* @return string Widget title.
*/
public function get_transformer_label() {
2024-04-09 19:20:53 +02:00
return 'Events Manager';
}
/**
* 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() {
2024-04-09 19:20:53 +02:00
return array();
}
/**
* Returns the ActivityStreams 2.0 Object-Type for an Event.
*
* @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-event
* @since 1.0.0
* @return string The Event Object-Type.
*/
protected function get_type() {
2024-04-09 19:20:53 +02:00
return 'Event';
}
2024-01-05 21:18:34 +01:00
protected function get_is_online() {
2024-04-09 19:20:53 +02:00
2024-01-05 21:18:34 +01:00
return 'url' === $this->em_event->event_location_type;
}
/**
* Get the event location.
*
* @param int $post_id The WordPress post ID.
* @return array The Place.
*/
public function get_location() {
2024-04-09 19:20:53 +02:00
2024-01-05 21:18:34 +01:00
if ( 'url' === $this->em_event->event_location_type ) {
return null;
}
2024-04-09 19:20:53 +02:00
$location = new Place();
2024-01-05 14:59:09 +01:00
$em_location = $this->em_event->get_location();
$location->set_name( $em_location->location_name );
2024-01-05 21:18:34 +01:00
$address = array(
2024-04-09 19:20:53 +02:00
'type' => 'PostalAddress',
'addressCountry' => $em_location->location_country,
2024-01-05 14:59:09 +01:00
'addressLocality' => $em_location->location_town,
2024-04-09 19:20:53 +02:00
'streetAddress' => $em_location->location_address,
'name' => $em_location->location_name,
2024-01-05 21:18:34 +01:00
);
if ( $em_location->location_state ) {
$address['addressRegion'] = $em_location->location_state;
}
if ( $em_location->location_postcode ) {
$address['postalCode'] = $em_location->location_postcode;
}
$location->set_address( $address );
2024-01-05 14:59:09 +01:00
return $location;
}
/**
* Get the end time from the events metadata.
*/
protected function get_end_time() {
2024-04-09 19:20:53 +02:00
return null;
}
/**
* Get the end time from the events metadata.
*/
protected function get_start_time() {
2024-04-09 19:20:53 +02:00
$date_string = $this->em_event->event_start_date;
$time_string = $this->em_event->event_start_time;
2024-01-05 14:59:09 +01:00
$timezone_string = $this->em_event->event_timezone;
// Create a DateTime object with the given date, time, and timezone
2024-01-05 21:18:34 +01:00
$datetime = new DateTime( $date_string . ' ' . $time_string, new DateTimeZone( $timezone_string ) );
2024-01-05 14:59:09 +01:00
// Set the timezone for proper formatting
2024-01-05 21:18:34 +01:00
$datetime->setTimezone( new DateTimeZone( 'UTC' ) );
2024-01-05 14:59:09 +01:00
// Format the DateTime object as 'Y-m-d\TH:i:s\Z'
2024-01-05 21:18:34 +01:00
$formatted_date = $datetime->format( 'Y-m-d\TH:i:s\Z' );
2024-01-05 14:59:09 +01:00
return $formatted_date;
}
protected function get_maximum_attendee_capacity() {
2024-04-09 19:20:53 +02:00
2024-01-05 14:59:09 +01:00
return $this->em_event->event_spaces;
}
/**
* @todo decide whether to include pending bookings or not!
*/
protected function get_remaining_attendee_capacity() {
2024-04-09 19:20:53 +02:00
$em_bookings = $this->em_event->get_bookings()->get_bookings();
2024-01-05 14:59:09 +01:00
$remaining_attendee_capacity = $this->em_event->event_spaces - count( $em_bookings->bookings );
return $remaining_attendee_capacity;
}
protected function get_participant_count() {
2024-04-09 19:20:53 +02:00
2024-01-05 14:59:09 +01:00
$em_bookings = $this->em_event->get_bookings()->get_bookings();
return count( $em_bookings->bookings );
}
2024-01-05 21:18:34 +01:00
2024-01-05 14:59:09 +01:00
protected function get_content() {
2024-04-09 19:20:53 +02:00
2024-01-05 14:59:09 +01:00
return $this->wp_object->post_content;
}
protected function get_summary() {
2024-04-09 19:20:53 +02:00
2024-01-05 14:59:09 +01:00
if ( $this->em_event->post_excerpt ) {
$excerpt = $this->em_event->post_excerpt;
} else {
$excerpt = $this->get_content();
}
2024-04-09 19:20:53 +02:00
$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' );
2024-01-05 14:59:09 +01:00
$start_time_string = wp_date( $datetime_format, $start_time );
2024-04-09 19:20:53 +02:00
$summary = "📍 {$address}\n📅 {$start_time_string}\n\n{$excerpt}";
2024-01-05 14:59:09 +01:00
return $summary;
}
2024-01-05 21:18:34 +01:00
// protected function get_join_mode() {
2024-04-09 19:20:53 +02:00
// return 'free';
2024-01-05 21:18:34 +01:00
// }
private function get_event_link_attachment() {
2024-04-09 19:20:53 +02:00
$event_link_url = $this->em_event->event_location->data['url'];
2024-01-05 21:18:34 +01:00
$event_link_text = $this->em_event->event_location->data['text'];
return array(
2024-04-09 19:20:53 +02:00
'type' => 'Link',
'name' => 'Website',
2024-01-05 21:18:34 +01:00
// 'name' => $event_link_text,
2024-04-09 19:20:53 +02:00
'href' => \esc_url( $event_link_url ),
2024-01-05 21:18:34 +01:00
'mediaType' => 'text/html',
);
}
/**
* Overrides/extends the get_attachments function to also add the event Link.
*/
protected function get_attachment() {
2024-01-05 21:18:34 +01:00
// Get attachments via parent function
2024-01-05 16:24:31 +01:00
$attachments = parent::get_attachment();
2024-01-05 21:18:34 +01:00
// The first attachment is the featured image, make sure it is compatible with Mobilizon.
2024-01-05 16:24:31 +01:00
if ( count( $attachments ) ) {
$attachments[0]['type'] = 'Document';
$attachments[0]['name'] = 'Banner';
}
2024-01-05 21:18:34 +01:00
if ( 'url' === $this->em_event->event_location_type ) {
$attachments[] = $this->get_event_link_attachment();
}
2024-04-09 19:20:53 +02:00
return $attachments;
}
/**
* This function tries to map VS-Event categories to Mobilizon event categories.
*
* @return string $category
*/
protected function get_category() {
2024-04-09 19:20:53 +02:00
2024-01-05 16:24:31 +01:00
$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.
2024-01-17 18:02:40 +01:00
$mobilizon_categories = array_map( 'strtolower', Event::DEFAULT_EVENT_CATEGORIES );
2024-01-05 16:24:31 +01:00
// Initialize variables to track the best match.
$best_mobilizon_category_match = '';
2024-04-09 19:20:53 +02:00
$best_match_length = 0;
2024-01-05 16:24:31 +01:00
// 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;
2024-04-09 19:20:53 +02:00
$best_match_length = $current_match_legnth;
2024-01-05 16:24:31 +01:00
}
}
}
}
}
return ( '' != $best_mobilizon_category_match ) ? strtoupper( $best_mobilizon_category_match ) : 'MEETING';
}
protected function get_tag() {
// The parent tag function also fetches the mentions.
$tags = parent::get_tag();
$post_tags = \wp_get_post_terms( $this->wp_object->ID, 'event-tags' );
if ( $post_tags ) {
foreach ( $post_tags as $post_tag ) {
2024-04-09 19:20:53 +02:00
$tag = array(
2024-01-05 16:24:31 +01:00
'type' => 'Hashtag',
'href' => \esc_url( \get_tag_link( $post_tag->term_id ) ),
'name' => esc_hashtag( $post_tag->name ),
);
$tags[] = $tag;
}
}
return $tags;
}
protected function get_name() {
2024-04-09 19:20:53 +02:00
2024-01-05 16:24:31 +01:00
return $this->em_event->event_name;
}
/**
* Transform the WordPress Object into an ActivityPub Object.
*
* @return Activitypub\Activity\Event
*/
public function to_object() {
2024-04-09 19:20:53 +02:00
$this->em_event = new EM_Event( $this->wp_object->ID, 'post_id' );
2024-01-17 18:02:40 +01:00
$activitypub_object = new Event();
2024-01-05 14:01:30 +01:00
2024-01-17 18:02:40 +01:00
$activitypub_object = $this->transform_object_properties( $activitypub_object );
2024-01-05 14:59:09 +01:00
2024-01-17 18:02:40 +01:00
$activitypub_object->set_external_participation_url( $this->get_url() );
2024-01-05 21:18:34 +01:00
2024-01-17 18:02:40 +01:00
return $activitypub_object;
}
}