PluginProbe
ThinkRank AI SEO – AI SEO Plugin for WordPress: Schema, XML Sitemaps, Meta Tags, Search Console & Local SEO / trunk
ThinkRank AI SEO – AI SEO Plugin for WordPress: Schema, XML Sitemaps, Meta Tags, Search Console & Local SEO vtrunk
2.5.0 2.4.0 2.3.0 2.2.0 2.1.1 2.1.0 2.0.2 2.0.1 2.0.0 1.32.0 1.31.0 1.30.0 1.29.0 1.28.0 1.27.0 1.26.0 1.25.0 trunk 1.0.0 1.0.1 1.0.2 1.1.0 1.10.0 1.11.0 1.12.0 All 46 releases
thinkrank / uninstall.php

uninstall.php in ThinkRank AI SEO – AI SEO Plugin for WordPress: Schema, XML Sitemaps, Meta Tags, Search Console & Local SEO trunk, at uninstall.php

394 lines 16.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Plugin Uninstall Script
4 *
5 * Handles complete plugin removal and cleanup
6 *
7 * @package ThinkRank
8 * @since 1.0.0
9 */
10
11 declare(strict_types=1);
12
13 // Prevent direct access
14 if (!defined('WP_UNINSTALL_PLUGIN')) {
15 exit;
16 }
17
18 /**
19 * ThinkRank Uninstaller Class
20 *
21 * Single Responsibility: Handle complete plugin removal
22 */
23 class ThinkRank_Uninstaller {
24
25 /**
26 * Option that records a deliberate uninstall.
27 *
28 * Keep in sync with ThinkRank\Core\Activator::UNINSTALLED_OPTION and with
29 * ThinkRank Pro's Free_Plugin_Installer.
30 */
31 private const UNINSTALLED_OPTION = 'thinkrank_uninstalled';
32
33 /**
34 * Run uninstall process
35 *
36 * @return void
37 */
38 public static function uninstall(): void {
39 // Everything below resolves through $wpdb->prefix and $wpdb->options,
40 // i.e. the current blog. On a network install WordPress runs this once,
41 // so every other site kept its tables, its options and its stored
42 // credentials — API keys and Google refresh tokens included (#399).
43 if (is_multisite()) {
44 $site_ids = get_sites([
45 'fields' => 'ids',
46 'number' => 0,
47 'update_site_meta_cache' => false,
48 ]);
49
50 foreach ($site_ids as $site_id) {
51 switch_to_blog((int) $site_id);
52 self::uninstall_site();
53 restore_current_blog();
54 }
55
56 return;
57 }
58
59 self::uninstall_site();
60 }
61
62 /**
63 * Remove everything ThinkRank created on the current blog.
64 *
65 * @return void
66 */
67 private static function uninstall_site(): void {
68 // Check if user wants to keep data. Default to TRUE (preserve) to match the
69 // Settings class default and the documented UI behavior — only nuke data when
70 // the user has explicitly enabled "Delete all data on uninstall".
71 $keep_data = (bool) get_option('thinkrank_keep_data_on_uninstall', true);
72
73 // Files first, and regardless of $keep_data.
74 //
75 // Ordering: every ownership test in there reads state the database
76 // cleanup below is about to destroy — sitemap filenames come from the
77 // settings table, the IndexNow key file's name from an option. Run it
78 // after, and there is nothing left to identify our files by.
79 //
80 // Not gated on $keep_data because these are not the user's data. They
81 // are artifacts of a plugin that is being removed, and leaving them is
82 // actively harmful: a real file at /sitemap_index.xml is served by the
83 // web server before WordPress boots, so it goes on shadowing RankMath,
84 // Squirrly or core's own sitemap indefinitely — while rendering blank,
85 // since the XSL stylesheet it points at leaves with the plugin (#510).
86 // "Keep my data" means keep the settings for a reinstall, not keep a
87 // dead file breaking the next plugin. On reinstall they are republished
88 // from the settings that were kept.
89 self::remove_webroot_files();
90
91 if (!$keep_data) {
92 self::remove_database_tables();
93 self::remove_options();
94 self::remove_user_meta();
95 self::remove_post_meta();
96 self::remove_term_meta();
97 }
98
99 self::clear_caches();
100 self::remove_cron_jobs();
101 self::remove_capabilities();
102 self::mark_uninstalled();
103 }
104
105 /**
106 * Delete every file ThinkRank published into the WordPress web root.
107 *
108 * The sitemap, robots.txt, llms.txt and the IndexNow key file are real files
109 * in `ABSPATH`, not routes, so removing the plugin has to remove them too —
110 * otherwise the web server keeps serving a sitemap belonging to a plugin
111 * that is no longer installed, and the next SEO plugin's own sitemap can
112 * never answer (#510).
113 *
114 * @since 2.1.0
115 *
116 * @return void
117 */
118 private static function remove_webroot_files(): void {
119 // No autoloader here — WP_UNINSTALL_PLUGIN loads this file without the
120 // plugin — so the removal logic comes from the same plain function file
121 // the deactivator and the sitemap generator use. One copy, not three.
122 $shared = __DIR__ . '/includes/cleanup-webroot.php';
123
124 if (!is_readable($shared)) {
125 return;
126 }
127
128 require_once $shared;
129
130 if (function_exists('thinkrank_webroot_cleanup')) {
131 thinkrank_webroot_cleanup();
132 }
133 }
134
135 /**
136 * The shared cleanup manifest.
137 *
138 * There is no autoloader here — WP_UNINSTALL_PLUGIN loads this file without
139 * the plugin — so the cron-hook and capability lists come from a plain
140 * array file that the deactivator reads too.
141 *
142 * @return array{cron_hooks: string[], capabilities: string[]}
143 */
144 private static function manifest(): array {
145 return require __DIR__ . '/includes/cleanup-manifest.php';
146 }
147
148 /**
149 * Record that the user deliberately uninstalled the plugin.
150 *
151 * ThinkRank Pro auto-installs and activates the free plugin whenever it
152 * finds it missing, which silently re-ran the activator and recreated every
153 * table this uninstall had just dropped. Pro reads this marker and stops
154 * auto-installing, falling back to its "Install ThinkRank" notice, so a
155 * deliberate removal stays removed until the user asks for it back.
156 *
157 * Written last, after remove_options() has wiped the `thinkrank_` namespace,
158 * and cleared again by Activator on the next activation.
159 *
160 * @return void
161 */
162 private static function mark_uninstalled(): void {
163 delete_option(self::UNINSTALLED_OPTION);
164 add_option(self::UNINSTALLED_OPTION, time(), '', 'no');
165 }
166
167 /**
168 * Remove custom database tables
169 *
170 * @return void
171 */
172 private static function remove_database_tables(): void {
173 global $wpdb;
174
175 $tables = [
176 // AI/Core Tables (from Database class)
177 $wpdb->prefix . 'thinkrank_ai_cache',
178 $wpdb->prefix . 'thinkrank_ai_usage',
179 $wpdb->prefix . 'thinkrank_content_briefs',
180 $wpdb->prefix . 'thinkrank_seo_scores',
181 $wpdb->prefix . 'thinkrank_seo_performance',
182 $wpdb->prefix . 'thinkrank_instant_indexing_logs',
183 // SEO Tables (from Database_Schema)
184 $wpdb->prefix . 'thinkrank_seo_settings',
185 $wpdb->prefix . 'thinkrank_seo_analysis',
186 $wpdb->prefix . 'thinkrank_seo_keywords',
187 $wpdb->prefix . 'thinkrank_seo_schema',
188 $wpdb->prefix . 'thinkrank_seo_social',
189 $wpdb->prefix . 'thinkrank_seo_local',
190 $wpdb->prefix . 'thinkrank_email_report_logs',
191 // AI Visibility Tables. These were registered in Database_Schema but
192 // never listed here, so an uninstall left them behind — bv_tasks in
193 // particular holds the full text of every AI answer (#302).
194 $wpdb->prefix . 'thinkrank_ai_traffic',
195 $wpdb->prefix . 'thinkrank_brand_visibility_checks',
196 $wpdb->prefix . 'thinkrank_bv_runs',
197 $wpdb->prefix . 'thinkrank_bv_tasks',
198 // Rank Tracker, Redirections, Broken Links and Local SEO tables are
199 // created and dropped by ThinkRank Pro's own uninstaller. Free used to
200 // drop them here, which destroyed a still-active Pro install's data
201 // when only the free plugin was removed (#384).
202 ];
203
204 foreach ($tables as $table) {
205 // Check WordPress version for %i support (introduced in 6.2)
206 if (version_compare($GLOBALS['wp_version'], '6.2', '>=')) {
207 // phpcs:disable WordPress.DB.PreparedSQL.NotPrepared,WordPress.DB.PreparedSQLPlaceholders.UnsupportedIdentifierPlaceholder,WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching,WordPress.DB.DirectDatabaseQuery.SchemaChange
208 $wpdb->query($wpdb->prepare("DROP TABLE IF EXISTS %i", $table));
209 // phpcs:enable WordPress.DB.PreparedSQL.NotPrepared,WordPress.DB.PreparedSQLPlaceholders.UnsupportedIdentifierPlaceholder,WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching,WordPress.DB.DirectDatabaseQuery.SchemaChange
210 } else {
211 // Fallback for older WordPress versions - table name is from our controlled list
212 $escaped_table = esc_sql($table);
213 // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared,WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching,WordPress.DB.DirectDatabaseQuery.SchemaChange
214 $wpdb->query("DROP TABLE IF EXISTS `{$escaped_table}`");
215 // phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared,WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching,WordPress.DB.DirectDatabaseQuery.SchemaChange
216 }
217 }
218 }
219
220 /**
221 * Remove plugin options
222 *
223 * @return void
224 */
225 private static function remove_options(): void {
226 // Remove all ThinkRank options using wildcard delete for completeness.
227 //
228 // `thinkrank_%` also matches `thinkrank_pro_%`, so this used to delete a
229 // still-active Pro install's license key and every module setting (#384).
230 // Pro ships its own uninstaller and owns that namespace exclusively, so
231 // every pattern here excludes it.
232 global $wpdb;
233 // phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching,WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Uninstall cleanup requires direct database access to remove all plugin options
234 $wpdb->query(
235 $wpdb->prepare(
236 "DELETE FROM {$wpdb->options}
237 WHERE (option_name LIKE %s AND option_name NOT LIKE %s)
238 OR option_name LIKE %s",
239 $wpdb->esc_like('thinkrank_') . '%',
240 $wpdb->esc_like('thinkrank_pro_') . '%',
241 // Appsero / WP Insights telemetry options (wpins_thinkrank_*)
242 $wpdb->esc_like('wpins_thinkrank_') . '%'
243 )
244 );
245 // phpcs:enable WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching,WordPress.DB.PreparedSQL.InterpolatedNotPrepared
246
247 // Remove transients (prefixed with _transient_, not caught by options wildcard)
248 // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared,WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- Core table name is safe, uninstall cleanup requires direct database access
249 $wpdb->query(
250 $wpdb->prepare(
251 "DELETE FROM {$wpdb->options}
252 WHERE (option_name LIKE %s AND option_name NOT LIKE %s)
253 OR (option_name LIKE %s AND option_name NOT LIKE %s)",
254 $wpdb->esc_like('_transient_thinkrank_') . '%',
255 $wpdb->esc_like('_transient_thinkrank_pro_') . '%',
256 $wpdb->esc_like('_transient_timeout_thinkrank_') . '%',
257 $wpdb->esc_like('_transient_timeout_thinkrank_pro_') . '%'
258 )
259 );
260 // phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared,WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching
261 }
262
263 /**
264 * Remove user meta data
265 *
266 * @return void
267 */
268 private static function remove_user_meta(): void {
269 global $wpdb;
270
271 // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared,WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- Core table name is safe, uninstall cleanup requires direct database access
272 $wpdb->query(
273 $wpdb->prepare(
274 "DELETE FROM {$wpdb->usermeta}
275 WHERE meta_key LIKE %s
276 OR meta_key LIKE %s",
277 $wpdb->esc_like('thinkrank_') . '%',
278 $wpdb->esc_like('_thinkrank_') . '%'
279 )
280 );
281 // phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared,WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching
282 }
283
284 /**
285 * Remove post meta data
286 *
287 * @return void
288 */
289 private static function remove_post_meta(): void {
290 global $wpdb;
291
292 // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared,WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- Core table name is safe, uninstall cleanup requires direct database access
293 $wpdb->query(
294 $wpdb->prepare(
295 "DELETE FROM {$wpdb->postmeta}
296 WHERE meta_key LIKE %s
297 OR meta_key LIKE %s",
298 $wpdb->esc_like('thinkrank_') . '%',
299 $wpdb->esc_like('_thinkrank_') . '%'
300 )
301 );
302 // phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared,WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching
303 }
304
305
306 /**
307 * Remove term meta data
308 *
309 * The plugin writes term meta from the term UI, the abilities API
310 * (Update_Term_Seo) and the importer (Snapshot_Migrator) — SEO title,
311 * meta description, the robots payload and the `_thinkrank_imported_from`
312 * marker — and there was no counterpart to remove_post_meta(), so all of
313 * it outlived the plugin (#399).
314 *
315 * @return void
316 */
317 private static function remove_term_meta(): void {
318 global $wpdb;
319
320 // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared,WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- Core table name is safe, uninstall cleanup requires direct database access
321 $wpdb->query(
322 $wpdb->prepare(
323 "DELETE FROM {$wpdb->termmeta}
324 WHERE meta_key LIKE %s
325 OR meta_key LIKE %s",
326 $wpdb->esc_like('thinkrank_') . '%',
327 $wpdb->esc_like('_thinkrank_') . '%'
328 )
329 );
330 // phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared,WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching
331 }
332
333 /**
334 * Clear all caches
335 *
336 * @return void
337 */
338 private static function clear_caches(): void {
339 // Clear ThinkRank-specific object cache group
340 if (function_exists('wp_cache_flush_group')) {
341 wp_cache_flush_group('thinkrank');
342 }
343 }
344
345 /**
346 * Remove scheduled cron jobs
347 *
348 * @return void
349 */
350 private static function remove_cron_jobs(): void {
351 // Was 2 hooks against the 15 the plugin schedules, and weaker than the
352 // deactivator's 5 — so uninstalling without deactivating first left
353 // even more behind (#389).
354 foreach (self::manifest()['cron_hooks'] as $job) {
355 wp_clear_scheduled_hook($job);
356 }
357 }
358
359 /**
360 * Remove custom capabilities
361 *
362 * @return void
363 */
364 private static function remove_capabilities(): void {
365 // Runs even when the user keeps their data, so the capability-sync marker
366 // has to go with it. Leaving it behind would make Capability_Manager::ensure()
367 // short-circuit on the next install and never re-grant thinkrank_access,
368 // locking administrators out of the admin menu.
369 delete_option('thinkrank_caps_version');
370
371 // From the shared manifest. The hand-mirrored copy that used to live
372 // here had drifted from Capability_Manager::capabilities() by four
373 // slugs — thinkrank_ai_insights, thinkrank_redirections,
374 // thinkrank_broken_links and thinkrank_woocommerce were still granted
375 // to every role after an uninstall (#399). The manifest also carries
376 // the pre-Role-Manager slugs so historical roles are fully cleaned.
377 $capabilities = self::manifest()['capabilities'];
378
379 $roles = wp_roles();
380
381 foreach ($roles->roles as $role_name => $role_info) {
382 $role = get_role($role_name);
383 if ($role) {
384 foreach ($capabilities as $cap) {
385 $role->remove_cap($cap);
386 }
387 }
388 }
389 }
390 }
391
392 // Run the uninstaller
393 ThinkRank_Uninstaller::uninstall();
394