Add transormer for WP Event Manager, closes #54 (#55)
All checks were successful
PHP Code Checker / PHP Code Checker (push) Successful in 44s
PHPUnit / PHPUnit – PHP 8.1 (push) Successful in 1m13s
PHPUnit / PHPUnit – PHP 8.2 (push) Successful in 1m9s
PHPUnit / PHPUnit – PHP 8.3 (push) Successful in 1m6s

Reviewed-on: #55
Co-authored-by: André Menrath <andre.menrath@posteo.de>
Co-committed-by: André Menrath <andre.menrath@posteo.de>
This commit is contained in:
André Menrath 2024-10-10 15:31:24 +02:00 committed by André Menrath
parent 0fba291706
commit d229529acc
13 changed files with 415 additions and 13 deletions

View file

@ -37,7 +37,7 @@ jobs:
path: |
${{ env.WP_CORE_DIR }}
${{ env.WP_TESTS_DIR }}
key: cache-wordpress-4
key: cache-wordpress-5
- name: Cache Composer
id: cache-composer-phpunit
@ -91,5 +91,10 @@ jobs:
- name: Run Integration tests for Events Manager
run: cd /workspace/Event-Federation/wordpress-activitypub-event-bridge/ && ./vendor/bin/phpunit --filter=events_manager
env:
PHP_VERSION: ${{ matrix.php-version }}
- name: Run Integration tests for WP Event Manager
run: cd /workspace/Event-Federation/wordpress-activitypub-event-bridge/ && ./vendor/bin/phpunit --filter=wp_event_manager
env:
PHP_VERSION: ${{ matrix.php-version }}

View file

@ -238,6 +238,7 @@ install_wp_plugins() {
install_wp_plugin very-simple-event-list
install_wp_plugin gatherpress
install_wp_plugin events-manager
install_wp_plugin wp-event-manager
}
install_wp

View file

@ -50,15 +50,17 @@
"@test-vs-event-list",
"@test-the-events-calendar",
"@test-gatherpress",
"@test-events-manager"
"@test-events-manager",
"@test-wp-event-manager"
],
"test-debug": [
"@prepare-test",
"@test-gatherpress"
"@test-wp-event-manager"
],
"test-vs-event-list": "phpunit --filter=vs_event_list",
"test-the-events-calendar": "phpunit --filter=the_events_calendar",
"test-gatherpress": "phpunit --filter=gatherpress",
"test-events-manager": "phpunit --filter=events_manager"
"test-events-manager": "phpunit --filter=events_manager",
"test-wp-event-manager": "phpunit --filter=wp_event_manager"
}
}

View file

@ -11,7 +11,6 @@ namespace ActivityPub_Event_Bridge\Activitypub\Transformer;
// Exit if accessed directly.
defined( 'ABSPATH' ) || exit; // @codeCoverageIgnore
use Activitypub\Activity\Extended_Object\Event;
use Activitypub\Activity\Extended_Object\Place;
use ActivityPub_Event_Bridge\Activitypub\Transformer\Event as Event_Transformer;
use DateTime;

View file

@ -13,7 +13,6 @@ defined( 'ABSPATH' ) || exit; // @codeCoverageIgnore
use Activitypub\Activity\Extended_Object\Event as Event_Object;
use Activitypub\Activity\Extended_Object\Place;
use Activitypub\Model\Blog;
use ActivityPub_Event_Bridge\Activitypub\Transformer\Event;
use GatherPress\Core\Event as GatherPress_Event;

View file

@ -11,7 +11,6 @@ namespace ActivityPub_Event_Bridge\Activitypub\Transformer;
// Exit if accessed directly.
defined( 'ABSPATH' ) || exit; // @codeCoverageIgnore
use Activitypub\Activity\Extended_Object\Event as Event_Object;
use Activitypub\Activity\Extended_Object\Place;
use ActivityPub_Event_Bridge\Activitypub\Transformer\Event;
use WP_Post;

View file

@ -11,7 +11,6 @@ namespace ActivityPub_Event_Bridge\Activitypub\Transformer;
// Exit if accessed directly.
defined( 'ABSPATH' ) || exit; // @codeCoverageIgnore
use Activitypub\Activity\Extended_Object\Event;
use Activitypub\Activity\Extended_Object\Place;
use ActivityPub_Event_Bridge\Activitypub\Transformer\Event as Event_Transformer;

View file

@ -0,0 +1,140 @@
<?php
/**
* ActivityPub Transformer for the plugin Very Simple Event List.
*
* @package ActivityPub_Event_Bridge
* @license AGPL-3.0-or-later
*/
namespace ActivityPub_Event_Bridge\Activitypub\Transformer;
// Exit if accessed directly.
defined( 'ABSPATH' ) || exit; // @codeCoverageIgnore
use Activitypub\Activity\Extended_Object\Place;
use ActivityPub_Event_Bridge\Activitypub\Transformer\Event as Event_Transformer;
use DateTime;
/**
* ActivityPub Transformer for events from the WordPress plugin 'Events Manager'
*
* @see https://wordpress.org/plugins/events-manager/
*
* @since 1.0.0
*/
final class WP_Event_Manager extends Event_Transformer {
/**
* Returns whether the even is online
*
* @return bool
*/
protected function get_is_online() {
$is_online_text = get_post_meta( $this->wp_object->ID, '_event_online', true );
$is_online = false;
// Radio buttons.
if ( 'yes' === $is_online_text ) {
$is_online = true;
}
// Checkbox.
if ( '1' === $is_online_text ) {
$is_online = true;
}
return $is_online;
}
/**
* Get the event location.
*
* @return array The Place.
*/
public function get_location(): ?Place {
$location_name = get_post_meta( $this->wp_object->ID, '_event_location', true );
if ( $location_name ) {
$location = new Place();
$location->set_name( $location_name );
$location->set_sensitive( null );
$location->set_address( $location_name );
return $location;
}
return null;
}
/**
* Get the end time from the events metadata.
*
* @return ?string The events end-datetime if is set, null otherwise.
*/
public function get_end_time(): ?string {
$end_date = get_post_meta( $this->wp_object->ID, '_event_end_date', true );
if ( $end_date ) {
$end_datetime = new DateTime( $end_date );
return \gmdate( 'Y-m-d\TH:i:s\Z', $end_datetime->getTimestamp() );
}
return null;
}
/**
* Get the end time from the events metadata.
*/
public function get_start_time(): string {
$start_date = get_post_meta( $this->wp_object->ID, '_event_start_date', true );
if ( ! is_numeric( $start_date ) ) {
$start_datetime = new DateTime( $start_date );
$start_timestamp = $start_datetime->getTimestamp();
} else {
$start_timestamp = (int) $start_date;
}
return \gmdate( 'Y-m-d\TH:i:s\Z', $start_timestamp );
}
/**
* Get the event link as an ActivityPub Link object, but as an associative array.
*
* @return ?array
*/
private function get_event_link_attachment(): ?array {
$event_link_url = get_post_meta( $this->wp_object->ID, '_event_video_url', true );
if ( str_starts_with( $event_link_url, 'http' ) ) {
return array(
'type' => 'Link',
'name' => \esc_html__( 'Video URL', 'activitypub-event-bridge' ),
'href' => \esc_url( $event_link_url ),
'mediaType' => 'text/html',
);
} else {
return null;
}
}
/**
* Overrides/extends the get_attachments function to also add the event Link.
*/
protected function get_attachment() {
// Get attachments via parent function.
$attachments = parent::get_attachment();
// The first attachment is the featured image, make sure it is compatible with Mobilizon.
if ( count( $attachments ) ) {
$attachments[0]['type'] = 'Document';
$attachments[0]['name'] = 'Banner';
}
if ( $this->get_event_link_attachment() ) {
$attachments[] = $this->get_event_link_attachment();
}
return $attachments;
}
/**
* Get the events title/name.
*
* @return string
*/
protected function get_name(): string {
return $this->wp_object->post_title;
}
}

View file

@ -128,6 +128,7 @@ class Setup {
'\ActivityPub_Event_Bridge\Plugins\GatherPress',
'\ActivityPub_Event_Bridge\Plugins\The_Events_Calendar',
'\ActivityPub_Event_Bridge\Plugins\VS_Event_List',
'\ActivityPub_Event_Bridge\Plugins\WP_Event_Manager',
);
/**

View file

@ -0,0 +1,72 @@
<?php
/**
* WP Event Manager.
*
* Defines all the necessary meta information for the WordPress event plugin
* "WP Event Manager"
*
* @link https://de.wordpress.org/plugins/wp-event-manager
* @package ActivityPub_Event_Bridge
* @since 1.0.0
*/
namespace ActivityPub_Event_Bridge\Plugins;
use ActivityPub_Event_Bridge\Plugins\Event_Plugin;
// Exit if accessed directly.
defined( 'ABSPATH' ) || exit; // @codeCoverageIgnore
/**
* Interface for a supported event plugin.
*
* This interface defines which information is necessary for a supported event plugin.
*
* @since 1.0.0
*/
final class WP_Event_Manager extends Event_Plugin {
/**
* Returns the full plugin file.
*
* @return string
*/
public static function get_plugin_file(): string {
return 'wp-event-manager/wp-event-manager.php';
}
/**
* Returns the event post type of the plugin.
*
* @return string
*/
public static function get_post_type(): string {
return 'event_listing';
}
/**
* Returns the ID of the main settings page of the plugin.
*
* @return string The settings page url.
*/
public static function get_settings_page(): string {
return 'event-manager-settings';
}
/**
* Returns the ActivityPub transformer class.
*
* @return string
*/
public static function get_activitypub_transformer_class_name(): string {
return 'WP_Event_Manager';
}
/**
* Returns the taxonomy used for the plugin's event categories.
*
* @return string
*/
public static function get_event_category_taxonomy(): string {
return 'event_listing_category';
}
}

View file

@ -58,6 +58,9 @@ function _manually_load_plugin() {
case 'gatherpress':
$plugin_file = 'gatherpress/gatherpress.php';
break;
case 'wp_event_manager':
$plugin_file = 'wp-event-manager/wp-event-manager.php';
break;
}
if ( $plugin_file ) {

View file

@ -44,7 +44,7 @@ class Test_VS_Event_List extends WP_UnitTestCase {
$wp_post_id = wp_insert_post(
array(
'post_title' => 'VSEL Test Event',
'post_status' => 'published',
'post_status' => 'publish',
'post_type' => 'event',
'meta_input' => array(
'event-start-date' => strtotime( '+10 days 15:00:00' ),
@ -69,7 +69,7 @@ class Test_VS_Event_List extends WP_UnitTestCase {
$wp_post_id = wp_insert_post(
array(
'post_title' => 'VSEL Test Event',
'post_status' => 'published',
'post_status' => 'publish',
'post_type' => 'event',
'meta_input' => array(
'event-start-date' => strtotime( '+10 days 15:00:00' ),
@ -102,7 +102,7 @@ class Test_VS_Event_List extends WP_UnitTestCase {
$wp_post_id = wp_insert_post(
array(
'post_title' => 'VSEL Test Event',
'post_status' => 'published',
'post_status' => 'publish',
'post_type' => 'event',
'meta_input' => array(
'event-start-date' => strtotime( '+10 days 15:00:00' ),
@ -147,7 +147,7 @@ class Test_VS_Event_List extends WP_UnitTestCase {
$wp_post_id = wp_insert_post(
array(
'post_title' => 'VSEL Test Event',
'post_status' => 'published',
'post_status' => 'publish',
'post_type' => 'event',
'meta_input' => array(
'event-start-date' => strtotime( '+10 days 15:00:00' ),
@ -182,7 +182,7 @@ class Test_VS_Event_List extends WP_UnitTestCase {
$wp_post_id = wp_insert_post(
array(
'post_title' => 'VSEL Test Event',
'post_status' => 'published',
'post_status' => 'publish',
'post_type' => 'event',
'meta_input' => array(
'event-start-date' => strtotime( '+10 days 15:00:00' ),

View file

@ -0,0 +1,182 @@
<?php
/**
* Class SampleTest
*
* @package ActivityPub_Event_Bridge
*/
/**
* Sample test case.
*/
class Test_WP_Event_Manager extends WP_UnitTestCase {
/**
* Override the setup function, so that tests don't run if the Events Calendar is not active.
*/
public function set_up() {
parent::set_up();
if ( ! function_exists( 'wp_event_manager_notify_new_user' ) ) {
self::markTestSkipped( 'WP Event Manager plugin is not active.' );
}
// Make sure that ActivityPub support is enabled for The Events Calendar.
$aec = \ActivityPub_Event_Bridge\Setup::get_instance();
$aec->activate_activitypub_support_for_active_event_plugins();
// Delete all posts afterwards.
_delete_all_posts();
}
/**
* Test that the right transformer gets applied.
*/
public function test_transformer_class() {
// We only test for one event plugin being active at the same time,
// even though we support multiple onces in theory.
// But testing all combinations is beyond scope.
$active_event_plugins = \ActivityPub_Event_Bridge\Setup::get_instance()->get_active_event_plugins();
$this->assertEquals( 1, count( $active_event_plugins ) );
// Enable ActivityPub support for the event plugin.
$this->assertContains( 'event_listing', get_option( 'activitypub_support_post_types' ) );
// Insert a new Event.
$wp_post_id = wp_insert_post(
array(
'post_title' => 'WP Event Manager TestEvent',
'post_status' => 'publish',
'post_type' => 'event_listing',
'meta_input' => array(
'event-start-date' => strtotime( '+10 days 15:00:00' ),
),
)
);
$wp_object = get_post( $wp_post_id );
// Call the transformer Factory.
$transformer = \Activitypub\Transformer\Factory::get_transformer( $wp_object );
// Check that we got the right transformer.
$this->assertInstanceOf( \ActivityPub_Event_Bridge\Activitypub\Transformer\WP_Event_Manager::class, $transformer );
}
/**
* Test the transformation to ActivityStreams of minimal event.
*/
public function test_transform_of_minimal_event() {
// Insert a new Event.
$wp_post_id = wp_insert_post(
array(
'post_title' => 'WP Event Manager TestEvent',
'post_status' => 'publish',
'post_type' => 'event_listing',
'post_content' => 'Come to my WP Event Manager event!',
'meta_input' => array(
'_event_start_date' => strtotime( '+10 days 15:00:00' ),
),
)
);
// Transform the event to ActivityStreams.
$event_array = \Activitypub\Transformer\Factory::get_transformer( get_post( $wp_post_id ) )->to_object()->to_array();
// Check that the event ActivityStreams representation contains everything as expected.
$this->assertEquals( 'Event', $event_array['type'] );
$this->assertEquals( 'WP Event Manager TestEvent', $event_array['name'] );
$this->assertEquals( 'Come to my WP Event Manager event!', wp_strip_all_tags( $event_array['content'] ) );
$this->assertEquals( gmdate( 'Y-m-d', strtotime( '+10 days 15:00:00' ) ) . 'T15:00:00Z', $event_array['startTime'] );
$this->assertArrayNotHasKey( 'endTime', $event_array );
$this->assertEquals( comments_open( $wp_post_id ), $event_array['commentsEnabled'] );
$this->assertEquals( comments_open( $wp_post_id ) ? 'allow_all' : 'closed', $event_array['repliesModerationOption'] );
$this->assertEquals( 'external', $event_array['joinMode'] );
$this->assertEquals( esc_url( get_permalink( $wp_post_id ) ), $event_array['externalParticipationUrl'] );
$this->assertArrayNotHasKey( 'location', $event_array );
$this->assertEquals( 'MEETING', $event_array['category'] );
}
/**
* Test the transformation to ActivityStreams of minimal event.
*/
public function test_transform_of_full_online_event() {
// Insert a new Event.
$wp_post_id = wp_insert_post(
array(
'post_title' => 'WP Event Manager TestEvent',
'post_status' => 'publish',
'post_type' => 'event_listing',
'post_content' => 'Come to my WP Event Manager event!',
'meta_input' => array(
'_event_start_date' => \gmdate( 'Y-m-d H:i:s', strtotime( '+10 days 15:00:00' ) ),
'_event_end_date' => \gmdate( 'Y-m-d H:i:s', strtotime( '+10 days 16:00:00' ) ),
'_event_video_url' => 'https://event-federation.eu/meeting-room',
'_event_online' => 'yes',
),
)
);
// Transform the event to ActivityStreams.
$event_array = \Activitypub\Transformer\Factory::get_transformer( get_post( $wp_post_id ) )->to_object()->to_array();
// Check that the event ActivityStreams representation contains everything as expected.
$this->assertEquals( 'Event', $event_array['type'] );
$this->assertEquals( 'WP Event Manager TestEvent', $event_array['name'] );
$this->assertEquals( 'Come to my WP Event Manager event!', wp_strip_all_tags( $event_array['content'] ) );
$this->assertEquals( gmdate( 'Y-m-d', strtotime( '+10 days 15:00:00' ) ) . 'T15:00:00Z', $event_array['startTime'] );
$this->assertEquals( gmdate( 'Y-m-d', strtotime( '+10 days 15:00:00' ) ) . 'T16:00:00Z', $event_array['endTime'] );
$this->assertEquals( comments_open( $wp_post_id ), $event_array['commentsEnabled'] );
$this->assertEquals( comments_open( $wp_post_id ) ? 'allow_all' : 'closed', $event_array['repliesModerationOption'] );
$this->assertEquals( 'external', $event_array['joinMode'] );
$this->assertEquals( true, $event_array['isOnline'] );
$this->assertEquals( esc_url( get_permalink( $wp_post_id ) ), $event_array['externalParticipationUrl'] );
$this->assertArrayNotHasKey( 'location', $event_array );
$this->assertEquals( 'MEETING', $event_array['category'] );
$this->assertContains(
array(
'type' => 'Link',
'name' => __( 'Video URL', 'activitypub-event-bridge' ),
'href' => 'https://event-federation.eu/meeting-room',
'mediaType' => 'text/html',
),
$event_array['attachment']
);
}
/**
* Test the transformation to ActivityStreams of minimal event.
*/
public function test_transform_of_event_with_location() {
// Insert a new Event.
$wp_post_id = wp_insert_post(
array(
'post_title' => 'WP Event Manager TestEvent',
'post_status' => 'publish',
'post_type' => 'event_listing',
'post_content' => 'Come to my WP Event Manager event!',
'meta_input' => array(
'_event_start_date' => \gmdate( 'Y-m-d H:i:s', strtotime( '+10 days 15:00:00' ) ),
'_event_end_date' => \gmdate( 'Y-m-d H:i:s', strtotime( '+10 days 16:00:00' ) ),
'_event_location' => 'Some text location',
'_event_online' => 'no',
),
)
);
// Transform the event to ActivityStreams.
$event_array = \Activitypub\Transformer\Factory::get_transformer( get_post( $wp_post_id ) )->to_object()->to_array();
// Check that the event ActivityStreams representation contains everything as expected.
$this->assertEquals( 'Event', $event_array['type'] );
$this->assertEquals( 'WP Event Manager TestEvent', $event_array['name'] );
$this->assertEquals( 'Come to my WP Event Manager event!', wp_strip_all_tags( $event_array['content'] ) );
$this->assertEquals( gmdate( 'Y-m-d', strtotime( '+10 days 15:00:00' ) ) . 'T15:00:00Z', $event_array['startTime'] );
$this->assertEquals( gmdate( 'Y-m-d', strtotime( '+10 days 15:00:00' ) ) . 'T16:00:00Z', $event_array['endTime'] );
$this->assertEquals( comments_open( $wp_post_id ), $event_array['commentsEnabled'] );
$this->assertEquals( comments_open( $wp_post_id ) ? 'allow_all' : 'closed', $event_array['repliesModerationOption'] );
$this->assertEquals( 'external', $event_array['joinMode'] );
$this->assertEquals( false, $event_array['isOnline'] );
$this->assertEquals( esc_url( get_permalink( $wp_post_id ) ), $event_array['externalParticipationUrl'] );
$this->assertArrayHasKey( 'location', $event_array );
$this->assertEquals( 'Some text location', $event_array['location']['address'] );
}
}