From 214ee89fa0f84ce0681eb13ea475d9836a442b37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Menrath?= Date: Fri, 5 Jan 2024 21:18:34 +0100 Subject: [PATCH] prepare more modular admin notices --- activitypub-event-transformers.php | 55 ++---------- admin/class-admin-notices.php | 84 +++++++++++++++++++ includes/activitypub/handler/class-join.php | 0 .../transformer/class-events-manager.php | 62 ++++++++++++-- .../activitypub/transformer/class-tribe.php | 34 ++++---- .../transformer/class-vs-event.php | 2 +- 6 files changed, 164 insertions(+), 73 deletions(-) create mode 100644 admin/class-admin-notices.php create mode 100644 includes/activitypub/handler/class-join.php diff --git a/activitypub-event-transformers.php b/activitypub-event-transformers.php index 1f6f2bd..501dec5 100644 --- a/activitypub-event-transformers.php +++ b/activitypub-event-transformers.php @@ -25,7 +25,7 @@ if ( ! defined( 'ABSPATH' ) ) { add_filter( 'activitypub_transformer', function( $transformer, $wp_object, $object_class ) { - if ( 'WP_Post' != $object_class) { + if ( 'WP_Post' != $object_class ) { return $transformer; } @@ -33,67 +33,30 @@ add_filter( * VS Event List * @see https://wordpress.org/plugins/very-simple-event-list/ */ - // if ( $wp_object->post_type === 'event' ) { - // require_once __DIR__ . '/includes/activitypub/transformer/class-vs-event.php'; - // return new \VS_Event( $object ); - // } + if ( class_exists( 'vsel_widget' ) && $wp_object->post_type === 'event' ) { + require_once __DIR__ . '/includes/activitypub/transformer/class-vs-event.php'; + return new \VS_Event( $object ); + } /** * Events manager * @see https://wordpress.org/plugins/events-manager/ */ - if ( class_exists( 'EM_Events') && $wp_object->post_type === 'event' ) { + if ( class_exists( 'EM_Events' ) && $wp_object->post_type === 'event' ) { require_once __DIR__ . '/includes/activitypub/transformer/class-events-manager.php'; return new \Events_Manager( $wp_object ); } // Return the default transformer. + return $transformer; }, 10, 3 ); -/** - * Add admin notices for improved usability. - */ -function check_some_other_plugin() { - if ( is_plugin_active( 'activitypub/activitypub.php' ) ) { - if ( is_plugin_active( 'very-simple-event-list/vsel.php' ) ) { - add_action( 'admin_notices', 'vsel_admin_notices' ); - } - } -} - -add_action( 'admin_init', 'check_some_other_plugin' ); - -function vsel_admin_notices() { - $is_vsel_edit_page = isset( $_GET['post_type'] ) && $_GET['post_type'] === 'event'; - $is_vsel_settings_page = strpos( $_SERVER['REQUEST_URI'], '/wp-admin/options-general.php?page=vsel' ) !== false; - $is_vsel_page = $is_vsel_edit_page || $is_vsel_settings_page; - $vsel_post_type_is_activitypub_enabeld = in_array( 'event', get_option( 'activitypub_support_post_types' ) ); - if ( $is_vsel_page && ! $vsel_post_type_is_activitypub_enabeld ) { - $vsel_plugin_data = get_plugin_data( WP_PLUGIN_DIR . '/very-simple-event-list/vsel.php' ); - $activitypub_plugin_data = get_plugin_data( WP_PLUGIN_DIR . '/activitypub/activitypub.php' ); - $notice = sprintf( - _x( - 'You have installed the %1$s plugin, but the event post type of %2$s is not enabled in the %1$s settings.', - 'admin notice', - 'your-text-domain' - ), - $activitypub_plugin_data['Name'], - $vsel_plugin_data['Name'], - admin_url( 'options-general.php?page=activitypub&tab=settings' ) - ); - wp_admin_notice( - $notice, - array( - 'type' => 'warning', - 'dismissible' => true, - ) - ); - } -} +require_once __DIR__ . '/admin/class-admin-notices.php'; +new \Admin_Notices(); /** * Add a filter for http_request_host_is_external diff --git a/admin/class-admin-notices.php b/admin/class-admin-notices.php new file mode 100644 index 0000000..c96410a --- /dev/null +++ b/admin/class-admin-notices.php @@ -0,0 +1,84 @@ +%1$s settings.', + 'admin notice', + 'your-text-domain' + ), + $activitypub_plugin_data['Name'], + $vsel_plugin_data['Name'], + admin_url( 'options-general.php?page=activitypub&tab=settings' ) + ); + wp_admin_notice( + $notice, + array( + 'type' => 'warning', + 'dismissible' => true, + ) + ); + } +} diff --git a/includes/activitypub/handler/class-join.php b/includes/activitypub/handler/class-join.php new file mode 100644 index 0000000..e69de29 diff --git a/includes/activitypub/transformer/class-events-manager.php b/includes/activitypub/transformer/class-events-manager.php index 981cf3e..774185a 100644 --- a/includes/activitypub/transformer/class-events-manager.php +++ b/includes/activitypub/transformer/class-events-manager.php @@ -22,7 +22,7 @@ if ( ! defined( 'ABSPATH' ) ) { /** * ActivityPub Transformer for events from the WordPress plugin 'Events Manager' - * + * * @see https://wordpress.org/plugins/events-manager/ * * @since 1.0.0 @@ -30,7 +30,7 @@ if ( ! defined( 'ABSPATH' ) ) { class Events_Manager extends Post { /** * Holds the EM_Event object. - * + * * @var EM_Event */ protected $em_event; @@ -85,6 +85,10 @@ class Events_Manager extends Post { return 'Event'; } + protected function get_is_online() { + return 'url' === $this->em_event->event_location_type; + } + /** * Get the event location. * @@ -92,20 +96,34 @@ class Events_Manager extends Post { * @return array The Place. */ public function get_location() { + if ( 'url' === $this->em_event->event_location_type ) { + return null; + } + $location = new Place(); $em_location = $this->em_event->get_location(); $location->set_name( $em_location->location_name ); - $location->set_address( array( + + $address = array( 'type' => 'PostalAddress', 'addressCountry' => $em_location->location_country, 'addressLocality' => $em_location->location_town, 'streetAddress' => $em_location->location_address, 'name' => $em_location->location_name, - )); + ); + if ( $em_location->location_state ) { + $address['addressRegion'] = $em_location->location_state; + } + if ( $em_location->location_postcode ) { + $address['postalCode'] = $em_location->location_postcode; + } + + $location->set_address( $address ); return $location; } + /** * Get the end time from the events metadata. */ @@ -122,13 +140,13 @@ class Events_Manager extends Post { $timezone_string = $this->em_event->event_timezone; // Create a DateTime object with the given date, time, and timezone - $datetime = new DateTime($date_string . ' ' . $time_string, new DateTimeZone($timezone_string)); + $datetime = new DateTime( $date_string . ' ' . $time_string, new DateTimeZone( $timezone_string ) ); // Set the timezone for proper formatting - $datetime->setTimezone(new DateTimeZone('UTC')); + $datetime->setTimezone( new DateTimeZone( 'UTC' ) ); // Format the DateTime object as 'Y-m-d\TH:i:s\Z' - $formatted_date = $datetime->format('Y-m-d\TH:i:s\Z'); + $formatted_date = $datetime->format( 'Y-m-d\TH:i:s\Z' ); return $formatted_date; } @@ -149,7 +167,7 @@ class Events_Manager extends Post { $em_bookings = $this->em_event->get_bookings()->get_bookings(); return count( $em_bookings->bookings ); } - + protected function get_content() { return $this->wp_object->post_content; } @@ -168,16 +186,40 @@ class Events_Manager extends Post { return $summary; } + // protected function get_join_mode() { + // return 'free'; + // } + + private function get_event_link_attachment() { + $event_link_url = $this->em_event->event_location->data['url']; + $event_link_text = $this->em_event->event_location->data['text']; + return array( + 'type' => 'Link', + 'name' => 'Website', + // 'name' => $event_link_text, + 'href' => \esc_url( $event_link_url ), + 'mediaType' => 'text/html', + ); + } + /** * 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 ( 'url' === $this->em_event->event_location_type ) { + $attachments[] = $this->get_event_link_attachment(); + } + return $attachments; + return $attachments; } @@ -258,11 +300,13 @@ class Events_Manager extends Post { * @return Activitypub\Activity\Event */ public function to_object() { - $this->em_event = new EM_Event( $this->wp_object->ID, 'post_id'); + $this->em_event = new EM_Event( $this->wp_object->ID, 'post_id' ); $activtiypub_object = new Event(); $activtiypub_object = $this->transform_object_properties( $activtiypub_object ); + $activtiypub_object->set_external_participation_url( $this->get_url() ); + return $activtiypub_object; } } diff --git a/includes/activitypub/transformer/class-tribe.php b/includes/activitypub/transformer/class-tribe.php index 270201f..16e38c1 100644 --- a/includes/activitypub/transformer/class-tribe.php +++ b/includes/activitypub/transformer/class-tribe.php @@ -152,21 +152,21 @@ class Tribe extends Post { */ public function get_event_location() { /* - 'post_title' => 'testvenue', - 'post_name' => 'testvenue', - 'guid' => 'http://localhost/venue/testvenue/', - 'post_type' => 'tribe_venue', - 'address' => 'testaddr', - 'country' => 'Austria', - 'city' => 'testcity', - 'state_province' => 'testprovince', - 'state' => '', - 'province' => 'testprovince', - 'zip' => '8000', - 'phone' => '+4312343', - 'permalink' => 'http://localhost/venue/testvenue/', - 'directions_link' => 'https://maps.google.com/maps?f=q&source=s_q&hl=en&geocode=&q=testaddr+testcity+testprovince+8000+Austria', - 'website' => 'https://test.at', + 'post_title' => 'testvenue', + 'post_name' => 'testvenue', + 'guid' => 'http://localhost/venue/testvenue/', + 'post_type' => 'tribe_venue', + 'address' => 'testaddr', + 'country' => 'Austria', + 'city' => 'testcity', + 'state_province' => 'testprovince', + 'state' => '', + 'province' => 'testprovince', + 'zip' => '8000', + 'phone' => '+4312343', + 'permalink' => 'http://localhost/venue/testvenue/', + 'directions_link' => 'https://maps.google.com/maps?f=q&source=s_q&hl=en&geocode=&q=testaddr+testcity+testprovince+8000+Austria', + 'website' => 'https://test.at', */ $venue = $this->tribe_event->venues[0]; return ( new Place() ) @@ -174,8 +174,8 @@ class Tribe extends Post { ->set_name( $venue->post_name ) ->set_address( $venue->address . "\n" . - $venue->zip . ', ' . $venue->city . "\n" . - $venue->country + $venue->zip . ', ' . $venue->city . "\n" . + $venue->country ); // todo add checks that everything exists here (lol) } } diff --git a/includes/activitypub/transformer/class-vs-event.php b/includes/activitypub/transformer/class-vs-event.php index 9e9266e..9cef4f1 100644 --- a/includes/activitypub/transformer/class-vs-event.php +++ b/includes/activitypub/transformer/class-vs-event.php @@ -206,7 +206,7 @@ class VS_Event extends Post { public function get_summary() { if ( $this->wp_object->excerpt ) { $excerpt = $this->wp_object->post_excerpt; - } else if ( get_post_meta( $this->wp_object->ID, 'event-summary', true ) ) { + } elseif ( get_post_meta( $this->wp_object->ID, 'event-summary', true ) ) { $excerpt = get_post_meta( $this->wp_object->ID, 'event-summary', true ); } else { $excerpt = $this->get_content();