-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuninstall.php
More file actions
134 lines (109 loc) · 5.5 KB
/
Copy pathuninstall.php
File metadata and controls
134 lines (109 loc) · 5.5 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
<?php
/**
* Uninstall WPConsent.
*
* Remove:
* - Custom post types and taxonomies
* - Plugin settings and options
* - Custom database tables
* - Uploaded files
* - Scheduled events
*
* @package WPConsent
*/
// Exit if accessed directly.
if ( ! defined( 'WP_UNINSTALL_PLUGIN' ) ) {
exit;
}
// If the function already exists we shouldn't run the uninstall as another version of the plugin is active.
if ( function_exists( 'wpconsent' ) ) {
return;
}
// Load the main plugin file to access the classes.
require_once 'wpconsent.php';
// Clear any scheduled events.
wp_clear_scheduled_hook( 'wpconsent_auto_scanner' );
wp_clear_scheduled_hook( 'wpconsent_cleanup_scan_history' );
wp_clear_scheduled_hook( 'wpconsent_usage_tracking_cron' );
// Remove notifications.
if ( class_exists( 'WPConsent_Notifications' ) ) {
WPConsent_Notifications::delete_notifications_data();
}
// Let's see if the uninstall_data option is set.
$wpconsent_settings = get_option( 'wpconsent_settings', array() );
if ( ! empty( $wpconsent_settings['uninstall_data'] ) ) {
// Remove custom post types and taxonomies.
global $wpdb;
// Let's make sure our post type and taxonomy are registered.
wpconsent()->load_components();
wpconsent()->cookies->register_post_type();
wpconsent()->cookies->register_taxonomy();
// Delete all posts of our custom post type.
$wpconsent_cookies = get_posts(
array(
'post_type' => 'wpconsent_cookie',
'post_status' => 'any',
'numberposts' => - 1,
'fields' => 'ids',
)
);
foreach ( $wpconsent_cookies as $wpconsent_cookie_id ) {
wp_delete_post( $wpconsent_cookie_id, true );
}
// Delete all terms and taxonomies.
$wpconsent_category_terms = get_terms(
array(
'taxonomy' => 'wpconsent_category',
'hide_empty' => false,
'fields' => 'ids',
)
);
if ( ! is_wp_error( $wpconsent_category_terms ) ) {
foreach ( $wpconsent_category_terms as $wpconsent_category_term_id ) {
wp_delete_term( $wpconsent_category_term_id, 'wpconsent_category' );
}
}
// Delete all plugin options.
$wpdb->query( "DELETE FROM {$wpdb->options} WHERE option_name LIKE 'wpconsent\_%'" ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
// Delete all plugin user meta.
$wpdb->query( "DELETE FROM {$wpdb->usermeta} WHERE meta_key LIKE 'wpconsent\_%'" ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
// Delete all plugin post meta.
$wpdb->query( "DELETE FROM {$wpdb->postmeta} WHERE meta_key LIKE 'wpconsent\_%'" ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$wpdb->query( "DELETE FROM {$wpdb->postmeta} WHERE meta_key LIKE '\_wpconsent\_%'" ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
// Remove any transients we've left behind.
$wpdb->query( "DELETE FROM {$wpdb->options} WHERE option_name LIKE '\_transient\_wpconsent\_%'" ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$wpdb->query( "DELETE FROM {$wpdb->options} WHERE option_name LIKE '\_site\_transient\_wpconsent\_%'" ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$wpdb->query( "DELETE FROM {$wpdb->options} WHERE option_name LIKE '\_transient\_timeout\_wpconsent\_%'" ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$wpdb->query( "DELETE FROM {$wpdb->options} WHERE option_name LIKE '\_site\_transient\_timeout\_wpconsent\_%'" ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
// Remove uploaded files.
$wpconsent_uploads_directory = wp_upload_dir();
if ( empty( $wpconsent_uploads_directory['error'] ) ) {
global $wp_filesystem;
WP_Filesystem();
// Remove the wpconsent directory from uploads.
$wp_filesystem->rmdir( $wpconsent_uploads_directory['basedir'] . '/wpconsent/', true );
}
// Remove translation files.
$wpconsent_languages_directory = defined( 'WP_LANG_DIR' ) ? trailingslashit( WP_LANG_DIR ) : trailingslashit( WP_CONTENT_DIR ) . 'languages/';
$wpconsent_translations = glob( wp_normalize_path( $wpconsent_languages_directory . 'plugins/wpconsent-*' ) );
if ( ! empty( $wpconsent_translations ) ) {
global $wp_filesystem;
WP_Filesystem();
foreach ( $wpconsent_translations as $wpconsent_file ) {
$wp_filesystem->delete( $wpconsent_file );
}
}
// Maybe drop the "records of consent" table.
if ( class_exists( 'WPConsent_Consent_Log' ) ) {
$wpconsent_records_table = esc_sql( $wpdb->prefix . 'wpconsent_consent_logs' );
$wpdb->query( "DROP TABLE IF EXISTS `{$wpconsent_records_table}`" ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared
}
// Maybe drop the "Do Not Track" table.
if ( class_exists( 'WPConsent_DNT_DB' ) ) {
$wpconsent_dnt_table = esc_sql( $wpdb->prefix . 'wpconsent_dnt_requests' );
$wpdb->query( "DROP TABLE IF EXISTS `{$wpconsent_dnt_table}`" ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared
}
// Drop the scan history table.
$wpconsent_scan_history_table = esc_sql( $wpdb->prefix . 'wpconsent_scan_history' );
$wpdb->query( "DROP TABLE IF EXISTS `{$wpconsent_scan_history_table}`" ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared
}