Sin descripción

uninstall.php 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. /**
  3. * Uninstall WPForms.
  4. *
  5. * Remove:
  6. * - Entries table
  7. * - Entry Meta table
  8. * - Entry fields table
  9. * - Form Preview page
  10. * - wpforms_log post type posts and post_meta
  11. * - wpforms post type posts and post_meta
  12. * - WPForms settings/options
  13. * - WPForms user meta
  14. * - WPForms term meta
  15. * - WPForms Uploads
  16. *
  17. * @since 1.4.5
  18. *
  19. * @var WP_Filesystem_Base $wp_filesystem
  20. */
  21. // phpcs:disable WordPress.DB.DirectDatabaseQuery
  22. // Exit if accessed directly.
  23. if ( ! defined( 'WP_UNINSTALL_PLUGIN' ) ) {
  24. exit;
  25. }
  26. // Load plugin file.
  27. require_once 'wpforms.php';
  28. // Disable Action Schedule Queue Runner.
  29. if ( class_exists( 'ActionScheduler_QueueRunner' ) ) {
  30. ActionScheduler_QueueRunner::instance()->unhook_dispatch_async_request();
  31. }
  32. // Confirm user has decided to remove all data, otherwise stop.
  33. $settings = get_option( 'wpforms_settings', [] );
  34. if ( empty( $settings['uninstall-data'] ) ) {
  35. return;
  36. }
  37. global $wpdb;
  38. // Delete entries table.
  39. $wpdb->query( 'DROP TABLE IF EXISTS ' . $wpdb->prefix . 'wpforms_entries' );
  40. // Delete entry meta table.
  41. $wpdb->query( 'DROP TABLE IF EXISTS ' . $wpdb->prefix . 'wpforms_entry_meta' );
  42. // Delete entry fields table.
  43. $wpdb->query( 'DROP TABLE IF EXISTS ' . $wpdb->prefix . 'wpforms_entry_fields' );
  44. // Delete tasks meta table.
  45. // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
  46. $wpdb->query( 'DROP TABLE IF EXISTS ' . \WPForms\Tasks\Meta::get_table_name() );
  47. // Delete logger table.
  48. // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
  49. $wpdb->query( 'DROP TABLE IF EXISTS ' . \WPForms\Logger\Repository::get_table_name() );
  50. // Delete Preview page.
  51. $preview_page = get_option( 'wpforms_preview_page', false );
  52. if ( ! empty( $preview_page ) ) {
  53. wp_delete_post( $preview_page, true );
  54. }
  55. // Delete wpforms and wpforms_log post type posts/post_meta.
  56. $wpforms_posts = get_posts(
  57. [
  58. 'post_type' => [ 'wpforms_log', 'wpforms' ],
  59. 'post_status' => 'any',
  60. 'numberposts' => - 1,
  61. 'fields' => 'ids',
  62. ]
  63. );
  64. if ( $wpforms_posts ) {
  65. foreach ( $wpforms_posts as $wpforms_post ) {
  66. wp_delete_post( $wpforms_post, true );
  67. }
  68. }
  69. // Delete all the plugin settings.
  70. $wpdb->query( "DELETE FROM {$wpdb->options} WHERE option_name LIKE 'wpforms\_%'" );
  71. // Delete widget settings.
  72. $wpdb->query( "DELETE FROM {$wpdb->options} WHERE option_name LIKE 'widget\_wpforms%'" );
  73. // Delete options from the previous version of the Notifications functionality.
  74. $wpdb->query( "DELETE FROM {$wpdb->options} WHERE option_name LIKE '\_amn\_wpforms\_%'" );
  75. // Delete plugin user meta.
  76. $wpdb->query( "DELETE FROM {$wpdb->usermeta} WHERE meta_key LIKE 'wpforms\_%'" );
  77. // Delete plugin term meta.
  78. $wpdb->query( "DELETE FROM {$wpdb->termmeta} WHERE meta_key LIKE 'wpforms\_%'" );
  79. // Remove any transients we've left behind.
  80. $wpdb->query( "DELETE FROM {$wpdb->options} WHERE option_name LIKE '\_transient\_wpforms\_%'" );
  81. $wpdb->query( "DELETE FROM {$wpdb->options} WHERE option_name LIKE '\_site\_transient\_wpforms\_%'" );
  82. $wpdb->query( "DELETE FROM {$wpdb->options} WHERE option_name LIKE '\_transient\_timeout\_wpforms\_%'" );
  83. $wpdb->query( "DELETE FROM {$wpdb->options} WHERE option_name LIKE '\_site\_transient\_timeout\_wpforms\_%'" );
  84. $wpdb->query( "DELETE FROM {$wpdb->options} WHERE option_name LIKE '\_wpforms\_transient\_%'" );
  85. global $wp_filesystem;
  86. // Remove uploaded files.
  87. $uploads_directory = wp_upload_dir();
  88. if ( empty( $uploads_directory['error'] ) ) {
  89. $wp_filesystem->rmdir( $uploads_directory['basedir'] . '/wpforms/', true );
  90. }
  91. // Remove translation files.
  92. $languages_directory = defined( 'WP_LANG_DIR' ) ? trailingslashit( WP_LANG_DIR ) : trailingslashit( WP_CONTENT_DIR ) . 'languages/';
  93. $translations = glob( wp_normalize_path( $languages_directory . 'plugins/wpforms-*' ) );
  94. if ( ! empty( $translations ) ) {
  95. foreach ( $translations as $file ) {
  96. $wp_filesystem->delete( $file );
  97. }
  98. }
  99. // Remove plugin cron jobs.
  100. wp_clear_scheduled_hook( 'wpforms_email_summaries_cron' );
  101. // Unschedule all plugin ActionScheduler actions.
  102. // Don't use wpforms() because 'tasks' in core are registered on `init` hook,
  103. // which is not executed on uninstall.
  104. ( new \WPForms\Tasks\Tasks() )->cancel_all();