wordpress-activitypub-event.../includes/activitypub/transformer/class-vs-event.php

252 lines
6.1 KiB
PHP
Raw Permalink Normal View History

2023-11-24 15:36:51 +01:00
<?php
/**
* ActivityPub Transformer for the plugin Very Simple Event List.
*
* @package activity-event-transformers
2023-12-04 19:27:57 +01:00
* @license AGPL-3.0-or-later
*/
namespace Activitypub_Event_Extensions\Activitypub\Transformer;
use Activitypub_Event_Extensions\Activitypub\Transformer\Event as Event_Transformer;
use Activitypub\Model\Blog;
2024-01-18 15:27:16 +01:00
use Activitypub\Activity\Extended_Object\Event;
use Activitypub\Activity\Extended_Object\Place;
2024-01-03 19:13:54 +01:00
use WP_Error;
2024-01-03 19:13:54 +01:00
use function Activitypub\get_rest_url_by_path;
2023-11-24 15:36:51 +01:00
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
/**
* ActivityPub Transformer for VS Event
2023-11-24 15:36:51 +01:00
*
* @since 1.0.0
*/
class VS_Event extends Event_Transformer {
2024-04-09 19:20:53 +02:00
2024-01-17 18:02:40 +01:00
/**
* The target transformer ActivityPub Event object.
2024-04-09 19:20:53 +02:00
*
2024-01-17 18:02:40 +01:00
* @var Event
*/
protected $ap_object;
2023-11-24 15:36:51 +01:00
/**
2024-01-03 19:13:54 +01:00
* Get transformer name.
2023-11-24 15:36:51 +01:00
*
2024-01-03 19:13:54 +01:00
* Retrieve the transformers name.
2023-11-24 15:36:51 +01:00
*
* @since 1.0.0
* @access public
* @return string Widget name.
*/
2024-01-03 19:13:54 +01:00
public function get_transformer_name() {
2023-11-24 15:36:51 +01:00
return 'activitypub-event-transformers/vs-event';
}
/**
2024-01-03 19:13:54 +01:00
* Get transformer title.
2023-11-24 15:36:51 +01:00
*
2024-01-03 19:13:54 +01:00
* Retrieve the transformers label.
2023-11-24 15:36:51 +01:00
*
* @since 1.0.0
* @access public
* @return string Widget title.
*/
2024-01-03 19:13:54 +01:00
public function get_transformer_label() {
2023-11-24 15:36:51 +01:00
return 'VS Event';
}
/**
* 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() {
return array( 'event' );
2023-11-24 15:36:51 +01:00
}
2024-01-03 19:13:54 +01:00
/**
* 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() {
return 'Event';
}
2023-11-24 15:36:51 +01:00
/**
* Get the event location.
*
2024-01-03 19:13:54 +01:00
* @return array The Place.
2023-11-24 15:36:51 +01:00
*/
2024-01-03 19:13:54 +01:00
public function get_location() {
$address = get_post_meta( $this->wp_object->ID, 'event-location', true );
2024-04-09 19:20:53 +02:00
$place = new Place();
2024-01-03 19:13:54 +01:00
$place->set_type( 'Place' );
$place->set_name( $address );
$place->set_address( $address );
return $place;
}
2023-11-25 12:05:12 +01:00
/**
* Get the end time from the events metadata.
*/
2023-12-22 19:07:59 +01:00
protected function get_end_time() {
$end_time = get_post_meta( $this->wp_object->ID, 'event-date', true );
2023-12-22 19:07:59 +01:00
return \gmdate( 'Y-m-d\TH:i:s\Z', $end_time );
}
/**
* Get the end time from the events metadata.
*/
protected function get_start_time() {
$start_time = get_post_meta( $this->wp_object->ID, 'event-start-date', true );
2024-01-03 19:13:54 +01:00
return \gmdate( 'Y-m-d\TH:i:s\Z', $start_time );
}
/**
* Get the event link from the events metadata.
*/
private function get_event_link() {
$event_link = get_post_meta( $this->wp_object->ID, 'event-link', true );
if ( $event_link ) {
return array(
2024-04-09 19:20:53 +02:00
'type' => 'Link',
'name' => 'Website',
'href' => \esc_url( $event_link ),
'mediaType' => 'text/html',
);
}
}
/**
2023-12-04 19:27:57 +01:00
* Overrides/extends the get_attachments function to also add the event Link.
*/
2023-12-22 19:07:59 +01:00
protected function get_attachment() {
$attachments = parent::get_attachment();
2024-01-05 16:24:31 +01:00
if ( count( $attachments ) ) {
$attachments[0]['type'] = 'Document';
$attachments[0]['name'] = 'Banner';
}
2023-12-04 19:27:57 +01:00
$event_link = $this->get_event_link();
if ( $event_link ) {
$attachments[] = $event_link;
2023-12-04 19:27:57 +01:00
}
return $attachments;
}
2024-01-03 19:13:54 +01:00
/**
* 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() {
if ( $this->wp_object->excerpt ) {
$excerpt = $this->wp_object->post_excerpt;
2024-01-05 21:18:34 +01:00
} elseif ( get_post_meta( $this->wp_object->ID, 'event-summary', true ) ) {
$excerpt = get_post_meta( $this->wp_object->ID, 'event-summary', true );
2024-01-03 19:13:54 +01:00
} else {
$excerpt = $this->get_content();
}
2024-04-09 19:20:53 +02:00
$address = get_post_meta( $this->wp_object->ID, 'event-location', true );
$start_time = get_post_meta( $this->wp_object->ID, 'event-start-date', true );
$datetime_format = get_option( 'date_format' ) . ' ' . get_option( 'time_format' );
2024-01-03 19:13:54 +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-03 19:13:54 +01:00
return $summary;
2023-12-22 19:07:59 +01:00
}
2024-01-12 18:02:37 +01:00
/**
* Generic setter.
*
* @param string $key The key to set.
* @param string $value The value to set.
*
* @return mixed The value.
*/
public function set( $key, $value ) {
2024-04-09 19:20:53 +02:00
2024-01-12 18:02:37 +01:00
if ( ! $this->ap_object->has( $key ) ) {
return new WP_Error( 'invalid_key', __( 'Invalid key', 'activitypub' ), array( 'status' => 404 ) );
}
$setter_function = 'set_' . $key;
2024-01-18 17:22:17 +01:00
$getter_function = 'get_' . $key;
2024-01-12 18:02:37 +01:00
2024-04-09 19:20:53 +02:00
if ( in_array( $getter_function, get_class_methods( $this ) ) ) {
2024-01-18 17:22:17 +01:00
$this->ap_object->$setter_function( $this->$getter_function() );
2024-01-12 18:02:37 +01:00
} else {
$this->ap_object->$setter_function( $value );
}
return $this;
}
/**
* Magic function to implement setter
*
* @param string $method The method name.
* @param string $params The method params.
*
* @return void
*/
public function __call( $method, $params ) {
2024-04-09 19:20:53 +02:00
2024-01-12 18:02:37 +01:00
$var = \strtolower( \substr( $method, 4 ) );
if ( \strncasecmp( $method, 'set', 3 ) === 0 ) {
return $this->set( $var, $params[0] );
}
// when do we need: call_user_func( array( $activitypub_object, $setter ), $value );
return $this;
}
/**
2023-12-22 19:07:59 +01:00
* Transform the WordPress Object into an ActivityPub Object.
2023-11-25 12:05:12 +01:00
*
2023-12-22 19:07:59 +01:00
* @return Activitypub\Activity\Event
2023-11-25 12:05:12 +01:00
*/
2023-12-22 19:07:59 +01:00
public function to_object() {
2024-04-09 19:20:53 +02:00
2024-01-12 18:02:37 +01:00
$this->ap_object = new Event();
2024-04-09 19:20:53 +02:00
2024-01-12 18:02:37 +01:00
$this
2024-04-09 19:20:53 +02:00
->set_content()
2024-01-12 18:02:37 +01:00
->set_content_map()
->set_attributed_to()
->set_published()
->set_start_time()
->set_end_time()
->set_type()
->set_category()
2024-04-09 19:20:53 +02:00
->set_attachment()
->set_comments_enabled( true )
2023-12-03 11:41:13 +01:00
->set_external_participation_url( $this->get_url() )
2024-04-09 19:20:53 +02:00
->set_status( 'CONFIRMED' )
->set_name( get_the_title( $this->wp_object->ID ) )
->set_is_online( false )
2024-01-03 19:13:54 +01:00
->set_in_language( $this->get_locale() )
->set_actor( get_rest_url_by_path( 'application' ) )
2024-01-18 17:34:17 +01:00
->set_to( array( 'https://www.w3.org/ns/activitystreams#Public' ) )
->set_location()
->set_id();
2024-01-18 17:22:17 +01:00
return $this->ap_object;
2023-11-24 15:36:51 +01:00
}
}