-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathdebug-bar-elasticpress.php
More file actions
226 lines (194 loc) · 5.7 KB
/
debug-bar-elasticpress.php
File metadata and controls
226 lines (194 loc) · 5.7 KB
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
<?php
/**
* Plugin Name: ElasticPress Debugging Add-On
* Plugin URI: https://wordpress.org/plugins/debug-bar-elasticpress
* Description: Extends the Query Monitor and Debug Bar plugins for ElasticPress queries.
* Version: 3.1.1
* Requires at least: 5.6
* Requires PHP: 7.0
* Author: 10up
* Author URI: https://10up.com
* License: GPLv2
* License URI: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
* Text Domain: debug-bar-elasticpress
* Domain Path: /lang
*
* @package DebugBarElasticPress
*/
namespace DebugBarElasticPress;
define( 'EP_DEBUG_VERSION', '3.1.1' );
define( 'EP_DEBUG_URL', plugin_dir_url( __FILE__ ) );
define( 'EP_DEBUG_MIN_EP_VERSION', '4.4.0' );
spl_autoload_register(
function ( $class_name ) {
// project-specific namespace prefix.
$prefix = 'DebugBarElasticPress\\';
// base directory for the namespace prefix.
$base_dir = __DIR__ . '/classes/';
// does the class use the namespace prefix?
$len = strlen( $prefix );
if ( strncmp( $prefix, $class_name, $len ) !== 0 ) {
return;
}
$relative_class = substr( $class_name, $len );
$file = $base_dir . str_replace( '\\', '/', $relative_class ) . '.php';
// if the file exists, require it.
if ( file_exists( $file ) ) {
require_once $file;
}
}
);
/**
* Setup plugin
*
* @since 3.0.0
*/
function setup() {
$n = function ( $function_name ) {
return __NAMESPACE__ . "\\$function_name";
};
if ( ! defined( 'EP_VERSION' ) || version_compare( EP_VERSION, EP_DEBUG_MIN_EP_VERSION, '<' ) ) {
add_action( 'admin_notices', $n( 'admin_notice_min_ep_version' ) );
return;
}
// If Query Monitor is active, do not add the Debug Bar panel (it will be potentially duplicated otherwise)
if ( class_exists( '\QM_Collectors' ) ) {
\QM_Collectors::add( new QueryMonitorCollector() );
add_filter( 'qm/outputter/html', $n( 'register_qm_output' ) );
add_action( 'qm/output/enqueued-assets', [ new CommonPanel(), 'enqueue_scripts_styles' ] );
} else {
add_filter( 'debug_bar_panels', $n( 'add_debug_bar_panel' ) );
add_filter( 'debug_bar_statuses', $n( 'add_debug_bar_stati' ) );
}
add_filter( 'ep_formatted_args', $n( 'add_explain_args' ), 10, 2 );
add_action( 'wp', $n( 'retrieve_raw_document_from_es' ) );
add_action( 'init', $n( 'i18n' ) );
QueryLog::factory();
}
add_action( 'plugins_loaded', __NAMESPACE__ . '\\setup' );
/**
* Load translations
*
* @since 3.1.1
*/
function i18n() {
load_plugin_textdomain( 'debug-bar-elasticpress', false, basename( __DIR__ ) . '/lang' );
}
/**
* Register panel
*
* @param array $panels Debug Bar Panels
* @return array
*/
function add_debug_bar_panel( $panels ) {
include_once __DIR__ . '/classes/EP_Debug_Bar_ElasticPress.php';
$panels[] = new \EP_Debug_Bar_ElasticPress();
return $panels;
}
/**
* Register status
*
* @since 2.1.0
* @param array $stati Debug Bar Stati
* @return array
*/
function add_debug_bar_stati( $stati ) {
$stati[] = array(
'ep_version',
esc_html__( 'ElasticPress Version', 'debug-bar-elasticpress' ),
defined( 'EP_VERSION' ) ? EP_VERSION : '',
);
$elasticsearch_version = '';
if (
class_exists( '\ElasticPress\Elasticsearch' ) &&
method_exists( \ElasticPress\Elasticsearch::factory(), 'get_elasticsearch_version' )
) {
$elasticsearch_version = \ElasticPress\Elasticsearch::factory()->get_elasticsearch_version();
}
if ( function_exists( '\ElasticPress\Utils\is_epio' ) && \ElasticPress\Utils\is_epio() ) {
$elasticsearch_version = esc_html__( 'ElasticPress.io Managed Platform', 'debug-bar-elasticpress' );
}
$stati[] = array(
'es_version',
esc_html__( 'Elasticsearch Version', 'debug-bar-elasticpress' ),
$elasticsearch_version,
);
return $stati;
}
/**
* Add explain=true to elastic post query
*
* @param array $formatted_args Formatted Elasticsearch query
* @return array
*/
function add_explain_args( $formatted_args ) {
if ( isset( $_GET['explain'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
$formatted_args['explain'] = true;
}
return $formatted_args;
}
/**
* Render an admin notice about the absence of the minimum ElasticPress plugin version.
*
* @since 3.0.0
*/
function admin_notice_min_ep_version() {
?>
<div class="notice notice-error">
<p>
<?php
printf(
/* translators: Min. EP version */
esc_html__( 'ElasticPress Debugging Add-On needs at least ElasticPress %s to work properly.', 'debug-bar-elasticpress' ),
esc_html( EP_DEBUG_MIN_EP_VERSION )
);
?>
</p>
</div>
<?php
}
/**
* Check if the current page is an indexable singular of an ES document or not.
*
* @since 3.1.0
* @return boolean
*/
function is_indexable_singular() {
if ( ! is_singular() ) {
return false;
}
$id = get_the_ID();
$post_type = get_post_type( $id );
$post_indexable = \ElasticPress\Indexables::factory()->get( 'post' );
$indexable_post_types = $post_indexable->get_indexable_post_types();
return in_array( $post_type, $indexable_post_types, true );
}
/**
* Get document from Elasticsearch.
*
* @since 3.1.0
* @return void
*/
function retrieve_raw_document_from_es() {
if ( empty( $_GET['ep-retrieve-es-document'] ) || empty( $_GET['_wpnonce'] ) || ! wp_verify_nonce( sanitize_key( $_GET['_wpnonce'] ), 'ep-retrieve-es-document' ) ) {
return;
}
if ( ! is_indexable_singular() ) {
return;
}
\ElasticPress\Indexables::factory()->get( 'post' )->get( get_the_ID() );
}
/**
* Include our QM Output
*
* @since 3.1.0
* @param array $output Array of registered output
* @return array
*/
function register_qm_output( $output ) {
$collector = \QM_Collectors::get( 'elasticpress' );
if ( $collector ) {
$output['elasticpress'] = new QueryMonitorOutput( $collector );
}
return $output;
}