Skip to content

Commit abdefc0

Browse files
committed
Move OD initialization to wp action instead of output buffer callback
1 parent 8efc280 commit abdefc0

File tree

1 file changed

+44
-29
lines changed

1 file changed

+44
-29
lines changed

plugins/optimization-detective/optimization.php

Lines changed: 44 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ static function ( string $output, ?int $phase ): string {
7878
*
7979
* @since 0.1.0
8080
* @access private
81+
*
82+
* @global WP_Query $wp_the_query WP_Query object.
8183
*/
8284
function od_maybe_add_template_output_buffer_filter(): void {
8385
$conditions = array(
@@ -112,7 +114,41 @@ static function () use ( $reasons ): void {
112114
return;
113115
}
114116

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+
116152
if (
117153
function_exists( 'perflab_wrap_server_timing' )
118154
&&
@@ -219,13 +255,14 @@ function od_is_response_html_content_type(): bool {
219255
* @since 0.1.0
220256
* @access private
221257
*
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.
225263
* @return string Filtered template output buffer.
226264
*/
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 {
229266

230267
// If the content-type is not HTML or the output does not start with '<', then abort since the buffer is definitely not HTML.
231268
if (
@@ -245,36 +282,14 @@ function od_optimize_template_output_buffer( string $buffer ): string {
245282
return $buffer;
246283
}
247284

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-
);
270285
$link_collection = new OD_Link_Collection();
271286
$visited_tag_state = new OD_Visited_Tag_State();
272287
$tag_visitor_context = new OD_Tag_Visitor_Context(
273288
$processor,
274289
$group_collection,
275290
$link_collection,
276291
$visited_tag_state,
277-
$post instanceof WP_Post && $post->ID > 0 ? $post->ID : null
292+
$post_id
278293
);
279294
$current_tag_bookmark = 'optimization_detective_current_tag';
280295
$visitors = iterator_to_array( $tag_visitor_registry );

0 commit comments

Comments
 (0)