@@ -78,6 +78,8 @@ static function ( string $output, ?int $phase ): string {
78
78
*
79
79
* @since 0.1.0
80
80
* @access private
81
+ *
82
+ * @global WP_Query $wp_the_query WP_Query object.
81
83
*/
82
84
function od_maybe_add_template_output_buffer_filter (): void {
83
85
$ conditions = array (
@@ -112,7 +114,41 @@ static function () use ( $reasons ): void {
112
114
return ;
113
115
}
114
116
115
- $ callback = 'od_optimize_template_output_buffer ' ;
117
+ $ slug = od_get_url_metrics_slug ( od_get_normalized_query_vars () );
118
+ $ post = OD_URL_Metrics_Post_Type::get_post ( $ slug );
119
+ $ post_id = $ post instanceof WP_Post && $ post ->ID > 0 ? $ post ->ID : null ;
120
+
121
+ $ tag_visitor_registry = new OD_Tag_Visitor_Registry ();
122
+
123
+ /**
124
+ * Fires to register tag visitors before walking over the document to perform optimizations.
125
+ *
126
+ * @since 0.3.0
127
+ *
128
+ * @param OD_Tag_Visitor_Registry $tag_visitor_registry Tag visitor registry.
129
+ */
130
+ do_action ( 'od_register_tag_visitors ' , $ tag_visitor_registry );
131
+
132
+ global $ wp_the_query ;
133
+ $ current_etag = od_get_current_url_metrics_etag ( $ tag_visitor_registry , $ wp_the_query , od_get_current_theme_template () ); // TODO: Make sure the template is set!!
134
+ $ group_collection = new OD_URL_Metric_Group_Collection (
135
+ $ post instanceof WP_Post ? OD_URL_Metrics_Post_Type::get_url_metrics_from_post ( $ post ) : array (),
136
+ $ current_etag ,
137
+ od_get_breakpoint_max_widths (),
138
+ od_get_url_metrics_breakpoint_sample_size (),
139
+ od_get_url_metric_freshness_ttl ()
140
+ );
141
+
142
+ $ callback = static function ( string $ buffer ) use ( $ tag_visitor_registry , $ group_collection , $ slug , $ post_id ): string {
143
+ return od_optimize_template_output_buffer (
144
+ $ buffer ,
145
+ $ tag_visitor_registry ,
146
+ $ group_collection ,
147
+ $ slug ,
148
+ $ post_id
149
+ );
150
+ };
151
+
116
152
if (
117
153
function_exists ( 'perflab_wrap_server_timing ' )
118
154
&&
@@ -219,13 +255,14 @@ function od_is_response_html_content_type(): bool {
219
255
* @since 0.1.0
220
256
* @access private
221
257
*
222
- * @global WP_Query $wp_the_query WP_Query object.
223
- *
224
- * @param string $buffer Template output buffer.
258
+ * @param string $buffer Template output buffer.
259
+ * @param OD_Tag_Visitor_Registry $tag_visitor_registry Tag visitor registry.
260
+ * @param OD_URL_Metric_Group_Collection $group_collection URL Metric group collection.
261
+ * @param non-empty-string $slug Slug.
262
+ * @param positive-int|null $post_id The ID for the od_url_metric post if it exists.
225
263
* @return string Filtered template output buffer.
226
264
*/
227
- function od_optimize_template_output_buffer ( string $ buffer ): string {
228
- global $ wp_the_query ;
265
+ function od_optimize_template_output_buffer ( string $ buffer , OD_Tag_Visitor_Registry $ tag_visitor_registry , OD_URL_Metric_Group_Collection $ group_collection , string $ slug , ?int $ post_id ): string {
229
266
230
267
// If the content-type is not HTML or the output does not start with '<', then abort since the buffer is definitely not HTML.
231
268
if (
@@ -245,36 +282,14 @@ function od_optimize_template_output_buffer( string $buffer ): string {
245
282
return $ buffer ;
246
283
}
247
284
248
- $ slug = od_get_url_metrics_slug ( od_get_normalized_query_vars () );
249
- $ post = OD_URL_Metrics_Post_Type::get_post ( $ slug );
250
-
251
- $ tag_visitor_registry = new OD_Tag_Visitor_Registry ();
252
-
253
- /**
254
- * Fires to register tag visitors before walking over the document to perform optimizations.
255
- *
256
- * @since 0.3.0
257
- *
258
- * @param OD_Tag_Visitor_Registry $tag_visitor_registry Tag visitor registry.
259
- */
260
- do_action ( 'od_register_tag_visitors ' , $ tag_visitor_registry );
261
-
262
- $ current_etag = od_get_current_url_metrics_etag ( $ tag_visitor_registry , $ wp_the_query , od_get_current_theme_template () );
263
- $ group_collection = new OD_URL_Metric_Group_Collection (
264
- $ post instanceof WP_Post ? OD_URL_Metrics_Post_Type::get_url_metrics_from_post ( $ post ) : array (),
265
- $ current_etag ,
266
- od_get_breakpoint_max_widths (),
267
- od_get_url_metrics_breakpoint_sample_size (),
268
- od_get_url_metric_freshness_ttl ()
269
- );
270
285
$ link_collection = new OD_Link_Collection ();
271
286
$ visited_tag_state = new OD_Visited_Tag_State ();
272
287
$ tag_visitor_context = new OD_Tag_Visitor_Context (
273
288
$ processor ,
274
289
$ group_collection ,
275
290
$ link_collection ,
276
291
$ visited_tag_state ,
277
- $ post instanceof WP_Post && $ post -> ID > 0 ? $ post -> ID : null
292
+ $ post_id
278
293
);
279
294
$ current_tag_bookmark = 'optimization_detective_current_tag ' ;
280
295
$ visitors = iterator_to_array ( $ tag_visitor_registry );
0 commit comments