-
-
Notifications
You must be signed in to change notification settings - Fork 218
/
Copy pathphp_errors.php
334 lines (270 loc) · 8.77 KB
/
php_errors.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
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
<?php declare(strict_types = 1);
/**
* PHP error output for HTML pages.
*
* @package query-monitor
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
class QM_Output_Html_PHP_Errors extends QM_Output_Html {
/**
* Collector instance.
*
* @var QM_Collector_PHP_Errors Collector.
*/
protected $collector;
public function __construct( QM_Collector $collector ) {
parent::__construct( $collector );
add_filter( 'qm/output/menus', array( $this, 'admin_menu' ), 10 );
add_filter( 'qm/output/panel_menus', array( $this, 'panel_menu' ), 10 );
add_filter( 'qm/output/menu_class', array( $this, 'admin_class' ) );
}
/**
* @return string
*/
public function name() {
return __( 'PHP Errors', 'query-monitor' );
}
/**
* @return void
*/
public function output() {
/** @var QM_Data_PHP_Errors $data */
$data = $this->collector->get_data();
if ( empty( $data->errors ) && empty( $data->silenced ) && empty( $data->suppressed ) ) {
return;
}
$levels = array(
'Warning',
'Notice',
'Strict',
'Deprecated',
);
$components = $data->components;
usort( $components, 'strcasecmp' );
$this->before_tabular_output();
echo '<thead>';
echo '<tr>';
echo '<th scope="col" class="qm-filterable-column">';
echo $this->build_filter( 'type', $levels, __( 'Level', 'query-monitor' ) ); // WPCS: XSS ok.
echo '</th>';
echo '<th scope="col" class="qm-col-message">' . esc_html__( 'Message', 'query-monitor' ) . '</th>';
echo '<th scope="col" class="qm-num">' . esc_html__( 'Count', 'query-monitor' ) . '</th>';
echo '<th scope="col">' . esc_html__( 'Location', 'query-monitor' ) . '</th>';
echo '<th scope="col" class="qm-filterable-column">';
echo $this->build_filter( 'component', $components, __( 'Component', 'query-monitor' ) ); // WPCS: XSS ok.
echo '</th>';
echo '</tr>';
echo '</thead>';
echo '<tbody>';
foreach ( $this->collector->types as $error_group => $error_types ) {
foreach ( $error_types as $type => $title ) {
if ( ! isset( $data->{$error_group}[ $type ] ) ) {
continue;
}
foreach ( $data->{$error_group}[ $type ] as $error_key => $error ) {
$row_attr = array();
$row_attr['data-qm-type'] = ucfirst( $type );
$row_attr['data-qm-key'] = $error_key;
if ( $error['component'] ) {
$component = $error['component'];
$row_attr['data-qm-component'] = $component->name;
if ( 'core' !== $component->context ) {
$row_attr['data-qm-component'] .= ' non-core';
}
}
$attr = '';
foreach ( $row_attr as $a => $v ) {
$attr .= ' ' . $a . '="' . esc_attr( $v ) . '"';
}
$is_warning = ( 'errors' === $error_group && 'warning' === $type );
if ( $is_warning ) {
$class = 'qm-warn';
} else {
$class = '';
}
echo '<tr ' . $attr . 'class="' . esc_attr( $class ) . '">'; // WPCS: XSS ok.
echo '<td class="qm-nowrap">';
if ( $is_warning ) {
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo QueryMonitor::icon( 'warning' );
} else {
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo QueryMonitor::icon( 'blank' );
}
echo esc_html( $title );
echo '</td>';
echo '<td class="qm-ltr">' . esc_html( $error['message'] ) . '</td>';
echo '<td class="qm-num">' . esc_html( number_format_i18n( $error['calls'] ) ) . '</td>';
$stack = array();
if ( $error['filtered_trace'] ) {
$filtered_trace = $error['filtered_trace'];
// debug_backtrace() (used within QM_Backtrace) doesn't like being used within an error handler so
// we need to handle its somewhat unreliable stack trace items.
// https://bugs.php.net/bug.php?id=39070
// https://bugs.php.net/bug.php?id=64987
foreach ( $filtered_trace as $i => $item ) {
if ( isset( $item['file'], $item['line'] ) ) {
$stack[] = self::output_filename( $item['display'], $item['file'], $item['line'] );
} elseif ( 0 === $i ) {
$stack[] = self::output_filename( $item['display'], $error['file'], $error['line'] );
} else {
$stack[] = $item['display'] . '<br><span class="qm-info qm-supplemental"><em>' . __( 'Unknown location', 'query-monitor' ) . '</em></span>';
}
}
}
echo '<td class="qm-row-caller qm-row-stack qm-nowrap qm-ltr qm-has-toggle">';
if ( ! empty( $stack ) ) {
echo self::build_toggler( $error['filename'] ); // WPCS: XSS ok;
}
echo '<ol>';
echo '<li>';
echo self::output_filename( $error['filename'] . ':' . $error['line'], $error['file'], $error['line'], true ); // WPCS: XSS ok.
echo '</li>';
if ( ! empty( $stack ) ) {
echo '<div class="qm-toggled"><li>' . implode( '</li><li>', $stack ) . '</li></div>'; // WPCS: XSS ok.
}
echo '</ol></td>';
if ( ! empty( $component ) ) {
echo '<td class="qm-nowrap">' . esc_html( $component->name ) . '</td>';
} else {
echo '<td><em>' . esc_html__( 'Unknown', 'query-monitor' ) . '</em></td>';
}
echo '</tr>';
}
}
}
echo '</tbody>';
$this->after_tabular_output();
}
/**
* @param array<int, string> $class
* @return array<int, string>
*/
public function admin_class( array $class ) {
/** @var QM_Data_PHP_Errors $data */
$data = $this->collector->get_data();
if ( ! empty( $data->errors ) ) {
foreach ( $data->errors as $type => $errors ) {
$class[] = 'qm-' . $type;
}
}
return $class;
}
/**
* @param array<string, mixed[]> $menu
* @return array<string, mixed[]>
*/
public function admin_menu( array $menu ) {
/** @var QM_Data_PHP_Errors $data */
$data = $this->collector->get_data();
$menu_label = array();
$types = array(
/* translators: %s: Number of deprecated PHP errors */
'deprecated' => _nx_noop( '%s Deprecated', '%s Deprecated', 'PHP error level', 'query-monitor' ),
/* translators: %s: Number of strict PHP errors */
'strict' => _nx_noop( '%s Strict', '%s Stricts', 'PHP error level', 'query-monitor' ),
/* translators: %s: Number of PHP notices */
'notice' => _nx_noop( '%s Notice', '%s Notices', 'PHP error level', 'query-monitor' ),
/* translators: %s: Number of PHP warnings */
'warning' => _nx_noop( '%s Warning', '%s Warnings', 'PHP error level', 'query-monitor' ),
);
$key = 'quiet';
$generic = false;
foreach ( $types as $type => $label ) {
$count = 0;
$has_errors = false;
if ( isset( $data->suppressed[ $type ] ) ) {
$has_errors = true;
$generic = true;
}
if ( isset( $data->silenced[ $type ] ) ) {
$has_errors = true;
$generic = true;
}
if ( isset( $data->errors[ $type ] ) ) {
$has_errors = true;
$key = $type;
$count += (int) array_sum( array_column( $data->errors[ $type ], 'calls' ) );
}
if ( ! $has_errors ) {
continue;
}
if ( $count ) {
$label = sprintf(
translate_nooped_plural(
$label,
$count,
'query-monitor'
),
number_format_i18n( $count )
);
$menu_label[] = $label;
}
}
if ( empty( $menu_label ) && ! $generic ) {
return $menu;
}
/* translators: %s: List of PHP error types */
$title = __( 'PHP Errors (%s)', 'query-monitor' );
/* translators: used between list items, there is a space after the comma */
$sep = __( ', ', 'query-monitor' );
if ( count( $menu_label ) ) {
$title = sprintf(
$title,
implode( $sep, array_reverse( $menu_label ) )
);
} else {
$title = __( 'PHP Errors', 'query-monitor' );
}
$menu[ $this->collector->id() ] = $this->menu( array(
'id' => "query-monitor-{$key}s",
'title' => $title,
) );
return $menu;
}
/**
* @param array<string, mixed[]> $menu
* @return array<string, mixed[]>
*/
public function panel_menu( array $menu ) {
if ( ! isset( $menu[ $this->collector->id() ] ) ) {
return $menu;
}
/** @var QM_Data_PHP_Errors $data */
$data = $this->collector->get_data();
$count = 0;
$types = array(
'suppressed',
'silenced',
'errors',
);
foreach ( $types as $type ) {
if ( ! empty( $data->{$type} ) ) {
foreach ( $data->{$type} as $errors ) {
$count += array_sum( array_column( $errors, 'calls' ) );
}
}
}
$menu[ $this->collector->id() ]['title'] = esc_html( sprintf(
/* translators: %s: Number of errors */
__( 'PHP Errors (%s)', 'query-monitor' ),
number_format_i18n( $count )
) );
return $menu;
}
}
/**
* @param array<string, QM_Output> $output
* @param QM_Collectors $collectors
* @return array<string, QM_Output>
*/
function register_qm_output_html_php_errors( array $output, QM_Collectors $collectors ) {
$collector = QM_Collectors::get( 'php_errors' );
if ( $collector ) {
$output['php_errors'] = new QM_Output_Html_PHP_Errors( $collector );
}
return $output;
}
add_filter( 'qm/outputter/html', 'register_qm_output_html_php_errors', 110, 2 );