Skip to content

Commit

Permalink
Merge pull request #4 from woocommerce/fix/db-name-v2
Browse files Browse the repository at this point in the history
Fix: check if DB_NAME is defined before using it
  • Loading branch information
claudiosanches authored Jul 16, 2019
2 parents 491a474 + 46e5a21 commit 3cc586d
Showing 1 changed file with 75 additions and 68 deletions.
143 changes: 75 additions & 68 deletions src/Controllers/Version2/class-wc-rest-system-status-v2-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -696,80 +696,87 @@ protected function add_db_table_prefix( $table ) {
public function get_database_info() {
global $wpdb;

$database_table_information = $wpdb->get_results(
$wpdb->prepare(
"SELECT
table_name AS 'name',
engine AS '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
)
);

// 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',
)
);
$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(
$wpdb->prepare(
"SELECT
table_name AS 'name',
engine AS '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
)
);

/**
* 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 );
// 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',
)
);

/**
* 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(),
);
/**
* 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,
);
$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';
$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,
);
$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;
$database_size['data'] += $table->data;
$database_size['index'] += $table->index;
}
}

// Return all database info. Described by JSON Schema.
Expand Down

0 comments on commit 3cc586d

Please sign in to comment.