improve admin notices

This commit is contained in:
André Menrath 2024-07-05 15:09:53 +02:00
parent 8aa90b1005
commit 9af8b4b397
2 changed files with 79 additions and 52 deletions

View file

@ -1,8 +1,8 @@
<?php <?php
/** /**
* Class responsible for Event Plugin related admin notices. * Class responsible for general admin notices.
* *
* Notices for guiding to proper configuration of ActivityPub with event plugins. * Notices for guiding to proper configuration of this plugin.
* *
* @package Activitypub_Event_Extensions * @package Activitypub_Event_Extensions
* @since 1.0.0 * @since 1.0.0
@ -11,45 +11,61 @@
namespace Activitypub_Event_Extensions\Admin; namespace Activitypub_Event_Extensions\Admin;
/** /**
* Class responsible for Event Plugin related admin notices. * Class responsible for general admin notices.
* *
* Notices for guiding to proper configuration of ActivityPub with event plugins. * Notices for guiding to proper configuration of this plugin.
* - ActivityPub plugin not installed and activated
* - No supported Event Plugin installed and activated
* *
* @since 1.0.0 * @since 1.0.0
*/ */
class General_Admin_Notices { class General_Admin_Notices {
/** /**
* Warning if the plugin is Active and the ActivityPub plugin is not. * URL of the ActivityPub plugin. Needed when the ActivityPub plugin is not installed.
*/ */
public static function do_admin_notice_activitypub_plugin_not_enabled() { const ACTIVITYPUB_PLUGIN_URL = 'https://wordpress.org/plugins/activitypub';
$activitypub_plugin_url = 'https://wordpress.org/plugins/activitypub/';
$notice = sprintf( const ACTIVITYPUB_EVENT_EXTENSIONS_SUPPORTED_EVENT_PLUGINS_URL = 'https://code.event-federation.eu/Event-Federation/wordpress-activitypub-event-extensions#events-plugin-that-will-be-supported-at-first';
/**
* Allowed HTML for admin notices.
*
* @var array
*/
const ALLOWED_HTML = array(
'a' => 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. */ /* translators: 1: the name of the event plugin a admin notice is shown. 2: The name of the ActivityPub plugin. */
_x( _x(
'For the ActivityPub Event Extensions to work, you will need to install and activate the <a href="%1$s">ActivityPub</a> plugin.', 'For the ActivityPub Event Extensions to work, you will need to install and activate the <a href="%1$s">ActivityPub</a> plugin.',
'admin notice', 'admin notice',
'activitypub-event-extensions' 'activitypub-event-extensions'
), ),
esc_html( $activitypub_plugin_url ), esc_html( self::ACTIVITYPUB_PLUGIN_URL ),
admin_url( 'options-general.php?page=activitypub&tab=settings' ) admin_url( 'options-general.php?page=activitypub&tab=settings' )
); );
$allowed_html = array(
'a' => array(
'href' => true,
'title' => true,
),
);
echo '<div class="notice notice-warning"><p>' . \wp_kses( $notice, $allowed_html ) . '</p></div>';
} }
/** /**
* Warning that no supported event plugin can be found. * Warning that no supported event plugin can be found.
*
* @return string
*/ */
public static function do_admin_notice_no_supported_event_plugin_active() { public static function get_admin_notice_no_supported_event_plugin_active(): string {
$supported_event_plugins_url = 'https://code.event-federation.eu/Event-Federation/wordpress-activitypub-event-extensions#events-plugin-that-will-be-supported-at-first'; return sprintf(
$notice = sprintf(
/* translators: 1: the name of the event plugin a admin notice is shown. 2: The name of the ActivityPub plugin. */ /* translators: 1: the name of the event plugin a admin notice is shown. 2: The name of the ActivityPub plugin. */
_x( _x(
'The Plugin <i>ActivityPub Event Extensions</i> is of no use, because you do not have installed and activated a supported Event Plugin. 'The Plugin <i>ActivityPub Event Extensions</i> 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', 'admin notice',
'activitypub-event-extensions' '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' ) admin_url( 'options-general.php?page=activitypub&tab=settings' )
); );
$allowed_html = array( }
'a' => array(
'href' => true, /**
'title' => true, * Warning if the plugin is Active and the ActivityPub plugin is not.
), *
'br', * @return void
'i', */
); public static function activitypub_plugin_not_enabled() {
echo '<div class="notice notice-warning"><p>' . \wp_kses( $notice, $allowed_html ) . '</p></div>'; $notice = self::get_admin_notice_activitypub_plugin_not_enabled();
echo '<div class="notice notice-warning"><p>' . \wp_kses( $notice, self::ALLOWED_HTML ) . '</p></div>';
}
/**
* 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 '<div class="notice notice-warning"><p>' . \wp_kses( $notice, self::ALLOWED_HTML ) . '</p></div>';
} }
} }

View file

@ -11,8 +11,9 @@
namespace Activitypub_Event_Extensions; 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\Event_Plugin_Admin_Notices;
use Activitypub_Event_Extensions\Admin\General_Admin_Notices;
// Exit if accessed directly. // Exit if accessed directly.
defined( 'ABSPATH' ) || exit; // @codeCoverageIgnore 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 { public function do_admin_notices(): void {
foreach ( $this->active_event_plugins as $event_plugin ) { foreach ( $this->active_event_plugins as $event_plugin ) {
new Event_Plugin_Admin_Notices( $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 ) { 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 ) ) { 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. // Don't allow plugin activation, when the ActivityPub plugin is not activated yet.
if ( ! $this->activitypub_plugin_is_active ) { if ( ! $this->activitypub_plugin_is_active ) {
deactivate_plugins( plugin_basename( ACTIVITYPUB_EVENT_EXTENSIONS_PLUGIN_FILE ) ); deactivate_plugins( plugin_basename( ACTIVITYPUB_EVENT_EXTENSIONS_PLUGIN_FILE ) );
$notice = sprintf( $notice = General_Admin_Notices::get_admin_notice_activitypub_plugin_not_enabled();
/* 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 <a href="%1$s">%2$s</a> 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,
),
);
wp_die( wp_die(
wp_kses( $notice, $allowed_html ), wp_kses( $notice, General_Admin_Notices::ALLOWED_HTML ),
'Plugin dependency check', 'Plugin dependency check',
array( 'back_link' => true ), 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() ); $activitypub_supported_post_types = get_option( 'activitypub_support_post_types', array() );
foreach ( $this->active_event_plugins as $event_plugin ) { foreach ( $this->active_event_plugins as $event_plugin ) {
if ( ! in_array( $event_plugin['post_type'], $activitypub_supported_post_types, true ) ) { if ( ! in_array( $event_plugin['post_type'], $activitypub_supported_post_types, true ) ) {