Skip to content

Commit 3b0f696

Browse files
committed
Initiaize OD objects at template_include filter
1 parent 64d63da commit 3b0f696

File tree

2 files changed

+35
-9
lines changed

2 files changed

+35
-9
lines changed

plugins/optimization-detective/optimization.php

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,6 @@ 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.
8381
*/
8482
function od_maybe_add_template_output_buffer_filter(): void {
8583
$conditions = array(
@@ -114,6 +112,31 @@ static function () use ( $reasons ): void {
114112
return;
115113
}
116114

115+
/*
116+
* The template_include filter with a max priority is used in order to obtain the $template without having to
117+
* rely on the $template global. Additionally, there is no hook which fires after the `template_include` filter
118+
* before the template starts rendering. Therefore, this is the last opportunity we have to initialize key
119+
* Optimization Detective objects while at the same time it is the first opportunity to do so since before here
120+
* the $template will not be known.
121+
*
122+
* Note that output buffering also starts at this same point in another filter, although if output buffering is
123+
* added to core, then it would be started either before the `template_redirect` action or after the
124+
* `template_include` filters have completely applied.
125+
*/
126+
add_filter( 'template_include', 'od_add_template_output_buffer_filter', PHP_INT_MAX );
127+
}
128+
129+
/**
130+
* Adds filter to optimize the template output buffer.
131+
*
132+
* @since n.e.x.t
133+
*
134+
* @global WP_Query $wp_the_query WP_Query object.
135+
*
136+
* @param non-empty-string|mixed $template Template.
137+
* @return non-empty-string|mixed Passed-through template.
138+
*/
139+
function od_add_template_output_buffer_filter( $template ) {
117140
$slug = od_get_url_metrics_slug( od_get_normalized_query_vars() );
118141
$post = OD_URL_Metrics_Post_Type::get_post( $slug );
119142
$post_id = $post instanceof WP_Post && $post->ID > 0 ? $post->ID : null;
@@ -130,8 +153,9 @@ static function () use ( $reasons ): void {
130153
do_action( 'od_register_tag_visitors', $tag_visitor_registry );
131154

132155
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(
156+
$current_theme_template = od_get_current_theme_template( is_string( $template ) ? $template : null );
157+
$current_etag = od_get_current_url_metrics_etag( $tag_visitor_registry, $wp_the_query, $current_theme_template );
158+
$group_collection = new OD_URL_Metric_Group_Collection(
135159
$post instanceof WP_Post ? OD_URL_Metrics_Post_Type::get_url_metrics_from_post( $post ) : array(),
136160
$current_etag,
137161
od_get_breakpoint_max_widths(),
@@ -140,7 +164,7 @@ static function () use ( $reasons ): void {
140164
);
141165

142166
/**
143-
* Fires when the current OD_URL_Metric_Group_Collection has been constructed for the response.
167+
* Fires when Optimization Detective is initialized to optimize the current response.
144168
*
145169
* @since n.e.x.t
146170
* @todo The parameters should be put into a context object as is done with other such actions.
@@ -171,6 +195,8 @@ function_exists( 'perflab_server_timing_use_output_buffer' )
171195
$callback = perflab_wrap_server_timing( $callback, 'optimization-detective', 'exist' );
172196
}
173197
add_filter( 'od_template_output_buffer', $callback );
198+
199+
return $template;
174200
}
175201

176202
/**

plugins/optimization-detective/storage/data.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,20 +144,20 @@ function od_get_url_metrics_slug( array $query_vars ): string {
144144
* @access private
145145
*
146146
* @global string|null $_wp_current_template_id Current template ID.
147-
* @global string|null $template Template file path.
148147
*
148+
* @param string|null $template Template.
149149
* @return string|WP_Block_Template|null Template.
150150
*/
151-
function od_get_current_theme_template() {
152-
global $template, $_wp_current_template_id;
151+
function od_get_current_theme_template( ?string $template ) {
152+
global $_wp_current_template_id; // TODO: Ideally we would not rely on a global here.
153153

154154
if ( wp_is_block_theme() && isset( $_wp_current_template_id ) ) {
155155
$block_template = get_block_template( $_wp_current_template_id, 'wp_template' );
156156
if ( $block_template instanceof WP_Block_Template ) {
157157
return $block_template;
158158
}
159159
}
160-
if ( isset( $template ) && is_string( $template ) ) {
160+
if ( isset( $template ) ) {
161161
return basename( $template );
162162
}
163163
return null;

0 commit comments

Comments
 (0)