| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Boot Query Monitor from the SQLite Database Integration plugin. |
| 5 |
* |
| 6 |
* When the Query Monitor plugin exists in its standard location, let's check |
| 7 |
* if it is active, so we can boot it eagerly. This is a workaround to avoid |
| 8 |
* SQLite and Query Monitor competing for the "wp-content/db.php" file. |
| 9 |
* |
| 10 |
* This file is a modified version of the original Query Monitor "db.php" file. |
| 11 |
* |
| 12 |
* See: https://github.com/johnbillion/query-monitor/blob/develop/wp-content/db.php |
| 13 |
*/ |
| 14 |
|
| 15 |
/* |
| 16 |
* In Playground, the SQLite plugin is preloaded without using the "db.php" file. |
| 17 |
* To prevent Query Monitor from injecting its own "db.php" file, we need to set |
| 18 |
* the "QM_DB_SYMLINK" constant to "false". |
| 19 |
*/ |
| 20 |
if ( ! defined( 'QM_DB_SYMLINK' ) ) { |
| 21 |
define( 'QM_DB_SYMLINK', false ); |
| 22 |
} |
| 23 |
|
| 24 |
// Check if we should load Query Monitor (as per the original "db.php" file). |
| 25 |
if ( ! defined( 'ABSPATH' ) ) { |
| 26 |
exit; |
| 27 |
} |
| 28 |
|
| 29 |
if ( ! defined( 'DB_USER' ) ) { |
| 30 |
return; |
| 31 |
} |
| 32 |
|
| 33 |
if ( defined( 'QM_DISABLED' ) && QM_DISABLED ) { |
| 34 |
return; |
| 35 |
} |
| 36 |
|
| 37 |
if ( defined( 'WP_INSTALLING' ) && WP_INSTALLING ) { |
| 38 |
return; |
| 39 |
} |
| 40 |
|
| 41 |
if ( 'cli' === php_sapi_name() && ! defined( 'QM_TESTS' ) ) { |
| 42 |
return; |
| 43 |
} |
| 44 |
|
| 45 |
if ( defined( 'DOING_CRON' ) && DOING_CRON ) { |
| 46 |
return; |
| 47 |
} |
| 48 |
|
| 49 |
if ( is_admin() ) { |
| 50 |
if ( isset( $_GET['action'] ) && 'upgrade-plugin' === $_GET['action'] ) { |
| 51 |
return; |
| 52 |
} |
| 53 |
|
| 54 |
if ( isset( $_POST['action'] ) && 'update-plugin' === $_POST['action'] ) { |
| 55 |
return; |
| 56 |
} |
| 57 |
} |
| 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 |
*/ |
| 93 |
global $wpdb; |
| 94 |
if ( ! isset( $wpdb ) ) { |
| 95 |
return; |
| 96 |
} |
| 97 |
|
| 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. |
| 113 |
if ( null === $wpdb->options ) { |
| 114 |
global $table_prefix; |
| 115 |
$wpdb->set_prefix( $table_prefix ?? '' ); |
| 116 |
} |
| 117 |
|
| 118 |
$query_monitor_active = false; |
| 119 |
try { |
| 120 |
// Make sure no errors are displayed when the query fails. |
| 121 |
$show_errors = $wpdb->hide_errors(); |
| 122 |
$value = $wpdb->get_row( |
| 123 |
$wpdb->prepare( |
| 124 |
"SELECT option_value FROM $wpdb->options WHERE option_name = %s LIMIT 1", |
| 125 |
'active_plugins' |
| 126 |
) |
| 127 |
); |
| 128 |
$wpdb->show_errors( $show_errors ); |
| 129 |
|
| 130 |
if ( null !== $value ) { |
| 131 |
$query_monitor_active = in_array( |
| 132 |
'query-monitor/query-monitor.php', |
| 133 |
unserialize( $value->option_value ), |
| 134 |
true |
| 135 |
); |
| 136 |
} |
| 137 |
} catch ( Throwable $e ) { |
| 138 |
return; |
| 139 |
} |
| 140 |
|
| 141 |
if ( ! $query_monitor_active ) { |
| 142 |
return; |
| 143 |
} |
| 144 |
|
| 145 |
// Load Query Monitor eagerly (as per the original "db.php" file). |
| 146 |
require_once $qm_php; |
| 147 |
|
| 148 |
if ( ! QM_PHP::version_met() ) { |
| 149 |
return; |
| 150 |
} |
| 151 |
|
| 152 |
if ( ! file_exists( "{$qm_dir}/vendor/autoload.php" ) ) { |
| 153 |
add_action( 'all_admin_notices', 'QM_PHP::vendor_nope' ); |
| 154 |
return; |
| 155 |
} |
| 156 |
|
| 157 |
require_once "{$qm_dir}/vendor/autoload.php"; |
| 158 |
|
| 159 |
if ( ! class_exists( 'QM_Backtrace' ) ) { |
| 160 |
return; |
| 161 |
} |
| 162 |
|
| 163 |
if ( ! defined( 'SAVEQUERIES' ) ) { |
| 164 |
define( 'SAVEQUERIES', true ); |
| 165 |
} |
| 166 |
|
| 167 |
// Mark the Query Monitor integration as loaded. |
| 168 |
define( 'SQLITE_QUERY_MONITOR_LOADED', true ); |
| 169 |
|