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

242 lines
6.6 KiB
PHP
Raw 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
*/
2023-12-22 19:07:59 +01:00
use Activitypub\Activity\Event;
use Activitypub\Activity\Place;
use Activitypub\Transformer\Post;
2024-01-03 19:13:54 +01:00
use Activitypub\Model\Blog_user;
use function Activitypub\get_rest_url_by_path;
2023-11-25 12:05:12 +01:00
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
*/
2023-12-22 19:07:59 +01:00
class VS_Event extends Post {
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.
*
* @param int $post_id The WordPress post ID.
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->object->ID, 'event-location', true );
$place = new Place();
$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->object->ID, 'event-date', true );
return \gmdate( 'Y-m-d\TH:i:s\Z', $end_time );
}
/**
* Get the end time from the events metadata.
*/
protected function get_start_time() {
2024-01-03 19:13:54 +01:00
$start_time = get_post_meta( $this->object->ID, 'event-start-date', true );
return \gmdate( 'Y-m-d\TH:i:s\Z', $start_time );
}
/**
* Get the event link from the events metadata.
*/
private function get_event_link() {
2023-12-22 19:07:59 +01:00
$event_link = get_post_meta( $this->object->ID, 'event-link', true );
if ( $event_link ) {
return array(
'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();
$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[] = $this->get_event_link();
}
return $attachments;
}
2024-01-03 19:13:54 +01:00
/**
* This function tries to map VS-Event categories to Mobilizon event categories.
*
* @return string $category
*/
protected function get_category() {
$post_categories = wp_get_post_terms( $this->object->ID, 'event_cat' );
if ( empty( $post_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 ( $post_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.
$mobilizon_categories = array_map( 'strtolower', Event::MOBILIZON_EVENT_CATEGORIES );
// Initialize variables to track the best match.
$best_mobilizon_category_match = '';
$best_match_length = 0;
// Check for the best match.
foreach ( $mobilizon_categories as $mobilizon_category ) {
foreach ( $category_info as $category ) {
if ( stripos( $category, $mobilizon_category ) !== false ) {
// Check if the current match is longer than the previous best match.
$current_match_legnth = strlen( $mobilizon_category );
if ( $current_match_legnth > $best_match_length ) {
$best_mobilizon_category_match = $mobilizon_category;
$best_match_length = $current_match_legnth;
}
}
}
}
return ( '' != $best_mobilizon_category_match ) ? strtoupper( $best_mobilizon_category_match ) : 'MEETING';
}
/**
* Returns the User-URL of the Author of the Post.
*
* If `single_user` mode is enabled, the URL of the Blog-User is returned.
*
* @return string The User-URL.
*/
protected function get_attributed_to() {
$user = new Blog_User();
return $user->get_url();
2023-12-22 19:07:59 +01:00
}
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->object->excerpt ) {
$excerpt = $this->object->post_excerpt;
} else if ( get_post_meta( $this->object->ID, 'event-summary', true ) ) {
$excerpt = get_post_meta( $this->object->ID, 'event-summary', true );
} else {
$excerpt = $this->get_content();
}
$address = get_post_meta( $this->object->ID, 'event-location', true );
$start_time = get_post_meta( $this->object->ID, 'event-start-date', true );
$datetime_format = get_option( 'date_format' ) . ' ' . get_option( 'time_format' );
$start_time_string = wp_date( $datetime_format, $start_time );
2024-01-03 19:22:33 +01: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
}
/**
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() {
$object = new Event();
2024-01-03 19:13:54 +01:00
$object = $this->transform_object_properties( $object );
2023-11-25 12:05:12 +01:00
2024-01-03 19:13:54 +01:00
// Set hardcoded values/one-liners that don't have a get(ter) function defined.
2023-12-22 19:07:59 +01:00
return $object
2024-01-03 19:13:54 +01:00
->set_comments_enabled( true )
2023-12-03 11:41:13 +01:00
->set_external_participation_url( $this->get_url() )
2023-12-04 19:27:57 +01:00
->set_status( 'CONFIRMED' )
2023-12-22 19:07:59 +01:00
->set_name( get_the_title( $this->object->ID ) )
2024-01-03 19:13:54 +01:00
->set_timezone( $object->get_locale )
->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' ) )
->set_to( array( 'https://www.w3.org/ns/activitystreams#Public' ) );
2023-11-24 15:36:51 +01:00
}
}