2024-01-05 12:12:29 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* ActivityPub Transformer for the plugin Very Simple Event List.
|
|
|
|
*
|
2024-10-02 20:09:37 +02:00
|
|
|
* @package ActivityPub_Event_Bridge
|
2024-01-05 12:12:29 +01:00
|
|
|
* @license AGPL-3.0-or-later
|
|
|
|
*/
|
|
|
|
|
2024-10-02 20:09:37 +02:00
|
|
|
namespace ActivityPub_Event_Bridge\Activitypub\Transformer;
|
2024-09-21 11:00:33 +02:00
|
|
|
|
2024-09-28 13:14:10 +02:00
|
|
|
// Exit if accessed directly.
|
|
|
|
defined( 'ABSPATH' ) || exit; // @codeCoverageIgnore
|
|
|
|
|
|
|
|
use Activitypub\Activity\Extended_Object\Event;
|
|
|
|
use Activitypub\Activity\Extended_Object\Place;
|
2024-10-02 20:09:37 +02:00
|
|
|
use ActivityPub_Event_Bridge\Activitypub\Transformer\Event as Event_Transformer;
|
2024-09-21 11:00:33 +02:00
|
|
|
use DateTime;
|
|
|
|
use DateTimeZone;
|
2024-01-05 14:01:30 +01:00
|
|
|
use EM_Event;
|
2024-01-05 16:24:31 +01:00
|
|
|
|
2024-09-28 13:14:10 +02:00
|
|
|
use function Activitypub\esc_hashtag;
|
2024-01-05 12:12:29 +01:00
|
|
|
|
|
|
|
/**
|
2024-01-05 12:49:46 +01:00
|
|
|
* ActivityPub Transformer for events from the WordPress plugin 'Events Manager'
|
2024-01-05 21:18:34 +01:00
|
|
|
*
|
2024-01-05 12:49:46 +01:00
|
|
|
* @see https://wordpress.org/plugins/events-manager/
|
2024-01-05 12:12:29 +01:00
|
|
|
*
|
|
|
|
* @since 1.0.0
|
|
|
|
*/
|
2024-09-11 00:38:21 +02:00
|
|
|
final class Events_Manager extends Event_Transformer {
|
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;
|
|
|
|
|
2024-01-05 12:12:29 +01:00
|
|
|
/**
|
2024-10-02 19:15:28 +02:00
|
|
|
* Extend the constructor, to also set the Eventsmanager objects.
|
2024-01-05 12:12:29 +01:00
|
|
|
*
|
2024-10-02 19:15:28 +02:00
|
|
|
* This is a special class object form The Events Calendar which
|
|
|
|
* has a lot of useful functions, we make use of our getter functions.
|
2024-01-05 12:12:29 +01:00
|
|
|
*
|
2024-10-02 19:15:28 +02:00
|
|
|
* @param WP_Post $wp_object The WordPress object.
|
|
|
|
* @param string $wp_taxonomy The taxonomy slug of the event post type.
|
2024-01-05 12:12:29 +01:00
|
|
|
*/
|
2024-10-02 19:15:28 +02:00
|
|
|
public function __construct( $wp_object, $wp_taxonomy ) {
|
|
|
|
parent::__construct( $wp_object, $wp_taxonomy );
|
|
|
|
$this->em_event = new EM_Event( $this->wp_object->ID, 'post_id' );
|
2024-01-05 12:12:29 +01:00
|
|
|
}
|
|
|
|
|
2024-08-01 20:08:35 +02:00
|
|
|
/**
|
|
|
|
* Returns whether the even is online
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2024-01-05 21:18:34 +01:00
|
|
|
protected function get_is_online() {
|
|
|
|
return 'url' === $this->em_event->event_location_type;
|
|
|
|
}
|
|
|
|
|
2024-01-05 12:12:29 +01:00
|
|
|
/**
|
|
|
|
* Get the event location.
|
|
|
|
*
|
|
|
|
* @return array The Place.
|
|
|
|
*/
|
2024-09-25 14:30:37 +02:00
|
|
|
public function get_location(): ?Place {
|
2024-01-05 21:18:34 +01:00
|
|
|
if ( 'url' === $this->em_event->event_location_type ) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2024-01-05 14:59:09 +01:00
|
|
|
$em_location = $this->em_event->get_location();
|
|
|
|
|
2024-09-25 11:27:17 +02:00
|
|
|
if ( '' === $em_location->location_id ) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$location = new Place();
|
2024-01-05 14:59:09 +01:00
|
|
|
$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-09-25 11:27:17 +02:00
|
|
|
'postalAddress' => $em_location->location_address,
|
|
|
|
'postalCode' => $em_location->location_postcode,
|
2024-04-09 19:20:53 +02:00
|
|
|
'name' => $em_location->location_name,
|
2024-01-05 21:18:34 +01:00
|
|
|
);
|
|
|
|
if ( $em_location->location_state ) {
|
|
|
|
$address['addressRegion'] = $em_location->location_state;
|
|
|
|
}
|
|
|
|
|
|
|
|
$location->set_address( $address );
|
2024-01-05 14:59:09 +01:00
|
|
|
return $location;
|
2024-01-05 12:12:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the end time from the events metadata.
|
|
|
|
*/
|
2024-09-25 14:24:30 +02:00
|
|
|
public function get_end_time(): ?string {
|
2024-01-05 12:12:29 +01:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the end time from the events metadata.
|
|
|
|
*/
|
2024-09-25 14:24:30 +02:00
|
|
|
public function get_start_time(): string {
|
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;
|
|
|
|
|
2024-08-01 20:08:35 +02:00
|
|
|
// 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
|
|
|
|
2024-08-01 20:08:35 +02: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
|
|
|
|
2024-08-01 20:08:35 +02: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;
|
|
|
|
}
|
|
|
|
|
2024-08-01 20:08:35 +02:00
|
|
|
/**
|
|
|
|
* Returns the maximum attendee capacity.
|
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
*/
|
2024-08-27 21:13:37 +02:00
|
|
|
public function get_maximum_attendee_capacity() {
|
2024-01-05 14:59:09 +01:00
|
|
|
return $this->em_event->event_spaces;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-08-01 20:08:35 +02:00
|
|
|
* Return the remaining attendee capacity
|
|
|
|
*
|
2024-09-21 11:00:33 +02:00
|
|
|
* @return int
|
2024-01-05 14:59:09 +01:00
|
|
|
*/
|
2024-08-27 21:13:37 +02:00
|
|
|
public function get_remaining_attendee_capacity() {
|
2024-10-02 19:15:28 +02:00
|
|
|
$em_bookings_count = $this->get_participant_count();
|
|
|
|
$remaining_attendee_capacity = $this->em_event->event_spaces - $em_bookings_count;
|
2024-01-05 14:59:09 +01:00
|
|
|
return $remaining_attendee_capacity;
|
|
|
|
}
|
|
|
|
|
2024-08-01 20:08:35 +02:00
|
|
|
/**
|
|
|
|
* Returns the current participant count.
|
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
*/
|
2024-09-21 11:00:33 +02:00
|
|
|
public function get_participant_count(): int {
|
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-08-27 21:13:37 +02:00
|
|
|
/**
|
|
|
|
* Get the event link as an ActivityPub Link object, but as an associative array.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
private function get_event_link_attachment(): array {
|
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',
|
2024-08-27 21:13:37 +02:00
|
|
|
'name' => $event_link_text ? $event_link_text : 'Website',
|
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',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-01-05 12:12:29 +01:00
|
|
|
/**
|
|
|
|
* Overrides/extends the get_attachments function to also add the event Link.
|
|
|
|
*/
|
|
|
|
protected function get_attachment() {
|
2024-07-05 13:29:13 +02: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;
|
|
|
|
}
|
2024-01-05 12:12:29 +01:00
|
|
|
|
|
|
|
/**
|
2024-08-27 21:13:37 +02:00
|
|
|
* Compose the events tags.
|
2024-01-05 12:12:29 +01:00
|
|
|
*/
|
2024-08-27 21:13:37 +02:00
|
|
|
public function get_tag() {
|
2024-01-05 16:24:31 +01:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2024-08-27 21:13:37 +02:00
|
|
|
/**
|
|
|
|
* Get the events title/name.
|
2024-09-25 11:27:17 +02:00
|
|
|
*
|
|
|
|
* @return string
|
2024-08-27 21:13:37 +02:00
|
|
|
*/
|
2024-09-25 11:27:17 +02:00
|
|
|
protected function get_name(): string {
|
2024-01-05 16:24:31 +01:00
|
|
|
return $this->em_event->event_name;
|
2024-01-05 12:12:29 +01:00
|
|
|
}
|
|
|
|
}
|