|
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 ( ! 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 | + */ |
59 | 97 | global $wpdb; |
60 | 98 | if ( ! isset( $wpdb ) ) { |
61 | 99 | return; |
62 | 100 | } |
63 | 101 |
|
64 | | -// 2. Check if Query Monitor is active. |
| 102 | +// Check if Query Monitor is active. |
65 | 103 | if ( null === $wpdb->options ) { |
66 | 104 | global $table_prefix; |
67 | 105 | $wpdb->set_prefix( $table_prefix ?? '' ); |
|
75 | 113 | 'active_plugins' |
76 | 114 | ) |
77 | 115 | ); |
78 | | - /** |
79 | | - * $value may be null during WordPress Playground multisite setup. |
80 | | - * @see https://github.com/WordPress/sqlite-database-integration/pull/219. |
81 | | - */ |
82 | 116 | if ( null !== $value ) { |
83 | 117 | $query_monitor_active = in_array( |
84 | 118 | 'query-monitor/query-monitor.php', |
|
94 | 128 | return; |
95 | 129 | } |
96 | 130 |
|
97 | | -// 3. Determine the plugins directory. |
| 131 | +// Determine the plugins directory. |
98 | 132 | if ( defined( 'WP_PLUGIN_DIR' ) ) { |
99 | 133 | $plugins_dir = WP_PLUGIN_DIR; |
100 | 134 | } else { |
101 | 135 | $plugins_dir = WP_CONTENT_DIR . '/plugins'; |
102 | 136 | } |
103 | 137 |
|
104 | | -// 4. Load Query Monitor (as per the original "db.php" file). |
| 138 | +// Load Query Monitor (as per the original "db.php" file). |
105 | 139 | $qm_dir = "{$plugins_dir}/query-monitor"; |
106 | 140 | $qm_php = "{$qm_dir}/classes/PHP.php"; |
107 | 141 |
|
|
129 | 163 | define( 'SAVEQUERIES', true ); |
130 | 164 | } |
131 | 165 |
|
132 | | -// 5. Mark the Query Monitor integration as loaded. |
| 166 | +// Mark the Query Monitor integration as loaded. |
133 | 167 | 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