Skip to content

Commit 0107138

Browse files
mreishusmatticbot
authored andcommitted
wpcomsh: bilmur: Add site timezone to tag (#41964)
* wpcomsh: bilmur: Add site timezone to tag * Changelog * try adding test * fix test expect * update false->0 for timezone_name_from_abbr * new tests - does this work * remove original test * add broken test to verify new file is running * Revert "add broken test to verify new file is running" This reverts commit 745537517fa20bf15c399c3e0743abe94f4ec252. Committed via a GitHub action: https://github.com/Automattic/jetpack/actions/runs/13501296705 Upstream-Ref: Automattic/jetpack@ed04ec5
1 parent 86e9969 commit 0107138

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ This is an alpha version! The changes listed here are not final.
2525
### Changed
2626
- Add Complete to AI features and sync other changes on wpcom features
2727
- Admin Bar: Point the Edit Site menu item to /site-editor.php
28+
- Bilmur RUM library now reports the site's timezone
2829
- Code: Use function-style exit() and die() with a default status code of 0.
2930
- Connection: Display connection status on Users page independent of the SSO module.
3031
- External Media: Move the GooglePhotosMedia, OpenverseMedia, PexelsMedia to @automattic/jetpack-shared-extension-utils

wpcomsh.php

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,49 @@ function wpcom_hide_scan_threats_from_api( $response ) {
540540
}
541541
add_filter( 'rest_post_dispatch', 'wpcom_hide_scan_threats_from_api' );
542542

543+
/**
544+
* Returns a standardized timezone string.
545+
*
546+
* `wp_timezone_string()` sometimes returns offsets (e.g. "-07:00"), which are
547+
* a non-standard representation of a UTC offset that only works in PHP.
548+
* This function returns a standardized timezone string instead, of the form
549+
* "Etc/GMT+7" for integer hour offsets, or a matching "<Area>/<City>" form for
550+
* fractional hour offsets (used e.g. in India).
551+
*/
552+
function wpcomsh_stats_timezone_string() {
553+
$wp_tz = wp_timezone_string();
554+
555+
// Did we get back an offset?
556+
if ( preg_match( '/^([+-])?(\d{1,2}):(\d{2})$/', $wp_tz, $matches ) ) {
557+
$sign = $matches[1] === '-' ? -1 : 1;
558+
$hours = intval( $matches[2], 10 );
559+
$minutes = intval( $matches[3], 10 );
560+
561+
// For fractional hour offsets, use `timezone_name_from_abbr` to get a
562+
// matching "<Area>/<City>" timezone.
563+
if ( $minutes > 0 ) {
564+
$offset = $sign * ( $hours * 3600 + $minutes * 60 );
565+
$city_tz = timezone_name_from_abbr( '', $offset, 0 );
566+
567+
if ( ! empty( $city_tz ) ) {
568+
return $city_tz;
569+
}
570+
}
571+
572+
// For integer hour offsets, use "Etc/GMT(+|-)<offset>".
573+
// The sign is flipped, to match how the `Etc` area is specced.
574+
//
575+
// This codepath is also followed if no city exists to match a
576+
// fractional offset, by simply discarding the fractional part.
577+
// This isn't ideal, but there's no standard way of describing
578+
// these offsets, and is likely to be an extreme edge case.
579+
return 'Etc/GMT' . ( $sign === -1 ? '+' : '-' ) . $hours;
580+
}
581+
582+
// For anything that's not an offset, return the string we got from WP.
583+
return $wp_tz;
584+
}
585+
543586
/**
544587
* Collect RUM performance data
545588
* p9o2xV-XY-p2
@@ -571,12 +614,15 @@ function wpcomsh_footer_rum_js() {
571614
$rum_kv = '';
572615
}
573616

617+
$data_site_tz = 'data-site-tz="' . esc_attr( wpcomsh_stats_timezone_string() ) . '"';
618+
574619
printf(
575-
'<script defer id="bilmur" %1$s data-provider="wordpress.com" data-service="%2$s" %3$s src="%4$s"></script>' . "\n", //phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedScript
620+
'<script defer id="bilmur" %1$s data-provider="wordpress.com" data-service="%2$s" %3$s src="%4$s" %5$s></script>' . "\n", //phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedScript
576621
$rum_kv, // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
577622
esc_attr( $service ),
578623
wp_kses_post( $allow_iframe ),
579-
esc_url( 'https://s0.wp.com/wp-content/js/bilmur.min.js?m=' . gmdate( 'YW' ) )
624+
esc_url( 'https://s0.wp.com/wp-content/js/bilmur.min.js?m=' . gmdate( 'YW' ) ),
625+
$data_site_tz // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
580626
);
581627
}
582628

0 commit comments

Comments
 (0)