Migration.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php if ( ! defined( 'ABSPATH' ) ) exit;
  2. require_once( ABSPATH . 'wp-admin/includes/upgrade.php');
  3. abstract class NF_Abstracts_Migration
  4. {
  5. public $table_name = '';
  6. public $charset_collate = '';
  7. public $flag = '';
  8. public function __construct( $table_name, $flag )
  9. {
  10. $this->table_name = $table_name;
  11. }
  12. public function table_name()
  13. {
  14. global $wpdb;
  15. return $wpdb->prefix . $this->table_name;
  16. }
  17. public function charset_collate()
  18. {
  19. global $wpdb;
  20. // If our mysql version is 5.5.3 or higher...
  21. if ( version_compare( $wpdb->db_version(), '5.5.3', '>=' ) ) {
  22. // We can use mb4.
  23. return 'DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci';
  24. } // Otherwise...
  25. else {
  26. // We use standard utf8.
  27. return 'DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci';
  28. }
  29. }
  30. /**
  31. * Function to get the column collate for ALTER TABLE statements.
  32. *
  33. * @since 3.3.7
  34. *
  35. * @return string
  36. */
  37. public function collate()
  38. {
  39. global $wpdb;
  40. // If our mysql version is 5.5.3 or higher...
  41. if ( version_compare( $wpdb->db_version(), '5.5.3', '>=' ) ) {
  42. // We can use mb4.
  43. return 'COLLATE utf8mb4_general_ci';
  44. } // Otherwise...
  45. else {
  46. // We use standard utf8.
  47. return 'COLLATE utf8_general_ci';
  48. }
  49. }
  50. /**
  51. * Function to run our stage one db updates.
  52. */
  53. public function _stage_one()
  54. {
  55. if ( method_exists( $this, 'do_stage_one' ) ) {
  56. $this->do_stage_one();
  57. }
  58. }
  59. public function _run()
  60. {
  61. // Check the flag
  62. if( get_option( $this->flag, FALSE ) ) return;
  63. // Run the migration
  64. $this->run();
  65. // Set the Flag
  66. update_option( $this->flag, TRUE );
  67. }
  68. protected abstract function run();
  69. public function _drop()
  70. {
  71. global $wpdb;
  72. if( ! $this->table_name ) return;
  73. if( 0 == $wpdb->query( $wpdb->prepare( "SHOW TABLES LIKE '%s'", $this->table_name() ) ) ) return;
  74. $wpdb->query( "DROP TABLE " . $this->table_name() );
  75. return $this->drop();
  76. }
  77. protected function drop()
  78. {
  79. // This section intentionally left blank.
  80. }
  81. }