Skip to content

Commit eaf8259

Browse files
committed
Add warnings to network plugins screen
1 parent 6542b7e commit eaf8259

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

src/BackgroundTask/CronBasedBackgroundTask.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use AmpProject\AmpWP\HasActivation;
1111
use AmpProject\AmpWP\HasDeactivation;
1212
use AmpProject\AmpWP\Service;
13+
use function WP_CLI\Utils\get_plugin_name;
1314

1415
/**
1516
* Abstract base class for using cron to execute a background task.
@@ -33,6 +34,15 @@ abstract class CronBasedBackgroundTask implements Service, HasDeactivation {
3334
self::DEFAULT_INTERVAL_DAILY,
3435
];
3536

37+
/**
38+
* Name of the plugin as WordPress is expecting it.
39+
*
40+
* This should usually have the form "amp/amp.php".
41+
*
42+
* @var string
43+
*/
44+
private $plugin_file;
45+
3646
/**
3747
* Register the service with the system.
3848
*
@@ -41,6 +51,10 @@ abstract class CronBasedBackgroundTask implements Service, HasDeactivation {
4151
public function register() {
4252
add_action( 'admin_init', [ $this, 'schedule_event' ] );
4353
add_action( $this->get_event_name(), [ $this, 'process' ] );
54+
55+
$this->plugin_file = plugin_basename( dirname( dirname( __DIR__ ) ) . '/amp.php' );
56+
add_action( "network_admin_plugin_action_links_{$this->plugin_file}", [ $this, 'add_warning_sign_to_network_deactivate_action' ], 10, 1 );
57+
add_action( 'plugin_row_meta', [ $this, 'add_warning_to_plugin_meta' ], 10, 2 );
4458
}
4559

4660
/**
@@ -70,6 +84,8 @@ public function schedule_event() {
7084
*
7185
* This should be hooked up to the WordPress deactivation hook.
7286
*
87+
* @todo Needs refactoring if used for more than one cron-based task, to avoid iterating over sites multiple times.
88+
*
7389
* @param bool $network_wide Whether the deactivation was done network-wide.
7490
* @return void
7591
*/
@@ -90,6 +106,49 @@ public function deactivate( $network_wide ) {
90106
}
91107
}
92108

109+
/**
110+
* Add a warning sign to the network deactivate action on the network plugins screen.
111+
*
112+
* @param string[] $actions An array of plugin action links. By default this can include 'activate',
113+
* 'deactivate', and 'delete'.
114+
* @return string[]
115+
*/
116+
public function add_warning_sign_to_network_deactivate_action( $actions ) {
117+
if ( ! wp_is_large_network() ) {
118+
return $actions;
119+
}
120+
121+
if ( ! array_key_exists( 'deactivate', $actions ) ) {
122+
return $actions;
123+
}
124+
125+
$actions['deactivate'] = preg_replace( '#^(<a [^>]*>.*)(</a>)$#i', '$1 ⚠️️️︎️$2', $actions['deactivate'] );
126+
127+
return $actions;
128+
}
129+
130+
/**
131+
* Add a warning to the plugin meta row on the network plugins screen.
132+
*
133+
* @param string[] $plugin_meta An array of the plugin's metadata, including the version, author, author URI, and
134+
* plugin URI.
135+
* @param string $plugin_file Path to the plugin file relative to the plugins directory.
136+
* @return string[]
137+
*/
138+
public function add_warning_to_plugin_meta( $plugin_meta, $plugin_file ) {
139+
if ( ! is_multisite() || ! wp_is_large_network() ) {
140+
return $plugin_meta;
141+
}
142+
143+
if ( $plugin_file !== $this->plugin_file ) {
144+
return $plugin_meta;
145+
}
146+
147+
$plugin_meta[] = '⚠️ Large site detected. Deactivation will leave orphaned scheduled events behind. ⚠️';
148+
149+
return $plugin_meta;
150+
}
151+
93152
/**
94153
* Get the interval to use for the event.
95154
*

0 commit comments

Comments
 (0)