-
Notifications
You must be signed in to change notification settings - Fork 10
/
uninstall.php
executable file
·188 lines (153 loc) · 4.53 KB
/
uninstall.php
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
<?php
/**
* CommentPress Core Uninstaller.
*
* @package CommentPress_Core
*/
// Kick out if uninstall not called from WordPress.
if ( ! defined( 'WP_UNINSTALL_PLUGIN' ) ) {
exit();
}
/**
* Restores all CommentPress-enabled Sites.
*
* NOTE: For a large Network, this could be a very lengthy process. There will
* be an AJAX-driven UI for this task in future.
*
* @since 4.0
*/
function commentpress_sites_restore() {
// Only restore current Site if not multisite.
if ( ! is_multisite() ) {
commentpress_schema_restore();
commentpress_options_delete();
return;
}
// Get the Site IDs.
$site_ids = get_site_option( 'commentpress_sites', [] );
if ( empty( $site_ids ) ) {
return;
}
// Restore each Site.
foreach ( $site_ids as $site_id ) {
// We have to switch to the Blog.
switch_to_blog( $site_id );
// Restore WordPress database schema.
commentpress_schema_restore();
// Remove our custom Comment Taxonomy.
commentpress_taxonomy_restore( 'comment_tags' );
// Delete options.
commentpress_options_delete();
}
// Restore.
restore_current_blog();
// Delete multisite options.
commentpress_site_options_delete();
}
/**
* Restores the WordPress database schema.
*
* @since 3.0
*
* @return boolean $result The result of the database operation.
*/
function commentpress_schema_restore() {
// Database object.
global $wpdb;
// Include WordPress install helper script.
require_once ABSPATH . 'wp-admin/install-helper.php';
// Drop the column, if already there.
$result = maybe_drop_column(
$wpdb->comments,
'comment_signature',
"ALTER TABLE `$wpdb->comments` DROP `comment_signature`;"
);
// Write something to the logs on failure.
if ( ! $result ) {
// Build message.
$message = sprintf(
'Could not drop "comment_signature" column in table "%s".',
$wpdb->comments
);
// Build data to log.
$e = new \Exception();
$trace = $e->getTraceAsString();
$data = [
'CommentPress Uninstall Error' => $message,
'backtrace' => $trace,
];
// Format data.
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_print_r
$error = print_r( $data, true );
// Write to log file.
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log
error_log( $error );
}
// --<
return $result;
}
/**
* Deletes a custom Taxonomy and all its data.
*
* @see https://gist.github.com/wpsmith/9285391#file-uninstall-terms-taxonomy-2-php
*
* @since 4.0
*
* @param str $taxonomy The name of the Taxonomy to delete.
*/
function commentpress_taxonomy_restore( $taxonomy ) {
// Access DB object.
global $wpdb;
// Get Terms.
// phpcs:ignore: WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$terms = $wpdb->get_results(
$wpdb->prepare(
"SELECT t.*, tt.* FROM {$wpdb->terms} AS t " .
"INNER JOIN {$wpdb->term_taxonomy} AS tt ON t.term_id = tt.term_id " .
'WHERE tt.taxonomy IN (%s) ' .
'ORDER BY t.name ASC',
$taxonomy
)
);
// Delete each one in turn - if we get any.
if ( ! empty( $terms ) ) {
foreach ( $terms as $term ) {
// phpcs:ignore: WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$wpdb->delete( $wpdb->term_taxonomy, [ 'term_taxonomy_id' => $term->term_taxonomy_id ] );
// phpcs:ignore: WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$wpdb->delete( $wpdb->term_relationships, [ 'term_taxonomy_id' => $term->term_taxonomy_id ] );
// phpcs:ignore: WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$wpdb->delete( $wpdb->terms, [ 'term_id' => $term->term_id ] );
}
}
// Delete the Taxonomy itself.
// phpcs:ignore: WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$wpdb->delete( $wpdb->term_taxonomy, [ 'taxonomy' => $taxonomy ], [ '%s' ] );
// Lastly, flush rules.
flush_rewrite_rules();
}
/**
* Deletes the core options.
*
* Make sure these match those declared in CommentPress_Core_Database.
*
* @since 4.0
*/
function commentpress_options_delete() {
delete_option( 'commentpress_version' );
delete_option( 'commentpress_options' );
}
/**
* Deletes the multisite options.
*
* Make sure these match those declared in CommentPress_Multisite_Database.
*
* @since 4.0
*/
function commentpress_site_options_delete() {
delete_site_option( 'cpmu_options' );
delete_site_option( 'cpmu_version' );
delete_site_option( 'commentpress_sites' );
}
// Restore all Sites to pre-CommentPress state.
commentpress_sites_restore();