Skip to content

Commit 0bc3d0f

Browse files
committed
Improve Query Monitor setup to better support multisite installs
1 parent 1dc9eec commit 0bc3d0f

1 file changed

Lines changed: 39 additions & 18 deletions

File tree

integrations/query-monitor/boot.php

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
define( 'QM_DB_SYMLINK', false );
2222
}
2323

24-
// 1. Check if we should load Query Monitor (as per the original "db.php" file).
24+
// Check if we should load Query Monitor (as per the original "db.php" file).
2525
if ( ! defined( 'ABSPATH' ) ) {
2626
exit;
2727
}
@@ -56,12 +56,46 @@
5656
}
5757
}
5858

59+
/*
60+
* Register SQLite enhancements for Query Monitor when plugins are loaded.
61+
*
62+
* This will also ensure that the plugin Query Monitor is fully initialized even
63+
* when we can't load it eagerly, e.g. on a multisite install.
64+
*/
65+
function register_sqlite_enhancements_for_query_monitor() {
66+
if ( ! class_exists( 'QM_Backtrace' ) ) {
67+
return;
68+
}
69+
70+
require_once __DIR__ . '/plugin.php';
71+
72+
if ( ! defined( 'SQLITE_QUERY_MONITOR_LOADED' ) ) {
73+
define( 'SQLITE_QUERY_MONITOR_LOADED', true );
74+
}
75+
}
76+
77+
if ( function_exists( 'add_action' ) ) {
78+
add_action( 'plugins_loaded', 'register_sqlite_enhancements_for_query_monitor' );
79+
}
80+
81+
/*
82+
* On a multisite install, we can't easily determine the current site eagerly.
83+
* Therefore, let's bail out and let Query Monitor activate later as a plugin.
84+
*/
85+
if ( is_multisite() ) {
86+
return;
87+
}
88+
89+
/*
90+
* Now, let's try to load Query Monitor eagerly, to start logging queries early.
91+
* When the plugin is installed, we will check the database if it is also active.
92+
*/
5993
global $wpdb;
6094
if ( ! isset( $wpdb ) ) {
6195
return;
6296
}
6397

64-
// 2. Check if Query Monitor is active.
98+
// Check if Query Monitor is active.
6599
if ( null === $wpdb->options ) {
66100
global $table_prefix;
67101
$wpdb->set_prefix( $table_prefix ?? '' );
@@ -75,10 +109,6 @@
75109
'active_plugins'
76110
)
77111
);
78-
/**
79-
* $value may be null during WordPress Playground multisite setup.
80-
* @see https://github.com/WordPress/sqlite-database-integration/pull/219.
81-
*/
82112
if ( null !== $value ) {
83113
$query_monitor_active = in_array(
84114
'query-monitor/query-monitor.php',
@@ -94,14 +124,14 @@
94124
return;
95125
}
96126

97-
// 3. Determine the plugins directory.
127+
// Determine the plugins directory.
98128
if ( defined( 'WP_PLUGIN_DIR' ) ) {
99129
$plugins_dir = WP_PLUGIN_DIR;
100130
} else {
101131
$plugins_dir = WP_CONTENT_DIR . '/plugins';
102132
}
103133

104-
// 4. Load Query Monitor (as per the original "db.php" file).
134+
// Load Query Monitor (as per the original "db.php" file).
105135
$qm_dir = "{$plugins_dir}/query-monitor";
106136
$qm_php = "{$qm_dir}/classes/PHP.php";
107137

@@ -129,14 +159,5 @@
129159
define( 'SAVEQUERIES', true );
130160
}
131161

132-
// 5. Mark the Query Monitor integration as loaded.
162+
// Mark the Query Monitor integration as loaded.
133163
define( 'SQLITE_QUERY_MONITOR_LOADED', true );
134-
135-
// 6. Register the SQLite enhancements for Query Monitor.
136-
function register_sqlite_enhancements_for_query_monitor() {
137-
require_once __DIR__ . '/plugin.php';
138-
}
139-
140-
if ( function_exists( 'add_action' ) ) {
141-
add_action( 'plugins_loaded', 'register_sqlite_enhancements_for_query_monitor' );
142-
}

0 commit comments

Comments
 (0)