From 9af8b4b397958f7675aaf07216e8072b88c4a0c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Menrath?= Date: Fri, 5 Jul 2024 15:09:53 +0200 Subject: [PATCH] improve admin notices --- .../admin/class-general-admin-notices.php | 87 ++++++++++++------- includes/class-setup.php | 44 +++++----- 2 files changed, 79 insertions(+), 52 deletions(-) diff --git a/includes/admin/class-general-admin-notices.php b/includes/admin/class-general-admin-notices.php index d264638..c265f00 100644 --- a/includes/admin/class-general-admin-notices.php +++ b/includes/admin/class-general-admin-notices.php @@ -1,8 +1,8 @@ array( + 'href' => true, + 'title' => true, + ), + 'br', + 'i', + ); + + /** + * Admin notice when the ActivityPub plugin is not enabled. + * + * @return string + */ + public static function get_admin_notice_activitypub_plugin_not_enabled(): string { + return sprintf( /* translators: 1: the name of the event plugin a admin notice is shown. 2: The name of the ActivityPub plugin. */ _x( 'For the ActivityPub Event Extensions to work, you will need to install and activate the ActivityPub plugin.', 'admin notice', 'activitypub-event-extensions' ), - esc_html( $activitypub_plugin_url ), + esc_html( self::ACTIVITYPUB_PLUGIN_URL ), admin_url( 'options-general.php?page=activitypub&tab=settings' ) ); - $allowed_html = array( - 'a' => array( - 'href' => true, - 'title' => true, - ), - ); - echo '

' . \wp_kses( $notice, $allowed_html ) . '

'; } /** * Warning that no supported event plugin can be found. + * + * @return string */ - public static function do_admin_notice_no_supported_event_plugin_active() { - $supported_event_plugins_url = 'https://code.event-federation.eu/Event-Federation/wordpress-activitypub-event-extensions#events-plugin-that-will-be-supported-at-first'; - - $notice = sprintf( + public static function get_admin_notice_no_supported_event_plugin_active(): string { + return sprintf( /* translators: 1: the name of the event plugin a admin notice is shown. 2: The name of the ActivityPub plugin. */ _x( 'The Plugin ActivityPub Event Extensions is of no use, because you do not have installed and activated a supported Event Plugin. @@ -57,17 +73,28 @@ class General_Admin_Notices { 'admin notice', 'activitypub-event-extensions' ), - esc_html( $supported_event_plugins_url ), + esc_html( self::ACTIVITYPUB_EVENT_EXTENSIONS_SUPPORTED_EVENT_PLUGINS_URL ), admin_url( 'options-general.php?page=activitypub&tab=settings' ) ); - $allowed_html = array( - 'a' => array( - 'href' => true, - 'title' => true, - ), - 'br', - 'i', - ); - echo '

' . \wp_kses( $notice, $allowed_html ) . '

'; + } + + /** + * Warning if the plugin is Active and the ActivityPub plugin is not. + * + * @return void + */ + public static function activitypub_plugin_not_enabled() { + $notice = self::get_admin_notice_activitypub_plugin_not_enabled(); + echo '

' . \wp_kses( $notice, self::ALLOWED_HTML ) . '

'; + } + + /** + * Warning when no supported Even Plugin is installed and active. + * + * @return void + */ + public static function no_supported_event_plugin_active() { + $notice = self::get_admin_notice_no_supported_event_plugin_active(); + echo '

' . \wp_kses( $notice, self::ALLOWED_HTML ) . '

'; } } diff --git a/includes/class-setup.php b/includes/class-setup.php index 8ce20b6..cf2f799 100644 --- a/includes/class-setup.php +++ b/includes/class-setup.php @@ -11,8 +11,9 @@ namespace Activitypub_Event_Extensions; -use Activitypub_Event_Extensions\Admin\General_Admin_Notices; use Activitypub_Event_Extensions\Admin\Event_Plugin_Admin_Notices; +use Activitypub_Event_Extensions\Admin\General_Admin_Notices; + // Exit if accessed directly. defined( 'ABSPATH' ) || exit; // @codeCoverageIgnore @@ -144,17 +145,21 @@ class Setup { } /** - * Fires the initialization of admin notices for all active supported event plugins.s + * Fires the initialization of admin notices. */ public function do_admin_notices(): void { foreach ( $this->active_event_plugins as $event_plugin ) { new Event_Plugin_Admin_Notices( $event_plugin ); } + // Check if any general admin notices are needed and add actions to insert the needed admin notices. + if ( ! $this->activitypub_plugin_is_active ) { - add_action( 'admin_notices', array( new General_Admin_Notices(), 'do_admin_notice_activitypub_plugin_not_enabled' ), 10, 1 ); + // The ActivityPub plugin is not active. + add_action( 'admin_notices', array( 'Activitypub_Event_Extensions\Admin\General_Admin_Notices', 'activitypub_plugin_not_enabled' ), 10, 1 ); } if ( empty( $this->active_event_plugins ) ) { - add_action( 'admin_notices', array( new General_Admin_Notices(), 'do_admin_notice_no_supported_event_plugin_active' ), 10, 1 ); + // No supported Event Plugin is active. + add_action( 'admin_notices', array( 'Activitypub_Event_Extensions\Admin\General_Admin_Notices', 'no_supported_event_plugin_active' ), 10, 1 ); } } @@ -198,29 +203,24 @@ class Setup { // Don't allow plugin activation, when the ActivityPub plugin is not activated yet. if ( ! $this->activitypub_plugin_is_active ) { deactivate_plugins( plugin_basename( ACTIVITYPUB_EVENT_EXTENSIONS_PLUGIN_FILE ) ); - $notice = sprintf( - /* translators: 1: the name of the event plugin a admin notice is shown. 2: The name of the ActivityPub plugin. */ - _x( - 'To use this plugin install and activate the %2$s plugin first.', - 'admin notice', - 'activitypub-event-extensions' - ), - esc_html( 'https://wordpress.org/plugins/activitypub' ), - esc_html( 'ActivityPub' ) - ); - $allowed_html = array( - 'a' => array( - 'href' => true, - 'title' => true, - ), - ); + $notice = General_Admin_Notices::get_admin_notice_activitypub_plugin_not_enabled(); wp_die( - wp_kses( $notice, $allowed_html ), + wp_kses( $notice, General_Admin_Notices::ALLOWED_HTML ), 'Plugin dependency check', array( 'back_link' => true ), ); } - // If someone installs this plugin, we simply enable ActivityPub support for the event post type, without asking. + + if ( empty( $this->active_event_plugins ) ) { + deactivate_plugins( plugin_basename( ACTIVITYPUB_EVENT_EXTENSIONS_PLUGIN_FILE ) ); + $notice = General_Admin_Notices::get_admin_notice_no_supported_event_plugin_active(); + wp_die( + wp_kses( $notice, General_Admin_Notices::ALLOWED_HTML ), + 'Plugin dependency check', + array( 'back_link' => true ), + ); + } + // If someone installs this plugin, we simply enable ActivityPub support for all currently active event post types. $activitypub_supported_post_types = get_option( 'activitypub_support_post_types', array() ); foreach ( $this->active_event_plugins as $event_plugin ) { if ( ! in_array( $event_plugin['post_type'], $activitypub_supported_post_types, true ) ) {