Add `verify_base_db` method to check if all base tables are present.

Optionally, also adds a notice in case all db tables are not present. Returns list of tables.

Note that we only check missing tables and don't care about exact table structure because many time tables are modified by merchants to better suit their needs (indexes, collations etc).
This commit is contained in:
vedanshujain 2020-05-11 23:34:51 +05:30
parent 93811e0a56
commit 3acc03c804
2 changed files with 66 additions and 1 deletions

View File

@ -0,0 +1,32 @@
<?php
/**
* Admin View: Notice - Base table missing.
*
* @package WooCommerce\Admin
*/
defined( 'ABSPATH' ) || exit;
?>
<div class="updated woocommerce-message">
<a class="woocommerce-message-close notice-dismiss"
href="<?php echo esc_url( wp_nonce_url( add_query_arg( 'wc-hide-notice', 'base_tables_missing' ), 'woocommerce_hide_notices_nonce', '_wc_notice_nonce' ) ); ?>">
<?php esc_html_e( 'Dismiss', 'woocommerce' ); ?>
</a>
<p>
<strong><?php esc_html_e( 'Database tables missing', 'woocommerce' ); ?></strong>
</p>
<p>
<?php
echo wp_kses_post(
sprintf(
/* translators: %1%s: WooCommerce status page %2$s: Link to check again */
__( 'One or more tables required for WooCommerce to function are missing, some features may not work as expected. Please visit <a href="%1$s">WooCommerce status page</a> to see details, or <a href="%2$s"> check again.</a>', 'woocommerce' ),
admin_url( 'admin.php?page=wc-status#status-database' ),
wp_nonce_url( admin_url( 'admin.php?page=wc-status&tab=tools&action=verify_db_tables' ), 'debug_action' )
)
);
?>
</p>
</div>

View File

@ -287,6 +287,7 @@ class WC_Install {
WC()->wpdb_table_fix();
self::remove_admin_notices();
self::create_tables();
self::verify_base_tables();
self::create_options();
self::create_roles();
self::setup_environment();
@ -303,6 +304,37 @@ class WC_Install {
do_action( 'woocommerce_installed' );
}
/**
* Check if all the base tables are present.
*
* @param bool $modify_notice Whether to modify notice based on if all tables are present.
*
* @return array List of querues.
*/
public static function verify_base_tables( $modify_notice = true ) {
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
$queries = dbDelta( self::get_schema(), false );
$missing_tables = array();
foreach ( $queries as $table_name => $result ) {
if ( "Created table $table_name" === $result ) {
$missing_tables[] = $table_name;
}
}
if ( 0 < count( $missing_tables ) ) {
if ( $modify_notice ) {
WC_Admin_Notices::add_notice( 'base_tables_missing' );
}
} else {
if ( $modify_notice ) {
WC_Admin_Notices::remove_notice( 'base_tables_missing' );
}
update_option( 'woocommerce-db-verified', WC()->version );
}
return $missing_tables;
}
/**
* Reset any notices added to admin.
*
@ -630,8 +662,9 @@ class WC_Install {
* woocommerce_order_itemmeta - Order line item meta is stored in a table for storing extra data.
* woocommerce_tax_rates - Tax Rates are stored inside 2 tables making tax queries simple and efficient.
* woocommerce_tax_rate_locations - Each rate can be applied to more than one postcode/city hence the second table.
* wc_reserved_stock - Hold reserved stock during a checkout.
*/
private static function create_tables() {
public static function create_tables() {
global $wpdb;
$wpdb->hide_errors();