woocommerce
Last commit date
assets
6 days ago
i18n
6 days ago
includes
6 days ago
lib
4 weeks ago
packages
4 weeks ago
patterns
3 months ago
sample-data
2 years ago
src
1 week ago
templates
1 week ago
vendor
6 days ago
license.txt
6 years ago
readme.txt
6 days ago
uninstall.php
3 months ago
woocommerce.php
6 days ago
uninstall.php
142 lines
| 1 | <?php |
| 2 | /** |
| 3 | * WooCommerce Uninstall |
| 4 | * |
| 5 | * Uninstalling WooCommerce deletes user roles, pages, tables, and options. |
| 6 | * |
| 7 | * @package WooCommerce\Uninstaller |
| 8 | * @version 2.3.0 |
| 9 | */ |
| 10 | |
| 11 | defined( 'WP_UNINSTALL_PLUGIN' ) || exit; |
| 12 | |
| 13 | global $wpdb, $wp_version, $wc_uninstalling_plugin; |
| 14 | |
| 15 | $wc_uninstalling_plugin = true; |
| 16 | |
| 17 | // Clear WordPress cron events. |
| 18 | wp_clear_scheduled_hook( 'woocommerce_scheduled_sales' ); |
| 19 | wp_clear_scheduled_hook( 'woocommerce_cancel_unpaid_orders' ); |
| 20 | wp_clear_scheduled_hook( 'woocommerce_cleanup_sessions' ); |
| 21 | wp_clear_scheduled_hook( 'woocommerce_cleanup_personal_data' ); |
| 22 | wp_clear_scheduled_hook( 'woocommerce_cleanup_logs' ); |
| 23 | wp_clear_scheduled_hook( 'woocommerce_geoip_updater' ); |
| 24 | wp_clear_scheduled_hook( 'woocommerce_tracker_send_event' ); |
| 25 | wp_clear_scheduled_hook( 'woocommerce_cleanup_rate_limits' ); |
| 26 | wp_clear_scheduled_hook( 'wc_admin_daily' ); |
| 27 | wp_clear_scheduled_hook( 'generate_category_lookup_table' ); |
| 28 | wp_clear_scheduled_hook( 'wc_admin_unsnooze_admin_notes' ); |
| 29 | |
| 30 | if ( class_exists( ActionScheduler::class ) && ActionScheduler::is_initialized() && function_exists( 'as_unschedule_all_actions' ) ) { |
| 31 | as_unschedule_all_actions( 'woocommerce_scheduled_sales' ); |
| 32 | as_unschedule_all_actions( 'woocommerce_cancel_unpaid_orders' ); |
| 33 | as_unschedule_all_actions( 'woocommerce_cleanup_sessions' ); |
| 34 | as_unschedule_all_actions( 'woocommerce_cleanup_personal_data' ); |
| 35 | as_unschedule_all_actions( 'woocommerce_cleanup_logs' ); |
| 36 | as_unschedule_all_actions( 'woocommerce_geoip_updater' ); |
| 37 | as_unschedule_all_actions( 'woocommerce_tracker_send_event' ); |
| 38 | as_unschedule_all_actions( 'woocommerce_cleanup_rate_limits' ); |
| 39 | as_unschedule_all_actions( 'wc_admin_daily' ); |
| 40 | as_unschedule_all_actions( 'generate_category_lookup_table' ); |
| 41 | as_unschedule_all_actions( 'wc_admin_unsnooze_admin_notes' ); |
| 42 | } |
| 43 | |
| 44 | /* |
| 45 | * Only remove ALL product and page data if WC_REMOVE_ALL_DATA constant is set to true in user's |
| 46 | * wp-config.php. This is to prevent data loss when deleting the plugin from the backend |
| 47 | * and to ensure only the site owner can perform this action. |
| 48 | */ |
| 49 | if ( defined( 'WC_REMOVE_ALL_DATA' ) && true === WC_REMOVE_ALL_DATA ) { |
| 50 | // Load WooCommerce so we can access the container, install routines, etc, during uninstall. |
| 51 | require_once __DIR__ . '/includes/class-wc-install.php'; |
| 52 | |
| 53 | // Drop custom WordPress tables indexes. See \WC_Install::create_tables() for details. |
| 54 | $index_exists = $wpdb->get_row( "SHOW INDEX FROM {$wpdb->comments} WHERE key_name = 'woo_idx_comment_type';" ); |
| 55 | if ( null !== $index_exists ) { |
| 56 | $wpdb->query( "ALTER TABLE {$wpdb->comments} DROP INDEX woo_idx_comment_type;" ); |
| 57 | } |
| 58 | $date_type_index_exists = $wpdb->get_row( "SHOW INDEX FROM {$wpdb->comments} WHERE key_name = 'woo_idx_comment_date_type';" ); |
| 59 | if ( null !== $date_type_index_exists ) { |
| 60 | $wpdb->query( "ALTER TABLE {$wpdb->comments} DROP INDEX woo_idx_comment_date_type;" ); |
| 61 | } |
| 62 | $comment_approved_type_index_exists = $wpdb->get_row( "SHOW INDEX FROM {$wpdb->comments} WHERE key_name = 'woo_idx_comment_approved_type';" ); |
| 63 | if ( null !== $comment_approved_type_index_exists ) { |
| 64 | $wpdb->query( "ALTER TABLE {$wpdb->comments} DROP INDEX woo_idx_comment_approved_type;" ); |
| 65 | } |
| 66 | |
| 67 | // Roles + caps. |
| 68 | WC_Install::remove_roles(); |
| 69 | |
| 70 | // Pages. |
| 71 | wp_trash_post( get_option( 'woocommerce_shop_page_id' ) ); |
| 72 | wp_trash_post( get_option( 'woocommerce_cart_page_id' ) ); |
| 73 | wp_trash_post( get_option( 'woocommerce_checkout_page_id' ) ); |
| 74 | wp_trash_post( get_option( 'woocommerce_myaccount_page_id' ) ); |
| 75 | wp_trash_post( get_option( 'woocommerce_edit_address_page_id' ) ); |
| 76 | wp_trash_post( get_option( 'woocommerce_view_order_page_id' ) ); |
| 77 | wp_trash_post( get_option( 'woocommerce_change_password_page_id' ) ); |
| 78 | wp_trash_post( get_option( 'woocommerce_logout_page_id' ) ); |
| 79 | |
| 80 | if ( $wpdb->get_var( "SHOW TABLES LIKE '{$wpdb->prefix}woocommerce_attribute_taxonomies';" ) ) { |
| 81 | $wc_attributes = array_filter( (array) $wpdb->get_col( "SELECT attribute_name FROM {$wpdb->prefix}woocommerce_attribute_taxonomies;" ) ); |
| 82 | } else { |
| 83 | $wc_attributes = array(); |
| 84 | } |
| 85 | |
| 86 | // Tables. |
| 87 | WC_Install::drop_tables(); |
| 88 | |
| 89 | // Delete options. |
| 90 | $wpdb->query( "DELETE FROM $wpdb->options WHERE option_name LIKE 'woocommerce\_%';" ); |
| 91 | $wpdb->query( "DELETE FROM $wpdb->options WHERE option_name LIKE 'widget\_woocommerce\_%';" ); |
| 92 | |
| 93 | // Delete usermeta. |
| 94 | $wpdb->query( "DELETE FROM $wpdb->usermeta WHERE meta_key LIKE 'woocommerce\_%';" ); |
| 95 | |
| 96 | // Delete our data from the post and post meta tables, and remove any additional tables we created. |
| 97 | $wpdb->query( "DELETE FROM {$wpdb->posts} WHERE post_type IN ( 'product', 'product_variation', 'shop_coupon', 'shop_order', 'shop_order_refund' );" ); |
| 98 | $wpdb->query( "DELETE meta FROM {$wpdb->postmeta} meta LEFT JOIN {$wpdb->posts} posts ON posts.ID = meta.post_id WHERE posts.ID IS NULL;" ); |
| 99 | |
| 100 | $wpdb->query( "DELETE FROM {$wpdb->comments} WHERE comment_type IN ( 'order_note' );" ); |
| 101 | $wpdb->query( "DELETE meta FROM {$wpdb->commentmeta} meta LEFT JOIN {$wpdb->comments} comments ON comments.comment_ID = meta.comment_id WHERE comments.comment_ID IS NULL;" ); |
| 102 | |
| 103 | // Delete terms if > WP 4.2 (term splitting was added in 4.2). |
| 104 | if ( version_compare( $wp_version, '4.2', '>=' ) ) { |
| 105 | // Delete term taxonomies. |
| 106 | foreach ( array( 'product_cat', 'product_tag', 'product_shipping_class', 'product_type' ) as $_taxonomy ) { |
| 107 | $wpdb->delete( |
| 108 | $wpdb->term_taxonomy, |
| 109 | array( |
| 110 | 'taxonomy' => $_taxonomy, |
| 111 | ) |
| 112 | ); |
| 113 | } |
| 114 | |
| 115 | // Delete term attributes. |
| 116 | foreach ( $wc_attributes as $_taxonomy ) { |
| 117 | $wpdb->delete( |
| 118 | $wpdb->term_taxonomy, |
| 119 | array( |
| 120 | 'taxonomy' => 'pa_' . $_taxonomy, |
| 121 | ) |
| 122 | ); |
| 123 | } |
| 124 | |
| 125 | // Delete orphan relationships. |
| 126 | $wpdb->query( "DELETE tr FROM {$wpdb->term_relationships} tr LEFT JOIN {$wpdb->posts} posts ON posts.ID = tr.object_id WHERE posts.ID IS NULL;" ); |
| 127 | |
| 128 | // Delete orphan terms. |
| 129 | $wpdb->query( "DELETE t FROM {$wpdb->terms} t LEFT JOIN {$wpdb->term_taxonomy} tt ON t.term_id = tt.term_id WHERE tt.term_id IS NULL;" ); |
| 130 | |
| 131 | // Delete orphan term meta. |
| 132 | if ( ! empty( $wpdb->termmeta ) ) { |
| 133 | $wpdb->query( "DELETE tm FROM {$wpdb->termmeta} tm LEFT JOIN {$wpdb->term_taxonomy} tt ON tm.term_id = tt.term_id WHERE tt.term_id IS NULL;" ); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | // Clear any cached data that has been removed. |
| 138 | wp_cache_flush(); |
| 139 | } |
| 140 | |
| 141 | unset( $wc_uninstalling_plugin ); |
| 142 |