updated plugin Easy Digital Downloads
version 3.1.1.2
This commit is contained in:
@ -38,7 +38,7 @@ final class Customers extends Table {
|
||||
* @since 3.0
|
||||
* @var int
|
||||
*/
|
||||
protected $version = 202006101;
|
||||
protected $version = 202301021;
|
||||
|
||||
/**
|
||||
* Array of upgrade versions and methods
|
||||
@ -50,6 +50,7 @@ final class Customers extends Table {
|
||||
protected $upgrades = array(
|
||||
'202002141' => 202002141,
|
||||
'202006101' => 202006101,
|
||||
'202301021' => 202301021,
|
||||
);
|
||||
|
||||
/**
|
||||
@ -77,62 +78,27 @@ final class Customers extends Table {
|
||||
KEY date_created (date_created)";
|
||||
}
|
||||
|
||||
/**
|
||||
* Override the Base class `maybe_upgrade()` routine to do a very unique and
|
||||
* special check against the old option.
|
||||
*
|
||||
* Maybe upgrades the database table from 2.x to 3.x standards. This method
|
||||
* should be kept up-to-date with schema changes in `set_schema()` above.
|
||||
*
|
||||
* - Hooked to the "admin_init" action.
|
||||
* - Calls the parent class `maybe_upgrade()` method
|
||||
*
|
||||
* @since 3.0
|
||||
*/
|
||||
public function maybe_upgrade() {
|
||||
|
||||
if ( $this->needs_initial_upgrade() ) {
|
||||
|
||||
// Delete old/irrelevant database options.
|
||||
delete_option( $this->table_prefix . 'edd_customers_db_version' );
|
||||
delete_option( 'wp_edd_customers_db_version' );
|
||||
|
||||
|
||||
// Modify existing columns.
|
||||
$this->get_db()->query( "ALTER TABLE {$this->table_name} MODIFY `email` varchar(100) NOT NULL default ''" );
|
||||
$this->get_db()->query( "ALTER TABLE {$this->table_name} MODIFY `name` varchar(255) NOT NULL default ''" );
|
||||
$this->get_db()->query( "ALTER TABLE {$this->table_name} MODIFY `user_id` bigint(20) unsigned NOT NULL default '0'" );
|
||||
$this->get_db()->query( "ALTER TABLE {$this->table_name} MODIFY `purchase_value` decimal(18,9) NOT NULL default '0'" );
|
||||
$this->get_db()->query( "ALTER TABLE {$this->table_name} MODIFY `purchase_count` bigint(20) unsigned NOT NULL default '0'" );
|
||||
$this->get_db()->query( "ALTER TABLE {$this->table_name} MODIFY `date_created` datetime NOT NULL default CURRENT_TIMESTAMP" );
|
||||
|
||||
if ( ! $this->column_exists( 'status' ) ) {
|
||||
$this->get_db()->query( "ALTER TABLE {$this->table_name} ADD COLUMN `status` varchar(20) NOT NULL default 'active' AFTER `name`;" );
|
||||
$this->get_db()->query( "ALTER TABLE {$this->table_name} ADD INDEX status (status(20))" );
|
||||
}
|
||||
|
||||
if ( ! $this->column_exists( 'date_modified' ) ) {
|
||||
$this->get_db()->query( "ALTER TABLE {$this->table_name} ADD COLUMN `date_modified` datetime DEFAULT CURRENT_TIMESTAMP AFTER `date_created`" );
|
||||
$this->get_db()->query( "UPDATE {$this->table_name} SET `date_modified` = `date_created`" );
|
||||
$this->get_db()->query( "ALTER TABLE {$this->table_name} ADD INDEX date_created (date_created)" );
|
||||
}
|
||||
|
||||
if ( ! $this->column_exists( 'uuid' ) ) {
|
||||
$this->get_db()->query( "ALTER TABLE {$this->table_name} ADD COLUMN `uuid` varchar(100) default '' AFTER `date_modified`;" );
|
||||
}
|
||||
}
|
||||
|
||||
parent::maybe_upgrade();
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether the initial upgrade from the 1.0 database needs to be run.
|
||||
*
|
||||
* @since 3.0.3
|
||||
* @since 3.1.1 Explicitly checks each of these cases to ensure a non-fully updated table is updated, but not currently used.
|
||||
* @return bool
|
||||
*/
|
||||
private function needs_initial_upgrade() {
|
||||
return $this->exists() && ! $this->column_exists( 'status' ) && ! $this->column_exists( 'uuid' );
|
||||
if ( $this->exists() && ! $this->column_exists( 'status' ) ) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if ( $this->exists() && ! $this->column_exists( 'uuid' ) ) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if ( $this->exists() && ! $this->column_exists( 'date_modified' ) ) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -179,4 +145,107 @@ final class Customers extends Table {
|
||||
return $this->is_success( $result );
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks the status of the edd_customers table for health and state and performs
|
||||
* any necessary changes that haven't been prevoiusly made.
|
||||
*
|
||||
* @since 3.1.1
|
||||
* @return bool
|
||||
*/
|
||||
protected function __202301021() {
|
||||
// Verify that columns that existed prior to 3.0 get the proper changes made.
|
||||
$columns = $this->get_db()->get_results( "SHOW COLUMNS FROM {$this->table_name}" );
|
||||
$existing_column_updates = array();
|
||||
|
||||
// Set an array to hold the result of each query run.
|
||||
$query_results = array();
|
||||
|
||||
foreach ( $columns as $column ) {
|
||||
switch ( $column->Field ) {
|
||||
case 'email':
|
||||
if ( 'varchar(100)' !== $column->Type ) {
|
||||
$existing_column_updates['alter-email-type'] = "ALTER TABLE {$this->table_name} MODIFY `email` varchar(100) NOT NULL default ''";
|
||||
}
|
||||
break;
|
||||
|
||||
case 'name':
|
||||
if ( 'varchar(255)' !== $column->Type ) {
|
||||
$existing_column_updates['alter-name-type'] = "ALTER TABLE {$this->table_name} MODIFY `name` varchar(255) NOT NULL default ''";
|
||||
}
|
||||
break;
|
||||
|
||||
case 'user_id':
|
||||
if ( 'bigint(20)' !== $column->Type ) {
|
||||
$existing_column_updates['alter-user_id-type'] = "ALTER TABLE {$this->table_name} MODIFY `user_id` bigint(20) unsigned NOT NULL default '0'";
|
||||
}
|
||||
break;
|
||||
|
||||
case 'purchase_value':
|
||||
if ( 'decimal(18,9)' !== $column->Type ) {
|
||||
$existing_column_updates['alter-purchase_value-type'] = "ALTER TABLE {$this->table_name} MODIFY `purchase_value` decimal(18,9) NOT NULL default '0'";
|
||||
}
|
||||
break;
|
||||
|
||||
case 'purchase_count':
|
||||
if ( 'bigint(20)' !== $column->Type ) {
|
||||
$existing_column_updates['alter-purchase_count-type'] = "ALTER TABLE {$this->table_name} MODIFY `purchase_count` bigint(20) unsigned NOT NULL default '0'";
|
||||
}
|
||||
break;
|
||||
|
||||
case 'date_created':
|
||||
if ( 'datetime' !== $column->Type ) {
|
||||
$existing_column_updates['alter-date_created-type'] = "ALTER TABLE {$this->table_name} MODIFY `date_created` datetime NOT NULL default CURRENT_TIMESTAMP";
|
||||
}
|
||||
break;
|
||||
|
||||
case 'payment_ids':
|
||||
$existing_column_updates['drop-payment_ids'] = "ALTER TABLE {$this->table_name} DROP COLUMN `payment_ids`";
|
||||
break;
|
||||
|
||||
case 'notes':
|
||||
// Only remove the customer notes column if they've already run the customer notes migration.
|
||||
if ( edd_has_upgrade_completed( 'v30_legacy_data_removed' ) ) {
|
||||
$existing_column_updates['drop-notes'] = "ALTER TABLE {$this->table_name} DROP COLUMN `notes`";
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if ( ! empty( $existing_column_updates ) ) {
|
||||
foreach ( $existing_column_updates as $query_key => $update_sql ) {
|
||||
$query_results[ $query_key ] = $this->is_success( $this->get_db()->query( $update_sql ) );
|
||||
}
|
||||
}
|
||||
|
||||
// Now verify that new columns are created and exist.
|
||||
if ( ! $this->column_exists( 'status' ) ) {
|
||||
$query_results['add-status-column'] = $this->is_success( $this->get_db()->query( "ALTER TABLE {$this->table_name} ADD COLUMN `status` varchar(20) NOT NULL default 'active' AFTER `name`;" ) );
|
||||
$query_results['index-status'] = $this->is_success( $this->get_db()->query( "ALTER TABLE {$this->table_name} ADD INDEX status (status(20))" ) );
|
||||
}
|
||||
|
||||
if ( ! $this->column_exists( 'date_modified' ) ) {
|
||||
$query_results['add-date_modified-column'] = $this->is_success( $this->get_db()->query( "ALTER TABLE {$this->table_name} ADD COLUMN `date_modified` datetime DEFAULT CURRENT_TIMESTAMP AFTER `date_created`" ) );
|
||||
$query_results['update-modified-with-created'] = $this->is_success( $this->get_db()->query( "UPDATE {$this->table_name} SET `date_modified` = `date_created`" ) );
|
||||
$query_results['index-date_created'] = $this->is_success( $this->get_db()->query( "ALTER TABLE {$this->table_name} ADD INDEX date_created (date_created)" ) );
|
||||
}
|
||||
|
||||
if ( ! $this->column_exists( 'uuid' ) ) {
|
||||
$query_results['add-uuid-column'] = $this->is_success( $this->get_db()->query( "ALTER TABLE {$this->table_name} ADD COLUMN `uuid` varchar(100) default '' AFTER `date_modified`;" ) );
|
||||
}
|
||||
|
||||
$return_result = true;
|
||||
|
||||
// Loop through each of the query results and force a debug log for any failures.
|
||||
foreach ( $query_results as $query_key => $query_result ) {
|
||||
if ( false === $query_result ) {
|
||||
$return_result = false;
|
||||
edd_debug_log( 'Customer\'s table version ' . $this->version . ' update failed for: ' . $query_key, true );
|
||||
}
|
||||
}
|
||||
|
||||
delete_option( $this->table_prefix . 'edd_customers_db_version' );
|
||||
delete_option( 'wp_edd_customers_db_version' );
|
||||
|
||||
return $this->is_success( $return_result );
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user