woocommerce
Last commit date
assets
1 year ago
i18n
1 year ago
includes
1 year ago
lib
1 year ago
packages
1 year ago
patterns
1 year ago
sample-data
2 years ago
src
1 year ago
templates
1 year ago
vendor
1 year ago
license.txt
6 years ago
readme.txt
1 year ago
uninstall.php
3 years ago
woocommerce.php
1 year ago
uninstall.php
118 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 | use Automattic\WooCommerce\Admin\Notes\Notes; |
| 12 | use Automattic\WooCommerce\Admin\ReportsSync; |
| 13 | use Automattic\WooCommerce\Internal\DataStores\Orders\OrdersTableDataStore; |
| 14 | |
| 15 | defined( 'WP_UNINSTALL_PLUGIN' ) || exit; |
| 16 | |
| 17 | global $wpdb, $wp_version; |
| 18 | |
| 19 | wp_clear_scheduled_hook( 'woocommerce_scheduled_sales' ); |
| 20 | wp_clear_scheduled_hook( 'woocommerce_cancel_unpaid_orders' ); |
| 21 | wp_clear_scheduled_hook( 'woocommerce_cleanup_sessions' ); |
| 22 | wp_clear_scheduled_hook( 'woocommerce_cleanup_personal_data' ); |
| 23 | wp_clear_scheduled_hook( 'woocommerce_cleanup_logs' ); |
| 24 | wp_clear_scheduled_hook( 'woocommerce_geoip_updater' ); |
| 25 | wp_clear_scheduled_hook( 'woocommerce_tracker_send_event' ); |
| 26 | wp_clear_scheduled_hook( 'woocommerce_cleanup_rate_limits' ); |
| 27 | wp_clear_scheduled_hook( 'wc_admin_daily' ); |
| 28 | wp_clear_scheduled_hook( 'generate_category_lookup_table' ); |
| 29 | wp_clear_scheduled_hook( 'wc_admin_unsnooze_admin_notes' ); |
| 30 | |
| 31 | /* |
| 32 | * Only remove ALL product and page data if WC_REMOVE_ALL_DATA constant is set to true in user's |
| 33 | * wp-config.php. This is to prevent data loss when deleting the plugin from the backend |
| 34 | * and to ensure only the site owner can perform this action. |
| 35 | */ |
| 36 | if ( defined( 'WC_REMOVE_ALL_DATA' ) && true === WC_REMOVE_ALL_DATA ) { |
| 37 | // Load WooCommerce so we can access the container, install routines, etc, during uninstall. |
| 38 | require_once __DIR__ . '/woocommerce.php'; |
| 39 | |
| 40 | // Roles + caps. |
| 41 | WC_Install::remove_roles(); |
| 42 | |
| 43 | // Pages. |
| 44 | wp_trash_post( get_option( 'woocommerce_shop_page_id' ) ); |
| 45 | wp_trash_post( get_option( 'woocommerce_cart_page_id' ) ); |
| 46 | wp_trash_post( get_option( 'woocommerce_checkout_page_id' ) ); |
| 47 | wp_trash_post( get_option( 'woocommerce_myaccount_page_id' ) ); |
| 48 | wp_trash_post( get_option( 'woocommerce_edit_address_page_id' ) ); |
| 49 | wp_trash_post( get_option( 'woocommerce_view_order_page_id' ) ); |
| 50 | wp_trash_post( get_option( 'woocommerce_change_password_page_id' ) ); |
| 51 | wp_trash_post( get_option( 'woocommerce_logout_page_id' ) ); |
| 52 | |
| 53 | if ( $wpdb->get_var( "SHOW TABLES LIKE '{$wpdb->prefix}woocommerce_attribute_taxonomies';" ) ) { |
| 54 | $wc_attributes = array_filter( (array) $wpdb->get_col( "SELECT attribute_name FROM {$wpdb->prefix}woocommerce_attribute_taxonomies;" ) ); |
| 55 | } else { |
| 56 | $wc_attributes = array(); |
| 57 | } |
| 58 | |
| 59 | // Tables. |
| 60 | WC_Install::drop_tables(); |
| 61 | |
| 62 | // Delete options. |
| 63 | $wpdb->query( "DELETE FROM $wpdb->options WHERE option_name LIKE 'woocommerce\_%';" ); |
| 64 | $wpdb->query( "DELETE FROM $wpdb->options WHERE option_name LIKE 'widget\_woocommerce\_%';" ); |
| 65 | |
| 66 | // Delete usermeta. |
| 67 | $wpdb->query( "DELETE FROM $wpdb->usermeta WHERE meta_key LIKE 'woocommerce\_%';" ); |
| 68 | |
| 69 | // Delete our data from the post and post meta tables, and remove any additional tables we created. |
| 70 | $wpdb->query( "DELETE FROM {$wpdb->posts} WHERE post_type IN ( 'product', 'product_variation', 'shop_coupon', 'shop_order', 'shop_order_refund' );" ); |
| 71 | $wpdb->query( "DELETE meta FROM {$wpdb->postmeta} meta LEFT JOIN {$wpdb->posts} posts ON posts.ID = meta.post_id WHERE posts.ID IS NULL;" ); |
| 72 | |
| 73 | $wpdb->query( "DELETE FROM {$wpdb->comments} WHERE comment_type IN ( 'order_note' );" ); |
| 74 | $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;" ); |
| 75 | |
| 76 | foreach ( wc_get_container()->get( OrdersTableDataStore::class )->get_all_table_names() as $cot_table ) { |
| 77 | // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
| 78 | $wpdb->query( "DROP TABLE IF EXISTS {$cot_table}" ); |
| 79 | } |
| 80 | |
| 81 | // Delete terms if > WP 4.2 (term splitting was added in 4.2). |
| 82 | if ( version_compare( $wp_version, '4.2', '>=' ) ) { |
| 83 | // Delete term taxonomies. |
| 84 | foreach ( array( 'product_cat', 'product_tag', 'product_shipping_class', 'product_type' ) as $_taxonomy ) { |
| 85 | $wpdb->delete( |
| 86 | $wpdb->term_taxonomy, |
| 87 | array( |
| 88 | 'taxonomy' => $_taxonomy, |
| 89 | ) |
| 90 | ); |
| 91 | } |
| 92 | |
| 93 | // Delete term attributes. |
| 94 | foreach ( $wc_attributes as $_taxonomy ) { |
| 95 | $wpdb->delete( |
| 96 | $wpdb->term_taxonomy, |
| 97 | array( |
| 98 | 'taxonomy' => 'pa_' . $_taxonomy, |
| 99 | ) |
| 100 | ); |
| 101 | } |
| 102 | |
| 103 | // Delete orphan relationships. |
| 104 | $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;" ); |
| 105 | |
| 106 | // Delete orphan terms. |
| 107 | $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;" ); |
| 108 | |
| 109 | // Delete orphan term meta. |
| 110 | if ( ! empty( $wpdb->termmeta ) ) { |
| 111 | $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;" ); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | // Clear any cached data that has been removed. |
| 116 | wp_cache_flush(); |
| 117 | } |
| 118 |