Passed
Push — master ( fa0560...6b8bc5 )
by Mike
04:54 queued 31s
created

DatabaseInformation   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 57
dl 0
loc 103
rs 10
c 0
b 0
f 0
wmc 7

2 Methods

Rating   Name   Duplication   Size   Complexity  
A add_db_table_prefix() 0 3 1
B get_database_info() 0 86 6
1
<?php
2
/**
3
 * Database information for status report.
4
 *
5
 * @package WooCommerce/Utilities
6
 */
7
8
namespace WooCommerce\RestApi\Controllers\Version4\Utilities;
9
10
/**
11
 * DatabaseInformation class.
12
 */
13
class DatabaseInformation {
14
	/**
15
	 * Add prefix to table.
16
	 *
17
	 * @param string $table Table name.
18
	 * @return string
19
	 */
20
	protected function add_db_table_prefix( $table ) {
21
		global $wpdb;
22
		return $wpdb->prefix . $table;
23
	}
24
25
	/**
26
	 * Get array of database information. Version, prefix, and table existence.
27
	 *
28
	 * @return array
29
	 */
30
	public function get_database_info() {
31
		global $wpdb;
32
33
		$database_table_information = $wpdb->get_results(
34
			$wpdb->prepare(
35
				"SELECT
36
				    table_name AS 'name',
37
					engine,
38
				    round( ( data_length / 1024 / 1024 ), 2 ) 'data',
39
				    round( ( index_length / 1024 / 1024 ), 2 ) 'index'
40
				FROM information_schema.TABLES
41
				WHERE table_schema = %s
42
				ORDER BY name ASC;",
43
				DB_NAME
44
			)
45
		);
46
47
		// WC Core tables to check existence of.
48
		$core_tables = apply_filters(
49
			'woocommerce_database_tables',
50
			array(
51
				'woocommerce_sessions',
52
				'woocommerce_api_keys',
53
				'woocommerce_attribute_taxonomies',
54
				'woocommerce_downloadable_product_permissions',
55
				'woocommerce_order_items',
56
				'woocommerce_order_itemmeta',
57
				'woocommerce_tax_rates',
58
				'woocommerce_tax_rate_locations',
59
				'woocommerce_shipping_zones',
60
				'woocommerce_shipping_zone_locations',
61
				'woocommerce_shipping_zone_methods',
62
				'woocommerce_payment_tokens',
63
				'woocommerce_payment_tokenmeta',
64
				'woocommerce_log',
65
			)
66
		);
67
68
		/**
69
		 * Adding the prefix to the tables array, for backwards compatibility.
70
		 *
71
		 * If we changed the tables above to include the prefix, then any filters against that table could break.
72
		 */
73
		$core_tables = array_map( array( $this, 'add_db_table_prefix' ), $core_tables );
74
75
		/**
76
		 * Organize WooCommerce and non-WooCommerce tables separately for display purposes later.
77
		 *
78
		 * To ensure we include all WC tables, even if they do not exist, pre-populate the WC array with all the tables.
79
		 */
80
		$tables = array(
81
			'woocommerce' => array_fill_keys( $core_tables, false ),
82
			'other'       => array(),
83
		);
84
85
		$database_size = array(
86
			'data'  => 0,
87
			'index' => 0,
88
		);
89
90
		$site_tables_prefix = $wpdb->get_blog_prefix( get_current_blog_id() );
91
		$global_tables      = $wpdb->tables( 'global', true );
92
		foreach ( $database_table_information as $table ) {
93
			// 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.
94
			if ( is_multisite() && 0 !== strpos( $table->name, $site_tables_prefix ) && ! in_array( $table->name, $global_tables, true ) ) {
95
				continue;
96
			}
97
			$table_type = in_array( $table->name, $core_tables, true ) ? 'woocommerce' : 'other';
98
99
			$tables[ $table_type ][ $table->name ] = array(
100
				'data'   => $table->data,
101
				'index'  => $table->index,
102
				'engine' => $table->engine,
103
			);
104
105
			$database_size['data']  += $table->data;
106
			$database_size['index'] += $table->index;
107
		}
108
109
		// Return all database info. Described by JSON Schema.
110
		return array(
111
			'wc_database_version'    => get_option( 'woocommerce_db_version' ),
112
			'database_prefix'        => $wpdb->prefix,
113
			'maxmind_geoip_database' => \WC_Geolocation::get_local_database_path(),
114
			'database_tables'        => $tables,
115
			'database_size'          => $database_size,
116
		);
117
	}
118
}
119