Migrations.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php if ( ! defined( 'ABSPATH' ) ) exit;
  2. class NF_Database_Migrations
  3. {
  4. protected $migrations = array();
  5. public function __construct()
  6. {
  7. $this->migrations[ 'forms' ] = new NF_Database_Migrations_Forms();
  8. $this->migrations[ 'form_meta' ] = new NF_Database_Migrations_FormMeta();
  9. $this->migrations[ 'fields' ] = new NF_Database_Migrations_Fields();
  10. $this->migrations[ 'field_meta' ] = new NF_Database_Migrations_FieldMeta();
  11. $this->migrations[ 'actions' ] = new NF_Database_Migrations_Actions();
  12. $this->migrations[ 'action_meta' ] = new NF_Database_Migrations_ActionMeta();
  13. $this->migrations[ 'objects' ] = new NF_Database_Migrations_Objects();
  14. $this->migrations[ 'object_meta' ] = new NF_Database_Migrations_ObjectMeta();
  15. $this->migrations[ 'relationships' ] = new NF_Database_Migrations_Relationships();
  16. $this->migrations[ 'settings' ] = new NF_Database_Migrations_Settings();
  17. $this->migrations[ 'upgrades' ] = new NF_Database_Migrations_Upgrades();
  18. $this->migrations[ 'chunks' ] = new NF_Database_Migrations_Chunks();
  19. }
  20. public function migrate()
  21. {
  22. foreach( $this->migrations as $migration ){
  23. $migration->_run();
  24. }
  25. }
  26. /**
  27. * Function to run all our stage one changes.
  28. */
  29. public function do_stage_one()
  30. {
  31. foreach( $this->migrations as $migration ) {
  32. $migration->_stage_one();
  33. }
  34. }
  35. /**
  36. * This function drops ninja forms tables and options
  37. *
  38. * @param $areYouSure
  39. * @param $areYouReallySure
  40. * @param $nuke_multisite
  41. *
  42. * @since 2.9.34
  43. * @updated 3.3.16
  44. */
  45. public function nuke( $areYouSure = FALSE, $areYouReallySure = FALSE, $nuke_multisite = TRUE )
  46. {
  47. if( ! $areYouSure || ! $areYouReallySure ) return;
  48. global $wpdb;
  49. if( ! function_exists( 'is_multisite' ) || ! is_multisite() ){
  50. $this->_nuke();
  51. return;
  52. }
  53. // adding this to make sure we don't nuke ALL subsites when upgrading one subsite
  54. if ( $nuke_multisite ) {
  55. $blog_ids = $wpdb->get_col( "SELECT blog_id FROM $wpdb->blogs" );
  56. foreach( $blog_ids as $blog_id ){
  57. switch_to_blog( $blog_id );
  58. $this->_nuke();
  59. restore_current_blog(); // Call after EVERY switch_to_blog().
  60. }
  61. } else {
  62. $this->_nuke();
  63. return;
  64. }
  65. }
  66. private function _nuke()
  67. {
  68. global $wpdb;
  69. /* Drop THREE Tables */
  70. foreach( $this->migrations as $migration ){
  71. $migration->_drop();
  72. }
  73. /* Delete form caches */
  74. $wpdb->query( "DELETE FROM `{$wpdb->options}` WHERE `option_name` LIKE 'nf_form_%'" );
  75. $wpdb->query( "DELETE FROM `{$wpdb->options}` WHERE `option_name` LIKE '_transient_nf_form_%'" );
  76. $wpdb->query( "DELETE FROM `{$wpdb->options}` WHERE `option_name` LIKE '_transient_timeout_nf_form_%'" );
  77. }
  78. public function nuke_settings( $areYouSure = FALSE, $areYouReallySure = FALSE )
  79. {
  80. if( ! $areYouSure || ! $areYouReallySure ) return;
  81. global $wpdb;
  82. if( ! function_exists( 'is_multisite' ) || ! is_multisite() ){
  83. $this->_nuke_settings();
  84. return;
  85. }
  86. $blog_ids = $wpdb->get_col( "SELECT blog_id FROM $wpdb->blogs" );
  87. foreach( $blog_ids as $blog_id ){
  88. switch_to_blog( $blog_id );
  89. $this->_nuke_settings();
  90. restore_current_blog(); // Call after EVERY switch_to_blog().
  91. }
  92. }
  93. private function _nuke_settings()
  94. {
  95. global $wpdb;
  96. /* Delete known options */
  97. delete_option( 'nf_admin_notice' );
  98. delete_option( 'nf_form_tel_data' );
  99. delete_option( 'nf_form_tel_sent' );
  100. delete_option( 'nf_tel_collate' );
  101. delete_option( 'ninja_forms_allow_tracking' );
  102. delete_option( 'ninja_forms_db_version' );
  103. delete_option( 'ninja_forms_do_not_allow_tracking' );
  104. delete_option( 'ninja_forms_load_deprecated' );
  105. delete_option( 'ninja_forms_mailchimp_interests' );
  106. delete_option( 'ninja_forms_oauth_client_secret' );
  107. delete_option( 'ninja_forms_optin_reported' );
  108. delete_option( 'ninja_forms_settings' );
  109. delete_option( 'ninja_forms_transactional_email_enabled' );
  110. delete_option( 'ninja_forms_version' );
  111. /* Delete possible options */
  112. $wpdb->query( "DELETE FROM `{$wpdb->options}` WHERE `option_name` LIKE 'wp_nf_%'" );
  113. $wpdb->query( "DELETE FROM `{$wpdb->options}` WHERE `option_name` LIKE 'ninja_forms_%'" );
  114. /* Delete background processing flags */
  115. $wpdb->query( "DELETE FROM `{$wpdb->options}` WHERE `option_name` LIKE 'wp_nf_update_fields_%'" );
  116. }
  117. public function nuke_deprecated( $areYouSure = FALSE, $areYouReallySure = FALSE )
  118. {
  119. if( ! $areYouSure || ! $areYouReallySure ) return;
  120. global $wpdb;
  121. if( ! function_exists( 'is_multisite' ) || ! is_multisite() ){
  122. $this->_nuke_deprecated();
  123. return;
  124. }
  125. $blog_ids = $wpdb->get_col( "SELECT blog_id FROM $wpdb->blogs" );
  126. foreach( $blog_ids as $blog_id ){
  127. switch_to_blog( $blog_id );
  128. $this->_nuke_deprecated();
  129. restore_current_blog(); // Call after EVERY switch_to_blog().
  130. }
  131. }
  132. private function _nuke_deprecated()
  133. {
  134. global $wpdb;
  135. /* Drop Deprecated Tables (v2.9.x) */
  136. $wpdb->query( "DROP TABLE IF EXISTS `{$wpdb->prefix}nf_objectmeta`" );
  137. $wpdb->query( "DROP TABLE IF EXISTS `{$wpdb->prefix}nf_objects`" );
  138. $wpdb->query( "DROP TABLE IF EXISTS `{$wpdb->prefix}nf_relationships`" );
  139. $wpdb->query( "DROP TABLE IF EXISTS `{$wpdb->prefix}ninja_forms_fav_fields`" );
  140. $wpdb->query( "DROP TABLE IF EXISTS `{$wpdb->prefix}ninja_forms_fields`" );
  141. /* Delete Deprecated Options (v2.9.x) */
  142. delete_option( 'nf_upgrade_notice' );
  143. delete_option( 'nf_converted_subs' );
  144. delete_option( 'nf_converted_forms' );
  145. delete_option( 'nf_convert_subs_num' );
  146. delete_option( 'nf_convert_subs_step' );
  147. delete_option( 'nf_convert_subs_step' );
  148. delete_option( 'nf_email_fav_updated' );
  149. delete_option( 'nf_database_migrations' );
  150. delete_option( 'nf_converted_form_reset' );
  151. delete_option( 'nf_version_upgraded_from' );
  152. delete_option( 'nf_convert_forms_complete' );
  153. delete_option( 'nf_convert_notifications_forms' );
  154. delete_option( 'nf_convert_notifications_complete' );
  155. delete_option( 'nf_update_email_settings_complete' );
  156. /* Delete Deprecarted Upgrade Options (v2.9.x) */
  157. $wpdb->query( "DELETE FROM `{$wpdb->options}` WHERE `option_name` LIKE 'nf_upgrade_%'" );
  158. /* Maybe Remove Deprecated Scheduled Cron (v2.9.x) */
  159. if( $timestamp = wp_next_scheduled( 'ninja_forms_daily_action' ) ){
  160. wp_unschedule_event( $timestamp, 'ninja_forms_daily_action' );
  161. }
  162. }
  163. }