Skip to content
This repository was archived by the owner on Jul 6, 2025. It is now read-only.

Commit 7d9babf

Browse files
Fix: check if DB_NAME is defined before using it
Port of #3 for v2 and v3
1 parent 1b3ea5e commit 7d9babf

File tree

1 file changed

+75
-68
lines changed

1 file changed

+75
-68
lines changed

src/Controllers/Version2/class-wc-rest-system-status-v2-controller.php

Lines changed: 75 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -696,80 +696,87 @@ protected function add_db_table_prefix( $table ) {
696696
public function get_database_info() {
697697
global $wpdb;
698698

699-
$database_table_information = $wpdb->get_results(
700-
$wpdb->prepare(
701-
"SELECT
702-
table_name AS 'name',
703-
engine AS 'engine',
704-
round( ( data_length / 1024 / 1024 ), 2 ) 'data',
705-
round( ( index_length / 1024 / 1024 ), 2 ) 'index'
706-
FROM information_schema.TABLES
707-
WHERE table_schema = %s
708-
ORDER BY name ASC;",
709-
DB_NAME
710-
)
711-
);
712-
713-
// WC Core tables to check existence of.
714-
$core_tables = apply_filters(
715-
'woocommerce_database_tables',
716-
array(
717-
'woocommerce_sessions',
718-
'woocommerce_api_keys',
719-
'woocommerce_attribute_taxonomies',
720-
'woocommerce_downloadable_product_permissions',
721-
'woocommerce_order_items',
722-
'woocommerce_order_itemmeta',
723-
'woocommerce_tax_rates',
724-
'woocommerce_tax_rate_locations',
725-
'woocommerce_shipping_zones',
726-
'woocommerce_shipping_zone_locations',
727-
'woocommerce_shipping_zone_methods',
728-
'woocommerce_payment_tokens',
729-
'woocommerce_payment_tokenmeta',
730-
'woocommerce_log',
731-
)
732-
);
699+
$tables = array();
700+
$database_size = array();
701+
702+
// It is not possible to get the database name from some classes that replace wpdb (e.g., HyperDB)
703+
// and that is why this if condition is needed.
704+
if ( defined( 'DB_NAME' ) ) {
705+
$database_table_information = $wpdb->get_results(
706+
$wpdb->prepare(
707+
"SELECT
708+
table_name AS 'name',
709+
engine AS 'engine',
710+
round( ( data_length / 1024 / 1024 ), 2 ) 'data',
711+
round( ( index_length / 1024 / 1024 ), 2 ) 'index'
712+
FROM information_schema.TABLES
713+
WHERE table_schema = %s
714+
ORDER BY name ASC;",
715+
DB_NAME
716+
)
717+
);
733718

734-
/**
735-
* Adding the prefix to the tables array, for backwards compatibility.
736-
*
737-
* If we changed the tables above to include the prefix, then any filters against that table could break.
738-
*/
739-
$core_tables = array_map( array( $this, 'add_db_table_prefix' ), $core_tables );
719+
// WC Core tables to check existence of.
720+
$core_tables = apply_filters(
721+
'woocommerce_database_tables',
722+
array(
723+
'woocommerce_sessions',
724+
'woocommerce_api_keys',
725+
'woocommerce_attribute_taxonomies',
726+
'woocommerce_downloadable_product_permissions',
727+
'woocommerce_order_items',
728+
'woocommerce_order_itemmeta',
729+
'woocommerce_tax_rates',
730+
'woocommerce_tax_rate_locations',
731+
'woocommerce_shipping_zones',
732+
'woocommerce_shipping_zone_locations',
733+
'woocommerce_shipping_zone_methods',
734+
'woocommerce_payment_tokens',
735+
'woocommerce_payment_tokenmeta',
736+
'woocommerce_log',
737+
)
738+
);
740739

741-
/**
742-
* Organize WooCommerce and non-WooCommerce tables separately for display purposes later.
743-
*
744-
* To ensure we include all WC tables, even if they do not exist, pre-populate the WC array with all the tables.
745-
*/
746-
$tables = array(
747-
'woocommerce' => array_fill_keys( $core_tables, false ),
748-
'other' => array(),
749-
);
740+
/**
741+
* Adding the prefix to the tables array, for backwards compatibility.
742+
*
743+
* If we changed the tables above to include the prefix, then any filters against that table could break.
744+
*/
745+
$core_tables = array_map( array( $this, 'add_db_table_prefix' ), $core_tables );
746+
747+
/**
748+
* Organize WooCommerce and non-WooCommerce tables separately for display purposes later.
749+
*
750+
* To ensure we include all WC tables, even if they do not exist, pre-populate the WC array with all the tables.
751+
*/
752+
$tables = array(
753+
'woocommerce' => array_fill_keys( $core_tables, false ),
754+
'other' => array(),
755+
);
750756

751-
$database_size = array(
752-
'data' => 0,
753-
'index' => 0,
754-
);
757+
$database_size = array(
758+
'data' => 0,
759+
'index' => 0,
760+
);
755761

756-
$site_tables_prefix = $wpdb->get_blog_prefix( get_current_blog_id() );
757-
$global_tables = $wpdb->tables( 'global', true );
758-
foreach ( $database_table_information as $table ) {
759-
// 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.
760-
if ( is_multisite() && 0 !== strpos( $table->name, $site_tables_prefix ) && ! in_array( $table->name, $global_tables, true ) ) {
761-
continue;
762-
}
763-
$table_type = in_array( $table->name, $core_tables ) ? 'woocommerce' : 'other';
762+
$site_tables_prefix = $wpdb->get_blog_prefix( get_current_blog_id() );
763+
$global_tables = $wpdb->tables( 'global', true );
764+
foreach ( $database_table_information as $table ) {
765+
// 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.
766+
if ( is_multisite() && 0 !== strpos( $table->name, $site_tables_prefix ) && ! in_array( $table->name, $global_tables, true ) ) {
767+
continue;
768+
}
769+
$table_type = in_array( $table->name, $core_tables ) ? 'woocommerce' : 'other';
764770

765-
$tables[ $table_type ][ $table->name ] = array(
766-
'data' => $table->data,
767-
'index' => $table->index,
768-
'engine' => $table->engine,
769-
);
771+
$tables[ $table_type ][ $table->name ] = array(
772+
'data' => $table->data,
773+
'index' => $table->index,
774+
'engine' => $table->engine,
775+
);
770776

771-
$database_size['data'] += $table->data;
772-
$database_size['index'] += $table->index;
777+
$database_size['data'] += $table->data;
778+
$database_size['index'] += $table->index;
779+
}
773780
}
774781

775782
// Return all database info. Described by JSON Schema.

0 commit comments

Comments
 (0)