Brak opisu

uninstall.php 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. // Drop WC Admin tables.
  26. include_once dirname( __FILE__ ) . '/packages/woocommerce-admin/src/Install.php';
  27. \Automattic\WooCommerce\Admin\Install::drop_tables();
  28. include_once dirname( __FILE__ ) . '/includes/class-wc-install.php';
  29. // Roles + caps.
  30. WC_Install::remove_roles();
  31. // Pages.
  32. wp_trash_post( get_option( 'woocommerce_shop_page_id' ) );
  33. wp_trash_post( get_option( 'woocommerce_cart_page_id' ) );
  34. wp_trash_post( get_option( 'woocommerce_checkout_page_id' ) );
  35. wp_trash_post( get_option( 'woocommerce_myaccount_page_id' ) );
  36. wp_trash_post( get_option( 'woocommerce_edit_address_page_id' ) );
  37. wp_trash_post( get_option( 'woocommerce_view_order_page_id' ) );
  38. wp_trash_post( get_option( 'woocommerce_change_password_page_id' ) );
  39. wp_trash_post( get_option( 'woocommerce_logout_page_id' ) );
  40. if ( $wpdb->get_var( "SHOW TABLES LIKE '{$wpdb->prefix}woocommerce_attribute_taxonomies';" ) ) {
  41. $wc_attributes = array_filter( (array) $wpdb->get_col( "SELECT attribute_name FROM {$wpdb->prefix}woocommerce_attribute_taxonomies;" ) );
  42. } else {
  43. $wc_attributes = array();
  44. }
  45. // Tables.
  46. WC_Install::drop_tables();
  47. // Delete options.
  48. $wpdb->query( "DELETE FROM $wpdb->options WHERE option_name LIKE 'woocommerce\_%';" );
  49. $wpdb->query( "DELETE FROM $wpdb->options WHERE option_name LIKE 'widget\_woocommerce\_%';" );
  50. // Delete usermeta.
  51. $wpdb->query( "DELETE FROM $wpdb->usermeta WHERE meta_key LIKE 'woocommerce\_%';" );
  52. // Delete posts + data.
  53. $wpdb->query( "DELETE FROM {$wpdb->posts} WHERE post_type IN ( 'product', 'product_variation', 'shop_coupon', 'shop_order', 'shop_order_refund' );" );
  54. $wpdb->query( "DELETE meta FROM {$wpdb->postmeta} meta LEFT JOIN {$wpdb->posts} posts ON posts.ID = meta.post_id WHERE posts.ID IS NULL;" );
  55. $wpdb->query( "DELETE FROM {$wpdb->comments} WHERE comment_type IN ( 'order_note' );" );
  56. $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;" );
  57. $wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}woocommerce_order_items" );
  58. $wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}woocommerce_order_itemmeta" );
  59. // Delete terms if > WP 4.2 (term splitting was added in 4.2).
  60. if ( version_compare( $wp_version, '4.2', '>=' ) ) {
  61. // Delete term taxonomies.
  62. foreach ( array( 'product_cat', 'product_tag', 'product_shipping_class', 'product_type' ) as $_taxonomy ) {
  63. $wpdb->delete(
  64. $wpdb->term_taxonomy,
  65. array(
  66. 'taxonomy' => $_taxonomy,
  67. )
  68. );
  69. }
  70. // Delete term attributes.
  71. foreach ( $wc_attributes as $_taxonomy ) {
  72. $wpdb->delete(
  73. $wpdb->term_taxonomy,
  74. array(
  75. 'taxonomy' => 'pa_' . $_taxonomy,
  76. )
  77. );
  78. }
  79. // Delete orphan relationships.
  80. $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;" );
  81. // Delete orphan terms.
  82. $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;" );
  83. // Delete orphan term meta.
  84. if ( ! empty( $wpdb->termmeta ) ) {
  85. $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;" );
  86. }
  87. }
  88. // Clear any cached data that has been removed.
  89. wp_cache_flush();
  90. }