<?php if ( ! defined( 'ABSPATH' ) ) exit;
require_once( ABSPATH . 'wp-admin/includes/upgrade.php');
abstract class NF_Abstracts_Migration
public $charset_collate = '';
* Constructor method for the NF_Abstracts_Migration class.
* @param $table_name (String) The database table name managed by the extension.
* @param $flag (String) The wp option set to determine if this migration has already been run.
public function __construct( $table_name, $flag )
$this->table_name = $table_name;
* Function to retrieve the full table name of the extension.
* @return (String) The full table name, including database prefix.
public function table_name()
return $wpdb->prefix . $this->table_name;
* Function to check for the existence of a column in the extension's table.
* @param $column (String) The name of the column to search for.
* @return (Boolean) Whether or not the column exists.
public function column_exists( $column )
// Fetch any records of the target column.
$sql = $wpdb->prepare( "SHOW COLUMNS FROM `{$this->table_name()}` WHERE `Field` = '%s';", $column );
$result = $wpdb->query( $sql );
// If we got anything back, say so.
if ( ! empty( $result ) ) $response = true;
* Funciton to get the charset and collate for migrations.
* @param $use_default (Boolean) Whether or not to include the DEFAULT keyword in the return pattern.
* @return (String) A SQL formatted charset and collate for use by table definition.
public function charset_collate( $use_default = false )
// If our mysql version is 5.5.3 or higher...
if ( version_compare( $wpdb->db_version(), '5.5.3', '>=' ) ) {
$response = 'CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci';
$response = 'CHARACTER SET utf8 COLLATE utf8_general_ci';
// If we need to use default...
// Append that to the response.
$response = 'DEFAULT ' . $response;
* Function to run our required update functions.
* @param $callback (String) The function to be run by this call.
public function _do_upgrade( $callback )
// If the method exists...
if ( method_exists( $this, $callback ) ) {
// If this callback method isn't blacklisted...
if ( ! in_array( $callback, $blacklist ) ) {
* Function to run our initial migration.
if( get_option( $this->flag, FALSE ) ) return;
update_option( $this->flag, TRUE );
* Abstract protection of inherited funciton run.
protected abstract function run();
* Function to drop the table managed by this migration.
// If we don't have a table name, exit early.
if( ! $this->table_name ) return;
// If the table doesn't exist, exit early.
if( 0 == $wpdb->query( $wpdb->prepare( "SHOW TABLES LIKE '%s'", $this->table_name() ) ) ) return;
$wpdb->query( "DROP TABLE " . $this->table_name() );
* Protection of inherited function drop.
protected function drop()
// This section intentionally left blank.