From d229529acc76f0ea1982de042cabb07fe15a1679 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Menrath?= Date: Thu, 10 Oct 2024 15:31:24 +0200 Subject: [PATCH] Add transormer for WP Event Manager, closes #54 (#55) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Reviewed-on: https://code.event-federation.eu/Event-Federation/wordpress-activitypub-event-bridge/pulls/55 Co-authored-by: André Menrath Co-committed-by: André Menrath --- .forgejo/workflows/phpunit.yml | 7 +- bin/install-wp-tests.sh | 1 + composer.json | 8 +- .../transformer/class-events-manager.php | 1 - .../transformer/class-gatherpress.php | 1 - .../transformer/class-the-events-calendar.php | 1 - .../transformer/class-vs-event-list.php | 1 - .../transformer/class-wp-event-manager.php | 140 ++++++++++++++ includes/class-setup.php | 1 + includes/plugins/class-wp-event-manager.php | 72 +++++++ tests/bootstrap.php | 3 + tests/test-class-plugin-vs-event-list.php | 10 +- tests/test-class-plugin-wp-event-manager.php | 182 ++++++++++++++++++ 13 files changed, 415 insertions(+), 13 deletions(-) create mode 100644 includes/activitypub/transformer/class-wp-event-manager.php create mode 100644 includes/plugins/class-wp-event-manager.php create mode 100644 tests/test-class-plugin-wp-event-manager.php diff --git a/.forgejo/workflows/phpunit.yml b/.forgejo/workflows/phpunit.yml index 5bdae8d..b67d8e9 100644 --- a/.forgejo/workflows/phpunit.yml +++ b/.forgejo/workflows/phpunit.yml @@ -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 }} \ No newline at end of file diff --git a/bin/install-wp-tests.sh b/bin/install-wp-tests.sh index 8d495bf..0e0e4ad 100755 --- a/bin/install-wp-tests.sh +++ b/bin/install-wp-tests.sh @@ -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 diff --git a/composer.json b/composer.json index 44b5406..1b3bb36 100644 --- a/composer.json +++ b/composer.json @@ -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" } } diff --git a/includes/activitypub/transformer/class-events-manager.php b/includes/activitypub/transformer/class-events-manager.php index 3acf6aa..1def3f7 100644 --- a/includes/activitypub/transformer/class-events-manager.php +++ b/includes/activitypub/transformer/class-events-manager.php @@ -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; diff --git a/includes/activitypub/transformer/class-gatherpress.php b/includes/activitypub/transformer/class-gatherpress.php index e3ec013..b67bc58 100644 --- a/includes/activitypub/transformer/class-gatherpress.php +++ b/includes/activitypub/transformer/class-gatherpress.php @@ -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; diff --git a/includes/activitypub/transformer/class-the-events-calendar.php b/includes/activitypub/transformer/class-the-events-calendar.php index 72e25c8..9d76aaa 100644 --- a/includes/activitypub/transformer/class-the-events-calendar.php +++ b/includes/activitypub/transformer/class-the-events-calendar.php @@ -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; diff --git a/includes/activitypub/transformer/class-vs-event-list.php b/includes/activitypub/transformer/class-vs-event-list.php index 35d33a2..c476404 100644 --- a/includes/activitypub/transformer/class-vs-event-list.php +++ b/includes/activitypub/transformer/class-vs-event-list.php @@ -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; diff --git a/includes/activitypub/transformer/class-wp-event-manager.php b/includes/activitypub/transformer/class-wp-event-manager.php new file mode 100644 index 0000000..6330152 --- /dev/null +++ b/includes/activitypub/transformer/class-wp-event-manager.php @@ -0,0 +1,140 @@ +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; + } +} diff --git a/includes/class-setup.php b/includes/class-setup.php index 39a4766..bafe050 100644 --- a/includes/class-setup.php +++ b/includes/class-setup.php @@ -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', ); /** diff --git a/includes/plugins/class-wp-event-manager.php b/includes/plugins/class-wp-event-manager.php new file mode 100644 index 0000000..e3d3493 --- /dev/null +++ b/includes/plugins/class-wp-event-manager.php @@ -0,0 +1,72 @@ + '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' ), diff --git a/tests/test-class-plugin-wp-event-manager.php b/tests/test-class-plugin-wp-event-manager.php new file mode 100644 index 0000000..318a295 --- /dev/null +++ b/tests/test-class-plugin-wp-event-manager.php @@ -0,0 +1,182 @@ +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'] ); + } +}