PluginProbe
SQLite Database Integration / 3.0.1
SQLite Database Integration v3.0.1
3.0.2 3.0.1 trunk 2.1.13 2.1.14 2.1.15 2.1.16 2.2.0 2.2.1 2.2.10 2.2.11 2.2.12 2.2.13 2.2.14 2.2.15 2.2.16 2.2.17 2.2.18 2.2.19 2.2.2 2.2.20 2.2.21 2.2.22 2.2.23 2.2.3 All 32 releases
sqlite-database-integration / integrations / query-monitor / boot.php

boot.php in SQLite Database Integration 3.0.1, at integrations/query-monitor/boot.php

173 lines 4.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 if ( defined( 'QM_VERSION' ) && version_compare( QM_VERSION, '4.0.0', '>=' ) ) {
71 require_once __DIR__ . '/qm4.php';
72 } else {
73 require_once __DIR__ . '/qm3.php';
74 }
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 */
97 global $wpdb;
98 if ( ! isset( $wpdb ) ) {
99 return;
100 }
101
102 // Check if Query Monitor is installed.
103 if ( defined( 'WP_PLUGIN_DIR' ) ) {
104 $plugins_dir = WP_PLUGIN_DIR;
105 } else {
106 $plugins_dir = WP_CONTENT_DIR . '/plugins';
107 }
108
109 $qm_dir = "{$plugins_dir}/query-monitor";
110 $qm_php = "{$qm_dir}/classes/PHP.php";
111
112 if ( ! is_readable( $qm_php ) ) {
113 return;
114 }
115
116 // Check if Query Monitor is active.
117 if ( null === $wpdb->options ) {
118 global $table_prefix;
119 $wpdb->set_prefix( $table_prefix ?? '' );
120 }
121
122 $query_monitor_active = false;
123 try {
124 // Make sure no errors are displayed when the query fails.
125 $show_errors = $wpdb->hide_errors();
126 $value = $wpdb->get_row(
127 $wpdb->prepare(
128 "SELECT option_value FROM $wpdb->options WHERE option_name = %s LIMIT 1",
129 'active_plugins'
130 )
131 );
132 $wpdb->show_errors( $show_errors );
133
134 if ( null !== $value ) {
135 $query_monitor_active = in_array(
136 'query-monitor/query-monitor.php',
137 unserialize( $value->option_value ),
138 true
139 );
140 }
141 } catch ( Throwable $e ) {
142 return;
143 }
144
145 if ( ! $query_monitor_active ) {
146 return;
147 }
148
149 // Load Query Monitor eagerly (as per the original "db.php" file).
150 require_once $qm_php;
151
152 if ( ! QM_PHP::version_met() ) {
153 return;
154 }
155
156 if ( ! file_exists( "{$qm_dir}/vendor/autoload.php" ) ) {
157 add_action( 'all_admin_notices', 'QM_PHP::vendor_nope' );
158 return;
159 }
160
161 require_once "{$qm_dir}/vendor/autoload.php";
162
163 if ( ! class_exists( 'QM_Backtrace' ) ) {
164 return;
165 }
166
167 if ( ! defined( 'SAVEQUERIES' ) ) {
168 define( 'SAVEQUERIES', true );
169 }
170
171 // Mark the Query Monitor integration as loaded.
172 define( 'SQLITE_QUERY_MONITOR_LOADED', true );
173