@@ -78,8 +78,6 @@ 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.
83
81
*/
84
82
function od_maybe_add_template_output_buffer_filter (): void {
85
83
$ conditions = array (
@@ -114,6 +112,31 @@ static function () use ( $reasons ): void {
114
112
return ;
115
113
}
116
114
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 ) {
117
140
$ slug = od_get_url_metrics_slug ( od_get_normalized_query_vars () );
118
141
$ post = OD_URL_Metrics_Post_Type::get_post ( $ slug );
119
142
$ post_id = $ post instanceof WP_Post && $ post ->ID > 0 ? $ post ->ID : null ;
@@ -130,8 +153,9 @@ static function () use ( $reasons ): void {
130
153
do_action ( 'od_register_tag_visitors ' , $ tag_visitor_registry );
131
154
132
155
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 (
135
159
$ post instanceof WP_Post ? OD_URL_Metrics_Post_Type::get_url_metrics_from_post ( $ post ) : array (),
136
160
$ current_etag ,
137
161
od_get_breakpoint_max_widths (),
@@ -140,7 +164,7 @@ static function () use ( $reasons ): void {
140
164
);
141
165
142
166
/**
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.
144
168
*
145
169
* @since n.e.x.t
146
170
* @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' )
171
195
$ callback = perflab_wrap_server_timing ( $ callback , 'optimization-detective ' , 'exist ' );
172
196
}
173
197
add_filter ( 'od_template_output_buffer ' , $ callback );
198
+
199
+ return $ template ;
174
200
}
175
201
176
202
/**
0 commit comments