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
|
<?php declare(strict_types = 1); /** * User capability checks output for HTML pages. * * @package query-monitor */
if ( ! defined( 'ABSPATH' ) ) { exit; }
class QM_Output_Html_Caps extends QM_Output_Html {
/** * Collector instance. * * @var QM_Collector_Caps Collector. */ protected $collector;
public function __construct( QM_Collector $collector ) { parent::__construct( $collector ); add_filter( 'qm/output/menus', array( $this, 'admin_menu' ), 105 ); }
/** * @return string */ public function name() { return __( 'Capability Checks', 'query-monitor' ); }
/** * @return void */ public function output() { $collector = $this->collector;
if ( ! $collector::enabled() ) { $this->before_non_tabular_output();
echo '<section>'; echo '<div class="qm-notice">'; echo '<p>'; printf( /* translators: %s: Configuration file name. */ esc_html__( 'For performance reasons, this panel is not enabled by default. To enable it, add the following code to your %s file:', 'query-monitor' ), '<code>wp-config.php</code>' ); echo '</p>'; echo "<p><code>define( 'QM_ENABLE_CAPS_PANEL', true );</code></p>"; echo '</div>'; echo '</section>';
$this->after_non_tabular_output();
return; }
/** @var QM_Data_Caps */ $data = $this->collector->get_data();
if ( ! empty( $data->caps ) ) { $this->before_tabular_output();
$results = array( 'true', 'false', ); $parts = $data->parts; $components = $data->components;
usort( $parts, 'strcasecmp' ); usort( $components, 'strcasecmp' );
echo '<thead>'; echo '<tr>'; echo '<th scope="col" class="qm-filterable-column">'; echo $this->build_filter( 'name', $parts, __( 'Capability Check', 'query-monitor' ) ); // WPCS: XSS ok; echo '</th>';
$users = $data->users;
sort( $users );
echo '<th scope="col" class="qm-filterable-column qm-num">'; echo $this->build_filter( 'user', $users, __( 'User', 'query-monitor' ) ); // WPCS: XSS ok; echo '</th>';
echo '<th scope="col" class="qm-filterable-column">'; echo $this->build_filter( 'result', $results, __( 'Result', 'query-monitor' ) ); // WPCS: XSS ok; echo '</th>'; echo '<th scope="col">' . esc_html__( 'Caller', '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 ( $data->caps as $row ) { $component = $row['component'];
$row_attr = array(); $row_attr['data-qm-name'] = implode( ' ', $row['parts'] ); $row_attr['data-qm-user'] = $row['user']; $row_attr['data-qm-component'] = $component->name; $row_attr['data-qm-result'] = ( $row['result'] ) ? 'true' : 'false';
if ( 'core' !== $component->context ) { $row_attr['data-qm-component'] .= ' non-core'; }
if ( '' === $row['name'] ) { $row_attr['class'] = 'qm-warn'; }
$attr = '';
foreach ( $row_attr as $a => $v ) { $attr .= ' ' . $a . '="' . esc_attr( $v ) . '"'; }
printf( // WPCS: XSS ok. '<tr %s>', $attr );
$name = esc_html( $row['name'] );
if ( ! empty( $row['args'] ) ) { foreach ( $row['args'] as $arg ) { $name .= ', ' . esc_html( QM_Util::display_variable( $arg ) ); } }
printf( // WPCS: XSS ok. '<td class="qm-ltr qm-nowrap"><code>%s</code></td>', $name );
printf( '<td class="qm-num">%s</td>', esc_html( $row['user'] ) );
$result = ( $row['result'] ) ? '<span class="qm-true">true ✓</span>' : 'false'; printf( // WPCS: XSS ok. '<td class="qm-nowrap">%s</td>', $result );
$stack = array();
foreach ( $row['filtered_trace'] as $frame ) { $stack[] = self::output_filename( $frame['display'], $frame['calling_file'], $frame['calling_line'] ); }
$caller = array_shift( $stack );
echo '<td class="qm-has-toggle qm-nowrap qm-ltr">';
if ( ! empty( $stack ) ) { echo self::build_toggler(); // WPCS: XSS ok; }
echo '<ol>';
echo "<li>{$caller}</li>"; // WPCS: XSS ok.
if ( ! empty( $stack ) ) { echo '<div class="qm-toggled"><li>' . implode( '</li><li>', $stack ) . '</li></div>'; // WPCS: XSS ok. }
echo '</ol></td>';
printf( '<td class="qm-nowrap">%s</td>', esc_html( $component->name ) );
echo '</tr>';
}
echo '</tbody>';
echo '<tfoot>';
$count = count( $data->caps );
echo '<tr>'; echo '<td colspan="5">'; printf( /* translators: %s: Number of user capability checks */ esc_html( _nx( 'Total: %s', 'Total: %s', $count, 'User capability checks', 'query-monitor' ) ), '<span class="qm-items-number">' . esc_html( number_format_i18n( $count ) ) . '</span>' ); echo '</td>'; echo '</tr>'; echo '</tfoot>';
$this->after_tabular_output(); } else { $this->before_non_tabular_output();
$notice = __( 'No capability checks were recorded.', 'query-monitor' ); echo $this->build_notice( $notice ); // WPCS: XSS ok.
$this->after_non_tabular_output(); } }
/** * @param array<string, mixed[]> $menu * @return array<string, mixed[]> */ public function admin_menu( array $menu ) { $menu[ $this->collector->id() ] = $this->menu( array( 'title' => $this->name(), ) ); return $menu;
}
}
/** * @param array<string, QM_Output> $output * @param QM_Collectors $collectors * @return array<string, QM_Output> */ function register_qm_output_html_caps( array $output, QM_Collectors $collectors ) { $collector = QM_Collectors::get( 'caps' ); if ( $collector ) { $output['caps'] = new QM_Output_Html_Caps( $collector ); } return $output; }
add_filter( 'qm/outputter/html', 'register_qm_output_html_caps', 105, 2 );
|