@@ -140,22 +140,99 @@ function od_get_url_metrics_slug( array $query_vars ): string {
140
140
return md5 ( (string ) wp_json_encode ( $ query_vars ) );
141
141
}
142
142
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
+
143
169
/**
144
170
* Gets the current ETag for URL Metrics.
145
171
*
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.
149
175
*
150
176
* @since n.e.x.t
151
177
* @access private
152
178
*
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.
154
182
* @return non-empty-string Current ETag.
155
183
*/
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
+
157
205
$ 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 ,
159
236
);
160
237
161
238
/**
0 commit comments