PluginProbe
Performance Lab / 4.1.0
Performance Lab v4.1.0
trunk 1.0.0 1.0.0-beta.1 1.0.0-beta.2 1.0.0-beta.3 1.0.0-rc.1 1.1.0 1.2.0 1.3.0 1.4.0 1.5.0 1.6.0 1.7.0 1.8.0 1.9.0 2.0.0 2.1.0 2.2.0 2.3.0 2.4.0 2.5.0 2.6.0 2.6.1 2.7.0 2.8.0 All 44 releases
performance-lab / includes / admin / server-timing.php

server-timing.php in Performance Lab 4.1.0, at includes/admin/server-timing.php

271 lines 8.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Server-Timing API admin integration file.
4 *
5 * @package performance-lab
6 */
7
8 // @codeCoverageIgnoreStart
9 if ( ! defined( 'ABSPATH' ) ) {
10 exit; // Exit if accessed directly.
11 }
12 // @codeCoverageIgnoreEnd
13
14 // Do not add any of the hooks if Server-Timing is disabled.
15 if ( defined( 'PERFLAB_DISABLE_SERVER_TIMING' ) && PERFLAB_DISABLE_SERVER_TIMING ) {
16 return;
17 }
18
19 /**
20 * Adds the Server-Timing page to the Tools menu.
21 *
22 * @since 2.6.0
23 */
24 function perflab_add_server_timing_page(): void {
25 $hook_suffix = add_management_page(
26 __( 'Server-Timing', 'performance-lab' ),
27 __( 'Server-Timing', 'performance-lab' ),
28 'manage_options',
29 PERFLAB_SERVER_TIMING_SCREEN,
30 'perflab_render_server_timing_page'
31 );
32
33 // Add the following hooks only if the screen was successfully added.
34 if ( false !== $hook_suffix ) {
35 add_action( "load-{$hook_suffix}", 'perflab_load_server_timing_page' );
36 }
37 }
38
39 add_action( 'admin_menu', 'perflab_add_server_timing_page' );
40
41 /**
42 * Initializes settings sections and fields for the Server-Timing page.
43 *
44 * @since 2.6.0
45 */
46 function perflab_load_server_timing_page(): void {
47 /*
48 * This settings section technically includes a field, however it is directly rendered as part of the section
49 * callback due to requiring custom markup.
50 */
51 add_settings_section(
52 'output-buffering',
53 __( 'Output Buffering', 'performance-lab' ),
54 'perflab_render_server_timing_page_output_buffering_section',
55 PERFLAB_SERVER_TIMING_SCREEN
56 );
57
58 // Minor style tweaks to improve appearance similar to other core settings screen instances.
59 add_action(
60 'admin_print_styles',
61 static function (): void {
62 ?>
63 <style>
64 .wrap p {
65 max-width: 800px;
66 }
67 .wrap .form-table .td-full {
68 padding-top: 0;
69 }
70 </style>
71 <?php
72 }
73 );
74
75 add_settings_section(
76 'benchmarking',
77 __( 'Benchmarking', 'performance-lab' ),
78 static function (): void {
79 ?>
80 <p>
81 <?php
82 echo wp_kses(
83 sprintf(
84 /* translators: %s: Server-Timing */
85 __( 'In this section, you can provide hook names to include measurements for them in the %s header.', 'performance-lab' ),
86 '<code>Server-Timing</code>'
87 ),
88 array( 'code' => array() )
89 );
90 echo ' ';
91 echo wp_kses(
92 __( 'For any hook name provided, the <strong>cumulative duration between all callbacks</strong> attached to the hook is measured, in milliseconds.', 'performance-lab' ),
93 array( 'strong' => array() )
94 );
95 ?>
96 </p>
97 <?php if ( ! perflab_server_timing_use_output_buffer() ) : ?>
98 <p>
99 <?php
100 echo wp_kses(
101 sprintf(
102 /* translators: 1: Server-Timing, 2: template_include, 3: anchor link */
103 __( 'Since the %1$s header is sent before the template is loaded, only hooks before the %2$s filter can be measured. Enable <a href="%3$s">Output Buffering</a> to measure hooks during template rendering.', 'performance-lab' ),
104 '<code>Server-Timing</code>',
105 '<code>template_include</code>',
106 esc_url( '#server_timing_output_buffering' )
107 ),
108 array(
109 'code' => array(),
110 'a' => array( 'href' => true ),
111 )
112 );
113 ?>
114 <?php endif; ?>
115 </p>
116 <?php
117 },
118 PERFLAB_SERVER_TIMING_SCREEN
119 );
120
121 /*
122 * For all settings fields, the field slug, option sub key, and label
123 * suffix have to match for the rendering in the callback to be
124 * semantically correct.
125 */
126 add_settings_field(
127 'benchmarking_actions',
128 __( 'Actions', 'performance-lab' ),
129 static function (): void {
130 perflab_render_server_timing_page_hooks_field( 'benchmarking_actions' );
131 },
132 PERFLAB_SERVER_TIMING_SCREEN,
133 'benchmarking',
134 array( 'label_for' => 'server_timing_benchmarking_actions' )
135 );
136 add_settings_field(
137 'benchmarking_filters',
138 __( 'Filters', 'performance-lab' ),
139 static function (): void {
140 perflab_render_server_timing_page_hooks_field( 'benchmarking_filters' );
141 },
142 PERFLAB_SERVER_TIMING_SCREEN,
143 'benchmarking',
144 array( 'label_for' => 'server_timing_benchmarking_filters' )
145 );
146 }
147
148 /**
149 * Renders the Server-Timing page.
150 *
151 * @since 2.6.0
152 */
153 function perflab_render_server_timing_page(): void {
154 ?>
155 <div class="wrap">
156 <?php settings_errors(); ?>
157 <h1>
158 <?php esc_html_e( 'Server-Timing', 'performance-lab' ); ?>
159 </h1>
160
161 <form action="options.php" method="post">
162 <?php settings_fields( PERFLAB_SERVER_TIMING_SCREEN ); ?>
163 <?php do_settings_sections( PERFLAB_SERVER_TIMING_SCREEN ); ?>
164 <?php submit_button(); ?>
165 </form>
166 </div>
167 <?php
168 }
169
170 /**
171 * Renders a hooks field for the given Server-Timing option.
172 *
173 * @since 2.6.0
174 *
175 * @param string $slug Slug of the field and sub-key in the Server-Timing option.
176 */
177 function perflab_render_server_timing_page_hooks_field( string $slug ): void {
178 $options = (array) get_option( PERFLAB_SERVER_TIMING_SETTING, array() );
179
180 // Value for the sub-key is an array of hook names.
181 $value = '';
182 if ( isset( $options[ $slug ] ) ) {
183 $value = implode( "\n", $options[ $slug ] );
184 }
185
186 $field_id = "server_timing_{$slug}";
187 $field_name = PERFLAB_SERVER_TIMING_SETTING . '[' . $slug . ']';
188 $description_id = "{$field_id}_description";
189
190 ?>
191 <textarea
192 id="<?php echo esc_attr( $field_id ); ?>"
193 name="<?php echo esc_attr( $field_name ); ?>"
194 aria-describedby="<?php echo esc_attr( $description_id ); ?>"
195 class="large-text code"
196 rows="8"
197 ><?php echo esc_textarea( $value ); ?></textarea>
198 <p id="<?php echo esc_attr( $description_id ); ?>" class="description">
199 <?php esc_html_e( 'Enter a single hook name per line.', 'performance-lab' ); ?>
200 </p>
201 <?php
202 }
203
204 /**
205 * Renders the section for enabling output buffering for Server-Timing.
206 *
207 * @since 2.6.0
208 */
209 function perflab_render_server_timing_page_output_buffering_section(): void {
210 $slug = 'output_buffering';
211 $field_id = "server_timing_{$slug}";
212 $field_name = PERFLAB_SERVER_TIMING_SETTING . '[' . $slug . ']';
213 $description_id = "{$field_id}_description";
214 $has_filter = has_filter( 'perflab_server_timing_use_output_buffer' );
215 $is_enabled = perflab_server_timing_use_output_buffer();
216
217 /*
218 * The hard-coded .form-table output here overall matches the WordPress core markup generated by
219 * `do_settings_sections()` and `do_settings_fields()`, however since it is impossible to modify the CSS classes on
220 * the `<td>` elements, it needs to be hard-coded to achieve the same appearance as e.g. the UI control for the
221 * `uploads_use_yearmonth_folders` option in the _Settings > Media_ screen, which is hard-coded as well.
222 */
223 ?>
224 <table class="form-table" role="presentation">
225 <tr>
226 <td class="td-full">
227 <label for="<?php echo esc_attr( $field_id ); ?>">
228 <input
229 type="checkbox"
230 id="<?php echo esc_attr( $field_id ); ?>"
231 name="<?php echo esc_attr( $field_name ); ?>"
232 aria-describedby="<?php echo esc_attr( $description_id ); ?>"
233 <?php disabled( $has_filter ); ?>
234 <?php checked( $is_enabled ); ?>
235 >
236 <?php esc_html_e( 'Enable output buffering of template rendering', 'performance-lab' ); ?>
237 </label>
238 <p id="<?php echo esc_attr( $description_id ); ?>" class="description">
239 <?php if ( $has_filter ) : ?>
240 <?php if ( $is_enabled ) : ?>
241 <?php
242 echo wp_kses(
243 sprintf(
244 /* translators: %s: perflab_server_timing_use_output_buffer */
245 __( 'Output buffering has been forcibly enabled via the %s filter.', 'performance-lab' ),
246 '<code>perflab_server_timing_use_output_buffer</code>'
247 ),
248 array( 'code' => array() )
249 );
250 ?>
251 <?php else : ?>
252 <?php
253 echo wp_kses(
254 sprintf(
255 /* translators: %s: perflab_server_timing_use_output_buffer */
256 __( 'Output buffering has been forcibly disabled via the %s filter.', 'performance-lab' ),
257 '<code>perflab_server_timing_use_output_buffer</code>'
258 ),
259 array( 'code' => array() )
260 );
261 ?>
262 <?php endif; ?>
263 <?php endif; ?>
264 <?php esc_html_e( 'Output buffering is needed to capture metrics after headers have been sent and while the template is being rendered. Note that output buffering may possibly cause an increase in TTFB if the response would be flushed multiple times.', 'performance-lab' ); ?>
265 </p>
266 </td>
267 </tr>
268 </table>
269 <?php
270 }
271