-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathuninstall.php
More file actions
202 lines (162 loc) · 5.79 KB
/
uninstall.php
File metadata and controls
202 lines (162 loc) · 5.79 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
<?php
/**
* Uninstall script.
*
* Handles complete cleanup when the plugin is uninstalled through WordPress.
* Removes all plugin options, transients, scheduled events, and log files.
*
* @package ThirdAudience
* @since 1.0.0
*/
// Exit if uninstall not called from WordPress.
if ( ! defined( 'WP_UNINSTALL_PLUGIN' ) ) {
exit;
}
// Prevent direct access.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Clean up all Third Audience plugin data.
*
* @since 1.1.0
*/
function ta_uninstall_cleanup() {
global $wpdb;
// =========================================================================
// Delete Plugin Options
// =========================================================================
// Core settings.
$options_to_delete = array(
// Service configuration.
'ta_router_url',
'ta_worker_url',
'ta_api_key',
'ta_api_key_encrypted',
// Cache settings.
'ta_cache_ttl',
// Feature settings.
'ta_enabled_post_types',
'ta_enable_content_negotiation',
'ta_enable_discovery_tags',
// Version tracking.
'ta_version',
'ta_db_version',
'ta_activated_at',
// SMTP settings.
'ta_smtp_settings',
// Notification settings.
'ta_notification_settings',
'ta_last_digest_sent',
// Logger data.
'ta_recent_errors',
'ta_error_stats',
// Bot Analytics.
'ta_bot_analytics_db_version',
);
foreach ( $options_to_delete as $option ) {
delete_option( $option );
}
// =========================================================================
// Delete Bot Analytics Database Table
// =========================================================================
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.SchemaChange
$wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}ta_bot_analytics" );
// =========================================================================
// Delete Transients
// =========================================================================
// Delete all cached markdown transients.
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$wpdb->query(
"DELETE FROM {$wpdb->options}
WHERE option_name LIKE '_transient_ta_md_%'
OR option_name LIKE '_transient_timeout_ta_md_%'"
);
// Delete notification rate-limiting transients.
delete_transient( 'ta_error_rate_notified' );
delete_transient( 'ta_worker_failure_notified' );
delete_transient( 'ta_cache_issue_notified' );
// Delete critical error notification transients.
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$wpdb->query(
"DELETE FROM {$wpdb->options}
WHERE option_name LIKE '_transient_ta_critical_notified_%'
OR option_name LIKE '_transient_timeout_ta_critical_notified_%'"
);
// Delete settings error transients.
delete_transient( 'settings_errors' );
// =========================================================================
// Clear Scheduled Events (Cron)
// =========================================================================
wp_clear_scheduled_hook( 'ta_daily_digest_cron' );
// =========================================================================
// Delete Log Files
// =========================================================================
$upload_dir = wp_upload_dir();
$log_dir = $upload_dir['basedir'] . '/third-audience-logs';
if ( is_dir( $log_dir ) ) {
// Get all files in the log directory.
$files = glob( $log_dir . '/*' );
if ( is_array( $files ) ) {
foreach ( $files as $file ) {
if ( is_file( $file ) ) {
// phpcs:ignore WordPress.WP.AlternativeFunctions.unlink_unlink
unlink( $file );
}
}
}
// Remove the directory itself.
// phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_rmdir
rmdir( $log_dir );
}
// =========================================================================
// Clean Up Rewrite Rules
// =========================================================================
// Flush rewrite rules to remove our custom rules.
flush_rewrite_rules();
// =========================================================================
// Multisite Cleanup (if applicable)
// =========================================================================
if ( is_multisite() ) {
// Get all site IDs.
$site_ids = get_sites( array( 'fields' => 'ids' ) );
foreach ( $site_ids as $site_id ) {
switch_to_blog( $site_id );
// Delete options for this site.
foreach ( $options_to_delete as $option ) {
delete_option( $option );
}
// Delete transients for this site.
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$wpdb->query(
"DELETE FROM {$wpdb->options}
WHERE option_name LIKE '_transient_ta_%'
OR option_name LIKE '_transient_timeout_ta_%'"
);
// Clear scheduled events.
wp_clear_scheduled_hook( 'ta_daily_digest_cron' );
// Delete bot analytics table.
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.SchemaChange
$wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}ta_bot_analytics" );
// Delete log files for this site.
$upload_dir = wp_upload_dir();
$log_dir = $upload_dir['basedir'] . '/third-audience-logs';
if ( is_dir( $log_dir ) ) {
$files = glob( $log_dir . '/*' );
if ( is_array( $files ) ) {
foreach ( $files as $file ) {
if ( is_file( $file ) ) {
// phpcs:ignore WordPress.WP.AlternativeFunctions.unlink_unlink
unlink( $file );
}
}
}
// phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_rmdir
rmdir( $log_dir );
}
restore_current_blog();
}
}
}
// Run the cleanup.
ta_uninstall_cleanup();