-
Notifications
You must be signed in to change notification settings - Fork 146
Server Timing - Allow description and make duration optional #2379
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
base: trunk
Are you sure you want to change the base?
Changes from 2 commits
3a2eefe
c91e96c
41b677b
ca2985e
d5b4eec
83dd4e2
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 |
|---|---|---|
|
|
@@ -37,6 +37,14 @@ class Perflab_Server_Timing_Metric { | |
| */ | ||
| private $before_value; | ||
|
|
||
| /** | ||
| * The metric description. | ||
| * | ||
| * @since n.e.x.t | ||
| * @var string|null | ||
| */ | ||
| private $description; | ||
|
|
||
| /** | ||
| * Constructor. | ||
| * | ||
|
|
@@ -142,4 +150,46 @@ public function measure_after(): void { | |
|
|
||
| $this->set_value( ( microtime( true ) - $this->before_value ) * 1000.0 ); | ||
| } | ||
|
|
||
| /** | ||
| * Sets the metric description. | ||
| * | ||
| * @since n.e.x.t | ||
| * | ||
| * @param string|mixed $description The metric description. | ||
| */ | ||
| public function set_description( $description ): void { | ||
| if ( ! is_string( $description ) ) { | ||
| _doing_it_wrong( | ||
| __METHOD__, | ||
| /* translators: %s: PHP parameter name */ | ||
| sprintf( esc_html__( 'The %s parameter must be a string.', 'performance-lab' ), '$description' ), | ||
| '' | ||
| ); | ||
| return; | ||
| } | ||
|
|
||
the-hercules marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if ( 0 !== did_action( 'perflab_server_timing_send_header' ) && ! doing_action( 'perflab_server_timing_send_header' ) ) { | ||
| _doing_it_wrong( | ||
| __METHOD__, | ||
| /* translators: %s: WordPress action name */ | ||
| sprintf( esc_html__( 'The method must be called before or during the %s action.', 'performance-lab' ), 'perflab_server_timing_send_header' ), | ||
| '' | ||
| ); | ||
| return; | ||
| } | ||
|
Comment on lines
+161
to
+170
|
||
|
|
||
| $this->description = $description; | ||
| } | ||
|
Comment on lines
+159
to
+173
|
||
|
|
||
| /** | ||
| * Gets the metric description. | ||
| * | ||
| * @since n.e.x.t | ||
| * | ||
| * @return string|null The metric description, or null if none set. | ||
| */ | ||
| public function get_description(): ?string { | ||
| return $this->description; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -290,23 +290,41 @@ function ( string $output, ?int $phase ): string { | |
| * @since 1.8.0 | ||
| * | ||
| * @param Perflab_Server_Timing_Metric $metric The metric to format. | ||
| * @return string|null Segment for the Server-Timing header, or null if no value set. | ||
| * @return string|null Segment for the Server-Timing header, or null if neither value nor description is set. | ||
| */ | ||
| private function format_metric_header_value( Perflab_Server_Timing_Metric $metric ): ?string { | ||
| $value = $metric->get_value(); | ||
| $value = $metric->get_value(); | ||
| $description = $metric->get_description(); | ||
|
|
||
| // If no value is set, make sure it's just passed through. | ||
| if ( null === $value ) { | ||
| // If neither value nor description is set, skip this metric. | ||
| if ( null === $value && null === $description ) { | ||
| return null; | ||
| } | ||
|
||
|
|
||
| if ( is_float( $value ) ) { | ||
| $value = round( $value, 2 ); | ||
| } | ||
|
|
||
| // See https://github.com/WordPress/performance/issues/955. | ||
| $name = preg_replace( '/[^!#$%&\'*+\-.^_`|~0-9a-zA-Z]/', '-', $metric->get_slug() ); | ||
|
|
||
| return sprintf( 'wp-%1$s;dur=%2$s', $name, $value ); | ||
| $parts = array( sprintf( 'wp-%s', $name ) ); | ||
|
|
||
| if ( null !== $value ) { | ||
| if ( is_float( $value ) ) { | ||
| $value = round( $value, 2 ); | ||
| } | ||
| $parts[] = sprintf( 'dur=%s', $value ); | ||
| } | ||
|
|
||
| if ( null !== $description ) { | ||
| // Sanitize description for HTTP header quoted-string format. | ||
| // Remove control characters (CR/LF) and escape backslashes and quotes. | ||
| $sanitized_description = preg_replace( '/[\r\n]/', '', $description ); | ||
the-hercules marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if ( null === $sanitized_description ) { | ||
| $sanitized_description = ''; | ||
| } | ||
| $sanitized_description = addcslashes( $sanitized_description, '\\' ); | ||
| $sanitized_description = addcslashes( $sanitized_description, '"' ); | ||
the-hercules marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| $parts[] = sprintf( 'desc="%s"', $sanitized_description ); | ||
| } | ||
|
|
||
| return implode( ';', $parts ); | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.