-
Notifications
You must be signed in to change notification settings - Fork 109
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
Implement logic to measure specific hook execution time with Server-Timing controlled by a WP Admin screen #784
Changes from 4 commits
a1fd89c
1c21d08
ec9708b
6a1205f
08d79c3
694a6d1
6b5d12a
61f5ccc
585911d
6db1e85
a8d141f
400c0a1
c5fe384
447f078
c48d206
b85959e
124a7a6
7f99021
05d849e
fd41db1
f484a7d
7c94b9e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
<?php | ||
/** | ||
* Server-Timing API admin integration file. | ||
* | ||
* @package performance-lab | ||
*/ | ||
|
||
/** | ||
* 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 esc_html_e( 'In this section, you can provide hook names to include measurements for them in the Server-Timing header.', 'performance-lab' ); ?> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since the |
||
</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" novalidate="novalidate"> | ||
felixarntz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
<?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.
Show resolved
Hide 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 | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,54 @@ | |
* @since 1.8.0 | ||
*/ | ||
|
||
define( 'PERFLAB_SERVER_TIMING_SETTING', 'perflab_server_timing_settings' ); | ||
define( 'PERFLAB_SERVER_TIMING_SCREEN', 'perflab-server-timing' ); | ||
felixarntz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/** | ||
* Registers the Server-Timing setting. | ||
* | ||
* @since n.e.x.t | ||
*/ | ||
function perflab_register_server_timing_setting() { | ||
register_setting( | ||
PERFLAB_SERVER_TIMING_SCREEN, | ||
PERFLAB_SERVER_TIMING_SETTING, | ||
array( | ||
'type' => 'object', | ||
'sanitize_callback' => 'perflab_sanitize_server_timing_setting', | ||
'default' => array(), | ||
) | ||
); | ||
felixarntz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
add_action( 'init', 'perflab_register_server_timing_setting' ); | ||
|
||
/** | ||
* Sanitizes the Server-Timing setting. | ||
* | ||
* @since n.e.x.t | ||
* | ||
* @param mixed $value Server-Timing setting value. | ||
* @return array Sanitized Server-Timing setting value. | ||
*/ | ||
function perflab_sanitize_server_timing_setting( $value ) { | ||
if ( ! is_array( $value ) ) { | ||
return array(); | ||
} | ||
|
||
// Ensure that every element is an indexed array of hook names. | ||
return array_filter( | ||
array_map( | ||
static function( $hooks ) { | ||
if ( ! is_array( $hooks ) ) { | ||
$hooks = explode( "\n", $hooks ); | ||
} | ||
return array_filter( array_map( 'sanitize_key', $hooks ) ); | ||
}, | ||
$value | ||
) | ||
); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This could do with a unit test IMO. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I haven't added unit tests yet, but definitely planning to do so. |
||
|
||
/** | ||
* Provides access the Server-Timing API. | ||
* | ||
|
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.