forked from Automattic/vip-go-mu-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
query-monitor.php
168 lines (147 loc) · 5.38 KB
/
query-monitor.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?php
/**
* Query Monitor plugin for WordPress
*
* @package query-monitor
* @link https://github.com/johnbillion/query-monitor
* @author John Blackbourn <[email protected]>
* @copyright 2009-2022 John Blackbourn
* @license GPL v2 or later
*
* Plugin Name: Query Monitor
* Description: The developer tools panel for WordPress.
* Version: 3.10.1
* Plugin URI: https://querymonitor.com/
* Author: John Blackbourn
* Author URI: https://querymonitor.com/
* Text Domain: query-monitor
* Domain Path: /languages/
* Requires PHP: 5.6.20
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
/**
* Determines if Query Monitor should be enabled. We don't
* want to load it if we don't have to.
*
* - If logged-in user has the `view_query_monitor`
* capability, Query Monitor is enabled.
* - If a QM_COOKIE is detected, Query Monitor is enabled.
*
* Note that we have to set the value for QM_COOKIE here,
* in order to detect it.
*
* Note that we cannot use is_automattician this early, as
* the user has not yet been set.
*
* @param $enable
*
* @return bool
*/
function wpcom_vip_qm_enable( $enable ) {
if ( ! defined( 'QM_COOKIE' ) ) {
define( 'QM_COOKIE', 'query_monitor_' . COOKIEHASH );
}
if ( defined( 'WP_INSTALLING' ) ) {
return false;
}
if ( current_user_can( 'view_query_monitor' ) ) {
return true;
}
// We're not validating the cookie here as QM will do that later
if ( isset( $_COOKIE[ QM_COOKIE ] ) ) {
return true;
}
return $enable;
}
add_filter( 'wpcom_vip_qm_enable', 'wpcom_vip_qm_enable' );
/**
* Require the plugin files for Query Monitor, faking a
* plugin activation, if it's the first time.
*/
function wpcom_vip_qm_require() {
/**
* Filter whether Query Monitor is activated; return true,
* if QM should be activated.
*
* @param bool $enable False by default
*/
if ( ! apply_filters( 'wpcom_vip_qm_enable', false ) ) {
return;
}
if ( ! defined( 'SAVEQUERIES' ) ) {
define( 'SAVEQUERIES', true );
}
// For hyperdb, which doesn't use SAVEQUERIES
global $wpdb;
$wpdb->save_queries = SAVEQUERIES;
$wpcom_vip_qm_file = __DIR__ . '/query-monitor/query-monitor.php';
require_once $wpcom_vip_qm_file;
// Something stopped QueryMonitor from loading; bail.
if ( ! class_exists( 'QueryMonitor' ) ) {
return;
}
// Because we're including Query Monitor as an MU plugin, we need to
// manually call the `activate` method on `activation`.
if ( 0 === get_option( 'wpcom_vip_qm_activated', 0 ) ) {
QM_Activation::init( $wpcom_vip_qm_file )->activate( true );
update_option( 'wpcom_vip_qm_activated', 1, true );
}
// We don't want to share our environment information via Query Monitor
remove_filter( 'qm/collectors', 'register_qm_collector_environment', 20, 2 );
// We know we haven't got the QM DB drop-in in place, so don't show the message
add_filter( 'qm/show_extended_query_prompt', '__return_false' );
}
add_action( 'plugins_loaded', 'wpcom_vip_qm_require', 1 );
/**
* Hooks the wp action to avoid showing Query Monitor on 404 pages
* to non-logged in users, as it is likely to get caught in the
* Varnish cache.
*/
function wpcom_vip_qm_disable_on_404() {
if ( is_404() && ! is_user_logged_in() && isset( $_COOKIE[ QM_COOKIE ] ) ) {
add_filter( 'qm/dispatch/ajax', '__return_false' );
add_filter( 'qm/dispatch/html', '__return_false' );
}
}
add_action( 'wp', 'wpcom_vip_qm_disable_on_404' );
// We are putting dispatchers as last so that QM still can catch other operations in shutdown action
// See https://github.com/johnbillion/query-monitor/pull/549
function change_dispatchers_shutdown_priority( array $dispatchers ) {
if ( is_array( $dispatchers ) ) {
if ( isset( $dispatchers['html'] ) ) {
$html_dispatcher = $dispatchers['html'];
remove_action( 'shutdown', array( $html_dispatcher, 'dispatch' ), 9 );
// To prevent collision with log2logstashs fastcgi_finish_request, set this priority just a bit before it.
add_action( 'shutdown', array( $html_dispatcher, 'dispatch' ), PHP_INT_MAX - 1 );
}
if ( isset( $dispatchers['ajax'] ) ) {
$ajax_dispatcher = $dispatchers['ajax'];
remove_action( 'shutdown', array( $ajax_dispatcher, 'dispatch' ), 0 );
// To prevent collision with log2logstashs fastcgi_finish_request, set this priority just a bit before it.
add_action( 'shutdown', array( $ajax_dispatcher, 'dispatch' ), PHP_INT_MAX - 1 );
}
}
return $dispatchers;
}
add_filter( 'qm/dispatchers', 'change_dispatchers_shutdown_priority', PHP_INT_MAX, 1 );
/**
* Load QM plugins
*/
require_once __DIR__ . '/qm-plugins/qm-alloptions/qm-alloptions.php';
require_once __DIR__ . '/qm-plugins/qm-object-cache/qm-object-cache.php';
require_once __DIR__ . '/qm-plugins/qm-apcu-cache/qm-apcu-cache.php';
if ( file_exists( __DIR__ . '/qm-plugins/qm-cron/qm-cron.php' ) ) {
require_once __DIR__ . '/qm-plugins/qm-cron/qm-cron.php';
}
if ( file_exists( __DIR__ . '/qm-plugins/qm-vip/qm-vip.php' ) ) {
require_once __DIR__ . '/qm-plugins/qm-vip/qm-vip.php';
}