prepare more modular admin notices
Some checks are pending
Deploy to https://wordpress-test.event-federation.eu/ / deploy (push) Waiting to run

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

@ -33,10 +33,10 @@ 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
@ -48,52 +48,15 @@ add_filter(
}
// 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.
*/
@ -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;
}
@ -263,6 +305,8 @@ class Events_Manager extends Post {
$activtiypub_object = $this->transform_object_properties( $activtiypub_object );
$activtiypub_object->set_external_participation_url( $this->get_url() );
return $activtiypub_object;
}
}