Set the orders table exist option if its not set. (#39616)

* Set the orders table exist option if its not set.

* Add changelog.

* Must use yoda conditions.

* Remove unnecessary option set.
This commit is contained in:
Vedanshu Jain 2023-08-08 21:31:00 +05:30 committed by GitHub
parent 75d64ee063
commit d4141958b2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 11 deletions

View File

@ -0,0 +1,4 @@
Significance: patch
Type: fix
Set the order table exist options value when its not present for smooth upgradation from lower WC versions.

View File

@ -128,8 +128,26 @@ class DataSynchronizer implements BatchProcessorInterface {
if ( count( $missing_tables ) === 0 ) {
update_option( self::ORDERS_TABLE_CREATED, 'yes' );
return true;
} else {
update_option( self::ORDERS_TABLE_CREATED, 'no' );
return false;
}
}
/**
* Returns the value of the orders table created option. If it's not set, then it checks the orders table and set it accordingly.
*
* @return bool Whether orders table exists.
*/
public function get_table_exists(): bool {
$table_exists = get_option( self::ORDERS_TABLE_CREATED );
switch ( $table_exists ) {
case 'no':
case 'yes':
return 'yes' === $table_exists;
default:
return $this->check_orders_table_exists();
}
return false;
}
/**
@ -137,10 +155,7 @@ class DataSynchronizer implements BatchProcessorInterface {
*/
public function create_database_tables() {
$this->database_util->dbdelta( $this->data_store->get_database_schema() );
if ( ! $this->check_orders_table_exists() ) {
return;
}
update_option( self::ORDERS_TABLE_CREATED, 'yes' );
$this->check_orders_table_exists();
}
/**
@ -223,7 +238,7 @@ class DataSynchronizer implements BatchProcessorInterface {
return 0;
}
if ( 'yes' !== get_option( self::ORDERS_TABLE_CREATED ) ) {
if ( ! $this->get_table_exists() ) {
$count = $wpdb->get_var(
// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare -- $order_post_type_placeholder is prepared.
$wpdb->prepare(
@ -652,12 +667,10 @@ ORDER BY orders.id ASC
return;
}
if ( 'yes' !== get_option( self::ORDERS_TABLE_CREATED ) ) {
return;
}
if ( $this->data_sync_is_enabled() ) {
$this->data_store->delete_order_data_from_custom_order_tables( $postid );
if ( $this->get_table_exists() ) {
$this->data_store->delete_order_data_from_custom_order_tables( $postid );
}
} elseif ( $this->custom_orders_table_is_authoritative() ) {
return;
}