/var/www/html_sp/wp-content/plugins/query-monitor/output/html/http.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
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
414
415
416
417
418
<?php declare(strict_types 1);
/**
 * HTTP API request output for HTML pages.
 *
 * @package query-monitor
 */

if ( ! defined'ABSPATH' ) ) {
    exit;
}

class 
QM_Output_Html_HTTP extends QM_Output_Html {

    
/**
     * Collector instance.
     *
     * @var QM_Collector_HTTP Collector.
     */
    
protected $collector;

    public function 
__constructQM_Collector $collector ) {
        
parent::__construct$collector );
        
add_filter'qm/output/menus', array( $this'admin_menu' ), 90 );
        
add_filter'qm/output/menu_class', array( $this'admin_class' ) );
    }

    
/**
     * @return string
     */
    
public function name() {
        return 
__'HTTP API Calls''query-monitor' );
    }

    
/**
     * @return void
     */
    
public function output() {
        
/** @var QM_Data_HTTP $data */
        
$data $this->collector->get_data();

        if ( ! empty( 
$data->http ) ) {
            
$statuses array_keys$data->types );
            
$components array_column$data->component_times'component' );

            
usort$statuses'strcasecmp' );
            
usort$components'strcasecmp' );

            
$status_output = array();
            
$hosts array_uniquearray_column$data->http'host' ) );
            
sort$hosts );

            foreach ( 
$statuses as $status ) {
                if ( 
'error' === $status ) {
                    
$status_output['error'] = __'Error''query-monitor' );
                } elseif ( 
'non-blocking' === $status ) {
                    
/* translators: A non-blocking HTTP API request */
                    
$status_output['non-blocking'] = __'Non-blocking''query-monitor' );
                } else {
                    
$status_output[] = $status;
                }
            }

            
$this->before_tabular_output();

            echo 
'<thead>';
            echo 
'<tr>';
            echo 
'<th scope="col">' esc_html__'Method''query-monitor' ) . '</th>';
            echo 
'<th scope="col" class="qm-filterable-column">';
            echo 
$this->build_filter'host'$hosts__'URL''query-monitor' ) ); // WPCS: XSS ok.
            
echo '</th>';
            echo 
'<th scope="col" class="qm-filterable-column">';
            echo 
$this->build_filter'type'$status_output__'Status''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 
'<th scope="col" class="qm-num">' esc_html__'Response Size''query-monitor' ) . '</th>';
            echo 
'<th scope="col" class="qm-num">' esc_html__'Timeout''query-monitor' ) . '</th>';
            echo 
'<th scope="col" class="qm-num">' esc_html__'Time''query-monitor' ) . '</th>';
            echo 
'</tr>';
            echo 
'</thead>';

            echo 
'<tbody>';
            
$i 0;

            foreach ( 
$data->http as $row ) {
                
$ltime $row['ltime'];
                
$i++;
                
$is_error false;
                
$row_attr = array();
                
$css '';

                if ( 
$row['response'] instanceof WP_Error ) {
                    
$response $row['response']->get_error_message();
                    
$is_error true;
                } elseif ( ! 
$row['args']['blocking'] ) {
                    
/* translators: A non-blocking HTTP API request */
                    
$response __'Non-blocking''query-monitor' );
                } else {
                    
$code wp_remote_retrieve_response_code$row['response'] );
                    
$msg wp_remote_retrieve_response_message$row['response'] );

                    if ( 
intval$code ) >= 400 ) {
                        
$is_error true;
                    }

                    
$response $code ' ' $msg;

                }

                if ( 
$is_error ) {
                    
$css 'qm-warn';
                }

                
$url self::format_url$row['url'] );
                
$info '';

                if ( ! 
$row['local'] ) {
                    
$url preg_replace'|^http:|''<span class="qm-warn">http</span>:'$url );

                    if ( 
'https' === parse_url$row['url'], PHP_URL_SCHEME ) && empty( $row['args']['sslverify'] ) ) {
                        
$info .= '<span class="qm-warn">' QueryMonitor::icon'warning' ) . esc_htmlsprintf(
                            
/* translators: An HTTP API request has disabled certificate verification. 1: Relevant argument name */
                            
__'Certificate verification disabled (%s)''query-monitor' ),
                            
'sslverify=false'
                        
) ) . '</span><br>';
                        
$url preg_replace'|^https:|''<span class="qm-warn">https</span>:'$url );
                    }
                }

                
$component $row['component'];

                
$stack = array();
                
$filtered_trace $row['filtered_trace'];

                foreach ( 
$filtered_trace as $frame ) {
                    
$stack[] = self::output_filename$frame['display'], $frame['calling_file'], $frame['calling_line'] );
                }

                
$row_attr['data-qm-component'] = $component->name;
                
$row_attr['data-qm-type'] = $row['type'];
                
$row_attr['data-qm-host'] = $row['host'];

                if ( ! 
$row['intercepted'] ) {
                    
$row_attr['data-qm-time'] = $row['ltime'];
                }

                if ( 
'core' !== $component->context ) {
                    
$row_attr['data-qm-component'] .= ' non-core';
                }

                
$attr '';
                foreach ( 
$row_attr as $a => $v ) {
                    
$attr .= ' ' $a '="' esc_attr( (string) $v ) . '"';
                }

                
printf// WPCS: XSS ok.
                    
'<tr %s class="%s">',
                    
$attr,
                    
esc_attr$css )
                );
                
printf(
                    
'<td>%s</td>',
                    
esc_html$row['args']['method'] )
                );

                if ( 
$row['intercepted'] ) {
                    
$url sprintf(
                        
'<span class="qm-warn">%1$s%2$s</span><br>',
                        
QueryMonitor::icon'warning' ),
                        
sprintf(
                            
/* translators: %s: The name of a filter that short-circuited an HTTP API request */
                            
esc_html__'This HTTP request was short-circuited by the %s filter and was not sent''query-monitor' ),
                            
'<code>pre_http_request</code>'
                        
)
                    ) . 
$url;
                }

                if ( ! empty( 
$row['redirected_to'] ) ) {
                    
$url .= sprintf(
                        
'<br><span class="qm-warn">%1$s%2$s</span><br>%3$s',
                        
QueryMonitor::icon'warning' ),
                        
/* translators: An HTTP API request redirected to another URL */
                        
esc_html__'This HTTP request was redirected to:''query-monitor' ),
                        
self::format_url$row['redirected_to'] )
                    );
                }

                
printf// WPCS: XSS ok.
                    
'<td class="qm-url qm-ltr qm-wrap">%s%s</td>',
                    
$info,
                    
$url
                
);

                
$size '';
                
$timeout $row['args']['timeout'];

                if ( 
$row['intercepted'] ) {
                    
$ltime 0;
                    
$timeout '';
                } elseif ( isset( 
$row['info']['size_download'] ) ) {
                    
$size sprintf(
                        
/* translators: %s: Memory used in kilobytes */
                        
__'%s kB''query-monitor' ),
                        
number_format_i18n$row['info']['size_download'] / 1024)
                    );
                } elseif ( 
is_array$row['response'] ) && isset( $row['response']['body'] ) ) {
                    
$size sprintf(
                        
/* translators: %s: Memory used in kilobytes */
                        
__'%s kB''query-monitor' ),
                        
number_format_i18nstrlen$row['response']['body'] ) / 1024)
                    );
                }

                if ( 
$row['intercepted'] && ! $is_error ) {
                    echo 
'<td class="qm-col-status"></td>';
                } else {
                    
$show_toggle = ! empty( $row['info'] );

                    echo 
'<td class="qm-has-toggle qm-col-status">';
                    if ( 
$is_error ) {
                        
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
                        
echo QueryMonitor::icon'warning' );
                    }
                    echo 
esc_html$response );

                    if ( 
$show_toggle ) {
                        echo 
self::build_toggler(); // WPCS: XSS ok;
                        
echo '<ul class="qm-toggled">';
                    }

                    if ( ! empty( 
$row['info'] ) ) {
                        
$time_fields = array(
                            
'namelookup_time' => __'DNS Resolution Time''query-monitor' ),
                            
'connect_time' => __'Connection Time''query-monitor' ),
                            
'starttransfer_time' => __'Transfer Start Time (TTFB)''query-monitor' ),
                        );
                        foreach ( 
$time_fields as $key => $value ) {
                            if ( ! isset( 
$row['info'][ $key ] ) ) {
                                continue;
                            }
                            
printf(
                                
'<li><span class="qm-info qm-supplemental">%1$s: %2$s</span></li>',
                                
esc_html$value ),
                                
esc_htmlnumber_format_i18n$row['info'][ $key ], ) )
                            );
                        }

                        
$other_fields = array(
                            
'content_type' => __'Response Content Type''query-monitor' ),
                            
'primary_ip' => __'IP Address''query-monitor' ),
                        );
                        foreach ( 
$other_fields as $key => $value ) {
                            if ( ! isset( 
$row['info'][ $key ] ) ) {
                                continue;
                            }
                            
printf(
                                
'<li><span class="qm-info qm-supplemental">%1$s: %2$s</span></li>',
                                
esc_html$value ),
                                
esc_html$row['info'][ $key ] )
                            );
                        }
                    }

                    if ( 
$show_toggle ) {
                        echo 
'</ul>';
                    }

                    echo 
'</td>';
                }

                
$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 )
                );

                
printf(
                    
'<td class="qm-nowrap qm-num">%s</td>',
                    
esc_html$size )
                );

                
printf(
                    
'<td class="qm-num">%s</td>',
                    
esc_html$timeout )
                );

                if ( empty( 
$ltime ) ) {
                    
$stime '';
                } else {
                    
$stime number_format_i18n$ltime);
                }

                
printf(
                    
'<td class="qm-num">%s</td>',
                    
esc_html$stime )
                );
                echo 
'</tr>';
            }

            echo 
'</tbody>';
            echo 
'<tfoot>';

            
$total_stime number_format_i18n$data->ltime);
            
$count count$data->http );

            echo 
'<tr>';
            
printf(
                
'<td colspan="7">%s</td>',
                
sprintf(
                    
/* translators: %s: Number of HTTP API requests */
                    
esc_html_nx'Total: %s''Total: %s'$count'HTTP API calls''query-monitor' ) ),
                    
'<span class="qm-items-number">' esc_htmlnumber_format_i18n$count ) ) . '</span>'
                
)
            );
            echo 
'<td class="qm-num qm-items-time">' esc_html$total_stime ) . '</td>';
            echo 
'</tr>';
            echo 
'</tfoot>';

            
$this->after_tabular_output();
        } else {
            
$this->before_non_tabular_output();

            
$notice __'No HTTP API calls.''query-monitor' );
            echo 
$this->build_notice$notice ); // WPCS: XSS ok.

            
$this->after_non_tabular_output();
        }
    }

    
/**
     * @param array<int, string> $class
     * @return array<int, string>
     */
    
public function admin_class( array $class ) {
        
/** @var QM_Data_HTTP $data */
        
$data $this->collector->get_data();

        if ( isset( 
$data->errors['alert'] ) ) {
            
$class[] = 'qm-alert';
        }
        if ( isset( 
$data->errors['warning'] ) ) {
            
$class[] = 'qm-warning';
        }

        return 
$class;

    }

    
/**
     * @param array<string, mixed[]> $menu
     * @return array<string, mixed[]>
     */
    
public function admin_menu( array $menu ) {
        
/** @var QM_Data_HTTP $data */
        
$data $this->collector->get_data();

        
$count = ! empty( $data->http ) ? count$data->http ) : 0;

        
$title = ( empty( $count ) )
            ? 
__'HTTP API Calls''query-monitor' )
            
/* translators: %s: Number of calls to the HTTP API */
            
__'HTTP API Calls (%s)''query-monitor' );

        
$args = array(
            
'title' => esc_htmlsprintf(
                
$title,
                
number_format_i18n$count )
            ) ),
        );

        if ( isset( 
$data->errors['alert'] ) ) {
            
$args['meta']['classname'] = 'qm-alert';
        }
        if ( isset( 
$data->errors['warning'] ) ) {
            
$args['meta']['classname'] = 'qm-warning';
        }

        
$menu$this->collector->id() ] = $this->menu$args );

        return 
$menu;

    }

}

/**
 * @param array<string, QM_Output> $output
 * @param QM_Collectors $collectors
 * @return array<string, QM_Output>
 */
function register_qm_output_html_http( array $outputQM_Collectors $collectors ) {
    
$collector QM_Collectors::get'http' );
    if ( 
$collector ) {
        
$output['http'] = new QM_Output_Html_HTTP$collector );
    }
    return 
$output;
}

add_filter'qm/outputter/html''register_qm_output_html_http'90);