Repository navigation
Implement logic to measure specific hook execution time with Server-Timing controlled by a WP Admin screen #784
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
a1fd89c
Implement logic to measure specific hook execution time with Server-T…
1c21d08
Merge branch 'trunk' into add/server-timing-hook-ui
ec9708b
Fix newly exposed lint violations.
6a1205f
Use more appropriate workaround when PHP_INT_MIN is not defined.
08d79c3
Merge branch 'trunk' into add/server-timing-hook-ui
694a6d1
Reuse logic to measure actions and filters, and provide inline docume…
6b5d12a
Clarify how the hooks are measured in WP Admin UI.
61f5ccc
Ensure that no duplicate hooks can be provided.
585911d
Add tests for server timing setting registration.
6db1e85
Add tests for server timing admin UI.
a8d141f
Remove XHTML syntax from WP Admin forms.
400c0a1
Modify hook measuring logic to use all hook.
c5fe384
Replace sanitize_key for hook name sanitization with custom regex to …
447f078
Have hook callbacks self-remove to only measure first occurrence.
c48d206
Remove novalidate attribute.
b85959e
Loosen hook name sanitization to rely on sanitize_text_field(), while…
124a7a6
Update test to cover whitespace in hookname.
7f99021
Use allowlist for Server-Timing option keys.
05d849e
Fix tests.
fd41db1
Add note on hook limitation unless output buffering is enabled.
f484a7d
Use placeholders for non-translatable strings.
7c94b9e
Add missing early return on top of procedural file.
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,165 @@ | ||
| <?php | ||
| /** | ||
| * Server-Timing API admin integration file. | ||
| * | ||
| * @package performance-lab | ||
| */ | ||
|
|
||
| if ( ! defined( 'ABSPATH' ) ) { | ||
| exit; // Exit if accessed directly. | ||
| } | ||
|
|
||
| /** | ||
| * Adds the Server-Timing page to the Tools menu. | ||
| * | ||
| * @since n.e.x.t | ||
| */ | ||
| function perflab_add_server_timing_page() { | ||
| $hook_suffix = add_management_page( | ||
| __( 'Server-Timing', 'performance-lab' ), | ||
| __( 'Server-Timing', 'performance-lab' ), | ||
| 'manage_options', | ||
| PERFLAB_SERVER_TIMING_SCREEN, | ||
| 'perflab_render_server_timing_page' | ||
| ); | ||
|
|
||
| // Add the following hooks only if the screen was successfully added. | ||
| if ( false !== $hook_suffix ) { | ||
| add_action( "load-{$hook_suffix}", 'perflab_load_server_timing_page' ); | ||
| } | ||
|
|
||
| return $hook_suffix; | ||
| } | ||
| add_action( 'admin_menu', 'perflab_add_server_timing_page' ); | ||
|
|
||
| /** | ||
| * Initializes settings sections and fields for the Server-Timing page. | ||
| * | ||
| * @since n.e.x.t | ||
| */ | ||
| function perflab_load_server_timing_page() { | ||
| add_settings_section( | ||
| 'benchmarking', | ||
| __( 'Benchmarking', 'performance-lab' ), | ||
| static function() { | ||
| ?> | ||
| <p> | ||
| <?php | ||
| echo wp_kses( | ||
| sprintf( | ||
| /* translators: %s: Server-Timing */ | ||
| __( 'In this section, you can provide hook names to include measurements for them in the %s header.', 'performance-lab' ), | ||
| '<code>Server-Timing</code>' | ||
| ), | ||
| array( 'code' => array() ) | ||
| ); | ||
| ?> | ||
| <br> | ||
| <?php | ||
| echo wp_kses( | ||
| __( 'For any hook name provided, the <strong>cumulative duration between all callbacks</strong> attached to the hook is measured, in milliseconds.', 'performance-lab' ), | ||
| array( 'strong' => array() ) | ||
| ); | ||
| if ( ! perflab_server_timing_use_output_buffer() ) { | ||
| ?> | ||
| <br> | ||
| <?php | ||
| echo wp_kses( | ||
| sprintf( | ||
| /* translators: 1: Server-Timing, 2: template_include */ | ||
| __( 'Since the %1$s header is sent before the template is loaded, only hooks before the %2$s filter can be measured.', 'performance-lab' ), | ||
| '<code>Server-Timing</code>', | ||
| '<code>template_include</code>' | ||
| ), | ||
| array( 'code' => array() ) | ||
| ); | ||
| } | ||
| ?> | ||
| </p> | ||
| <?php | ||
| }, | ||
| PERFLAB_SERVER_TIMING_SCREEN | ||
| ); | ||
|
|
||
| /* | ||
| * For all settings fields, the field slug, option sub key, and label | ||
| * suffix have to match for the rendering in the callback to be | ||
| * semantically correct. | ||
| */ | ||
| add_settings_field( | ||
| 'benchmarking_actions', | ||
| __( 'Actions', 'performance-lab' ), | ||
| static function() { | ||
| perflab_render_server_timing_page_field( 'benchmarking_actions' ); | ||
| }, | ||
| PERFLAB_SERVER_TIMING_SCREEN, | ||
| 'benchmarking', | ||
| array( 'label_for' => 'server_timing_benchmarking_actions' ) | ||
| ); | ||
| add_settings_field( | ||
| 'benchmarking_filters', | ||
| __( 'Filters', 'performance-lab' ), | ||
| static function() { | ||
| perflab_render_server_timing_page_field( 'benchmarking_filters' ); | ||
| }, | ||
| PERFLAB_SERVER_TIMING_SCREEN, | ||
| 'benchmarking', | ||
| array( 'label_for' => 'server_timing_benchmarking_filters' ) | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Renders the Server-Timing page. | ||
| * | ||
| * @since n.e.x.t | ||
| */ | ||
| function perflab_render_server_timing_page() { | ||
| ?> | ||
| <div class="wrap"> | ||
| <?php settings_errors(); ?> | ||
| <h1> | ||
| <?php esc_html_e( 'Server-Timing', 'performance-lab' ); ?> | ||
| </h1> | ||
|
|
||
| <form action="options.php" method="post"> | ||
| <?php settings_fields( PERFLAB_SERVER_TIMING_SCREEN ); ?> | ||
| <?php do_settings_sections( PERFLAB_SERVER_TIMING_SCREEN ); ?> | ||
| <?php submit_button(); ?> | ||
| </form> | ||
| </div> | ||
| <?php | ||
| } | ||
|
|
||
| /** | ||
| * Renders a field for the given Server-Timing option. | ||
| * | ||
| * @since n.e.x.t | ||
| * | ||
| * @param string $slug Slug of the field and sub-key in the Server-Timing option. | ||
| */ | ||
| function perflab_render_server_timing_page_field( $slug ) { | ||
| $options = (array) get_option( PERFLAB_SERVER_TIMING_SETTING, array() ); | ||
|
|
||
| // Value for the sub-key is an array of hook names. | ||
| $value = ''; | ||
| if ( isset( $options[ $slug ] ) ) { | ||
| $value = implode( "\n", $options[ $slug ] ); | ||
| } | ||
|
|
||
| $field_id = "server_timing_{$slug}"; | ||
| $field_name = PERFLAB_SERVER_TIMING_SETTING . '[' . $slug . ']'; | ||
| $description_id = "{$field_id}_description"; | ||
|
|
||
| ?> | ||
| <textarea | ||
| id="<?php echo esc_attr( $field_id ); ?>" | ||
| name="<?php echo esc_attr( $field_name ); ?>" | ||
| aria-describedby="<?php echo esc_attr( $description_id ); ?>" | ||
| class="large-text code" | ||
| rows="8" | ||
|
felixarntz marked this conversation as resolved.
|
||
| ><?php echo esc_textarea( $value ); ?></textarea> | ||
| <p id="<?php echo esc_attr( $description_id ); ?>" class="description"> | ||
| <?php esc_html_e( 'Enter a single hook name per line.', 'performance-lab' ); ?> | ||
| </p> | ||
| <?php | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What are you returning this for? This is hooked into an action right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, this would only be useful for test coverage, which I'm planning to add.