forked from Automattic/vip-go-mu-plugins-built
-
Notifications
You must be signed in to change notification settings - Fork 0
/
query-monitor.php
104 lines (88 loc) · 2.84 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
<?php
/*
Plugin Name: Query Monitor
Description: Monitoring of database queries, hooks, conditionals and more (including VIP Go tweaks).
Version: 2.9.1
Plugin URI: https://querymonitor.com/
Author: John Blackbourn
Author URI: https://johnblackbourn.com/
Text Domain: query-monitor
Domain Path: /languages/
License: GPL v2 or later
*/
/**
* Determines if Query Monitor should be enabled. We don't
* want to load it if we don't have to.
*
* * If a QM_COOKIE is detected, Query monitor is enabled
* * If the WPCOM_VIP_QM_ENABLE constant is true, 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 ( current_user_can( 'view_query_monitor' ) ) {
return true;
}
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 );
// 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 ) ) {
QueryMonitor::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' );