PluginProbe
Performance Lab / 2.6.1
Performance Lab v2.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 / admin / server-timing.php

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

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