Skip to content

Commit

Permalink
Add warnings to network plugins screen
Browse files Browse the repository at this point in the history
  • Loading branch information
schlessera committed Mar 21, 2020
1 parent 6542b7e commit eaf8259
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions src/BackgroundTask/CronBasedBackgroundTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
*
Expand All @@ -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 );
}

/**
Expand Down Expand Up @@ -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
*/
Expand All @@ -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( '#^(<a [^>]*>.*)(</a>)$#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.
*
Expand Down

0 comments on commit eaf8259

Please sign in to comment.