@@ -165,35 +165,27 @@ function od_add_template_output_buffer_filter( $template ) {
165
165
);
166
166
$ link_collection = new OD_Link_Collection ();
167
167
168
+ $ context = new OD_Template_Optimization_Context (
169
+ $ group_collection ,
170
+ $ tag_visitor_registry ,
171
+ $ post_id ,
172
+ $ query_vars ,
173
+ $ slug ,
174
+ $ current_etag ,
175
+ $ link_collection
176
+ );
177
+
168
178
/**
169
179
* Fires when Optimization Detective is initialized to optimize the current response.
170
180
*
171
181
* @since n.e.x.t
172
182
*
173
183
* @param OD_Template_Optimization_Context $context Template optimization context.
174
184
*/
175
- do_action (
176
- 'od_start_template_optimization ' ,
177
- new OD_Template_Optimization_Context (
178
- $ group_collection ,
179
- $ tag_visitor_registry ,
180
- $ post_id ,
181
- $ query_vars ,
182
- $ slug ,
183
- $ current_etag ,
184
- $ link_collection
185
- )
186
- );
185
+ do_action ( 'od_start_template_optimization ' , $ context );
187
186
188
- $ callback = static function ( string $ buffer ) use ( $ tag_visitor_registry , $ group_collection , $ link_collection , $ slug , $ post_id ): string {
189
- return od_optimize_template_output_buffer (
190
- $ buffer ,
191
- $ tag_visitor_registry ,
192
- $ group_collection ,
193
- $ link_collection ,
194
- $ slug ,
195
- $ post_id
196
- );
187
+ $ callback = static function ( string $ buffer ) use ( $ context ): string {
188
+ return od_optimize_template_output_buffer ( $ buffer , $ context );
197
189
};
198
190
199
191
if (
@@ -304,15 +296,11 @@ function od_is_response_html_content_type(): bool {
304
296
* @since 0.1.0
305
297
* @access private
306
298
*
307
- * @param string $buffer Template output buffer.
308
- * @param OD_Tag_Visitor_Registry $tag_visitor_registry Tag visitor registry.
309
- * @param OD_URL_Metric_Group_Collection $group_collection URL Metric group collection.
310
- * @param OD_Link_Collection $link_collection Link collection.
311
- * @param non-empty-string $slug Slug.
312
- * @param positive-int|null $post_id The ID for the od_url_metric post if it exists.
299
+ * @param string $buffer Template output buffer.
300
+ * @param OD_Template_Optimization_Context $context Template optimization context.
313
301
* @return string Filtered template output buffer.
314
302
*/
315
- function od_optimize_template_output_buffer ( string $ buffer , OD_Tag_Visitor_Registry $ tag_visitor_registry , OD_URL_Metric_Group_Collection $ group_collection , OD_Link_Collection $ link_collection , string $ slug , ? int $ post_id ): string {
303
+ function od_optimize_template_output_buffer ( string $ buffer , OD_Template_Optimization_Context $ context ): string {
316
304
317
305
// If the content-type is not HTML or the output does not start with '<', then abort since the buffer is definitely not HTML.
318
306
if (
@@ -322,6 +310,12 @@ function od_optimize_template_output_buffer( string $buffer, OD_Tag_Visitor_Regi
322
310
return $ buffer ;
323
311
}
324
312
313
+ $ link_collection = $ context ->link_collection ;
314
+ $ post_id = $ context ->url_metrics_id ;
315
+ $ group_collection = $ context ->url_metric_group_collection ;
316
+ $ slug = $ context ->url_metrics_slug ;
317
+ $ tag_visitor_registry = $ context ->tag_visitor_registry ;
318
+
325
319
// If the initial tag is not an open HTML tag, then abort since the buffer is not a complete HTML document.
326
320
$ processor = new OD_HTML_Tag_Processor ( $ buffer );
327
321
if ( ! (
0 commit comments