From 579f0e5f57270be6ca8378e48f901425505b175a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Menrath?= Date: Fri, 1 Nov 2024 10:53:50 +0100 Subject: [PATCH] Prepare ActivityPub resolveable locations Use own transformers for `Place` --- composer.json | 2 +- .../transformer/{ => event}/class-event.php | 17 +-- .../transformer/{ => event}/class-eventin.php | 6 +- .../{ => event}/class-events-manager.php | 4 +- .../{ => event}/class-gatherpress.php | 4 +- .../class-modern-events-calendar-lite.php | 5 +- .../{ => event}/class-the-events-calendar.php | 44 +----- .../{ => event}/class-vs-event-list.php | 4 +- .../{ => event}/class-wp-event-manager.php | 4 +- .../location/class-the-events-calendar.php | 125 ++++++++++++++++++ includes/plugins/class-event-plugin.php | 2 +- tests/test-class-plugin-eventin.php | 2 +- tests/test-class-plugin-events-manger.php | 2 +- tests/test-class-plugin-gatherpress.php | 2 +- ...ass-plugin-modern-events-calendar-lite.php | 2 +- .../test-class-plugin-the-events-calendar.php | 2 +- tests/test-class-plugin-vs-event-list.php | 2 +- tests/test-class-plugin-wp-event-manager.php | 2 +- 18 files changed, 155 insertions(+), 76 deletions(-) rename includes/activitypub/transformer/{ => event}/class-event.php (96%) rename includes/activitypub/transformer/{ => event}/class-eventin.php (96%) rename includes/activitypub/transformer/{ => event}/class-events-manager.php (97%) rename includes/activitypub/transformer/{ => event}/class-gatherpress.php (96%) rename includes/activitypub/transformer/{ => event}/class-modern-events-calendar-lite.php (95%) rename includes/activitypub/transformer/{ => event}/class-the-events-calendar.php (78%) rename includes/activitypub/transformer/{ => event}/class-vs-event-list.php (95%) rename includes/activitypub/transformer/{ => event}/class-wp-event-manager.php (95%) create mode 100644 includes/activitypub/transformer/location/class-the-events-calendar.php diff --git a/composer.json b/composer.json index 4da8b48..f036b3d 100644 --- a/composer.json +++ b/composer.json @@ -58,7 +58,7 @@ ], "test-debug": [ "@prepare-test", - "@test-gatherpress" + "@test-the-events-calendar" ], "test-vs-event-list": "phpunit --filter=vs_event_list", "test-the-events-calendar": "phpunit --filter=the_events_calendar", diff --git a/includes/activitypub/transformer/class-event.php b/includes/activitypub/transformer/event/class-event.php similarity index 96% rename from includes/activitypub/transformer/class-event.php rename to includes/activitypub/transformer/event/class-event.php index babf2b9..a71a08b 100644 --- a/includes/activitypub/transformer/class-event.php +++ b/includes/activitypub/transformer/event/class-event.php @@ -6,7 +6,7 @@ * @license AGPL-3.0-or-later */ -namespace ActivityPub_Event_Bridge\Activitypub\Transformer; +namespace ActivityPub_Event_Bridge\Activitypub\Transformer\Event; // Exit if accessed directly. defined( 'ABSPATH' ) || exit; // @codeCoverageIgnore @@ -64,17 +64,6 @@ abstract class Event extends Post { return '[ap_content]'; } - /** - * Returns the title of the event. - * - * @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-name - * - * @return string The name. - */ - protected function get_name(): string { - return $this->wp_object->post_title; - } - /** * Extend the construction of the Post Transformer to also set the according taxonomy of the event post type. * @@ -213,7 +202,7 @@ abstract class Event extends Post { return ''; } $address = $location->get_address(); - if ( ! $address ) { + if ( ! $address && is_string( $location->get_name() ) ) { return $location->get_name(); } if ( is_string( $address ) ) { @@ -370,7 +359,7 @@ abstract class Event extends Post { $activitypub_object->set_to( array( 'https://www.w3.org/ns/activitystreams#Public', - $this->get_actor_object()->get_followers(), // this fails on my machine. + $this->get_actor_object()->get_followers(), ) ); diff --git a/includes/activitypub/transformer/class-eventin.php b/includes/activitypub/transformer/event/class-eventin.php similarity index 96% rename from includes/activitypub/transformer/class-eventin.php rename to includes/activitypub/transformer/event/class-eventin.php index b47d93e..d201567 100644 --- a/includes/activitypub/transformer/class-eventin.php +++ b/includes/activitypub/transformer/event/class-eventin.php @@ -8,15 +8,13 @@ * @license AGPL-3.0-or-later */ -namespace ActivityPub_Event_Bridge\Activitypub\Transformer; +namespace ActivityPub_Event_Bridge\Activitypub\Transformer\Event; // Exit if accessed directly. defined( 'ABSPATH' ) || exit; // @codeCoverageIgnore use Activitypub\Activity\Extended_Object\Place; -use ActivityPub_Event_Bridge\Activitypub\Transformer\Event; -use DateTime; -use DateTimeZone; +use ActivityPub_Event_Bridge\Activitypub\Transformer\Event\Event; use Etn\Core\Event\Event_Model; use function Activitypub\esc_hashtag; diff --git a/includes/activitypub/transformer/class-events-manager.php b/includes/activitypub/transformer/event/class-events-manager.php similarity index 97% rename from includes/activitypub/transformer/class-events-manager.php rename to includes/activitypub/transformer/event/class-events-manager.php index 1def3f7..97d74f3 100644 --- a/includes/activitypub/transformer/class-events-manager.php +++ b/includes/activitypub/transformer/event/class-events-manager.php @@ -6,13 +6,13 @@ * @license AGPL-3.0-or-later */ -namespace ActivityPub_Event_Bridge\Activitypub\Transformer; +namespace ActivityPub_Event_Bridge\Activitypub\Transformer\Event; // 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 ActivityPub_Event_Bridge\Activitypub\Transformer\Event\Event as Event_Transformer; use DateTime; use DateTimeZone; use EM_Event; diff --git a/includes/activitypub/transformer/class-gatherpress.php b/includes/activitypub/transformer/event/class-gatherpress.php similarity index 96% rename from includes/activitypub/transformer/class-gatherpress.php rename to includes/activitypub/transformer/event/class-gatherpress.php index 756524a..bc88405 100644 --- a/includes/activitypub/transformer/class-gatherpress.php +++ b/includes/activitypub/transformer/event/class-gatherpress.php @@ -6,14 +6,14 @@ * @license AGPL-3.0-or-later */ -namespace ActivityPub_Event_Bridge\Activitypub\Transformer; +namespace ActivityPub_Event_Bridge\Activitypub\Transformer\Event; // 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 ActivityPub_Event_Bridge\Activitypub\Transformer\Event\Event; use GatherPress\Core\Event as GatherPress_Event; /** diff --git a/includes/activitypub/transformer/class-modern-events-calendar-lite.php b/includes/activitypub/transformer/event/class-modern-events-calendar-lite.php similarity index 95% rename from includes/activitypub/transformer/class-modern-events-calendar-lite.php rename to includes/activitypub/transformer/event/class-modern-events-calendar-lite.php index 8622de4..d3ea686 100644 --- a/includes/activitypub/transformer/class-modern-events-calendar-lite.php +++ b/includes/activitypub/transformer/event/class-modern-events-calendar-lite.php @@ -6,14 +6,13 @@ * @license AGPL-3.0-or-later */ -namespace ActivityPub_Event_Bridge\Activitypub\Transformer; +namespace ActivityPub_Event_Bridge\Activitypub\Transformer\Event; // Exit if accessed directly. defined( 'ABSPATH' ) || exit; // @codeCoverageIgnore use Activitypub\Activity\Extended_Object\Place; -use ActivityPub_Event_Bridge\Activitypub\Transformer\Event; - +use ActivityPub_Event_Bridge\Activitypub\Transformer\Event\Event; use MEC; use MEC\Events\Event as MEC_Event; use MEC_main; diff --git a/includes/activitypub/transformer/class-the-events-calendar.php b/includes/activitypub/transformer/event/class-the-events-calendar.php similarity index 78% rename from includes/activitypub/transformer/class-the-events-calendar.php rename to includes/activitypub/transformer/event/class-the-events-calendar.php index f50fa29..16a5726 100644 --- a/includes/activitypub/transformer/class-the-events-calendar.php +++ b/includes/activitypub/transformer/event/class-the-events-calendar.php @@ -6,13 +6,14 @@ * @license AGPL-3.0-or-later */ -namespace ActivityPub_Event_Bridge\Activitypub\Transformer; +namespace ActivityPub_Event_Bridge\Activitypub\Transformer\Event; // Exit if accessed directly. defined( 'ABSPATH' ) || exit; // @codeCoverageIgnore use Activitypub\Activity\Extended_Object\Place; -use ActivityPub_Event_Bridge\Activitypub\Transformer\Event; +use ActivityPub_Event_Bridge\Activitypub\Transformer\Event\Event; +use ActivityPub_Event_Bridge\Activitypub\Transformer\Location\The_Events_Calendar as The_Events_Calendar_Location; use WP_Post; use function Activitypub\esc_hashtag; @@ -140,42 +141,9 @@ final class The_Events_Calendar extends Event { return null; } - // Set the address. - $address = array(); - - if ( ! empty( $venue->country ) ) { - $address['addressCountry'] = $venue->country; - } - - if ( ! empty( $venue->city ) ) { - $address['addressLocality'] = $venue->city; - } - - if ( ! empty( $venue->province ) ) { - $address['addressRegion'] = $venue->province; - } - - if ( ! empty( $venue->zip ) ) { - $address['postalCode'] = $venue->zip; - } - - if ( ! empty( $venue->address ) ) { - $address['streetAddress'] = $venue->address; - } - if ( ! empty( $venue->post_title ) ) { - $address['name'] = $venue->post_title; - } - $address['type'] = 'PostalAddress'; - - $location = new Place(); - if ( count( $address ) > 1 ) { - $location->set_address( $address ); - } else { - $location->set_address( $venue->post_title ); - } - $location->set_id( $venue->ID ); - $location->set_name( $venue->post_title ); - + $location_transformer = new The_Events_Calendar_Location( $venue ); + $full_location_object = false; + $location = $location_transformer->to_object( $full_location_object ); return $location; } diff --git a/includes/activitypub/transformer/class-vs-event-list.php b/includes/activitypub/transformer/event/class-vs-event-list.php similarity index 95% rename from includes/activitypub/transformer/class-vs-event-list.php rename to includes/activitypub/transformer/event/class-vs-event-list.php index c476404..4dcd16c 100644 --- a/includes/activitypub/transformer/class-vs-event-list.php +++ b/includes/activitypub/transformer/event/class-vs-event-list.php @@ -6,13 +6,13 @@ * @license AGPL-3.0-or-later */ -namespace ActivityPub_Event_Bridge\Activitypub\Transformer; +namespace ActivityPub_Event_Bridge\Activitypub\Transformer\Event; // 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 ActivityPub_Event_Bridge\Activitypub\Transformer\Event\Event as Event_Transformer; /** * ActivityPub Transformer for VS Event. diff --git a/includes/activitypub/transformer/class-wp-event-manager.php b/includes/activitypub/transformer/event/class-wp-event-manager.php similarity index 95% rename from includes/activitypub/transformer/class-wp-event-manager.php rename to includes/activitypub/transformer/event/class-wp-event-manager.php index 6330152..d308cf4 100644 --- a/includes/activitypub/transformer/class-wp-event-manager.php +++ b/includes/activitypub/transformer/event/class-wp-event-manager.php @@ -6,13 +6,13 @@ * @license AGPL-3.0-or-later */ -namespace ActivityPub_Event_Bridge\Activitypub\Transformer; +namespace ActivityPub_Event_Bridge\Activitypub\Transformer\Event; // 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 ActivityPub_Event_Bridge\Activitypub\Transformer\Event\Event as Event_Transformer; use DateTime; /** diff --git a/includes/activitypub/transformer/location/class-the-events-calendar.php b/includes/activitypub/transformer/location/class-the-events-calendar.php new file mode 100644 index 0000000..f40c286 --- /dev/null +++ b/includes/activitypub/transformer/location/class-the-events-calendar.php @@ -0,0 +1,125 @@ +wp_object->country ) ) { + $address['addressCountry'] = $this->wp_object->country; + } + + if ( ! empty( $this->wp_object->city ) ) { + $address['addressLocality'] = $this->wp_object->city; + } + + if ( ! empty( $this->wp_object->province ) ) { + $address['addressRegion'] = $this->wp_object->province; + } + + if ( ! empty( $this->wp_object->zip ) ) { + $address['postalCode'] = $this->wp_object->zip; + } + + if ( ! empty( $this->wp_object->address ) ) { + $address['streetAddress'] = $this->wp_object->address; + } + if ( ! empty( $this->wp_object->post_title ) ) { + $address['name'] = $this->wp_object->post_title; + } + $address['type'] = 'PostalAddress'; + + if ( count( $address ) > 1 ) { + return $address; + } else { + return $this->get_name(); + } + } + + /** + * Generic function that converts an WP-Event object to an ActivityPub-Event object. + * + * @param bool $full_object bool Return an object with all properties set, or a minimal one as used within an `as:Event`s location. + * @return Event_Object + */ + public function to_object( $full_object = true ): Place { + $activitypub_object = new Place(); + $activitypub_object = $this->transform_object_properties( $activitypub_object ); + + if ( ! empty( $activitypub_object->content ) ) { + $activitypub_object->set_content_map( + array( + $this->get_locale() => $this->get_content(), + ) + ); + } + + if ( $full_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_to( + array( + 'https://www.w3.org/ns/activitystreams#Public', + $this->get_actor_object()->get_followers(), + ) + ); + } + + $activitypub_object->set_address( $this->get_address() ); + + return $activitypub_object; + } +} diff --git a/includes/plugins/class-event-plugin.php b/includes/plugins/class-event-plugin.php index a5c5734..801836c 100644 --- a/includes/plugins/class-event-plugin.php +++ b/includes/plugins/class-event-plugin.php @@ -83,6 +83,6 @@ abstract class Event_Plugin { * Returns the Activitypub transformer for the event plugins event post type. */ public static function get_activitypub_event_transformer_class(): string { - return str_replace( 'Plugins', 'Activitypub\Transformer', static::class ); + return str_replace( 'Plugins', 'Activitypub\Transformer\Event', static::class ); } } diff --git a/tests/test-class-plugin-eventin.php b/tests/test-class-plugin-eventin.php index d844922..8d78a45 100644 --- a/tests/test-class-plugin-eventin.php +++ b/tests/test-class-plugin-eventin.php @@ -64,7 +64,7 @@ class Test_Eventin extends WP_UnitTestCase { $transformer = \Activitypub\Transformer\Factory::get_transformer( get_post( $event->id ) ); // Check that we got the right transformer. - $this->assertInstanceOf( \ActivityPub_Event_Bridge\Activitypub\Transformer\Eventin::class, $transformer ); + $this->assertInstanceOf( \ActivityPub_Event_Bridge\Activitypub\Transformer\Event\Eventin::class, $transformer ); } /** diff --git a/tests/test-class-plugin-events-manger.php b/tests/test-class-plugin-events-manger.php index 665b3c8..02e6363 100644 --- a/tests/test-class-plugin-events-manger.php +++ b/tests/test-class-plugin-events-manger.php @@ -61,7 +61,7 @@ class Test_Events_Manager extends WP_UnitTestCase { $transformer = \Activitypub\Transformer\Factory::get_transformer( $wp_object ); // Check that we got the right transformer. - $this->assertInstanceOf( \ActivityPub_Event_Bridge\Activitypub\Transformer\Events_Manager::class, $transformer ); + $this->assertInstanceOf( \ActivityPub_Event_Bridge\Activitypub\Transformer\Event\Events_Manager::class, $transformer ); } /** diff --git a/tests/test-class-plugin-gatherpress.php b/tests/test-class-plugin-gatherpress.php index 1fffa4b..ed4fc4e 100644 --- a/tests/test-class-plugin-gatherpress.php +++ b/tests/test-class-plugin-gatherpress.php @@ -65,7 +65,7 @@ class Test_GatherPress extends WP_UnitTestCase { $transformer = \Activitypub\Transformer\Factory::get_transformer( $event->event ); // Check that we got the right transformer. - $this->assertInstanceOf( \ActivityPub_Event_Bridge\Activitypub\Transformer\GatherPress::class, $transformer ); + $this->assertInstanceOf( \ActivityPub_Event_Bridge\Activitypub\Transformer\Event\GatherPress::class, $transformer ); } /** diff --git a/tests/test-class-plugin-modern-events-calendar-lite.php b/tests/test-class-plugin-modern-events-calendar-lite.php index 55114d9..2566ff7 100644 --- a/tests/test-class-plugin-modern-events-calendar-lite.php +++ b/tests/test-class-plugin-modern-events-calendar-lite.php @@ -74,7 +74,7 @@ class Test_Modern_Events_Calendar_Lite extends WP_UnitTestCase { $transformer = \Activitypub\Transformer\Factory::get_transformer( $wp_object ); // Check that we got the right transformer. - $this->assertInstanceOf( \ActivityPub_Event_Bridge\Activitypub\Transformer\Modern_Events_Calendar_Lite::class, $transformer ); + $this->assertInstanceOf( \ActivityPub_Event_Bridge\Activitypub\Transformer\Event\Modern_Events_Calendar_Lite::class, $transformer ); } /** diff --git a/tests/test-class-plugin-the-events-calendar.php b/tests/test-class-plugin-the-events-calendar.php index 8edad47..ebb3639 100644 --- a/tests/test-class-plugin-the-events-calendar.php +++ b/tests/test-class-plugin-the-events-calendar.php @@ -91,7 +91,7 @@ class Test_The_Events_Calendar extends WP_UnitTestCase { $transformer = \Activitypub\Transformer\Factory::get_transformer( $wp_object ); // Check that we got the right transformer. - $this->assertInstanceOf( \ActivityPub_Event_Bridge\Activitypub\Transformer\The_Events_Calendar::class, $transformer ); + $this->assertInstanceOf( \ActivityPub_Event_Bridge\Activitypub\Transformer\Event\The_Events_Calendar::class, $transformer ); } /** diff --git a/tests/test-class-plugin-vs-event-list.php b/tests/test-class-plugin-vs-event-list.php index d15e7ef..9f66e9c 100644 --- a/tests/test-class-plugin-vs-event-list.php +++ b/tests/test-class-plugin-vs-event-list.php @@ -58,7 +58,7 @@ class Test_VS_Event_List extends WP_UnitTestCase { $transformer = \Activitypub\Transformer\Factory::get_transformer( $wp_object ); // Check that we got the right transformer. - $this->assertInstanceOf( \ActivityPub_Event_Bridge\Activitypub\Transformer\VS_Event_List::class, $transformer ); + $this->assertInstanceOf( \ActivityPub_Event_Bridge\Activitypub\Transformer\Event\VS_Event_List::class, $transformer ); } /** diff --git a/tests/test-class-plugin-wp-event-manager.php b/tests/test-class-plugin-wp-event-manager.php index 318a295..b2f5eb4 100644 --- a/tests/test-class-plugin-wp-event-manager.php +++ b/tests/test-class-plugin-wp-event-manager.php @@ -58,7 +58,7 @@ class Test_WP_Event_Manager extends WP_UnitTestCase { $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 ); + $this->assertInstanceOf( \ActivityPub_Event_Bridge\Activitypub\Transformer\Event\WP_Event_Manager::class, $transformer ); } /**