-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuninstall.php
More file actions
297 lines (261 loc) · 7.43 KB
/
uninstall.php
File metadata and controls
297 lines (261 loc) · 7.43 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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
<?php
/**
* Uninstall handler
*
* Handles complete removal of plugin data when uninstalled.
* This file is called by WordPress when the user deletes the plugin.
*
* IMPORTANT: By default, this script preserves ALL data to prevent accidental data loss.
* Data is only removed if the user has explicitly enabled "Remove all data on uninstall"
* in the plugin settings page. This includes:
* - Database tables
* - Options
* - User meta
* - Post meta
* - Capabilities and roles
* - Transients
*
* @package PressPrimer_Quiz
* @since 1.0.0
*/
// If uninstall not called from WordPress, exit
if ( ! defined( 'WP_UNINSTALL_PLUGIN' ) ) {
exit;
}
// Define plugin path constant if not already defined
if ( ! defined( 'PRESSPRIMER_QUIZ_PLUGIN_PATH' ) ) {
define( 'PRESSPRIMER_QUIZ_PLUGIN_PATH', plugin_dir_path( __FILE__ ) );
}
/**
* Remove plugin data
*
* Only runs if user has explicitly opted in to remove all data.
* By default, all data is preserved to prevent accidental data loss.
*/
function pressprimer_quiz_uninstall() {
global $wpdb;
// Get settings - use empty array as default
$settings = get_option( 'pressprimer_quiz_settings', [] );
// Check if user has EXPLICITLY opted to remove data on uninstall
// Default behavior is to KEEP all data for safety
// CRITICAL: Only delete data if the setting is EXPLICITLY set to boolean true
$remove_data = false;
if ( is_array( $settings ) && isset( $settings['remove_data_on_uninstall'] ) ) {
$remove_data = ( true === $settings['remove_data_on_uninstall'] || '1' === $settings['remove_data_on_uninstall'] || 1 === $settings['remove_data_on_uninstall'] );
}
if ( ! $remove_data ) {
// Keep all data - exit without removing anything
return;
}
// User has explicitly chosen to remove all data
// Proceed with complete removal
// Remove all database tables
pressprimer_quiz_drop_tables();
// Remove all options
pressprimer_quiz_remove_options();
// Remove all user meta
pressprimer_quiz_remove_user_meta();
// Remove all post meta
pressprimer_quiz_remove_post_meta();
// Remove capabilities and roles
pressprimer_quiz_remove_capabilities();
// Clear any remaining transients
pressprimer_quiz_clear_transients();
}
/**
* Drop all plugin database tables
*
* Handles both single site and multisite installations.
* For multisite, drops tables for all sites in the network.
*
* @since 1.0.0
*/
function pressprimer_quiz_drop_tables() {
global $wpdb;
// Table names without prefix
$table_names = [
'ppq_questions',
'ppq_question_revisions',
'ppq_categories',
'ppq_question_tax',
'ppq_banks',
'ppq_bank_questions',
'ppq_quizzes',
'ppq_quiz_items',
'ppq_quiz_rules',
'ppq_groups',
'ppq_group_members',
'ppq_assignments',
'ppq_attempts',
'ppq_attempt_items',
'ppq_events',
];
if ( is_multisite() ) {
// Get all site IDs in the network
$site_ids = get_sites( [ 'fields' => 'ids' ] );
foreach ( $site_ids as $site_id ) {
$prefix = $wpdb->get_blog_prefix( $site_id );
foreach ( $table_names as $table_name ) {
$full_table_name = $prefix . $table_name;
$wpdb->query( "DROP TABLE IF EXISTS {$full_table_name}" ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
}
}
} else {
// Single site installation
foreach ( $table_names as $table_name ) {
$full_table_name = $wpdb->prefix . $table_name;
$wpdb->query( "DROP TABLE IF EXISTS {$full_table_name}" ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
}
}
}
/**
* Remove all plugin options
*
* Handles both single site and multisite installations.
* For multisite, removes options from all sites and network options.
*
* @since 1.0.0
*/
function pressprimer_quiz_remove_options() {
global $wpdb;
if ( is_multisite() ) {
// Get all site IDs in the network
$site_ids = get_sites( [ 'fields' => 'ids' ] );
foreach ( $site_ids as $site_id ) {
$prefix = $wpdb->get_blog_prefix( $site_id );
// Delete all options that start with pressprimer_quiz_ for this site
$wpdb->query(
$wpdb->prepare(
"DELETE FROM {$prefix}options WHERE option_name LIKE %s", // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
$wpdb->esc_like( 'pressprimer_quiz_' ) . '%'
)
);
}
// Delete network-wide site options
$wpdb->query(
$wpdb->prepare(
"DELETE FROM {$wpdb->sitemeta}
WHERE meta_key LIKE %s",
$wpdb->esc_like( 'pressprimer_quiz_' ) . '%'
)
);
} else {
// Single site - delete all options that start with pressprimer_quiz_
$wpdb->query(
$wpdb->prepare(
"DELETE FROM {$wpdb->options}
WHERE option_name LIKE %s",
$wpdb->esc_like( 'pressprimer_quiz_' ) . '%'
)
);
}
}
/**
* Remove all plugin user meta
*
* @since 1.0.0
*/
function pressprimer_quiz_remove_user_meta() {
global $wpdb;
// Delete all user meta that starts with pressprimer_quiz_
$wpdb->query(
$wpdb->prepare(
"DELETE FROM {$wpdb->usermeta}
WHERE meta_key LIKE %s",
$wpdb->esc_like( 'pressprimer_quiz_' ) . '%'
)
);
}
/**
* Remove all plugin post meta
*
* @since 1.0.0
*/
function pressprimer_quiz_remove_post_meta() {
global $wpdb;
// Delete all post meta that starts with pressprimer_quiz_
$wpdb->query(
$wpdb->prepare(
"DELETE FROM {$wpdb->postmeta}
WHERE meta_key LIKE %s",
$wpdb->esc_like( 'pressprimer_quiz_' ) . '%'
)
);
}
/**
* Remove capabilities from all roles
*
* Handles both single site and multisite installations.
* For multisite, removes capabilities from all sites.
*
* @since 1.0.0
*/
function pressprimer_quiz_remove_capabilities() {
if ( is_multisite() ) {
// Get all site IDs in the network
$site_ids = get_sites( [ 'fields' => 'ids' ] );
foreach ( $site_ids as $site_id ) {
switch_to_blog( $site_id );
pressprimer_quiz_remove_site_capabilities();
restore_current_blog();
}
} else {
pressprimer_quiz_remove_site_capabilities();
}
}
/**
* Remove capabilities from roles for a single site
*
* @since 1.0.0
*/
function pressprimer_quiz_remove_site_capabilities() {
// Remove capabilities from administrator
$admin = get_role( 'administrator' );
if ( $admin ) {
$admin->remove_cap( 'pressprimer_quiz_manage_all' );
$admin->remove_cap( 'pressprimer_quiz_manage_own' );
$admin->remove_cap( 'pressprimer_quiz_view_results_all' );
$admin->remove_cap( 'pressprimer_quiz_view_results_own' );
$admin->remove_cap( 'pressprimer_quiz_take_quiz' );
$admin->remove_cap( 'pressprimer_quiz_manage_settings' );
}
// Remove capabilities from subscriber
$subscriber = get_role( 'subscriber' );
if ( $subscriber ) {
$subscriber->remove_cap( 'pressprimer_quiz_take_quiz' );
}
// Remove custom teacher role
remove_role( 'pressprimer_quiz_teacher' );
}
/**
* Clear all plugin transients
*
* @since 1.0.0
*/
function pressprimer_quiz_clear_transients() {
global $wpdb;
// Delete all transients that start with pressprimer_quiz_
$wpdb->query(
$wpdb->prepare(
"DELETE FROM {$wpdb->options}
WHERE option_name LIKE %s
OR option_name LIKE %s",
$wpdb->esc_like( '_transient_pressprimer_quiz_' ) . '%',
$wpdb->esc_like( '_transient_timeout_pressprimer_quiz_' ) . '%'
)
);
// If multisite, delete site transients
if ( is_multisite() ) {
$wpdb->query(
$wpdb->prepare(
"DELETE FROM {$wpdb->sitemeta}
WHERE meta_key LIKE %s
OR meta_key LIKE %s",
$wpdb->esc_like( '_site_transient_pressprimer_quiz_' ) . '%',
$wpdb->esc_like( '_site_transient_timeout_pressprimer_quiz_' ) . '%'
)
);
}
}
// Run the uninstall
pressprimer_quiz_uninstall();