Skip to content

Commit 0ad7f79

Browse files
committed
Reuse template optimization context in od_optimize_template_output_buffer()
1 parent 51feaad commit 0ad7f79

File tree

1 file changed

+22
-28
lines changed

1 file changed

+22
-28
lines changed

plugins/optimization-detective/optimization.php

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -165,35 +165,27 @@ function od_add_template_output_buffer_filter( $template ) {
165165
);
166166
$link_collection = new OD_Link_Collection();
167167

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+
168178
/**
169179
* Fires when Optimization Detective is initialized to optimize the current response.
170180
*
171181
* @since n.e.x.t
172182
*
173183
* @param OD_Template_Optimization_Context $context Template optimization context.
174184
*/
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 );
187186

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 );
197189
};
198190

199191
if (
@@ -304,15 +296,11 @@ function od_is_response_html_content_type(): bool {
304296
* @since 0.1.0
305297
* @access private
306298
*
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.
313301
* @return string Filtered template output buffer.
314302
*/
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 {
316304

317305
// If the content-type is not HTML or the output does not start with '<', then abort since the buffer is definitely not HTML.
318306
if (
@@ -322,6 +310,12 @@ function od_optimize_template_output_buffer( string $buffer, OD_Tag_Visitor_Regi
322310
return $buffer;
323311
}
324312

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+
325319
// If the initial tag is not an open HTML tag, then abort since the buffer is not a complete HTML document.
326320
$processor = new OD_HTML_Tag_Processor( $buffer );
327321
if ( ! (

0 commit comments

Comments
 (0)