Skip to content

Commit e49af0a

Browse files
committed
Implement basic integration with Query Monitor
1 parent fd0d282 commit e49af0a

2 files changed

Lines changed: 59 additions & 2 deletions

File tree

wp-includes/sqlite/class-wp-sqlite-db.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,9 @@ public function query( $query ) {
427427
// Keep track of the last query for debug.
428428
$this->last_query = $query;
429429

430+
// Save the query count before running another query.
431+
$last_query_count = count( $this->queries ?? array() );
432+
430433
/*
431434
* @TODO: WPDB uses "$this->check_current_query" to check table/column
432435
* charset and strip all invalid characters from the query.
@@ -478,6 +481,13 @@ public function query( $query ) {
478481
$return_val = $num_rows;
479482
}
480483

484+
// When Query Monitor is active, let's add the Query Monitor backtrace,
485+
// so it can display extended query information in the developer panel.
486+
$query_index = $last_query_count;
487+
if ( class_exists( 'QM_Backtrace' ) && isset( $this->queries[ $query_index ] ) ) {
488+
$this->queries[ $query_index ]['trace'] = new QM_Backtrace();
489+
}
490+
481491
return $return_val;
482492
}
483493

wp-includes/sqlite/db.php

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,57 @@
6161
* that are present in the GitHub repository
6262
* but not the plugin published on WordPress.org.
6363
*/
64+
global $wpdb;
6465
$crosscheck_tests_file_path = dirname( __DIR__, 2 ) . '/tests/class-wp-sqlite-crosscheck-db.php';
6566
if ( defined( 'SQLITE_DEBUG_CROSSCHECK' ) && SQLITE_DEBUG_CROSSCHECK && file_exists( $crosscheck_tests_file_path ) ) {
6667
require_once $crosscheck_tests_file_path;
67-
$GLOBALS['wpdb'] = new WP_SQLite_Crosscheck_DB( DB_NAME );
68+
$wpdb = new WP_SQLite_Crosscheck_DB( DB_NAME );
6869
} else {
69-
$GLOBALS['wpdb'] = new WP_SQLite_DB( defined( 'DB_NAME' ) ? DB_NAME : '' );
70+
// Allow plugins to wrap the wpdb object.
71+
$wpdb = new WP_SQLite_DB( defined( 'DB_NAME' ) ? DB_NAME : '' );
72+
73+
/*
74+
* Query Monitor integration:
75+
*
76+
* When the Query Monitor plugin exists in its standard location, let's check
77+
* if it is active, so we can load it eagerly. This is a workaround to avoid
78+
* SQLite and Query Monitor competing for the "wp-content/db.php" file.
79+
*/
80+
global $table_prefix;
81+
$plugins_dir = defined( 'WP_PLUGIN_DIR' ) ? WP_PLUGIN_DIR : WP_CONTENT_DIR . '/plugins';
82+
$qm_db_file = $plugins_dir . '/query-monitor/wp-content/db.php';
83+
if ( isset( $table_prefix ) && file_exists( $qm_db_file ) ) {
84+
// Check if Query Monitor is active.
85+
$wpdb->set_prefix( $table_prefix );
86+
$value = $wpdb->get_row(
87+
$wpdb->prepare(
88+
"SELECT option_value FROM $wpdb->options WHERE option_name = %s LIMIT 1",
89+
'active_plugins'
90+
)
91+
);
92+
93+
$query_monitor_active = in_array(
94+
'query-monitor/query-monitor.php',
95+
unserialize( $value->option_value ),
96+
true
97+
);
98+
99+
if ( $query_monitor_active ) {
100+
// Load Query Monitor's "db.php" file. This overrides the global $wpdb
101+
// object, so we need to back it up before and restore after loading.
102+
$_wpdb = $wpdb;
103+
require_once $qm_db_file;
104+
$wpdb = $_wpdb;
105+
106+
// Remove Query Monitor's deactivation hook so that it doesn't delete
107+
// the "db.php" file. Currently, it attempts so when QM_DB is defined.
108+
add_action(
109+
'deactivate_query-monitor/query-monitor.php',
110+
function () {
111+
remove_all_actions( 'deactivate_query-monitor/query-monitor.php' );
112+
},
113+
0
114+
);
115+
}
116+
}
70117
}

0 commit comments

Comments
 (0)