PluginProbe
Performance Lab / 3.6.1
Performance Lab v3.6.1
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 3.6.1, at includes/admin/server-timing.php

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