add general admin notices

This commit is contained in:
André Menrath 2024-07-05 13:19:15 +02:00
parent 4b9dbbef06
commit e0afda10e2
3 changed files with 89 additions and 15 deletions

View file

@ -1,6 +1,6 @@
<?php
/**
* Class responsible for admin notices.
* Class responsible for Event Plugin related admin notices.
*
* Notices for guiding to proper configuration of ActivityPub with event plugins.
*
@ -8,16 +8,16 @@
* @since 1.0.0
*/
namespace Activitypub_Event_Extensions;
namespace Activitypub_Event_Extensions\Admin;
/**
* Class Admin Notices.
* Class responsible for Event Plugin related admin notices.
*
* This class is responsible for admin notices.
* Notices for guiding to proper configuration of ActivityPub with event plugins.
*
* @since 1.0.0
*/
class Admin_Notices {
class Event_Plugin_Admin_Notices {
/**
* Information about the event plugin.
*

View file

@ -0,0 +1,45 @@
<?php
/**
* Class responsible for Event Plugin related admin notices.
*
* Notices for guiding to proper configuration of ActivityPub with event plugins.
*
* @package Activitypub_Event_Extensions
* @since 1.0.0
*/
namespace Activitypub_Event_Extensions\Admin;
/**
* Class responsible for Event Plugin related admin notices.
*
* Notices for guiding to proper configuration of ActivityPub with event plugins.
*
* @since 1.0.0
*/
class General_Admin_Notices {
/**
* Warning if the plugin is Active and the ActivityPub plugin is not.
*/
public static function do_admin_notice_activitypub_plugin_not_enabled() {
$activitypub_plugin_url = 'https://wordpress.org/plugins/activitypub/';
$notice = 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 <a href="%1$s">ActivityPub</a> plugin.',
'admin notice',
'activitypub-event-extensions'
),
esc_html( $activitypub_plugin_url ),
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>';
}
}

View file

@ -11,6 +11,9 @@
namespace Activitypub_Event_Extensions;
use Activitypub_Event_Extensions\Admin\General_Admin_Notices;
use Activitypub_Event_Extensions\Admin\Event_Plugin_Admin_Notices;
// Exit if accessed directly.
defined( 'ABSPATH' ) || exit; // @codeCoverageIgnore
@ -51,6 +54,13 @@ class Setup {
),
);
/**
* Keep the information whether the ActivityPub plugin is active.
*
* @var boolean
*/
protected $activitypub_plugin_is_active = false;
/**
* Holds an array of the currently activated supported event plugins.
*
@ -66,10 +76,8 @@ class Setup {
* @since 1.0.0
*/
protected function __construct() {
$this->active_event_plugins = self::detect_supported_event_plugins();
if ( empty( $this->active_event_plugins ) ) {
return;
}
$this->activitypub_plugin_is_active = is_plugin_active( 'activitypub/activitypub.php' );
$this->active_event_plugins = self::detect_supported_event_plugins();
$this->setup_hooks();
}
@ -127,6 +135,11 @@ class Setup {
add_action( 'admin_init', array( $this, 'do_admin_notices' ) );
// If we don't have any active event plugins, or the ActivityPub plugin is not enabled, abort here.
if ( empty( $this->active_event_plugins ) || ! $this->activitypub_plugin_is_active ) {
return;
}
add_filter( 'activitypub_transformer', array( $this, 'register_activitypub_event_transformer' ), 10, 3 );
}
@ -135,7 +148,10 @@ class Setup {
*/
public function do_admin_notices(): void {
foreach ( $this->active_event_plugins as $event_plugin ) {
new Admin_Notices( $event_plugin );
new Event_Plugin_Admin_Notices( $event_plugin );
}
if ( ! $this->activitypub_plugin_is_active ) {
add_action( 'admin_notices', array( new General_Admin_Notices(), 'do_admin_notice_activitypub_plugin_not_enabled' ), 10, 1 );
}
}
@ -177,13 +193,26 @@ class Setup {
*/
public function activate() {
// Don't allow plugin activation, when the ActivityPub plugin is not activated yet.
if ( ! is_plugin_active( 'activitypub/activitypub.php' ) ) {
if ( ! $this->activitypub_plugin_is_active ) {
deactivate_plugins( plugin_basename( ACTIVITYPUB_EVENT_EXTENSIONS_PLUGIN_FILE ) );
wp_die(
esc_html_e(
'Please install and activate the <a href="https://wordpress.org/plugins/activitypub/">ActivityPub plugin</a> first.',
'activitypub-event-extensions',
$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 <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_kses( $notice, $allowed_html ),
'Plugin dependency check',
array( 'back_link' => true ),
);