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:
Rodrigo Primo 2019-06-28 17:49:39 -03:00
parent 356ad18826
commit b08fd05a7d
2 changed files with 133 additions and 111 deletions

View File

@ -448,6 +448,7 @@ $untested_plugins = $plugin_updates->get_untested_plugins( WC()->version, 'min
</tr> </tr>
<?php } ?> <?php } ?>
<?php if ( ! empty( $database['database_size'] ) && ! empty( $database['database_tables'] ) ) : ?>
<tr> <tr>
<td><?php esc_html_e( 'Total Database Size', 'woocommerce' ); ?></td> <td><?php esc_html_e( 'Total Database Size', 'woocommerce' ); ?></td>
<td class="help">&nbsp;</td> <td class="help">&nbsp;</td>
@ -495,6 +496,20 @@ $untested_plugins = $plugin_updates->get_untested_plugins( WC()->version, 'min
</td> </td>
</tr> </tr>
<?php } ?> <?php } ?>
<?php else : ?>
<tr>
<td><?php esc_html_e( 'Database information:', 'woocommerce' ); ?></td>
<td class="help">&nbsp;</td>
<td>
<?php
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 endif; ?>
</tbody> </tbody>
</table> </table>
<table class="wc_status_table widefat" cellspacing="0"> <table class="wc_status_table widefat" cellspacing="0">

View File

@ -696,6 +696,12 @@ class WC_REST_System_Status_V2_Controller extends WC_REST_Controller {
public function get_database_info() { public function get_database_info() {
global $wpdb; global $wpdb;
$tables = array();
$database_size = array();
// 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( $database_table_information = $wpdb->get_results(
$wpdb->prepare( $wpdb->prepare(
"SELECT "SELECT
@ -771,6 +777,7 @@ class WC_REST_System_Status_V2_Controller extends WC_REST_Controller {
$database_size['data'] += $table->data; $database_size['data'] += $table->data;
$database_size['index'] += $table->index; $database_size['index'] += $table->index;
} }
}
// Return all database info. Described by JSON Schema. // Return all database info. Described by JSON Schema.
return array( return array(