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
|
<?php declare(strict_types = 1); /** * Request and response headers output for HTML pages. * * @package query-monitor */
if ( ! defined( 'ABSPATH' ) ) { exit; }
class QM_Output_Html_Headers extends QM_Output_Html {
/** * Collector instance. * * @var QM_Collector_Raw_Request Collector. */ protected $collector;
public function __construct( QM_Collector $collector ) { parent::__construct( $collector ); add_filter( 'qm/output/panel_menus', array( $this, 'panel_menu' ), 20 ); }
/** * Collector name. * * This is unused. * * @return string */ public function name() { return __( 'Request Data', 'query-monitor' ); }
/** * @return void */ public function output() { $this->output_request(); $this->output_response(); }
/** * @return void */ public function output_request() { /** @var QM_Data_Raw_Request $data */ $data = $this->collector->get_data();
$this->before_tabular_output();
$this->output_header_table( $data->request['headers'], __( 'Request Header Name', 'query-monitor' ) );
$this->after_tabular_output(); }
/** * @return void */ public function output_response() { /** @var QM_Data_Raw_Request $data */ $data = $this->collector->get_data(); $id = sprintf( 'qm-%s-response', $this->collector->id );
$this->before_tabular_output( $id );
$this->output_header_table( $data->response['headers'], __( 'Response Header Name', 'query-monitor' ) );
$this->after_tabular_output(); }
/** * @param array<string, string> $headers * @param string $title * @return void */ protected function output_header_table( array $headers, $title ) { echo '<thead>'; echo '<tr>'; echo '<th>'; echo esc_html( $title ); echo '</th><th>'; esc_html_e( 'Value', 'query-monitor' ); echo '</th></tr>'; echo '<tbody>';
foreach ( $headers as $name => $value ) { echo '<tr>'; $formatted = str_replace( ' ', '-', ucwords( strtolower( str_replace( array( '-', '_' ), ' ', $name ) ) ) ); printf( '<th scope="row"><code>%s</code></th>', esc_html( $formatted ) ); printf( '<td><pre class="qm-pre-wrap"><code>%s</code></pre></td>', esc_html( $value ) ); echo '</tr>'; }
echo '</tbody>';
echo '<tfoot>'; echo '<tr>'; echo '<td colspan="2">'; esc_html_e( 'Note that header names are not case-sensitive.', 'query-monitor' ); echo '</td>'; echo '</tr>'; echo '</tfoot>'; }
/** * @param array<string, mixed[]> $menu * @return array<string, mixed[]> */ public function panel_menu( array $menu ) { if ( ! isset( $menu['qm-request'] ) ) { return $menu; }
$ids = array( $this->collector->id() => __( 'Request Headers', 'query-monitor' ), $this->collector->id() . '-response' => __( 'Response Headers', 'query-monitor' ), ); foreach ( $ids as $id => $title ) { $menu['qm-request']['children'][] = array( 'id' => $id, 'href' => '#' . $id, 'title' => esc_html( $title ), ); }
return $menu; } }
/** * @param array<string, QM_Output> $output * @param QM_Collectors $collectors * @return array<string, QM_Output> */ function register_qm_output_html_headers( array $output, QM_Collectors $collectors ) { $collector = QM_Collectors::get( 'raw_request' ); if ( $collector ) { $output['raw_request'] = new QM_Output_Html_Headers( $collector ); } return $output; }
add_filter( 'qm/outputter/html', 'register_qm_output_html_headers', 100, 2 );
|