prepare more modular admin notices

This commit is contained in:
André Menrath 2024-01-05 21:18:34 +01:00
parent 47a6d619a9
commit 214ee89fa0
6 changed files with 164 additions and 73 deletions

View file

@ -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 <a href="%3$s">%1$s settings</a>.',
'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

View file

@ -0,0 +1,84 @@
<?php
/**
* ActivityPub Transformer for the plugin Very Simple Event List.
*
* @package activity-event-transformers
* @license AGPL-3.0-or-later
*/
class Admin_Notices {
const VSEL_PLUGIN_FILE = 'very-simple-event-list/vsel.php';
const VSEL_POST_TYPE = 'event';
const EVENT_MANAGER_PLUGIN_FILE = 'events-manager/events-manager.php';
const EVENT_MANAGER_POTS_TYPE = 'event';
const ACTIVITYPUB_PLUGIN_FILE = 'activitypub/activitypub.php';
/**
* Add actions and filters.
*/
public function __construct() {
add_action( 'admin_init', array( self::class, 'check_for_admin_notices' ) );
}
/**
* Check the conditions for admin notices
*
* These should mainly improve usability.
*/
public static function check_for_admin_notices() {
if ( is_admin() && is_plugin_active( self::ACTIVITYPUB_PLUGIN_FILE ) ) {
// Check for VSEL
if ( is_plugin_active( self::VSEL_PLUGIN_FILE ) && self::post_type_is_not_activitypub_enabled( self::VSEL_POST_TYPE ) ) {
add_action( 'admin_notices', array( self::class, 'vsel_admin_notices' ) );
}
}
}
/**
* Check if ActivityPub is enabled for the custom post type of the event plugin.
*
* @param string $post_type The post type of the event plugin.
* @return bool
*/
private static function post_type_is_not_activitypub_enabled( $post_type ) {
return ! in_array( $post_type, get_option( 'activitypub_support_post_types' ) );
}
/**
* Check whether to do any admin notices for VSEL
*/
public static 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;
if ( $is_vsel_page ) {
self::do_admin_notice_post_type_not_activitypub_enabled( self::VSEL_PLUGIN_FILE );
}
}
/**
* Print admin notice that the current post type is not enabled in the ActivityPub plugin.
*/
private static function do_admin_notice_post_type_not_activitypub_enabled( $event_plugin_file ) {
$vsel_plugin_data = get_plugin_data( WP_PLUGIN_DIR . '/' . $event_plugin_file );
$activitypub_plugin_data = get_plugin_data( WP_PLUGIN_DIR . '/' . self::ACTIVITYPUB_PLUGIN_FILE );
$notice = sprintf(
_x(
'You have installed the %1$s plugin, but the event post type of %2$s is not enabled in the <a href="%3$s">%1$s settings</a>.',
'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,
)
);
}
}

View file

@ -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;
}
@ -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;
}
}

View file

@ -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&#038;source=s_q&#038;hl=en&#038;geocode=&#038;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&#038;source=s_q&#038;hl=en&#038;geocode=&#038;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)
}
}

View file

@ -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();