-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement logic to measure specific hook execution time with Server-T…
…iming controlled by a WP Admin screen.
- Loading branch information
1 parent
450761e
commit a1fd89c
Showing
4 changed files
with
261 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,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' ); ?> | ||
</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"> | ||
<?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" | ||
><?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 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 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 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