forked from getdokan/dokan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
uninstall.php
344 lines (309 loc) · 11.5 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
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
<?php
if ( ! defined( 'WP_UNINSTALL_PLUGIN' ) ) {
exit;
}
/**
* Dokan Uninstall
*
* Uninstalling Dokan deletes user roles, tables, pages, meta data and options.
*
* @since 3.2.15
*
* @package Dokan\Uninstaller
*/
class Dokan_Uninstaller {
/**
* Constructor for the class Dokan_Uninstaller
*
* @since 3.2.15
*/
public function __construct() {
global $wpdb;
$general_options = get_option( 'dokan_general', [] );
$setting = array_key_exists( 'data_clear_on_uninstall', $general_options ) ? $general_options['data_clear_on_uninstall'] : 'off';
/*
* Only remove data when Dokan data clear setting is enabled by admin
* This is to prevent data loss when deleting the plugin and to ensure only the site owner can perform this action.
*/
if ( 'on' === $setting ) {
// Make existing vendors to Woocommerce Customer
$this->change_vendor_role_to_customer();
// Roles + caps.
$this->remove_roles();
// Drop Dokan related Tables
$this->drop_tables();
// delete user metas related to dokan and dokan pro
$this->delete_usermeta();
// Delete Pages created by dokan
$pages = get_option( 'dokan_pages', [] );
foreach ( $pages as $page_id ) {
wp_delete_post( $page_id, true );
}
// Delete Dokan related options
$wpdb->query( "DELETE FROM $wpdb->options WHERE option_name LIKE '%dokan%' AND option_name NOT LIKE '{$wpdb->prefix}user_roles'" );
// Clear any cached data that has been removed.
wp_cache_flush();
}
}
/**
* Return a list of Dokan capabilities
*
* @since 3.2.15
*
* @return string[]
*/
private function get_dokan_capabilities() {
require_once dirname( __FILE__ ) . '/includes/functions.php';
$capabilities = [];
foreach ( dokan_get_all_caps() as $cap ) {
$capabilities = array_merge( $capabilities, array_keys( $cap ) );
}
$capabilities[] = 'dokandar';
/* Capabilities added from RMA module */
$capabilities = array_merge( $capabilities, [ 'dokan_view_store_rma_menu', 'dokan_view_store_rma_settings_menu' ] );
/* Capabilities added from Booking module */
$capabilities = array_merge(
$capabilities,
[
'dokan_view_booking_menu',
'dokan_add_booking_product',
'dokan_edit_booking_product',
'dokan_delete_booking_product',
'dokan_manage_booking_products',
'dokan_manage_booking_calendar',
'dokan_manage_bookings',
'dokan_manage_booking_resource',
]
);
/* Capabilities added from Support Ticket module */
$capabilities = array_merge( $capabilities, [ 'dokan_manage_support_tickets' ] );
/* Capabilities added from ExportImport module */
$capabilities = array_merge( $capabilities, [ 'dokan_view_tools_menu' ] );
/* Capabilities added from Vendor Verification module */
$capabilities = array_merge( $capabilities, [ 'dokan_view_store_verification_menu' ] );
/* Capabilities added from Simple Auction module */
$capabilities = array_merge(
$capabilities, [
'dokan_view_auction_menu',
'dokan_add_auction_product',
'dokan_edit_auction_product',
'dokan_delete_auction_product',
]
);
return $capabilities;
}
/**
* Remove Dokan roles.
*
* @since 3.2.15
*
* @return void
*/
private function remove_roles() {
$wp_roles = wp_roles();
if ( ! is_a( $wp_roles, 'WP_Roles' ) ) {
return;
}
foreach ( $this->get_dokan_capabilities() as $capability ) {
$wp_roles->remove_cap( 'administrator', $capability );
$wp_roles->remove_cap( 'shop_manager', $capability );
}
remove_role( 'seller' );
remove_role( 'vendor_staff' );
}
/**
* Return a list of tables. Used to make sure all Dokan tables are dropped
* when uninstalling the plugin
*
* @since 3.2.15
*
* @return array Dokan tables.
*/
private function get_tables() {
return [
'dokan_refund',
'dokan_withdraw',
'dokan_announcement',
'dokan_orders',
'dokan_vendor_balance',
'dokan_product_map',
'dokan_follow_store_followers',
'dokan_report_abuse_reports',
'dokan_rma_conversations',
'dokan_rma_request',
'dokan_rma_request_product',
'dokan_shipping_zone_methods',
'dokan_shipping_zone_locations',
'dokan_shipping_tracking',
'dokan_delivery_time',
'dokan_seller_badge',
'dokan_seller_badge_level',
'dokan_seller_badge_acquired',
'dokan_request_quotes',
'dokan_request_quote_rules',
'dokan_request_quote_details',
'dokan_reverse_withdrawal',
'dokan_advertised_products',
'dokan_table_rate_shipping',
'dokan_distance_rate_shipping',
];
}
/**
* Drop all tables created by Dokan Lite and Pro
*
* @since 3.2.15
*
* @return void
*/
private function drop_tables() {
global $wpdb;
$tables = $this->get_tables();
foreach ( $tables as $table ) {
$wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}{$table}" ); // phpcs:ignore
}
}
/**
* Change Dokan Vendor to WooCommerce Customer
*
* @since 3.2.15
*
* @return void
*/
private function change_vendor_role_to_customer() {
global $wpdb;
$args = [
'role__in' => [ 'seller' ],
'number' => - 1,
'fields' => 'ids',
];
$user_query = new WP_User_Query( $args );
$results = $user_query->get_results();
if ( empty( $results ) ) {
return;
}
$capabilities = maybe_serialize( [ 'customer' => 1 ] );
$wpdb->query( "UPDATE {$wpdb->prefix}usermeta SET meta_value = '{$capabilities}' WHERE meta_key = '{$wpdb->prefix}capabilities' AND user_id IN (" . implode( ",", array_map( 'absint', $results ) ) . ")" ); // phpcs:ignore
}
/**
* Delete Dokan and Dokan Pro related user metas
*
* @since 3.7.12
*
* @return void
*/
private function delete_usermeta() {
global $wpdb;
$user_metas = [
'billing_dokan_company_id_number',
'billing_dokan_vat_number',
'billing_dokan_bank_name',
'billing_dokan_bank_iban',
'dokan_admin_percentage_type',
'dokan_admin_percentage',
'dokan_admin_additional_fee',
'dokan_bank_name',
'dokan_bank_iban',
'dokan_company_name',
'dokan_company_id_number',
'dokan_disable_auction',
'dokan_enable_selling',
'dokan_enable_seller_vacation',
'dokan_feature_seller',
'dokan_geo_latitude',
'dokan_geo_longitude',
'dokan_geo_public',
'dokan_geo_address',
'dokan_has_active_cancelled_subscrption',
'dokan_profile_settings',
'dokan_publishing',
'dokan_reverse_withdrawal_product_id',
'dokan_rank_math_edit_post_id',
'dokan_store_name',
'dokan_stripe_customer_id',
'dokan_vat_number',
'dokan_vendor_verification_folder_hash',
'dokan_vendor_seen_setup_wizard',
'dokan_verification_status',
'dokan_wcpdf_documents_settings_invoice',
'dokan_withdraw_selected_schedule',
'dokan_withdraw_selected_minimum_balance',
'dokan_withdraw_selected_reserve_balance',
'dokan_withdraw_default_method',
'product_pack_startdate',
'can_post_product',
'product_pack_enddate',
'product_package_id',
'product_order_id',
'product_no_with_pack',
'_customer_recurring_subscription',
'subscription_code',
'shipstation_auth_key',
'shipstation_export_statuses',
'shipstation_shipped_status',
'sms_last_sent',
'vendor_moip_token',
'vendor_moip_account',
'withdraw_date_limit',
'_dokan_vendor_delivery_time_settings',
'_dokan_vendor_delivery_time_slots',
'_dokan_mangopay_account_id_sandbox',
'_dokan_mangopay_account_id',
'_dokan_mangopay_user_business_type',
'_dokan_mangopay_user_birthday',
'_dokan_mangopay_user_nationality',
'_dokan_mangopay_user_status',
'_dokan_mangopay_bank_account_id_IBAN_sandbox',
'_dokan_mangopay_bank_account_id_IBAN',
'_dokan_mangopay_active_bank_account_sandbox',
'_dokan_mangopay_active_bank_account',
'_dokan_mangopay_kyc_error',
'_dokan_paypal_create_partner_referral_debug_id',
'_dokan_paypal_marketplace_vendor_subscription_id',
'_dokan_paypal_marketplace_settings',
'_dokan_paypal_test_marketplace_settings',
'_dokan_paypal_test_merchant_id',
'_dokan_paypal_merchant_id',
'_dokan_paypal_enable_for_receive_payment',
'_dokan_paypal_test_enable_for_receive_payment',
'_dokan_paypal_payments_receivable',
'_dokan_paypal_test_payments_receivable',
'_dokan_paypal_primary_email_confirmed',
'_dokan_paypal_test_primary_email_confirmed',
'_dokan_paypal_test_enable_for_ucc',
'_dokan_paypal_enable_for_ucc',
'_dokan_rma_settings',
'_dokan_reverse_withdrawal_failed_actions',
'_dokan_reverse_withdrawal_threshold_exceeded_date',
'_dokan_razorpay_test_enable_for_receive_payment',
'_dokan_razorpay_enable_for_receive_payment',
'_dokan_razorpay_payments_receivable',
'_dokan_razorpay_test_payments_receivable',
'_dokan_razorpay_account_id',
'_dokan_razorpay_test_account_id',
'_dokan_razorpay_test_account_id_trashed',
'_dokan_razorpay_account_id_trashed',
'_dokan_subscription_is_on_trial',
'_dokan_subscription_trial_until',
'_dokan_vendor_delivery_time_settings',
'_dokan_vendor_delivery_time_slots',
'_dps_zone_wise_settings',
'_dps_shipping_enable',
'_dokan_shipping_type',
'_dps_shipping_type_price',
'_dps_additional_product',
'_dps_additional_qty',
'_dps_pt',
'_dps_ship_policy',
'_dps_refund_policy',
'_dps_form_location',
'_dps_country_rates',
'_dps_state_rates',
'_is_dokan_wholesale_customer',
'_dokan_wholesale_customer_status',
];
// Delete Dokan related user_meta
$meta_keys = "'" . implode( "','", esc_sql( $user_metas ) ) . "'";
$wpdb->query( "DELETE FROM $wpdb->usermeta WHERE meta_key IN ($meta_keys)" ); //phpcs:ignore
}
}
new Dokan_Uninstaller();