10
10
use AmpProject \AmpWP \HasActivation ;
11
11
use AmpProject \AmpWP \HasDeactivation ;
12
12
use AmpProject \AmpWP \Service ;
13
+ use function WP_CLI \Utils \get_plugin_name ;
13
14
14
15
/**
15
16
* Abstract base class for using cron to execute a background task.
@@ -33,6 +34,15 @@ abstract class CronBasedBackgroundTask implements Service, HasDeactivation {
33
34
self ::DEFAULT_INTERVAL_DAILY ,
34
35
];
35
36
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
+
36
46
/**
37
47
* Register the service with the system.
38
48
*
@@ -41,6 +51,10 @@ abstract class CronBasedBackgroundTask implements Service, HasDeactivation {
41
51
public function register () {
42
52
add_action ( 'admin_init ' , [ $ this , 'schedule_event ' ] );
43
53
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 );
44
58
}
45
59
46
60
/**
@@ -70,6 +84,8 @@ public function schedule_event() {
70
84
*
71
85
* This should be hooked up to the WordPress deactivation hook.
72
86
*
87
+ * @todo Needs refactoring if used for more than one cron-based task, to avoid iterating over sites multiple times.
88
+ *
73
89
* @param bool $network_wide Whether the deactivation was done network-wide.
74
90
* @return void
75
91
*/
@@ -90,6 +106,49 @@ public function deactivate( $network_wide ) {
90
106
}
91
107
}
92
108
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
+
93
152
/**
94
153
* Get the interval to use for the event.
95
154
*
0 commit comments