Skip to content

Commit 2359b9a

Browse files
authored
Merge pull request #1722 from ShyamGadde/update/incorporate-page-state-for-etag
Incorporate page state into ETag computation
2 parents ad6a211 + b083a29 commit 2359b9a

File tree

8 files changed

+346
-42
lines changed

8 files changed

+346
-42
lines changed

plugins/optimization-detective/optimization.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,10 +170,14 @@ function od_is_response_html_content_type(): bool {
170170
* @since 0.1.0
171171
* @access private
172172
*
173+
* @global WP_Query $wp_the_query WP_Query object.
174+
*
173175
* @param string $buffer Template output buffer.
174176
* @return string Filtered template output buffer.
175177
*/
176178
function od_optimize_template_output_buffer( string $buffer ): string {
179+
global $wp_the_query;
180+
177181
// If the content-type is not HTML or the output does not start with '<', then abort since the buffer is definitely not HTML.
178182
if (
179183
! od_is_response_html_content_type() ||
@@ -206,7 +210,7 @@ function od_optimize_template_output_buffer( string $buffer ): string {
206210
*/
207211
do_action( 'od_register_tag_visitors', $tag_visitor_registry );
208212

209-
$current_etag = od_get_current_url_metrics_etag( $tag_visitor_registry );
213+
$current_etag = od_get_current_url_metrics_etag( $tag_visitor_registry, $wp_the_query, od_get_current_theme_template() );
210214
$group_collection = new OD_URL_Metric_Group_Collection(
211215
$post instanceof WP_Post ? OD_URL_Metrics_Post_Type::get_url_metrics_from_post( $post ) : array(),
212216
$current_etag,

plugins/optimization-detective/storage/data.php

Lines changed: 83 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -140,22 +140,99 @@ function od_get_url_metrics_slug( array $query_vars ): string {
140140
return md5( (string) wp_json_encode( $query_vars ) );
141141
}
142142

143+
/**
144+
* Gets the current template for a block theme or a classic theme.
145+
*
146+
* @since n.e.x.t
147+
* @access private
148+
*
149+
* @global string|null $_wp_current_template_id Current template ID.
150+
* @global string|null $template Template file path.
151+
*
152+
* @return string|WP_Block_Template|null Template.
153+
*/
154+
function od_get_current_theme_template() {
155+
global $template, $_wp_current_template_id;
156+
157+
if ( wp_is_block_theme() && isset( $_wp_current_template_id ) ) {
158+
$block_template = get_block_template( $_wp_current_template_id, 'wp_template' );
159+
if ( $block_template instanceof WP_Block_Template ) {
160+
return $block_template;
161+
}
162+
}
163+
if ( isset( $template ) && is_string( $template ) ) {
164+
return basename( $template );
165+
}
166+
return null;
167+
}
168+
143169
/**
144170
* Gets the current ETag for URL Metrics.
145171
*
146-
* The ETag is a hash based on the IDs of the registered tag visitors
147-
* in the current environment. It is used for marking the URL Metrics as stale
148-
* when its value changes.
172+
* Generates a hash based on the IDs of registered tag visitors, the queried object,
173+
* posts in The Loop, and theme information in the current environment. This ETag
174+
* is used to assess if the URL Metrics are stale when its value changes.
149175
*
150176
* @since n.e.x.t
151177
* @access private
152178
*
153-
* @param OD_Tag_Visitor_Registry $tag_visitor_registry Tag visitor registry.
179+
* @param OD_Tag_Visitor_Registry $tag_visitor_registry Tag visitor registry.
180+
* @param WP_Query|null $wp_query The WP_Query instance.
181+
* @param string|WP_Block_Template|null $current_template The current template being used.
154182
* @return non-empty-string Current ETag.
155183
*/
156-
function od_get_current_url_metrics_etag( OD_Tag_Visitor_Registry $tag_visitor_registry ): string {
184+
function od_get_current_url_metrics_etag( OD_Tag_Visitor_Registry $tag_visitor_registry, ?WP_Query $wp_query, $current_template ): string {
185+
$queried_object = $wp_query instanceof WP_Query ? $wp_query->get_queried_object() : null;
186+
$queried_object_data = array(
187+
'id' => null,
188+
'type' => null,
189+
);
190+
191+
if ( $queried_object instanceof WP_Post ) {
192+
$queried_object_data['id'] = $queried_object->ID;
193+
$queried_object_data['type'] = 'post';
194+
$queried_object_data['post_modified_gmt'] = $queried_object->post_modified_gmt;
195+
} elseif ( $queried_object instanceof WP_Term ) {
196+
$queried_object_data['id'] = $queried_object->term_id;
197+
$queried_object_data['type'] = 'term';
198+
} elseif ( $queried_object instanceof WP_User ) {
199+
$queried_object_data['id'] = $queried_object->ID;
200+
$queried_object_data['type'] = 'user';
201+
} elseif ( $queried_object instanceof WP_Post_Type ) {
202+
$queried_object_data['type'] = $queried_object->name;
203+
}
204+
157205
$data = array(
158-
'tag_visitors' => array_keys( iterator_to_array( $tag_visitor_registry ) ),
206+
'tag_visitors' => array_keys( iterator_to_array( $tag_visitor_registry ) ),
207+
'queried_object' => $queried_object_data,
208+
'queried_posts' => array_filter(
209+
array_map(
210+
static function ( $post ): ?array {
211+
if ( is_int( $post ) ) {
212+
$post = get_post( $post );
213+
}
214+
if ( ! ( $post instanceof WP_Post ) ) {
215+
return null;
216+
}
217+
return array(
218+
'id' => $post->ID,
219+
'post_modified_gmt' => $post->post_modified_gmt,
220+
);
221+
},
222+
( $wp_query instanceof WP_Query && $wp_query->post_count > 0 ) ? $wp_query->posts : array()
223+
)
224+
),
225+
'active_theme' => array(
226+
'template' => array(
227+
'name' => get_template(),
228+
'version' => wp_get_theme( get_template() )->get( 'Version' ),
229+
),
230+
'stylesheet' => array(
231+
'name' => get_stylesheet(),
232+
'version' => wp_get_theme()->get( 'Version' ),
233+
),
234+
),
235+
'current_template' => $current_template instanceof WP_Block_Template ? get_object_vars( $current_template ) : $current_template,
159236
);
160237

161238
/**
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/*
2+
Theme Name: Block Theme
3+
Theme URI: https://wordpress.org/
4+
Description: For testing purposes only.
5+
Version: 1.0.0
6+
Text Domain: block-theme
7+
*/

plugins/optimization-detective/tests/data/themes/block-theme/templates/index.html

Whitespace-only changes.

0 commit comments

Comments
 (0)