WIP on transformers

This commit is contained in:
André Menrath 2024-07-05 11:31:46 +02:00
parent 9eebf588ab
commit 4b9dbbef06
5 changed files with 83 additions and 86 deletions

View file

@ -8,9 +8,12 @@
namespace Activitypub_Event_Extensions\Activitypub\Transformer;
use Activitypub\Activity\Extended_Object\Event as Event_Object;
use Activitypub\Model\Blog;
use Activitypub\Transformer\Post;
use function Activitypub\get_rest_url_by_path;
/**
* Base transformer for WordPress event post types to ActivityPub events.
*/
@ -26,4 +29,51 @@ class Event extends Post {
$blog = new Blog();
return $blog->get_id();
}
/**
* Returns the ActivityStreams 2.0 Object-Type for an Event.
*
* @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-event
*
* @return string The Event Object-Type.
*/
protected function get_object_type() {
return 'Event';
}
/**
* Generic function that converts an WP-Event object to an ActivityPub-Event object.
*
* @return Event_Object
*/
public function to_object() {
$activitypub_object = new Event_Object();
$activitypub_object = $this->transform_object_properties( $activitypub_object );
$published = \strtotime( $this->wp_object->post_date_gmt );
$activitypub_object->set_published( \gmdate( 'Y-m-d\TH:i:s\Z', $published ) );
$updated = \strtotime( $this->wp_object->post_modified_gmt );
if ( $updated > $published ) {
$activitypub_object->set_updated( \gmdate( 'Y-m-d\TH:i:s\Z', $updated ) );
}
$activitypub_object->set_content_map(
array(
$this->get_locale() => $this->get_content(),
)
);
$path = sprintf( 'actors/%d/followers', intval( $this->wp_object->post_author ) );
$activitypub_object->set_to(
array(
'https://www.w3.org/ns/activitystreams#Public',
get_rest_url_by_path( $path ),
)
);
return $activitypub_object;
}
}

View file

@ -8,7 +8,7 @@
use Activitypub\Transformer\Post;
use Activitypub\Model\Blog_user;
use Activitypub\Activity\Extended_Object\Event;
use Activitypub\Activity\Extended_Object\Event as Event_Object;
use Activitypub\Activity\Extended_Object\Place;
use GatherPress\Core\Event as GatherPress_Event;

View file

@ -1,5 +1,4 @@
<?php
/**
* ActivityPub Tribe Transformer
*
@ -7,20 +6,22 @@
* @license AGPL-3.0-or-later
*/
namespace Activitypub_Event_Extensions\Activitypub\Transformer;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
use Activitypub_Event_Extensions\Activitypub\Transformer\Event as Event_Transformer;
use Activitypub\Activity\Extended_Object\Event;
use Activitypub_Event_Extensions\Activitypub\Transformer\Event;
use Activitypub\Activity\Extended_Object\Place;
use WP_Error;
/**
* ActivityPub Tribe Transformer
*
* @since 1.0.0
*/
class Tribe extends Event_Transformer {
class Tribe extends Event {
/**
* The Tribe Event object.
@ -40,69 +41,14 @@ class Tribe extends Event_Transformer {
// $this->tribe_event = tribe_get_event( $wp_post->ID );
// }
/**
* Get widget name.
*
* Retrieve oEmbed widget name.
*
* @since 1.0.0
* @access public
* @return string Widget name.
*/
public function get_name() {
return 'activitypub-event-transformers/tribe';
}
/**
* Get widget title.
*
* Retrieve Transformer title.
*
* @since 1.0.0
* @access public
* @return string Widget title.
*/
public function get_label() {
return 'The Events Calendar';
}
/**
* Returns the ActivityStreams 2.0 Object-Type for an Event.
*
* @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-event
*
* @return string The Event Object-Type.
*/
protected function get_object_type() {
return '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( 'tribe_events' );
}
/**
* Get tribe category of wp_post
*
* @return string|null tribe category if it exists
*/
public function get_tribe_category() {
// todo make it possible that one event can have multiple categories?
// using cat_slugs isn't 100% nice way to do this, don't know if it's a good idea
// TODO: make it possible that one event can have multiple categories?
// Using cat_slugs isn't the best way to do this, don't know if it's a good idea.
$categories = tribe_get_event_cat_slugs( $this->wp_object->ID );
if ( count( $categories ) === 0 ) {
@ -123,7 +69,7 @@ class Tribe extends Event_Transformer {
return 'CANCELLED';
}
if ( 'postponed' === $this->tribe_event->event_status ) {
return 'CANCELLED'; // this will be reflected in the cancelled reason
return 'CANCELLED'; // This will be reflected in the cancelled reason.
}
if ( '' === $this->tribe_event->event_status ) {
return 'CONFIRMED';
@ -142,12 +88,12 @@ class Tribe extends Event_Transformer {
protected function get_content() {
$content = parent::get_content();
// todo remove link at the end of the content
// TODO: remove link at the end of the content.
// todo add organizer
// $this->tribe_event->organizers[0]
// TODO: add organizer
// $this->tribe_event->organizers[0].
// todo add Canclled reason in the content (maybe at the end)
// TODO: do add Cancelled reason in the content (maybe at the end).
return $content;
}
@ -155,11 +101,12 @@ class Tribe extends Event_Transformer {
/**
* Get the event location.
*
* @param int $post_id The WordPress post ID.
* @returns array The Place.
*/
public function get_event_location() {
/*
This is how the Tribe event looks like:
TODO: Remove this comment.
'post_title' => 'testvenue',
'post_name' => 'testvenue',
'guid' => 'http://localhost/venue/testvenue/',
@ -184,6 +131,6 @@ class Tribe extends Event_Transformer {
$venue->address . "\n" .
$venue->zip . ', ' . $venue->city . "\n" .
$venue->country
); // todo add checks that everything exists here (lol)
); // TODO: add checks that everything exists here.
}
}

View file

@ -53,7 +53,7 @@ class Admin_Notices {
// Get the current page.
$screen = get_current_screen();
// Check if we are on a edit page for the event, or on the settings page of the event plugin.
$is_event_plugins_edit_page = 'edit' === $screen->base && $this->event_plugin['post_type'] === $screen->event_post_type;
$is_event_plugins_edit_page = 'edit' === $screen->base && $this->event_plugin['post_type'] === $screen->post_type;
$is_event_plugins_settings_page = $this->event_plugin['settings_page_id'] === $screen->id;
if ( $is_event_plugins_edit_page || $is_event_plugins_settings_page ) {

View file

@ -40,7 +40,7 @@ class Setup {
'the_events_calendar' => array(
'plugin_file' => 'the-events-calendar/the-events-calendar.php',
'post_type' => 'tribe_events',
'transformer_class' => 'Tribe_Events',
'transformer_class' => 'Tribe',
'settings_page_id' => 'tribe_general',
),
'vsel' => array(