Fix: check if DB_NAME is defined before using it
WooCommerce relies on the constant DB_NAME to display information about the database tables in the system status page. The problem is that this constant is not always defined (e.g., when the plugin HyperDB is used to replace the standard wpdb class). When that is the case, WooCommerce will incorrectly say that its core tables are missing and the following PHP warning will be generated: ``` Use of undefined constant DB_NAME - assumed 'DB_NAME' (this will throw an Error in a future version of PHP) wp-content/plugins/woocommerce/includes/api/v2/class-wc-rest-system-status-v2-controller.php:708 ``` To fix this, this commit checks to see if DB_NAME is defined before using it. When the constant is not set, WooCommerce will display the following message to the users instead of the list of supposedly missing tables: ``` Database information: Unable to retrieve database information. Usually, this is not a problem, and it only means that your install is using a class that replaces the WordPress database class (e.g., HyperDB) and WooCommerce is unable to get database information. ```
This commit is contained in:
parent
356ad18826
commit
b08fd05a7d
|
@ -448,53 +448,68 @@ $untested_plugins = $plugin_updates->get_untested_plugins( WC()->version, 'min
|
|||
</tr>
|
||||
<?php } ?>
|
||||
|
||||
<tr>
|
||||
<td><?php esc_html_e( 'Total Database Size', 'woocommerce' ); ?></td>
|
||||
<td class="help"> </td>
|
||||
<td><?php printf( '%.2fMB', esc_html( $database['database_size']['data'] + $database['database_size']['index'] ) ); ?></td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td><?php esc_html_e( 'Database Data Size', 'woocommerce' ); ?></td>
|
||||
<td class="help"> </td>
|
||||
<td><?php printf( '%.2fMB', esc_html( $database['database_size']['data'] ) ); ?></td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td><?php esc_html_e( 'Database Index Size', 'woocommerce' ); ?></td>
|
||||
<td class="help"> </td>
|
||||
<td><?php printf( '%.2fMB', esc_html( $database['database_size']['index'] ) ); ?></td>
|
||||
</tr>
|
||||
|
||||
<?php foreach ( $database['database_tables']['woocommerce'] as $table => $table_data ) { ?>
|
||||
<?php if ( ! empty( $database['database_size'] ) && ! empty( $database['database_tables'] ) ) : ?>
|
||||
<tr>
|
||||
<td><?php echo esc_html( $table ); ?></td>
|
||||
<td><?php esc_html_e( 'Total Database Size', 'woocommerce' ); ?></td>
|
||||
<td class="help"> </td>
|
||||
<td><?php printf( '%.2fMB', esc_html( $database['database_size']['data'] + $database['database_size']['index'] ) ); ?></td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td><?php esc_html_e( 'Database Data Size', 'woocommerce' ); ?></td>
|
||||
<td class="help"> </td>
|
||||
<td><?php printf( '%.2fMB', esc_html( $database['database_size']['data'] ) ); ?></td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td><?php esc_html_e( 'Database Index Size', 'woocommerce' ); ?></td>
|
||||
<td class="help"> </td>
|
||||
<td><?php printf( '%.2fMB', esc_html( $database['database_size']['index'] ) ); ?></td>
|
||||
</tr>
|
||||
|
||||
<?php foreach ( $database['database_tables']['woocommerce'] as $table => $table_data ) { ?>
|
||||
<tr>
|
||||
<td><?php echo esc_html( $table ); ?></td>
|
||||
<td class="help"> </td>
|
||||
<td>
|
||||
<?php
|
||||
if ( ! $table_data ) {
|
||||
echo '<mark class="error"><span class="dashicons dashicons-warning"></span> ' . esc_html__( 'Table does not exist', 'woocommerce' ) . '</mark>';
|
||||
} else {
|
||||
/* Translators: %1$f: Table size, %2$f: Index size, %3$s Engine. */
|
||||
printf( esc_html__( 'Data: %1$.2fMB + Index: %2$.2fMB + Engine %3$s', 'woocommerce' ), esc_html( wc_format_decimal( $table_data['data'], 2 ) ), esc_html( wc_format_decimal( $table_data['index'], 2 ) ), esc_html( $table_data['engine'] ) );
|
||||
}
|
||||
?>
|
||||
</td>
|
||||
</tr>
|
||||
<?php } ?>
|
||||
|
||||
<?php foreach ( $database['database_tables']['other'] as $table => $table_data ) { ?>
|
||||
<tr>
|
||||
<td><?php echo esc_html( $table ); ?></td>
|
||||
<td class="help"> </td>
|
||||
<td>
|
||||
<?php
|
||||
/* Translators: %1$f: Table size, %2$f: Index size, %3$s Engine. */
|
||||
printf( esc_html__( 'Data: %1$.2fMB + Index: %2$.2fMB + Engine %3$s', 'woocommerce' ), esc_html( wc_format_decimal( $table_data['data'], 2 ) ), esc_html( wc_format_decimal( $table_data['index'], 2 ) ), esc_html( $table_data['engine'] ) );
|
||||
?>
|
||||
</td>
|
||||
</tr>
|
||||
<?php } ?>
|
||||
<?php else : ?>
|
||||
<tr>
|
||||
<td><?php esc_html_e( 'Database information:', 'woocommerce' ); ?></td>
|
||||
<td class="help"> </td>
|
||||
<td>
|
||||
<?php
|
||||
if ( ! $table_data ) {
|
||||
echo '<mark class="error"><span class="dashicons dashicons-warning"></span> ' . esc_html__( 'Table does not exist', 'woocommerce' ) . '</mark>';
|
||||
} else {
|
||||
/* Translators: %1$f: Table size, %2$f: Index size, %3$s Engine. */
|
||||
printf( esc_html__( 'Data: %1$.2fMB + Index: %2$.2fMB + Engine %3$s', 'woocommerce' ), esc_html( wc_format_decimal( $table_data['data'], 2 ) ), esc_html( wc_format_decimal( $table_data['index'], 2 ) ), esc_html( $table_data['engine'] ) );
|
||||
}
|
||||
esc_html_e(
|
||||
'Unable to retrieve database information. Usually, this is not a problem, and it only means that your install is using a class that replaces the WordPress database class (e.g., HyperDB) and WooCommerce is unable to get database information.',
|
||||
'woocommerce'
|
||||
);
|
||||
?>
|
||||
</td>
|
||||
</tr>
|
||||
<?php } ?>
|
||||
|
||||
<?php foreach ( $database['database_tables']['other'] as $table => $table_data ) { ?>
|
||||
<tr>
|
||||
<td><?php echo esc_html( $table ); ?></td>
|
||||
<td class="help"> </td>
|
||||
<td>
|
||||
<?php
|
||||
/* Translators: %1$f: Table size, %2$f: Index size, %3$s Engine. */
|
||||
printf( esc_html__( 'Data: %1$.2fMB + Index: %2$.2fMB + Engine %3$s', 'woocommerce' ), esc_html( wc_format_decimal( $table_data['data'], 2 ) ), esc_html( wc_format_decimal( $table_data['index'], 2 ) ), esc_html( $table_data['engine'] ) );
|
||||
?>
|
||||
</td>
|
||||
</tr>
|
||||
<?php } ?>
|
||||
<?php endif; ?>
|
||||
</tbody>
|
||||
</table>
|
||||
<table class="wc_status_table widefat" cellspacing="0">
|
||||
|
|
|
@ -696,80 +696,87 @@ class WC_REST_System_Status_V2_Controller extends WC_REST_Controller {
|
|||
public function get_database_info() {
|
||||
global $wpdb;
|
||||
|
||||
$database_table_information = $wpdb->get_results(
|
||||
$wpdb->prepare(
|
||||
"SELECT
|
||||
table_name AS 'name',
|
||||
engine,
|
||||
round( ( data_length / 1024 / 1024 ), 2 ) 'data',
|
||||
round( ( index_length / 1024 / 1024 ), 2 ) 'index'
|
||||
FROM information_schema.TABLES
|
||||
WHERE table_schema = %s
|
||||
ORDER BY name ASC;",
|
||||
DB_NAME
|
||||
)
|
||||
);
|
||||
$tables = array();
|
||||
$database_size = array();
|
||||
|
||||
// WC Core tables to check existence of.
|
||||
$core_tables = apply_filters(
|
||||
'woocommerce_database_tables',
|
||||
array(
|
||||
'woocommerce_sessions',
|
||||
'woocommerce_api_keys',
|
||||
'woocommerce_attribute_taxonomies',
|
||||
'woocommerce_downloadable_product_permissions',
|
||||
'woocommerce_order_items',
|
||||
'woocommerce_order_itemmeta',
|
||||
'woocommerce_tax_rates',
|
||||
'woocommerce_tax_rate_locations',
|
||||
'woocommerce_shipping_zones',
|
||||
'woocommerce_shipping_zone_locations',
|
||||
'woocommerce_shipping_zone_methods',
|
||||
'woocommerce_payment_tokens',
|
||||
'woocommerce_payment_tokenmeta',
|
||||
'woocommerce_log',
|
||||
)
|
||||
);
|
||||
|
||||
/**
|
||||
* Adding the prefix to the tables array, for backwards compatibility.
|
||||
*
|
||||
* If we changed the tables above to include the prefix, then any filters against that table could break.
|
||||
*/
|
||||
$core_tables = array_map( array( $this, 'add_db_table_prefix' ), $core_tables );
|
||||
|
||||
/**
|
||||
* Organize WooCommerce and non-WooCommerce tables separately for display purposes later.
|
||||
*
|
||||
* To ensure we include all WC tables, even if they do not exist, pre-populate the WC array with all the tables.
|
||||
*/
|
||||
$tables = array(
|
||||
'woocommerce' => array_fill_keys( $core_tables, false ),
|
||||
'other' => array(),
|
||||
);
|
||||
|
||||
$database_size = array(
|
||||
'data' => 0,
|
||||
'index' => 0,
|
||||
);
|
||||
|
||||
$site_tables_prefix = $wpdb->get_blog_prefix( get_current_blog_id() );
|
||||
$global_tables = $wpdb->tables( 'global', true );
|
||||
foreach ( $database_table_information as $table ) {
|
||||
// Only include tables matching the prefix of the current site, this is to prevent displaying all tables on a MS install not relating to the current.
|
||||
if ( is_multisite() && 0 !== strpos( $table->name, $site_tables_prefix ) && ! in_array( $table->name, $global_tables, true ) ) {
|
||||
continue;
|
||||
}
|
||||
$table_type = in_array( $table->name, $core_tables ) ? 'woocommerce' : 'other';
|
||||
|
||||
$tables[ $table_type ][ $table->name ] = array(
|
||||
'data' => $table->data,
|
||||
'index' => $table->index,
|
||||
'engine' => $table->engine,
|
||||
// It is not possible to get the database name from some classes that replace wpdb (e.g., HyperDB)
|
||||
// and that is why this if condition is needed.
|
||||
if ( defined( 'DB_NAME' ) ) {
|
||||
$database_table_information = $wpdb->get_results(
|
||||
$wpdb->prepare(
|
||||
"SELECT
|
||||
table_name AS 'name',
|
||||
engine,
|
||||
round( ( data_length / 1024 / 1024 ), 2 ) 'data',
|
||||
round( ( index_length / 1024 / 1024 ), 2 ) 'index'
|
||||
FROM information_schema.TABLES
|
||||
WHERE table_schema = %s
|
||||
ORDER BY name ASC;",
|
||||
DB_NAME
|
||||
)
|
||||
);
|
||||
|
||||
$database_size['data'] += $table->data;
|
||||
$database_size['index'] += $table->index;
|
||||
// WC Core tables to check existence of.
|
||||
$core_tables = apply_filters(
|
||||
'woocommerce_database_tables',
|
||||
array(
|
||||
'woocommerce_sessions',
|
||||
'woocommerce_api_keys',
|
||||
'woocommerce_attribute_taxonomies',
|
||||
'woocommerce_downloadable_product_permissions',
|
||||
'woocommerce_order_items',
|
||||
'woocommerce_order_itemmeta',
|
||||
'woocommerce_tax_rates',
|
||||
'woocommerce_tax_rate_locations',
|
||||
'woocommerce_shipping_zones',
|
||||
'woocommerce_shipping_zone_locations',
|
||||
'woocommerce_shipping_zone_methods',
|
||||
'woocommerce_payment_tokens',
|
||||
'woocommerce_payment_tokenmeta',
|
||||
'woocommerce_log',
|
||||
)
|
||||
);
|
||||
|
||||
/**
|
||||
* Adding the prefix to the tables array, for backwards compatibility.
|
||||
*
|
||||
* If we changed the tables above to include the prefix, then any filters against that table could break.
|
||||
*/
|
||||
$core_tables = array_map( array( $this, 'add_db_table_prefix' ), $core_tables );
|
||||
|
||||
/**
|
||||
* Organize WooCommerce and non-WooCommerce tables separately for display purposes later.
|
||||
*
|
||||
* To ensure we include all WC tables, even if they do not exist, pre-populate the WC array with all the tables.
|
||||
*/
|
||||
$tables = array(
|
||||
'woocommerce' => array_fill_keys( $core_tables, false ),
|
||||
'other' => array(),
|
||||
);
|
||||
|
||||
$database_size = array(
|
||||
'data' => 0,
|
||||
'index' => 0,
|
||||
);
|
||||
|
||||
$site_tables_prefix = $wpdb->get_blog_prefix( get_current_blog_id() );
|
||||
$global_tables = $wpdb->tables( 'global', true );
|
||||
foreach ( $database_table_information as $table ) {
|
||||
// Only include tables matching the prefix of the current site, this is to prevent displaying all tables on a MS install not relating to the current.
|
||||
if ( is_multisite() && 0 !== strpos( $table->name, $site_tables_prefix ) && ! in_array( $table->name, $global_tables, true ) ) {
|
||||
continue;
|
||||
}
|
||||
$table_type = in_array( $table->name, $core_tables ) ? 'woocommerce' : 'other';
|
||||
|
||||
$tables[ $table_type ][ $table->name ] = array(
|
||||
'data' => $table->data,
|
||||
'index' => $table->index,
|
||||
'engine' => $table->engine,
|
||||
);
|
||||
|
||||
$database_size['data'] += $table->data;
|
||||
$database_size['index'] += $table->index;
|
||||
}
|
||||
}
|
||||
|
||||
// Return all database info. Described by JSON Schema.
|
||||
|
|
Loading…
Reference in New Issue