| @@ -4,8 +4,12 @@ | ||
| 4 | 4 | * |
| 5 | 5 | * This file runs when the plugin is deleted via WordPress admin. |
| 6 | 6 | * It removes all plugin data including database tables and options. |
| 7 | 7 | * |
| 8 | + * On a network it visits every site: tables, options, transients and cron | |
| 9 | + * events are per site, so cleaning only the site that runs the uninstall | |
| 10 | + * left everything else behind until 2.11.0 (S13 of the 28 Aug 2026 audit). | |
| 11 | + * | |
| 8 | 12 | * @package Vigilante |
| 9 | 13 | */ |
| 10 | 14 | |
| 11 | 15 | // Exit if not called by WordPress |
| @@ -30,8 +34,86 @@ | ||
| 30 | 34 | function vigilante_uninstall() { |
| 31 | 35 | global $wpdb; |
| 32 | 36 | |
| 33 | 37 | /* |
| 38 | + * Per-site data. switch_to_blog() repoints $wpdb->prefix, $wpdb->options | |
| 39 | + * and the cron option, so the same routine serves every site of a network. | |
| 40 | + * 'number' => 0 lifts the default cap of 100 sites: an uninstall that | |
| 41 | + * cleaned the first hundred sites and left the rest would be the same bug | |
| 42 | + * with a bigger threshold. | |
| 43 | + */ | |
| 44 | + if ( is_multisite() ) { | |
| 45 | + $site_ids = get_sites( | |
| 46 | + array( | |
| 47 | + 'fields' => 'ids', | |
| 48 | + 'number' => 0, | |
| 49 | + ) | |
| 50 | + ); | |
| 51 | + | |
| 52 | + foreach ( $site_ids as $site_id ) { | |
| 53 | + switch_to_blog( $site_id ); | |
| 54 | + vigilante_uninstall_site(); | |
| 55 | + restore_current_blog(); | |
| 56 | + } | |
| 57 | + } else { | |
| 58 | + vigilante_uninstall_site(); | |
| 59 | + } | |
| 60 | + | |
| 61 | + /* | |
| 62 | + * Network options. Since 2.11.3 the baseline of the critical files lives in | |
| 63 | + * a single network option, because both files it watches, wp-config.php and | |
| 64 | + * the root .htaccess, belong to the installation and not to any one site. | |
| 65 | + * It is stored redacted, but it is still a copy of the configuration and it | |
| 66 | + * goes when the plugin goes. Once, not per site. | |
| 67 | + */ | |
| 68 | + if ( is_multisite() ) { | |
| 69 | + delete_site_option( 'vigilante_critical_files_baseline' ); | |
| 70 | + delete_site_option( 'vigilante_baseline_sweep' ); | |
| 71 | + delete_site_option( 'vigilante_owned_blocks' ); | |
| 72 | + delete_site_option( 'vigilante_owned_blocks_claim' ); | |
| 73 | + delete_site_option( 'vigilante_config_copies_sweep' ); | |
| 74 | + delete_site_option( 'vigilante_results_sweep' ); | |
| 75 | + } | |
| 76 | + | |
| 77 | + // Remove backup directory. WP_CONTENT_DIR is shared by the whole network, | |
| 78 | + // so this happens once. | |
| 79 | + $backup_dir = WP_CONTENT_DIR . '/vigilante-backups/'; | |
| 80 | + if ( is_dir( $backup_dir ) ) { | |
| 81 | + vigilante_recursive_rmdir( $backup_dir ); | |
| 82 | + } | |
| 83 | + | |
| 84 | + // Delete all user meta with vigilante_ prefix. The usermeta table is global | |
| 85 | + // on a network, so this also happens once. | |
| 86 | + // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Plugin's own user meta (prefix vigilante_) swept by pattern at uninstall; no cache to invalidate once the plugin is gone. | |
| 87 | + $wpdb->query( | |
| 88 | + "DELETE FROM {$wpdb->usermeta} WHERE meta_key LIKE 'vigilante\_%'" | |
| 89 | + ); | |
| 90 | + | |
| 91 | + /* | |
| 92 | + * The plugin is still loaded in the request that runs this file, so | |
| 93 | + * whatever it does later, on shutdown or on a late hook, writes its data | |
| 94 | + * back after the cleanup above has finished. Measured on 22 aug 2026: an | |
| 95 | + * uninstall left 112 rows of plugin status transients and its last check | |
| 96 | + * timestamp behind, all of them written after this file had run. So the | |
| 97 | + * sweep is repeated at the very end of the request. The loaded plugin is | |
| 98 | + * the current site's instance and writes to the current site, which is | |
| 99 | + * why the sweep does not visit the network again. | |
| 100 | + */ | |
| 101 | + add_action( 'shutdown', 'vigilante_uninstall_final_sweep', PHP_INT_MAX ); | |
| 102 | +} | |
| 103 | + | |
| 104 | +/** | |
| 105 | + * Remove the data of the current site (tables, options, transients, cron) | |
| 106 | + * | |
| 107 | + * Runs once on a single site and once per site on a network, after | |
| 108 | + * switch_to_blog(). | |
| 109 | + * | |
| 110 | + * @since 2.11.0 Extracted from vigilante_uninstall() so a network can loop it. | |
| 111 | + */ | |
| 112 | +function vigilante_uninstall_site() { | |
| 113 | + global $wpdb; | |
| 114 | + | |
| 115 | + /* | |
| 34 | 116 | * The scheduled events go first. WordPress can spawn wp-cron in the middle |
| 35 | 117 | * of an uninstall, and that loopback request runs in its own process with |
| 36 | 118 | * the plugin still on disk: clearing the events before anything else means |
| 37 | 119 | * it finds nothing to run. |
| @@ -68,9 +150,9 @@ | ||
| 68 | 150 | $wpdb->prefix . 'vigilante_2fa_totp', |
| 69 | 151 | ); |
| 70 | 152 | |
| 71 | 153 | foreach ( $tables as $table ) { |
| 72 | - // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching,WordPress.DB.DirectDatabaseQuery.SchemaChange,WordPress.DB.PreparedSQL.InterpolatedNotPrepared | |
| 154 | + // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching,WordPress.DB.DirectDatabaseQuery.SchemaChange,WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Dropping the plugin's own tables at uninstall; the name comes from $wpdb->prefix and a literal, never from input. | |
| 73 | 155 | $wpdb->query( "DROP TABLE IF EXISTS {$table}" ); |
| 74 | 156 | } |
| 75 | 157 | |
| 76 | 158 | // Delete options |
| @@ -76,8 +158,12 @@ | ||
| 76 | 158 | // Delete options |
| 77 | 159 | $options_to_delete = array( |
| 78 | 160 | 'vigilante_options', |
| 79 | 161 | 'vigilante_db_version', |
| 162 | + 'vigilante_purge_2_11_0_done', | |
| 163 | + 'vigilante_baseline_redaction', | |
| 164 | + 'vigilante_owned_blocks', | |
| 165 | + 'vigilante_owned_blocks_claim', | |
| 80 | 166 | 'vigilante_backup_timestamp', |
| 81 | 167 | 'vigilante_last_integrity_scan', |
| 82 | 168 | 'vigilante_last_integrity_results', |
| 83 | 169 | 'vigilante_ignored_files', |
| @@ -101,8 +187,11 @@ | ||
| 101 | 187 | // authentication salts, so leaving it behind would keep them readable in |
| 102 | 188 | // the options table long after the plugin is gone. |
| 103 | 189 | 'vigilante_htaccess_backup', |
| 104 | 190 | 'vigilante_wpconfig_backup', |
| 191 | + 'vigilante_config_copies_purged', | |
| 192 | + 'vigilante_config_copies_sweep_lock', | |
| 193 | + 'vigilante_htaccess_write_lock', | |
| 105 | 194 | 'vigilante_plugin_status_state', |
| 106 | 195 | 'vigilante_plugin_status_last_check', |
| 107 | 196 | 'vigilante_ignored_closed_plugins', |
| 108 | 197 | ); |
| @@ -112,43 +201,20 @@ | ||
| 112 | 201 | } |
| 113 | 202 | |
| 114 | 203 | // Per-backup records are named after their timestamp |
| 115 | 204 | // (vigilante_backup_info_<Y-m-d_H-i-s>), so a fixed list cannot reach them. |
| 116 | - // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching | |
| 205 | + // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- Plugin's own options swept by pattern at uninstall; the options API has no LIKE. | |
| 117 | 206 | $wpdb->query( |
| 118 | 207 | "DELETE FROM {$wpdb->options} WHERE option_name LIKE 'vigilante_backup_info_%'" |
| 119 | 208 | ); |
| 120 | 209 | |
| 121 | 210 | // Delete all transients |
| 122 | - // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching | |
| 211 | + // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- Plugin's own transients swept by pattern at uninstall; the transients API has no LIKE. | |
| 123 | 212 | $wpdb->query( |
| 124 | - "DELETE FROM {$wpdb->options} | |
| 125 | - WHERE option_name LIKE '_transient_vigilante_%' | |
| 213 | + "DELETE FROM {$wpdb->options} | |
| 214 | + WHERE option_name LIKE '_transient_vigilante_%' | |
| 126 | 215 | OR option_name LIKE '_transient_timeout_vigilante_%'" |
| 127 | 216 | ); |
| 128 | - | |
| 129 | - // Remove backup directory | |
| 130 | - $backup_dir = WP_CONTENT_DIR . '/vigilante-backups/'; | |
| 131 | - if ( is_dir( $backup_dir ) ) { | |
| 132 | - vigilante_recursive_rmdir( $backup_dir ); | |
| 133 | - } | |
| 134 | - | |
| 135 | - | |
| 136 | - // Delete all user meta with vigilante_ prefix | |
| 137 | - // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching | |
| 138 | - $wpdb->query( | |
| 139 | - "DELETE FROM {$wpdb->usermeta} WHERE meta_key LIKE 'vigilante\_%'" | |
| 140 | - ); | |
| 141 | - | |
| 142 | - /* | |
| 143 | - * The plugin is still loaded in the request that runs this file, so | |
| 144 | - * whatever it does later, on shutdown or on a late hook, writes its data | |
| 145 | - * back after the cleanup above has finished. Measured on 22 aug 2026: an | |
| 146 | - * uninstall left 112 rows of plugin status transients and its last check | |
| 147 | - * timestamp behind, all of them written after this file had run. So the | |
| 148 | - * sweep is repeated at the very end of the request. | |
| 149 | - */ | |
| 150 | - add_action( 'shutdown', 'vigilante_uninstall_final_sweep', PHP_INT_MAX ); | |
| 151 | 217 | } |
| 152 | 218 | |
| 153 | 219 | /** |
| 154 | 220 | * Second pass at the end of the request, for anything written after the first one |
| @@ -181,9 +247,9 @@ | ||
| 181 | 247 | foreach ( $hooks as $hook ) { |
| 182 | 248 | wp_unschedule_hook( $hook ); |
| 183 | 249 | } |
| 184 | 250 | |
| 185 | - // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching | |
| 251 | + // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- Same sweep as vigilante_uninstall_site(), repeated at shutdown for rows the still-loaded plugin wrote back after the first pass. | |
| 186 | 252 | $wpdb->query( |
| 187 | 253 | "DELETE FROM {$wpdb->options} |
| 188 | 254 | WHERE option_name LIKE '_transient_vigilante\_%' |
| 189 | 255 | OR option_name LIKE '_transient_timeout_vigilante\_%' |
| @@ -191,8 +257,13 @@ | ||
| 191 | 257 | OR option_name LIKE 'vigilante_backup_info\_%' |
| 192 | 258 | OR option_name IN ( |
| 193 | 259 | 'vigilante_options', |
| 194 | 260 | 'vigilante_db_version', |
| 261 | + 'vigilante_purge_2_11_0_done', | |
| 262 | + 'vigilante_baseline_redaction', | |
| 263 | + 'vigilante_critical_files_baseline', | |
| 264 | + 'vigilante_owned_blocks', | |
| 265 | + 'vigilante_owned_blocks_claim', | |
| 195 | 266 | 'vigilante_ignored_closed_plugins', |
| 196 | 267 | 'vigilante_ignored_files', |
| 197 | 268 | 'vigilante_dismissed_notices', |
| 198 | 269 | 'vigilante_under_attack_mode', |
| @@ -231,5 +302,5 @@ | ||
| 231 | 302 | return $wp_filesystem->delete( $dir, true ); |
| 232 | 303 | } |
| 233 | 304 | |
| 234 | 305 | // Run uninstall |
| 235 | -vigilante_uninstall(); | |
| 306 | +vigilante_uninstall(); | |