Skip to content

Commit f7ed7c3

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

1 file changed

Lines changed: 43 additions & 18 deletions

File tree

integrations/query-monitor/boot.php

Lines changed: 43 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,50 @@
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 ( ! is_plugin_active( 'query-monitor/query-monitor.php' ) ) {
67+
return;
68+
}
69+
70+
if ( ! class_exists( 'QM_Backtrace' ) ) {
71+
return;
72+
}
73+
74+
require_once __DIR__ . '/plugin.php';
75+
76+
if ( ! defined( 'SQLITE_QUERY_MONITOR_LOADED' ) ) {
77+
define( 'SQLITE_QUERY_MONITOR_LOADED', true );
78+
}
79+
}
80+
81+
if ( function_exists( 'add_action' ) ) {
82+
add_action( 'plugins_loaded', 'register_sqlite_enhancements_for_query_monitor' );
83+
}
84+
85+
/*
86+
* On a multisite install, we can't easily determine the current site eagerly.
87+
* Therefore, let's bail out and let Query Monitor activate later as a plugin.
88+
*/
89+
if ( is_multisite() ) {
90+
return;
91+
}
92+
93+
/*
94+
* Now, let's try to load Query Monitor eagerly, to start logging queries early.
95+
* When the plugin is installed, we will check the database if it is also active.
96+
*/
5997
global $wpdb;
6098
if ( ! isset( $wpdb ) ) {
6199
return;
62100
}
63101

64-
// 2. Check if Query Monitor is active.
102+
// Check if Query Monitor is active.
65103
if ( null === $wpdb->options ) {
66104
global $table_prefix;
67105
$wpdb->set_prefix( $table_prefix ?? '' );
@@ -75,10 +113,6 @@
75113
'active_plugins'
76114
)
77115
);
78-
/**
79-
* $value may be null during WordPress Playground multisite setup.
80-
* @see https://github.com/WordPress/sqlite-database-integration/pull/219.
81-
*/
82116
if ( null !== $value ) {
83117
$query_monitor_active = in_array(
84118
'query-monitor/query-monitor.php',
@@ -94,14 +128,14 @@
94128
return;
95129
}
96130

97-
// 3. Determine the plugins directory.
131+
// Determine the plugins directory.
98132
if ( defined( 'WP_PLUGIN_DIR' ) ) {
99133
$plugins_dir = WP_PLUGIN_DIR;
100134
} else {
101135
$plugins_dir = WP_CONTENT_DIR . '/plugins';
102136
}
103137

104-
// 4. Load Query Monitor (as per the original "db.php" file).
138+
// Load Query Monitor (as per the original "db.php" file).
105139
$qm_dir = "{$plugins_dir}/query-monitor";
106140
$qm_php = "{$qm_dir}/classes/PHP.php";
107141

@@ -129,14 +163,5 @@
129163
define( 'SAVEQUERIES', true );
130164
}
131165

132-
// 5. Mark the Query Monitor integration as loaded.
166+
// Mark the Query Monitor integration as loaded.
133167
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)