Repository navigation
Expand file tree
/
Copy pathuninstall.php
More file actions
229 lines (205 loc) · 7.37 KB
/
Copy pathuninstall.php
File metadata and controls
229 lines (205 loc) · 7.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
<?php
/**
* Uninstall cleanup.
*
* Runs when the plugin is deleted from the Plugins screen. Revokes the site's
* API tokens, then removes everything the plugin stored: options and transients (including the API tokens and the
* WooCheckout probe secret), Scanfully's Action Scheduler jobs and the
* WooCheckout probe user.
*
* The plugin itself is not loaded during uninstall, so this file only uses
* WordPress functions and literal names instead of the plugin's classes.
*
* @package Scanfully
*/
if ( ! defined( 'WP_UNINSTALL_PLUGIN' ) ) {
exit;
}
/**
* Remove Scanfully's data from the current site.
*
* @return void
*/
function scanfully_uninstall_site(): void {
scanfully_uninstall_revoke_tokens();
scanfully_uninstall_probe_user();
scanfully_uninstall_scheduled_actions();
scanfully_uninstall_options();
}
/**
* Ask the API to revoke the site's tokens before they are deleted below, so a
* copy of them stops working too. Mirrors Connect\Controller::revoke_tokens():
* best effort with a short timeout, and never blocks the uninstall.
*
* @return void
*/
function scanfully_uninstall_revoke_tokens(): void {
$site_id = (string) get_option( 'scanfully_connect_site_id', '' );
$access_token = (string) get_option( 'scanfully_connect_access_token', '' );
if ( '' === $site_id || '' === $access_token ) {
return;
}
$api_url = (string) apply_filters( 'scanfully_api_url', 'https://api.scanfully.com/v1' );
// Say the plugin was deleted, and by whom, so the site's owner learns why
// it stopped reporting.
wp_remote_post(
$api_url . '/sites/' . rawurlencode( $site_id ) . '/connect/revoke',
[
'headers' => [
'Authorization' => sprintf( 'Bearer %s', $access_token ),
'Content-Type' => 'application/json',
],
'body' => wp_json_encode(
[
'reason' => 'uninstalled',
'by' => scanfully_uninstall_user_name(),
]
),
'timeout' => 5,
'blocking' => true,
'httpversion' => '1.0',
'sslverify' => (bool) apply_filters( 'scanfully_sslverify', true ),
]
);
}
/**
* The display name of the user who deletes the plugin, safe to send: as
* Events\Event::safe_display_name(), a name that holds an @ is replaced by
* the user's first and last name, or left empty, so no email address leaves
* the site.
*
* @return string
*/
function scanfully_uninstall_user_name(): string {
$user = wp_get_current_user();
$name = trim( (string) $user->display_name );
if ( false === strpos( $name, '@' ) ) {
return $name;
}
$name = trim( $user->first_name . ' ' . $user->last_name );
return false === strpos( $name, '@' ) ? $name : '';
}
/**
* Delete the WooCheckout probe user, but only when it is still the account the
* plugin created: a `scanfully_probe` login, an @scanfully.invalid email and
* only the customer role. Any other account is left alone.
*
* @return void
*/
function scanfully_uninstall_probe_user(): void {
$user_id = (int) get_option( 'scanfully_woocheckout_probe_user_id', 0 );
if ( $user_id <= 0 ) {
return;
}
$user = get_userdata( $user_id );
if ( ! $user instanceof WP_User ) {
return;
}
$is_probe_user = ( 'scanfully_probe' === $user->user_login || 0 === strpos( $user->user_login, 'scanfully_probe_' ) )
&& '@scanfully.invalid' === substr( $user->user_email, -strlen( '@scanfully.invalid' ) )
&& [ 'customer' ] === array_values( $user->roles );
if ( ! $is_probe_user ) {
return;
}
require_once ABSPATH . 'wp-admin/includes/user.php';
wp_delete_user( $user_id );
}
/**
* Delete Scanfully's Action Scheduler jobs and their logs.
*
* Action Scheduler isn't loaded during uninstall, so its tables are cleaned
* directly. Other plugins' jobs are untouched: only the `scanfully` group is
* removed.
*
* @return void
*/
function scanfully_uninstall_scheduled_actions(): void {
global $wpdb;
$actions = $wpdb->prefix . 'actionscheduler_actions';
$groups = $wpdb->prefix . 'actionscheduler_groups';
$logs = $wpdb->prefix . 'actionscheduler_logs';
// phpcs:disable WordPress.DB.DirectDatabaseQuery, WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Action Scheduler tables have no API during uninstall; table names come from $wpdb->prefix.
if ( $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->esc_like( $actions ) ) ) !== $actions
|| $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->esc_like( $groups ) ) ) !== $groups
) {
return;
}
$group_id = (int) $wpdb->get_var( $wpdb->prepare( "SELECT group_id FROM {$groups} WHERE slug = %s", 'scanfully' ) );
if ( $group_id <= 0 ) {
return;
}
if ( $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->esc_like( $logs ) ) ) === $logs ) {
$wpdb->query(
$wpdb->prepare(
"DELETE logs FROM {$logs} logs INNER JOIN {$actions} actions ON actions.action_id = logs.action_id WHERE actions.group_id = %d",
$group_id
)
);
}
$wpdb->delete( $actions, [ 'group_id' => $group_id ], [ '%d' ] );
$wpdb->delete( $groups, [ 'group_id' => $group_id ], [ '%d' ] );
// phpcs:enable WordPress.DB.DirectDatabaseQuery, WordPress.DB.PreparedSQL.InterpolatedNotPrepared
// Action Scheduler 4.1+ caches group ids for ten minutes. Drop the cached id
// so jobs scheduled after a reinstall create the group again instead of
// pointing at the deleted row.
wp_cache_delete( 'scanfully', 'action_scheduler_groups' );
}
/**
* Delete every Scanfully option and transient. All of them start with
* `scanfully_`.
*
* @return void
*/
function scanfully_uninstall_options(): void {
global $wpdb;
// phpcs:ignore WordPress.DB.DirectDatabaseQuery -- One-off lookup of the plugin's own option names during uninstall.
$names = $wpdb->get_col(
$wpdb->prepare(
"SELECT option_name FROM {$wpdb->options} WHERE option_name LIKE %s OR option_name LIKE %s OR option_name LIKE %s",
$wpdb->esc_like( 'scanfully_' ) . '%',
$wpdb->esc_like( '_transient_scanfully_' ) . '%',
$wpdb->esc_like( '_transient_timeout_scanfully_' ) . '%'
)
);
foreach ( $names as $name ) {
delete_option( $name );
}
// With a persistent object cache, transients live in the cache instead
// of the options table. Delete the fixed-name ones explicitly; the rest
// expire on their own within an hour.
foreach ( [ 'scanfully_connect_state', 'scanfully_refresh_failures', 'scanfully_refresh_error', 'scanfully_sync_last_run', 'scanfully_email_deliverability_run_now_lock' ] as $transient ) {
delete_transient( $transient );
}
}
/**
* Remove Scanfully's must-use plugin, which serves the whole network, when
* it is Scanfully's (as MustUse\MuPlugin::remove()).
*
* @return void
*/
function scanfully_uninstall_mu_plugin(): void {
$dir = defined( 'WPMU_PLUGIN_DIR' ) ? (string) WPMU_PLUGIN_DIR : WP_CONTENT_DIR . '/mu-plugins';
$path = rtrim( $dir, '/\\' ) . '/scanfully-mu.php';
if ( ! is_file( $path ) ) {
return;
}
$head = @file_get_contents( $path, false, null, 0, 1024 ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged, WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- A local file that may be unreadable.
if ( is_string( $head ) && false !== strpos( $head, 'Plugin Name: Scanfully (must-use)' ) ) {
wp_delete_file( $path );
}
}
scanfully_uninstall_mu_plugin();
if ( is_multisite() ) {
foreach ( get_sites(
[
'fields' => 'ids',
'number' => 0,
]
) as $scanfully_site_id ) {
switch_to_blog( (int) $scanfully_site_id );
scanfully_uninstall_site();
restore_current_blog();
}
} else {
scanfully_uninstall_site();
}