Catch more cases for errors in admin UI when ActivityPub plugin is not active
All checks were successful
PHP Code Checker / PHP Code Checker (pull_request) Successful in 48s
PHPUnit / PHPUnit – PHP 7.4 (pull_request) Successful in 1m2s
PHPUnit / PHPUnit – PHP 8.0 (pull_request) Successful in 1m2s
PHPUnit / PHPUnit – PHP 8.1 (pull_request) Successful in 1m3s
PHPUnit / PHPUnit – PHP 8.2 (pull_request) Successful in 1m3s
PHPUnit / PHPUnit – PHP 8.3 (pull_request) Successful in 1m4s
PHPUnit / PHPUnit – PHP 8.4 (pull_request) Successful in 1m6s

This commit is contained in:
André Menrath 2024-12-23 14:19:26 +01:00
parent a1c776641f
commit 355dfb8991
4 changed files with 79 additions and 45 deletions

View file

@ -92,6 +92,15 @@ class Setup {
return self::$instance; return self::$instance;
} }
/**
* Getter function for whether the ActivityPub plugin is active.
*
* @return bool True when the ActivityPub plugin is active.
*/
public function is_activitypub_plugin_active(): bool {
return $this->activitypub_plugin_is_active;
}
/** /**
* LooksUp the current version of the ActivityPub. * LooksUp the current version of the ActivityPub.
* *
@ -135,6 +144,9 @@ class Setup {
* @return void * @return void
*/ */
public function redetect_active_event_plugins(): void { public function redetect_active_event_plugins(): void {
if ( ! $this->activitypub_plugin_is_active ) {
return;
}
delete_transient( 'event_bridge_for_activitypub_active_event_plugins' ); delete_transient( 'event_bridge_for_activitypub_active_event_plugins' );
$this->detect_active_event_plugins(); $this->detect_active_event_plugins();
} }
@ -152,6 +164,11 @@ class Setup {
return $active_event_plugins; return $active_event_plugins;
} }
// Detection will fail in case the ActivityPub plugin is not active.
if ( ! $this->activitypub_plugin_is_active ) {
return array();
}
if ( ! function_exists( 'get_plugins' ) ) { if ( ! function_exists( 'get_plugins' ) ) {
require_once ABSPATH . 'wp-admin/includes/plugin.php'; require_once ABSPATH . 'wp-admin/includes/plugin.php';
} }
@ -203,16 +220,6 @@ class Setup {
$this->activitypub_plugin_is_active = defined( 'ACTIVITYPUB_PLUGIN_VERSION' ) || \is_plugin_active( 'activitypub/activitypub.php' ); $this->activitypub_plugin_is_active = defined( 'ACTIVITYPUB_PLUGIN_VERSION' ) || \is_plugin_active( 'activitypub/activitypub.php' );
$this->activitypub_plugin_version = self::get_activitypub_plugin_version(); $this->activitypub_plugin_version = self::get_activitypub_plugin_version();
// Detect active supported event plugins.
$this->detect_active_event_plugins();
// Register hook that runs when this plugin gets activated.
register_activation_hook( EVENT_BRIDGE_FOR_ACTIVITYPUB_PLUGIN_FILE, array( $this, 'activate' ) );
// Register listeners whenever any plugin gets activated or deactivated.
add_action( 'activated_plugin', array( $this, 'redetect_active_event_plugins' ) );
add_action( 'deactivated_plugin', array( $this, 'redetect_active_event_plugins' ) );
// Add hook that takes care of all notices in the Admin UI. // Add hook that takes care of all notices in the Admin UI.
add_action( 'admin_init', array( $this, 'do_admin_notices' ) ); add_action( 'admin_init', array( $this, 'do_admin_notices' ) );
@ -222,6 +229,13 @@ class Setup {
// Add hook that loads CSS and JavaScript files for the Admin UI. // Add hook that loads CSS and JavaScript files for the Admin UI.
add_action( 'admin_enqueue_scripts', array( self::class, 'enqueue_styles' ) ); add_action( 'admin_enqueue_scripts', array( self::class, 'enqueue_styles' ) );
// Register hook that runs when this plugin gets activated.
register_activation_hook( EVENT_BRIDGE_FOR_ACTIVITYPUB_PLUGIN_FILE, array( $this, 'activate' ) );
// Register listeners whenever any plugin gets activated or deactivated.
add_action( 'activated_plugin', array( $this, 'redetect_active_event_plugins' ) );
add_action( 'deactivated_plugin', array( $this, 'redetect_active_event_plugins' ) );
// Register the settings page(s) of this plugin to the WordPress admin menu. // Register the settings page(s) of this plugin to the WordPress admin menu.
add_action( 'admin_menu', array( Settings_Page::class, 'admin_menu' ) ); add_action( 'admin_menu', array( Settings_Page::class, 'admin_menu' ) );
add_filter( add_filter(
@ -230,7 +244,15 @@ class Setup {
); );
// If we don't have any active event plugins, or the ActivityPub plugin is not enabled, abort here. // 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 ) { if ( ! $this->activitypub_plugin_is_active ) {
return;
}
// Detect active supported event plugins.
$this->detect_active_event_plugins();
// If we don't have any active event plugins, or the ActivityPub plugin is not enabled, abort here.
if ( empty( $this->active_event_plugins ) ) {
return; return;
} }
@ -292,15 +314,17 @@ class Setup {
// Check if any general admin notices are needed and add actions to insert the needed admin notices. // 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 ) {
// The ActivityPub plugin is not active. // The ActivityPub plugin is not active.
add_action( 'admin_notices', array( 'Event_Bridge_For_ActivityPub\Admin\General_Admin_Notices', 'activitypub_plugin_not_enabled' ), 10, 1 ); add_action( 'admin_notices', array( General_Admin_Notices::class, 'activitypub_plugin_not_enabled' ), 10, 1 );
return;
} }
if ( ! version_compare( $this->activitypub_plugin_version, EVENT_BRIDGE_FOR_ACTIVITYPUB_ACTIVITYPUB_PLUGIN_MIN_VERSION ) ) { if ( ! version_compare( $this->activitypub_plugin_version, EVENT_BRIDGE_FOR_ACTIVITYPUB_ACTIVITYPUB_PLUGIN_MIN_VERSION ) ) {
// The ActivityPub plugin is too old. // The ActivityPub plugin is too old.
add_action( 'admin_notices', array( 'Event_Bridge_For_ActivityPub\Admin\General_Admin_Notices', 'activitypub_plugin_version_too_old' ), 10, 1 ); add_action( 'admin_notices', array( General_Admin_Notices::class, 'activitypub_plugin_version_too_old' ), 10, 1 );
return;
} }
if ( empty( $this->active_event_plugins ) ) { if ( empty( $this->active_event_plugins ) ) {
// No supported Event Plugin is active. // No supported Event Plugin is active.
add_action( 'admin_notices', array( 'Event_Bridge_For_ActivityPub\Admin\General_Admin_Notices', 'no_supported_event_plugin_active' ), 10, 1 ); add_action( 'admin_notices', array( General_Admin_Notices::class, 'no_supported_event_plugin_active' ), 10, 1 );
} }
} }

View file

@ -20,36 +20,38 @@ defined( 'ABSPATH' ) || exit; // @codeCoverageIgnore
?> ?>
<div class="wrap event_bridge_for_activitypub-admin-table-container"> <div class="wrap event_bridge_for_activitypub-admin-table-container">
<!-- Table title with add new button like on post edit pages --> <?php if ( defined( 'ACTIVITYPUB_PLUGIN_VERSION' ) ) { ?>
<div class="event_bridge_for_activitypub-admin-table-top"> <!-- Table title with add new button like on post edit pages -->
<h2 class="wp-heading-inline"> <?php esc_html_e( 'List of Event Sources', 'event-bridge-for-activitypub' ); ?> </h2> <div class="event_bridge_for_activitypub-admin-table-top">
<!-- Button that triggers ThickBox --> <h2 class="wp-heading-inline"> <?php esc_html_e( 'List of Event Sources', 'event-bridge-for-activitypub' ); ?> </h2>
<a href="#TB_inline?width=600&height=400&inlineId=Event_Bridge_For_ActivityPub_add_new_source" class="thickbox page-title-action"> <!-- Button that triggers ThickBox -->
<?php esc_html_e( 'Add Event Source', 'event-bridge-for-activitypub' ); ?> <a href="#TB_inline?width=600&height=400&inlineId=Event_Bridge_For_ActivityPub_add_new_source" class="thickbox page-title-action">
</a> <?php esc_html_e( 'Add Event Source', 'event-bridge-for-activitypub' ); ?>
</div> </a>
</div>
<!-- ThickBox content (hidden initially) --> <!-- ThickBox content (hidden initially) -->
<div id="Event_Bridge_For_ActivityPub_add_new_source" style="display:none;"> <div id="Event_Bridge_For_ActivityPub_add_new_source" style="display:none;">
<h2><?php esc_html_e( 'Add new ActivityPub follow', 'event-bridge-for-activitypub' ); ?> </h2> <h2><?php esc_html_e( 'Add new ActivityPub follow', 'event-bridge-for-activitypub' ); ?> </h2>
<p> <?php esc_html_e( 'Here you can enter either a Fediverse handle (@username@example.social), URL of an ActivityPub Account (https://example.social/user/username) or instance URL.', 'event-bridge-for-activitypub' ); ?> </p> <p> <?php esc_html_e( 'Here you can enter either a Fediverse handle (@username@example.social), URL of an ActivityPub Account (https://example.social/user/username) or instance URL.', 'event-bridge-for-activitypub' ); ?> </p>
<form method="post" action="options.php"> <form method="post" action="options.php">
<?php \settings_fields( 'event-bridge-for-activitypub-event-sources' ); ?> <?php \settings_fields( 'event-bridge-for-activitypub-event-sources' ); ?>
<input type="text" name="event_bridge_for_activitypub_event_source" id="event_bridge_for_activitypub_event_source" value=""> <input type="text" name="event_bridge_for_activitypub_event_source" id="event_bridge_for_activitypub_event_source" value="">
<?php \submit_button( __( 'Add Event Source', 'event-bridge-for-activitypub' ) ); ?> <?php \submit_button( __( 'Add Event Source', 'event-bridge-for-activitypub' ) ); ?>
</form> </form>
</div> </div>
<div class="wrap activitypub-followers-page"> <div class="wrap activitypub-followers-page">
<form method="get"> <form method="get">
<input type="hidden" name="page" value="event-bridge-for-activitypub" /> <input type="hidden" name="page" value="event-bridge-for-activitypub" />
<input type="hidden" name="tab" value="event-sources" /> <input type="hidden" name="tab" value="event-sources" />
<?php <?php
$table = new \Event_Bridge_For_ActivityPub\Table\Event_Sources(); $table = new \Event_Bridge_For_ActivityPub\Table\Event_Sources();
$table->prepare_items(); $table->prepare_items();
$table->search_box( 'Search', 'search' ); $table->search_box( 'Search', 'search' );
$table->display(); $table->display();
?> ?>
</form> </form>
</div> </div>
<?php } ?>
</div> </div>

View file

@ -23,6 +23,9 @@ defined( 'ABSPATH' ) || exit; // @codeCoverageIgnore
); );
use Activitypub\Activity\Extended_Object\Event; use Activitypub\Activity\Extended_Object\Event;
use Event_Bridge_For_ActivityPub\Setup;
$activitypub_plugin_is_active = Setup::get_instance()->is_activitypub_plugin_active();
if ( ! isset( $args ) || ! array_key_exists( 'event_terms', $args ) ) { if ( ! isset( $args ) || ! array_key_exists( 'event_terms', $args ) ) {
return; return;
@ -100,6 +103,7 @@ $current_category_mapping = \get_option( 'event_bridge_for_activitypub_ev
</div> </div>
</div> </div>
<?php if ( $activitypub_plugin_is_active ) { ?>
<div class="box"> <div class="box">
<h2><?php \esc_html_e( 'Event Sources', 'event-bridge-for-activitypub' ); ?></h2> <h2><?php \esc_html_e( 'Event Sources', 'event-bridge-for-activitypub' ); ?></h2>
<?php <?php
@ -215,6 +219,7 @@ $current_category_mapping = \get_option( 'event_bridge_for_activitypub_ev
} }
?> ?>
</div> </div>
<?php } ?>
<div class="box"> <div class="box">
<h2> <?php esc_html_e( 'ActivityPub Event Category', 'event-bridge-for-activitypub' ); ?> </h2> <h2> <?php esc_html_e( 'ActivityPub Event Category', 'event-bridge-for-activitypub' ); ?> </h2>

View file

@ -24,6 +24,7 @@ use Event_Bridge_For_ActivityPub\Admin\Health_Check;
); );
$active_event_plugins = Setup::get_instance()->get_active_event_plugins(); $active_event_plugins = Setup::get_instance()->get_active_event_plugins();
$activitypub_plugin_is_active = Setup::get_instance()->is_activitypub_plugin_active();
$event_bridge_for_activitypub_status_ok = true; $event_bridge_for_activitypub_status_ok = true;
$example_event_post = Health_Check::get_most_recent_event_posts(); $example_event_post = Health_Check::get_most_recent_event_posts();
@ -45,7 +46,9 @@ WP_Filesystem();
<h2><?php \esc_html_e( 'Status', 'event-bridge-for-activitypub' ); ?></h2> <h2><?php \esc_html_e( 'Status', 'event-bridge-for-activitypub' ); ?></h2>
<p><?php \esc_html_e( 'The Event Bridge for ActivityPub detected the following (activated) event plugins:', 'event-bridge-for-activitypub' ); ?></p> <p><?php \esc_html_e( 'The Event Bridge for ActivityPub detected the following (activated) event plugins:', 'event-bridge-for-activitypub' ); ?></p>
<?php <?php
if ( empty( $active_event_plugins ) ) { if ( ! $activitypub_plugin_is_active ) {
$notice = General_Admin_Notices::get_admin_notice_activitypub_plugin_not_enabled();
} elseif ( empty( $active_event_plugins ) ) {
$notice = General_Admin_Notices::get_admin_notice_no_supported_event_plugin_active(); $notice = General_Admin_Notices::get_admin_notice_no_supported_event_plugin_active();
} }
echo '<p>⚠' . \wp_kses( $notice, General_Admin_Notices::ALLOWED_HTML ) . '</p>'; echo '<p>⚠' . \wp_kses( $notice, General_Admin_Notices::ALLOWED_HTML ) . '</p>';