Fix: HPOS keeping disabled when the database tables were created via enabling the setting (#40466)
* Fix: when the HPOS tables are created, HPOS was always disabled. After the fix HPOS will be disabled only if the creation of the tables fail. Additionally, failure to create the tables will be logged. * Add changelog file
This commit is contained in:
parent
1a3ecebc5e
commit
73c90bd3b1
|
@ -0,0 +1,4 @@
|
|||
Significance: patch
|
||||
Type: tweak
|
||||
|
||||
Fix: HPOS keeping disabled when the database tables were created via enabling the setting
|
|
@ -252,23 +252,6 @@ class CustomOrdersTableController {
|
|||
return $tools_array;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create the custom orders tables in response to the user pressing the tool button.
|
||||
*
|
||||
* @param bool $verify_nonce True to perform the nonce verification, false to skip it.
|
||||
*
|
||||
* @throws \Exception Can't create the tables.
|
||||
*/
|
||||
private function create_custom_orders_tables( bool $verify_nonce = true ) {
|
||||
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput
|
||||
if ( $verify_nonce && ( ! isset( $_REQUEST['_wpnonce'] ) || wp_verify_nonce( $_REQUEST['_wpnonce'], 'debug_action' ) === false ) ) {
|
||||
throw new \Exception( 'Invalid nonce' );
|
||||
}
|
||||
|
||||
$this->data_synchronizer->create_database_tables();
|
||||
update_option( self::CUSTOM_ORDERS_TABLE_USAGE_ENABLED_OPTION, 'no' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete the custom orders tables and any related options and data in response to the user pressing the tool button.
|
||||
*
|
||||
|
@ -371,7 +354,10 @@ class CustomOrdersTableController {
|
|||
}
|
||||
|
||||
if ( ! $this->data_synchronizer->check_orders_table_exists() ) {
|
||||
$this->create_custom_orders_tables( false );
|
||||
$success = $this->data_synchronizer->create_database_tables();
|
||||
if ( ! $success ) {
|
||||
update_option( self::CUSTOM_ORDERS_TABLE_USAGE_ENABLED_OPTION, 'no' );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -467,7 +453,7 @@ class CustomOrdersTableController {
|
|||
'disabled' => $disabled_option,
|
||||
'desc' => $plugin_incompat_warning,
|
||||
'desc_at_end' => true,
|
||||
'row_class' => self::CUSTOM_ORDERS_TABLE_USAGE_ENABLED_OPTION,
|
||||
'row_class' => self::CUSTOM_ORDERS_TABLE_USAGE_ENABLED_OPTION,
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -501,10 +487,12 @@ class CustomOrdersTableController {
|
|||
wc_get_container()->get( FeaturesController::class )->get_features_page_url()
|
||||
);
|
||||
|
||||
$sync_message[] = wp_kses_data( __(
|
||||
'You can switch order data storage <strong>only when the posts and orders tables are in sync</strong>.',
|
||||
'woocommerce'
|
||||
) );
|
||||
$sync_message[] = wp_kses_data(
|
||||
__(
|
||||
'You can switch order data storage <strong>only when the posts and orders tables are in sync</strong>.',
|
||||
'woocommerce'
|
||||
)
|
||||
);
|
||||
|
||||
$sync_message[] = sprintf(
|
||||
'<a href="%1$s" class="button button-link">%2$s</a>',
|
||||
|
|
|
@ -175,11 +175,19 @@ class DataSynchronizer implements BatchProcessorInterface {
|
|||
}
|
||||
|
||||
/**
|
||||
* Create the custom orders database tables.
|
||||
* Create the custom orders database tables and log an error if that's not possible.
|
||||
*
|
||||
* @return bool True if all the tables were successfully created, false otherwise.
|
||||
*/
|
||||
public function create_database_tables() {
|
||||
$this->database_util->dbdelta( $this->data_store->get_database_schema() );
|
||||
$this->check_orders_table_exists();
|
||||
$success = $this->check_orders_table_exists();
|
||||
if ( ! $success ) {
|
||||
$missing_tables = $this->database_util->get_missing_tables( $this->data_store->get_database_schema() );
|
||||
$missing_tables = implode( ', ', $missing_tables );
|
||||
$this->error_logger->error( "HPOS tables are missing in the database and couldn't be created. The missing tables are: $missing_tables" );
|
||||
}
|
||||
return $success;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -52,7 +52,7 @@ class DatabaseUtil {
|
|||
|
||||
foreach ( $dbdelta_output as $table_name => $result ) {
|
||||
if ( "Created table $table_name" === $result ) {
|
||||
$created_tables[] = $table_name;
|
||||
$created_tables[] = str_replace( '(', '', $table_name );
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue