|
21 | 21 | define( 'QM_DB_SYMLINK', false );
|
22 | 22 | }
|
23 | 23 |
|
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). |
25 | 25 | if ( ! defined( 'ABSPATH' ) ) {
|
26 | 26 | exit;
|
27 | 27 | }
|
|
56 | 56 | }
|
57 | 57 | }
|
58 | 58 |
|
| 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 | + */ |
59 | 93 | global $wpdb;
|
60 | 94 | if ( ! isset( $wpdb ) ) {
|
61 | 95 | return;
|
62 | 96 | }
|
63 | 97 |
|
64 |
| -// 2. Check if Query Monitor is active. |
| 98 | +// Check if Query Monitor is installed. |
| 99 | +if ( defined( 'WP_PLUGIN_DIR' ) ) { |
| 100 | + $plugins_dir = WP_PLUGIN_DIR; |
| 101 | +} else { |
| 102 | + $plugins_dir = WP_CONTENT_DIR . '/plugins'; |
| 103 | +} |
| 104 | + |
| 105 | +$qm_dir = "{$plugins_dir}/query-monitor"; |
| 106 | +$qm_php = "{$qm_dir}/classes/PHP.php"; |
| 107 | + |
| 108 | +if ( ! is_readable( $qm_php ) ) { |
| 109 | + return; |
| 110 | +} |
| 111 | + |
| 112 | +// Check if Query Monitor is active. |
65 | 113 | if ( null === $wpdb->options ) {
|
66 | 114 | global $table_prefix;
|
67 | 115 | $wpdb->set_prefix( $table_prefix ?? '' );
|
68 | 116 | }
|
69 | 117 |
|
70 | 118 | $query_monitor_active = false;
|
71 | 119 | try {
|
72 |
| - $value = $wpdb->get_row( |
| 120 | + // Make sure no errors are displayed when the query fails. |
| 121 | + $show_errors = $wpdb->hide_errors(); |
| 122 | + $value = $wpdb->get_row( |
73 | 123 | $wpdb->prepare(
|
74 | 124 | "SELECT option_value FROM $wpdb->options WHERE option_name = %s LIMIT 1",
|
75 | 125 | 'active_plugins'
|
76 | 126 | )
|
77 | 127 | );
|
78 |
| - /** |
79 |
| - * $value may be null during WordPress Playground multisite setup. |
80 |
| - * @see https://github.com/WordPress/sqlite-database-integration/pull/219. |
81 |
| - */ |
| 128 | + $wpdb->show_errors( $show_errors ); |
| 129 | + |
82 | 130 | if ( null !== $value ) {
|
83 | 131 | $query_monitor_active = in_array(
|
84 | 132 | 'query-monitor/query-monitor.php',
|
|
94 | 142 | return;
|
95 | 143 | }
|
96 | 144 |
|
97 |
| -// 3. Determine the plugins directory. |
98 |
| -if ( defined( 'WP_PLUGIN_DIR' ) ) { |
99 |
| - $plugins_dir = WP_PLUGIN_DIR; |
100 |
| -} else { |
101 |
| - $plugins_dir = WP_CONTENT_DIR . '/plugins'; |
102 |
| -} |
103 |
| - |
104 |
| -// 4. Load Query Monitor (as per the original "db.php" file). |
105 |
| -$qm_dir = "{$plugins_dir}/query-monitor"; |
106 |
| -$qm_php = "{$qm_dir}/classes/PHP.php"; |
107 |
| - |
108 |
| -if ( ! is_readable( $qm_php ) ) { |
109 |
| - return; |
110 |
| -} |
| 145 | +// Load Query Monitor eagerly (as per the original "db.php" file). |
111 | 146 | require_once $qm_php;
|
112 | 147 |
|
113 | 148 | if ( ! QM_PHP::version_met() ) {
|
|
129 | 164 | define( 'SAVEQUERIES', true );
|
130 | 165 | }
|
131 | 166 |
|
132 |
| -// 5. Mark the Query Monitor integration as loaded. |
| 167 | +// Mark the Query Monitor integration as loaded. |
133 | 168 | 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