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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
|
<?php declare(strict_types = 1); /** * General overview output for HTML pages. * * @package query-monitor */
if ( ! defined( 'ABSPATH' ) ) { exit; }
class QM_Output_Html_Overview extends QM_Output_Html {
/** * Collector instance. * * @var QM_Collector_Overview Collector. */ protected $collector;
public function __construct( QM_Collector $collector ) { parent::__construct( $collector ); add_filter( 'qm/output/title', array( $this, 'admin_title' ), 10 ); }
/** * @return string */ public function name() { return __( 'Overview', 'query-monitor' ); }
/** * @return void */ public function output() { /** @var QM_Data_Overview $data */ $data = $this->collector->get_data();
$db_query_num = null; /** @var QM_Collector_DB_Queries|null $db_queries */ $db_queries = QM_Collectors::get( 'db_queries' );
if ( $db_queries ) { /** @var QM_Data_DB_Queries $db_queries_data */ $db_queries_data = $db_queries->get_data(); if ( ! empty( $db_queries_data->types ) ) { $db_query_num = $db_queries_data->types; } }
/** @var QM_Collector_Raw_Request|null $raw_request */ $raw_request = QM_Collectors::get( 'raw_request' );
/** @var QM_Collector_Cache|null $cache */ $cache = QM_Collectors::get( 'cache' );
/** @var QM_Collector_HTTP|null $http */ $http = QM_Collectors::get( 'http' );
$qm_broken = __( 'A JavaScript problem on the page is preventing Query Monitor from working correctly. jQuery may have been blocked from loading.', 'query-monitor' ); $ajax_errors = __( 'PHP errors were triggered during an Ajax request. See your browser developer console for details.', 'query-monitor' );
$this->before_non_tabular_output();
echo '<section id="qm-broken">'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped echo '<p class="qm-warn">' . QueryMonitor::icon( 'warning' ) . esc_html( $qm_broken ) . '</p>'; echo '</section>';
echo '<section id="qm-ajax-errors">'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped echo '<p class="qm-warn">' . QueryMonitor::icon( 'warning' ) . esc_html( $ajax_errors ) . '</p>'; echo '</section>';
if ( $raw_request ) { echo '<section id="qm-overview-raw-request">'; /** @var QM_Data_Raw_Request $raw_data */ $raw_data = $raw_request->get_data();
if ( ! empty( $raw_data->response['status'] ) ) { $status = $raw_data->response['status']; } else { $status = __( 'Unknown HTTP Response Code', 'query-monitor' ); }
printf( '<h3>%1$s %2$s → %3$s</h3>', esc_html( $raw_data->request['method'] ), esc_html( $raw_data->request['url'] ), esc_html( $status ) ); echo '</section>'; }
echo '</div>'; echo '<div class="qm-grid">';
echo '<section>'; echo '<h3>' . esc_html__( 'Page Generation Time', 'query-monitor' ) . '</h3>'; echo '<p>'; echo esc_html( sprintf( /* translators: %s: A time in seconds with a decimal fraction. No space between value and unit symbol. */ _x( '%ss', 'Time in seconds', 'query-monitor' ), number_format_i18n( $data->time_taken, 4 ) ) );
if ( $data->time_limit > 0 ) { if ( $data->display_time_usage_warning ) { // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped echo '<br><span class="qm-warn">' . QueryMonitor::icon( 'warning' ); } else { echo '<br><span class="qm-info">'; } echo esc_html( sprintf( /* translators: 1: Percentage of time limit used, 2: Time limit in seconds */ __( '%1$s%% of %2$ss limit', 'query-monitor' ), number_format_i18n( $data->time_usage, 1 ), number_format_i18n( $data->time_limit ) ) ); echo '</span>'; } else { // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped echo '<br><span class="qm-warn">' . QueryMonitor::icon( 'warning' ); printf( /* translators: 1: Name of the PHP directive, 2: Value of the PHP directive */ esc_html__( 'No execution time limit. The %1$s PHP configuration directive is set to %2$s.', 'query-monitor' ), '<code>max_execution_time</code>', '0' ); echo '</span>'; } echo '</p>'; echo '</section>';
echo '<section>'; echo '<h3>' . esc_html__( 'Peak Memory Usage', 'query-monitor' ) . '</h3>'; echo '<p>';
if ( empty( $data->memory ) ) { esc_html_e( 'Unknown', 'query-monitor' ); } else { echo esc_html( sprintf( /* translators: 1: Memory used in bytes, 2: Memory used in megabytes */ __( '%1$s bytes (%2$s MB)', 'query-monitor' ), number_format_i18n( $data->memory ), number_format_i18n( ( $data->memory / 1024 / 1024 ), 1 ) ) );
if ( $data->memory_limit > 0 ) { if ( $data->display_memory_usage_warning ) { // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped echo '<br><span class="qm-warn">' . QueryMonitor::icon( 'warning' ); } else { echo '<br><span class="qm-info">'; } echo esc_html( sprintf( /* translators: 1: Percentage of memory limit used, 2: Memory limit in megabytes */ __( '%1$s%% of %2$s MB server limit', 'query-monitor' ), number_format_i18n( $data->memory_usage, 1 ), number_format_i18n( $data->memory_limit / 1024 / 1024 ) ) ); echo '</span>'; } else { // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped echo '<br><span class="qm-warn">' . QueryMonitor::icon( 'warning' ); printf( /* translators: 1: Name of the PHP directive, 2: Value of the PHP directive */ esc_html__( 'No memory limit. The %1$s PHP configuration directive is set to %2$s.', 'query-monitor' ), '<code>memory_limit</code>', '0' ); echo '</span>'; } }
echo '</p>'; echo '</section>';
echo '<section>'; echo '<h3>' . esc_html__( 'Database Queries', 'query-monitor' ) . '</h3>';
if ( isset( $db_query_num, $db_queries_data ) ) { echo '<p>'; echo esc_html( sprintf( /* translators: %s: A time in seconds with a decimal fraction. No space between value and unit symbol. */ _x( '%ss', 'Time in seconds', 'query-monitor' ), number_format_i18n( $db_queries_data->total_time, 4 ) ) ); echo '</p>';
echo '<p>';
if ( ! isset( $db_query_num['SELECT'] ) || count( $db_query_num ) > 1 ) { foreach ( $db_query_num as $type_name => $type_count ) { $label = sprintf( '%1$s: %2$s', esc_html( $type_name ), esc_html( number_format_i18n( $type_count ) ) ); echo self::build_filter_trigger( 'db_queries', 'type', (string) $type_name, esc_html( $label ) ); // WPCS: XSS ok; echo '<br>'; } }
$label = sprintf( '%1$s: %2$s', esc_html( _x( 'Total', 'database queries', 'query-monitor' ) ), esc_html( number_format_i18n( $db_queries_data->total_qs ) ) ); echo self::build_filter_trigger( 'db_queries', 'type', '', esc_html( $label ) ); // WPCS: XSS ok;
echo '</p>'; } else { printf( '<p><em>%s</em></p>', esc_html__( 'None', 'query-monitor' ) ); }
echo '</section>';
if ( $http ) { echo '<section>'; echo '<h3>' . esc_html__( 'HTTP API Calls', 'query-monitor' ) . '</h3>';
$http_data = $http->get_data();
if ( ! empty( $http_data->http ) ) { echo '<p>'; echo esc_html( sprintf( /* translators: %s: A time in seconds with a decimal fraction. No space between value and unit symbol. */ _x( '%ss', 'Time in seconds', 'query-monitor' ), number_format_i18n( $http_data->ltime, 4 ) ) ); echo '</p>';
$label = sprintf( '%1$s: %2$s', esc_html( _x( 'Total', 'HTTP API calls', 'query-monitor' ) ), esc_html( number_format_i18n( count( $http_data->http ) ) ) ); echo self::build_filter_trigger( 'http', 'type', '', esc_html( $label ) ); // WPCS: XSS ok; } else { printf( '<p><em>%s</em></p>', esc_html__( 'None', 'query-monitor' ) ); }
echo '</section>'; }
echo '<section>'; echo '<h3>' . esc_html__( 'Object Cache', 'query-monitor' ) . '</h3>';
if ( $cache ) { /** @var QM_Data_Cache $cache_data */ $cache_data = $cache->get_data();
if ( ! empty( $cache_data->stats ) && ! empty( $cache_data->cache_hit_percentage ) ) { $cache_hit_percentage = $cache_data->cache_hit_percentage;
echo '<p>'; echo esc_html( sprintf( /* translators: 1: Cache hit rate percentage, 2: number of cache hits, 3: number of cache misses */ __( '%1$s%% hit rate (%2$s hits, %3$s misses)', 'query-monitor' ), number_format_i18n( $cache_hit_percentage, 1 ), number_format_i18n( $cache_data->stats['cache_hits'], 0 ), number_format_i18n( $cache_data->stats['cache_misses'], 0 ) ) ); echo '</p>'; }
if ( $cache_data->has_object_cache ) { echo '<p><span class="qm-info">'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped echo self::build_link( network_admin_url( 'plugins.php?plugin_status=dropins' ), esc_html__( 'Persistent object cache plugin in use', 'query-monitor' ) ); echo '</span></p>'; } else { echo '<p><span class="qm-warn">'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped echo QueryMonitor::icon( 'warning' ); echo esc_html__( 'Persistent object cache plugin not in use', 'query-monitor' ); echo '</span></p>';
$potentials = array_filter( $cache_data->object_cache_extensions );
if ( ! empty( $potentials ) ) { foreach ( $potentials as $name => $value ) { $url = sprintf( 'https://wordpress.org/plugins/search/%s/', strtolower( $name ) ); echo '<p>'; echo wp_kses( sprintf( /* translators: 1: PHP extension name, 2: URL to plugin directory */ __( 'The %1$s object cache extension for PHP is installed but is not in use by WordPress. You should <a href="%2$s" target="_blank" class="qm-external-link">install a %1$s plugin</a>.', 'query-monitor' ), esc_html( $name ), esc_url( $url ) ), array( 'a' => array( 'href' => array(), 'target' => array(), 'class' => array(), ), ) ); echo '</p>'; } } else { echo '<p>'; echo esc_html__( 'Speak to your web host about enabling an object cache extension such as Redis or Memcached.', 'query-monitor' ); echo '</p>'; } } } else { echo '<p>'; echo esc_html__( 'Object cache statistics are not available', 'query-monitor' ); echo '</p>'; }
echo '</section>';
if ( $cache ) { /** @var QM_Data_Cache $cache_data */ $cache_data = $cache->get_data();
echo '<section>'; echo '<h3>' . esc_html__( 'Opcode Cache', 'query-monitor' ) . '</h3>';
if ( $cache_data->has_opcode_cache ) { foreach ( array_filter( $cache_data->opcode_cache_extensions ) as $opcache_name => $opcache_state ) { echo '<p>'; echo esc_html( sprintf( /* translators: %s: Name of cache driver */ __( 'Opcode cache in use: %s', 'query-monitor' ), $opcache_name ) ); echo '</p>'; } } else { echo '<p><span class="qm-warn">'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped echo QueryMonitor::icon( 'warning' ); echo esc_html__( 'Opcode cache not in use', 'query-monitor' ); echo '</span></p>'; echo '<p>'; echo esc_html__( 'Speak to your web host about enabling an opcode cache such as OPcache.', 'query-monitor' ); echo '</p>'; }
echo '</section>'; }
$this->after_non_tabular_output(); }
/** * @param array<int, string> $title * @return array<int, string> */ public function admin_title( array $title ) { /** @var QM_Data_Overview $data */ $data = $this->collector->get_data();
if ( empty( $data->memory ) ) { $memory = '??'; } else { $memory = number_format_i18n( ( $data->memory / 1024 / 1024 ), 1 ); }
$title[] = sprintf( /* translators: %s: A time in seconds with a decimal fraction. No space between value and unit symbol. */ esc_html_x( '%ss', 'Time in seconds', 'query-monitor' ), number_format_i18n( $data->time_taken, 2 ) ); $title[] = preg_replace( '#\s?([^0-9,\.]+)#', '<small>$1</small>', sprintf( /* translators: %s: Memory usage in megabytes with a decimal fraction. Note the space between value and unit symbol. */ esc_html__( '%s MB', 'query-monitor' ), $memory ) );
return $title; }
}
/** * @param array<string, QM_Output> $output * @param QM_Collectors $collectors * @return array<string, QM_Output> */ function register_qm_output_html_overview( array $output, QM_Collectors $collectors ) { $collector = QM_Collectors::get( 'overview' ); if ( $collector ) { $output['overview'] = new QM_Output_Html_Overview( $collector ); } return $output; }
add_filter( 'qm/outputter/html', 'register_qm_output_html_overview', 10, 2 );
|