From eaf8259b87cc76a0782599ebea5583d8e5d143d2 Mon Sep 17 00:00:00 2001 From: Alain Schlesser Date: Sat, 21 Mar 2020 08:27:15 +0100 Subject: [PATCH] Add warnings to network plugins screen --- .../CronBasedBackgroundTask.php | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/src/BackgroundTask/CronBasedBackgroundTask.php b/src/BackgroundTask/CronBasedBackgroundTask.php index 4e32a97a57c..e4dd2144882 100644 --- a/src/BackgroundTask/CronBasedBackgroundTask.php +++ b/src/BackgroundTask/CronBasedBackgroundTask.php @@ -10,6 +10,7 @@ use AmpProject\AmpWP\HasActivation; use AmpProject\AmpWP\HasDeactivation; use AmpProject\AmpWP\Service; +use function WP_CLI\Utils\get_plugin_name; /** * Abstract base class for using cron to execute a background task. @@ -33,6 +34,15 @@ abstract class CronBasedBackgroundTask implements Service, HasDeactivation { self::DEFAULT_INTERVAL_DAILY, ]; + /** + * Name of the plugin as WordPress is expecting it. + * + * This should usually have the form "amp/amp.php". + * + * @var string + */ + private $plugin_file; + /** * Register the service with the system. * @@ -41,6 +51,10 @@ abstract class CronBasedBackgroundTask implements Service, HasDeactivation { public function register() { add_action( 'admin_init', [ $this, 'schedule_event' ] ); add_action( $this->get_event_name(), [ $this, 'process' ] ); + + $this->plugin_file = plugin_basename( dirname( dirname( __DIR__ ) ) . '/amp.php' ); + add_action( "network_admin_plugin_action_links_{$this->plugin_file}", [ $this, 'add_warning_sign_to_network_deactivate_action' ], 10, 1 ); + add_action( 'plugin_row_meta', [ $this, 'add_warning_to_plugin_meta' ], 10, 2 ); } /** @@ -70,6 +84,8 @@ public function schedule_event() { * * This should be hooked up to the WordPress deactivation hook. * + * @todo Needs refactoring if used for more than one cron-based task, to avoid iterating over sites multiple times. + * * @param bool $network_wide Whether the deactivation was done network-wide. * @return void */ @@ -90,6 +106,49 @@ public function deactivate( $network_wide ) { } } + /** + * Add a warning sign to the network deactivate action on the network plugins screen. + * + * @param string[] $actions An array of plugin action links. By default this can include 'activate', + * 'deactivate', and 'delete'. + * @return string[] + */ + public function add_warning_sign_to_network_deactivate_action( $actions ) { + if ( ! wp_is_large_network() ) { + return $actions; + } + + if ( ! array_key_exists( 'deactivate', $actions ) ) { + return $actions; + } + + $actions['deactivate'] = preg_replace( '#^(]*>.*)()$#i', '$1 ⚠️️️︎️$2', $actions['deactivate'] ); + + return $actions; + } + + /** + * Add a warning to the plugin meta row on the network plugins screen. + * + * @param string[] $plugin_meta An array of the plugin's metadata, including the version, author, author URI, and + * plugin URI. + * @param string $plugin_file Path to the plugin file relative to the plugins directory. + * @return string[] + */ + public function add_warning_to_plugin_meta( $plugin_meta, $plugin_file ) { + if ( ! is_multisite() || ! wp_is_large_network() ) { + return $plugin_meta; + } + + if ( $plugin_file !== $this->plugin_file ) { + return $plugin_meta; + } + + $plugin_meta[] = '⚠️ Large site detected. Deactivation will leave orphaned scheduled events behind. ⚠️'; + + return $plugin_meta; + } + /** * Get the interval to use for the event. *