From 4e2c1fb23a3321c9d900e1faf520c2c2ede64445 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Menrath?= Date: Wed, 11 Sep 2024 13:55:37 +0200 Subject: [PATCH] add checks for minimum version of the activitypub plugin --- activitypub-event-extensions.php | 3 +- .../admin/class-general-admin-notices.php | 36 ++++++++++++++++--- includes/class-setup.php | 24 ++++++++++++- 3 files changed, 56 insertions(+), 7 deletions(-) diff --git a/activitypub-event-extensions.php b/activitypub-event-extensions.php index 42f8b48..392601a 100644 --- a/activitypub-event-extensions.php +++ b/activitypub-event-extensions.php @@ -11,7 +11,7 @@ * License URI: https://www.gnu.org/licenses/agpl-3.0.de.html * Requires PHP: 8.1 * - * ActivityPub plugin tested up to: 3.2.2 + * Requires at least ActivityPub plugin with version >= 3.2.2. ActivityPub plugin tested up to: 3.2.2. * * @package activitypub-event-extensions * @license AGPL-3.0-or-later @@ -26,6 +26,7 @@ define( 'ACTIVITYPUB_EVENT_EXTENSIONS_PLUGIN_FILE', plugin_dir_path( __FILE__ ) define( 'ACTIVITYPUB_EVENT_EXTENSIONS_PLUGIN_URL', plugin_dir_url( __FILE__ ) ); define( 'ACTIVITYPUB_EVENT_EXTENSIONS_PLUGIN_VERSION', current( get_file_data( __FILE__, array( 'Version' ), 'plugin' ) ) ); define( 'ACTIVITYPUB_EVENT_EXTENSIONS_DOMAIN', 'activitypub-event-extensions' ); +define( 'ACTIVITYPUB_EVENT_EXTENSIONS_ACTIVITYPUB_PLUGIN_MIN_VERSION', '3.2.2' ); // Include and register the autoloader class for automatic loading of plugin classes. require_once ACTIVITYPUB_EVENT_EXTENSIONS_PLUGIN_DIR . '/includes/class-autoloader.php'; diff --git a/includes/admin/class-general-admin-notices.php b/includes/admin/class-general-admin-notices.php index a4de156..2751287 100644 --- a/includes/admin/class-general-admin-notices.php +++ b/includes/admin/class-general-admin-notices.php @@ -49,14 +49,31 @@ class General_Admin_Notices { */ 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: An URL that points to 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( self::ACTIVITYPUB_PLUGIN_URL ) + ); + } + + /** + * Admin notice when the ActivityPub plugin version is too old. + * + * @return string + */ + public static function get_admin_notice_activitypub_plugin_version_too_old(): string { + return sprintf( + /* translators: 1: The name of the ActivityPub plugin. 2: The minimum required version number of the ActivityPub plugin. */ + _x( + 'Please upgrade your ActivityPub plugin. At least version %2$s is required for the ActivityPub event extensions to work.', + 'admin notice', + 'activitypub-event-extensions' + ), esc_html( self::ACTIVITYPUB_PLUGIN_URL ), - admin_url( 'options-general.php?page=activitypub&tab=settings' ) + esc_html( ACTIVITYPUB_EVENT_EXTENSIONS_ACTIVITYPUB_PLUGIN_MIN_VERSION ) ); } @@ -67,15 +84,14 @@ class General_Admin_Notices { */ 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. */ + /* translators: 1: An URL to the list of supported event plugins. */ _x( 'The Plugin ActivityPub Event Extensions is of no use, because you do not have installed and activated a supported Event Plugin.
For a list of supported Event Plugins see here.', 'admin notice', 'activitypub-event-extensions' ), - esc_html( self::ACTIVITYPUB_EVENT_EXTENSIONS_SUPPORTED_EVENT_PLUGINS_URL ), - admin_url( 'options-general.php?page=activitypub&tab=settings' ) + esc_html( self::ACTIVITYPUB_EVENT_EXTENSIONS_SUPPORTED_EVENT_PLUGINS_URL ) ); } @@ -89,6 +105,16 @@ class General_Admin_Notices { echo '

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

'; } + /** + * Warning if the plugin is Active and the ActivityPub plugins version is too old. + * + * @return void + */ + public static function activitypub_plugin_version_too_old(): void { + $notice = self::get_admin_notice_activitypub_plugin_version_too_old(); + echo '

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

'; + } + /** * Warning when no supported Even Plugin is installed and active. * diff --git a/includes/class-setup.php b/includes/class-setup.php index e075ee0..fe4f55b 100644 --- a/includes/class-setup.php +++ b/includes/class-setup.php @@ -37,6 +37,13 @@ class Setup { */ protected $activitypub_plugin_is_active = false; + /** + * Keep the current version of the current ActivityPub plugin. + * + * @var string + */ + protected $activitypub_plugin_version = ''; + /** * Holds an array of the currently activated supported event plugins. * @@ -53,7 +60,13 @@ class Setup { */ protected function __construct() { $this->activitypub_plugin_is_active = is_plugin_active( 'activitypub/activitypub.php' ); + // TODO: decide whether we want to do anything at all when ActivityPub plugin is note active. + // if ( ! $this->activitypub_plugin_is_active ) { + // deactivate_plugins( ACTIVITYPUB_EVENT_EXTENSIONS_PLUGIN_FILE ); + // return; + // }. $this->active_event_plugins = self::detect_active_event_plugins(); + $this->activitypub_plugin_version = get_file_data( WP_PLUGIN_DIR . '/activitypub/activitypub.php', array( 'Version' ) )[0]; $this->setup_hooks(); } @@ -151,6 +164,12 @@ class Setup { 'plugin_action_links_' . ACTIVITYPUB_EVENT_EXTENSIONS_PLUGIN_BASENAME, array( Settings_Page::class, 'settings_link' ) ); + + // Check if the minimum required version of the ActivityPub plugin is installed. + if ( version_compare( $this->activitypub_plugin_version, ACTIVITYPUB_EVENT_EXTENSIONS_ACTIVITYPUB_PLUGIN_MIN_VERSION ) ) { + return; + } + add_filter( 'activitypub_transformer', array( $this, 'register_activitypub_event_transformer' ), 10, 3 ); } @@ -183,11 +202,14 @@ class Setup { 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 ) { // 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 ( version_compare( $this->activitypub_plugin_version, ACTIVITYPUB_EVENT_EXTENSIONS_ACTIVITYPUB_PLUGIN_MIN_VERSION ) ) { + // The ActivityPub plugin is too old. + add_action( 'admin_notices', array( 'Activitypub_Event_Extensions\Admin\General_Admin_Notices', 'activitypub_plugin_version_too_old' ), 10, 1 ); + } if ( empty( $this->active_event_plugins ) ) { // 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 );