uninstall.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. defined( 'WP_UNINSTALL_PLUGIN' ) || exit;
  11. global $wpdb, $wp_version;
  12. wp_clear_scheduled_hook( 'woocommerce_scheduled_sales' );
  13. wp_clear_scheduled_hook( 'woocommerce_cancel_unpaid_orders' );
  14. wp_clear_scheduled_hook( 'woocommerce_cleanup_sessions' );
  15. wp_clear_scheduled_hook( 'woocommerce_cleanup_personal_data' );
  16. wp_clear_scheduled_hook( 'woocommerce_cleanup_logs' );
  17. wp_clear_scheduled_hook( 'woocommerce_geoip_updater' );
  18. wp_clear_scheduled_hook( 'woocommerce_tracker_send_event' );
  19. /*
  20. * Only remove ALL product and page data if WC_REMOVE_ALL_DATA constant is set to true in user's
  21. * wp-config.php. This is to prevent data loss when deleting the plugin from the backend
  22. * and to ensure only the site owner can perform this action.
  23. */
  24. if ( defined( 'WC_REMOVE_ALL_DATA' ) && true === WC_REMOVE_ALL_DATA ) {
  25. include_once dirname( __FILE__ ) . '/includes/class-wc-install.php';
  26. // Roles + caps.
  27. WC_Install::remove_roles();
  28. // Pages.
  29. wp_trash_post( get_option( 'woocommerce_shop_page_id' ) );
  30. wp_trash_post( get_option( 'woocommerce_cart_page_id' ) );
  31. wp_trash_post( get_option( 'woocommerce_checkout_page_id' ) );
  32. wp_trash_post( get_option( 'woocommerce_myaccount_page_id' ) );
  33. wp_trash_post( get_option( 'woocommerce_edit_address_page_id' ) );
  34. wp_trash_post( get_option( 'woocommerce_view_order_page_id' ) );
  35. wp_trash_post( get_option( 'woocommerce_change_password_page_id' ) );
  36. wp_trash_post( get_option( 'woocommerce_logout_page_id' ) );
  37. if ( $wpdb->get_var( "SHOW TABLES LIKE '{$wpdb->prefix}woocommerce_attribute_taxonomies';" ) ) {
  38. $wc_attributes = array_filter( (array) $wpdb->get_col( "SELECT attribute_name FROM {$wpdb->prefix}woocommerce_attribute_taxonomies;" ) );
  39. } else {
  40. $wc_attributes = array();
  41. }
  42. // Tables.
  43. WC_Install::drop_tables();
  44. // Delete options.
  45. $wpdb->query( "DELETE FROM $wpdb->options WHERE option_name LIKE 'woocommerce\_%';" );
  46. $wpdb->query( "DELETE FROM $wpdb->options WHERE option_name LIKE 'widget\_woocommerce\_%';" );
  47. // Delete usermeta.
  48. $wpdb->query( "DELETE FROM $wpdb->usermeta WHERE meta_key LIKE 'woocommerce\_%';" );
  49. // Delete posts + data.
  50. $wpdb->query( "DELETE FROM {$wpdb->posts} WHERE post_type IN ( 'product', 'product_variation', 'shop_coupon', 'shop_order', 'shop_order_refund' );" );
  51. $wpdb->query( "DELETE meta FROM {$wpdb->postmeta} meta LEFT JOIN {$wpdb->posts} posts ON posts.ID = meta.post_id WHERE posts.ID IS NULL;" );
  52. $wpdb->query( "DELETE FROM {$wpdb->comments} WHERE comment_type IN ( 'order_note' );" );
  53. $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;" );
  54. $wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}woocommerce_order_items" );
  55. $wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}woocommerce_order_itemmeta" );
  56. // Delete terms if > WP 4.2 (term splitting was added in 4.2).
  57. if ( version_compare( $wp_version, '4.2', '>=' ) ) {
  58. // Delete term taxonomies.
  59. foreach ( array( 'product_cat', 'product_tag', 'product_shipping_class', 'product_type' ) as $taxonomy ) {
  60. $wpdb->delete(
  61. $wpdb->term_taxonomy,
  62. array(
  63. 'taxonomy' => $taxonomy,
  64. )
  65. );
  66. }
  67. // Delete term attributes.
  68. foreach ( $wc_attributes as $taxonomy ) {
  69. $wpdb->delete(
  70. $wpdb->term_taxonomy,
  71. array(
  72. 'taxonomy' => 'pa_' . $taxonomy,
  73. )
  74. );
  75. }
  76. // Delete orphan relationships.
  77. $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;" );
  78. // Delete orphan terms.
  79. $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;" );
  80. // Delete orphan term meta.
  81. if ( ! empty( $wpdb->termmeta ) ) {
  82. $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;" );
  83. }
  84. }
  85. // Clear any cached data that has been removed.
  86. wp_cache_flush();
  87. }