wordpress-event-bridge-for-.../includes/activitypub/transformer/class-my-calendar.php

138 lines
4 KiB
PHP
Raw Permalink Normal View History

2024-10-07 17:42:31 +02:00
<?php
/**
2024-10-26 17:05:58 +02:00
* ActivityPub Transformer for the WordPress plugin "My Calendar Accessible Event Manager".
*
* @see https://wordpress.org/plugins/my-calendar/
2024-10-07 17:42:31 +02:00
*
* @package Activitypub_Event_Extensions
* @license AGPL-3.0-or-later
*/
2024-10-10 14:41:22 +02:00
namespace ActivityPub_Event_Bridge\Activitypub\Transformer;
2024-10-07 17:42:31 +02:00
// Exit if accessed directly.
defined( 'ABSPATH' ) || exit; // @codeCoverageIgnore
use Activitypub\Activity\Extended_Object\Place;
2024-10-10 14:41:22 +02:00
use ActivityPub_Event_Bridge\Activitypub\Transformer\Event as Event_Transformer;
2024-10-07 17:42:31 +02:00
use DateTime;
use DateTimeZone;
/**
2024-10-26 17:05:58 +02:00
* ActivityPub Transformer for events from the WordPress plugin "My Calendar Accessible Event Manager".
2024-10-07 17:42:31 +02:00
*
* @see https://wordpress.org/plugins/my-calendar/
*
* @since 1.0.0
*/
final class My_Calendar extends Event_Transformer {
/**
2024-10-26 17:05:58 +02:00
* Holds the My Calendar post object.
2024-10-07 17:42:31 +02:00
*
2024-10-26 17:05:58 +02:00
* @var object $event Event object.
2024-10-07 17:42:31 +02:00
*/
protected $mc_event;
2024-10-26 17:05:58 +02:00
/**
* Holds the My Calendar Schema object.
*
* @var array JSON/LD Schema for event.
*/
2024-10-10 14:42:26 +02:00
protected $mc_event_schema;
2024-10-07 17:42:31 +02:00
/**
2024-10-26 17:05:58 +02:00
* Extend the constructor, to also set the Event plugins API objects.
2024-10-07 17:42:31 +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-10-26 17:05:58 +02:00
* @param WP_Post $wp_object The WordPress object.
* @param string $wp_taxonomy The taxonomy slug of the event post type.
2024-10-07 17:42:31 +02:00
*/
public function __construct( $wp_object, $wp_taxonomy ) {
parent::__construct( $wp_object, $wp_taxonomy );
2024-10-26 17:05:58 +02:00
$mc_event_id = get_post_meta( $this->wp_object->ID, '_mc_event_id', true );
$this->mc_event = mc_get_event( $mc_event_id );
2024-10-10 14:42:26 +02:00
$this->mc_event_schema = mc_event_schema( $this->mc_event );
2024-10-07 17:42:31 +02:00
}
/**
2024-10-26 17:05:58 +02:00
* Formats time from the plugin to the activitypub standard.
*
* @param string $date_string The plugins string representation for a date without time.
* @param string $time_string The plugins string representation for a time.
*
* @return string
2024-10-07 17:42:31 +02:00
*/
private function convert_time( $date_string, $time_string ): string {
// Create a DateTime object with the given date, time, and timezone.
$datetime = new DateTime( $date_string . ' ' . $time_string );
// Set the timezone for proper formatting.
$datetime->setTimezone( new DateTimeZone( 'UTC' ) );
// Format the DateTime object as 'Y-m-d\TH:i:s\Z'.
$formatted_date = $datetime->format( 'Y-m-d\TH:i:s\Z' );
return $formatted_date;
}
/**
* Get the start time from the events metadata.
2024-10-26 17:05:58 +02:00
*
* @return string The events start date-time.
2024-10-07 17:42:31 +02:00
*/
public function get_start_time(): string {
2024-10-26 17:05:58 +02:00
return $this->convert_time( $this->mc_event->event_begin, $this->mc_event->event_time );
2024-10-07 17:42:31 +02:00
}
/**
* Get the end time from the events metadata.
2024-10-26 17:05:58 +02:00
*
* @return string The events start end-time.
2024-10-07 17:42:31 +02:00
*/
public function get_end_time(): ?string {
2024-10-26 17:05:58 +02:00
return $this->convert_time( $this->mc_event->event_end, $this->mc_event->event_endtime );
2024-10-07 17:42:31 +02:00
}
2024-10-10 14:44:08 +02:00
/**
* Get the event location.
*
* @return Place|null The place/venue if one is set.
*/
public function get_location(): ?Place {
2024-10-27 21:36:52 +01:00
if ( array_key_exists( 'location', $this->mc_event_schema ) && 'Place' === $this->mc_event_schema['location']['@type'] ) {
2024-10-10 14:44:08 +02:00
$mc_place = $this->mc_event_schema['location'];
$place = new Place();
$place->set_name( $mc_place['name'] );
$place->set_url( $mc_place['url'] );
$place->set_address( $mc_place['address'] );
if ( ! empty( $mc_place['geo'] ) ) {
$place->set_latitude( $mc_place['geo']['latitude'] );
$place->set_longitude( $mc_place['geo']['longitude'] );
}
return $place;
}
return null;
}
2024-10-10 14:44:26 +02:00
/**
* Get status of the event
*
* @return string status of the event
*/
public function get_status(): ?string {
2024-10-26 17:05:58 +02:00
return 'CONFIRMED'; // My Calendar doesn't implement canceled events.
2024-10-07 17:42:31 +02:00
}
2024-10-27 21:36:52 +01:00
/**
* Extract the external participation url.
*
* @return ?string The external participation URL.
*/
public function get_external_participation_url(): ?string {
return $this->mc_event->event_tickets ? $this->mc_event->event_tickets : null;
}
2024-10-07 17:42:31 +02:00
}