Skip to content

Commit f7da494

Browse files
committed
Introduce OD_Template_Optimization_Context
1 parent 3b0f696 commit f7da494

File tree

4 files changed

+154
-9
lines changed

4 files changed

+154
-9
lines changed
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
<?php
2+
/**
3+
* Optimization Detective: OD_Template_Optimization_Context class
4+
*
5+
* @package optimization-detective
6+
* @since n.e.x.t
7+
*/
8+
9+
// @codeCoverageIgnoreStart
10+
if ( ! defined( 'ABSPATH' ) ) {
11+
exit; // Exit if accessed directly.
12+
}
13+
// @codeCoverageIgnoreEnd
14+
15+
/**
16+
* Context for optimizing a template prior to rendering.
17+
*
18+
* @since n.e.x.t
19+
*
20+
* @property-read OD_URL_Metric_Group_Collection $url_metric_group_collection URL Metric group collection.
21+
* @property-read OD_Tag_Visitor_Registry $tag_visitor_registry Tag visitor registry.
22+
* @property-read positive-int|null $url_metrics_id ID for the od_url_metrics post which provided the URL Metrics in the collection.
23+
* @property-read array<string, mixed> $normalized_query_vars Normalized query vars.
24+
* @property-read non-empty-string $url_metrics_slug Slug for the od_url_metrics post.
25+
* @property-read non-empty-string $current_etag Current ETag.
26+
*/
27+
final class OD_Template_Optimization_Context {
28+
29+
/**
30+
* URL Metric group collection.
31+
*
32+
* @since n.e.x.t
33+
* @var OD_URL_Metric_Group_Collection
34+
*/
35+
private $url_metric_group_collection;
36+
37+
/**
38+
* Tag visitor registry.
39+
*
40+
* @since n.e.x.t
41+
* @var OD_Tag_Visitor_Registry
42+
*/
43+
private $tag_visitor_registry;
44+
45+
/**
46+
* ID for the od_url_metrics post which provided the URL Metrics in the collection.
47+
*
48+
* May be null if no post has been created yet.
49+
*
50+
* @since n.e.x.t
51+
* @var positive-int|null
52+
*/
53+
private $url_metrics_id;
54+
55+
/**
56+
* Normalized query vars.
57+
*
58+
* @since n.e.x.t
59+
* @var array<string, mixed>
60+
*/
61+
private $normalized_query_vars;
62+
63+
/**
64+
* Slug for the od_url_metrics post.
65+
*
66+
* @since n.e.x.t
67+
* @var non-empty-string
68+
*/
69+
private $url_metrics_slug;
70+
71+
/**
72+
* Current ETag.
73+
*
74+
* @since n.e.x.t
75+
* @var non-empty-string
76+
*/
77+
private $current_etag;
78+
79+
/**
80+
* Constructor.
81+
*
82+
* @since n.e.x.t
83+
*
84+
* @param OD_URL_Metric_Group_Collection $url_metric_group_collection URL Metric group collection.
85+
* @param OD_Tag_Visitor_Registry $tag_visitor_registry Tag visitor registry.
86+
* @param positive-int|null $url_metrics_id ID for the od_url_metrics post which provided the URL Metrics in the collection. May be null if no post has been created yet.
87+
* @param array<string, mixed> $normalized_query_vars Normalized query vars.
88+
* @param non-empty-string $url_metrics_slug Slug for the od_url_metrics post.
89+
* @param non-empty-string $current_etag Current ETag.
90+
*/
91+
public function __construct( OD_URL_Metric_Group_Collection $url_metric_group_collection, OD_Tag_Visitor_Registry $tag_visitor_registry, ?int $url_metrics_id, array $normalized_query_vars, string $url_metrics_slug, string $current_etag ) {
92+
$this->url_metric_group_collection = $url_metric_group_collection;
93+
$this->tag_visitor_registry = $tag_visitor_registry;
94+
$this->url_metrics_id = $url_metrics_id;
95+
$this->normalized_query_vars = $normalized_query_vars;
96+
$this->url_metrics_slug = $url_metrics_slug;
97+
$this->current_etag = $current_etag;
98+
}
99+
100+
/**
101+
* Gets a property.
102+
*
103+
* @since n.e.x.t
104+
*
105+
* @param string $name Property name.
106+
* @return mixed Property value.
107+
*
108+
* @throws Error When property is unknown.
109+
*/
110+
public function __get( string $name ) {
111+
switch ( $name ) {
112+
case 'tag_visitor_registry':
113+
return $this->tag_visitor_registry;
114+
case 'url_metrics_id':
115+
return $this->url_metrics_id;
116+
case 'url_metric_group_collection':
117+
return $this->url_metric_group_collection;
118+
case 'normalized_query_vars':
119+
return $this->normalized_query_vars;
120+
case 'url_metrics_slug':
121+
return $this->url_metrics_slug;
122+
case 'current_etag':
123+
return $this->current_etag;
124+
default:
125+
throw new Error(
126+
esc_html(
127+
sprintf(
128+
/* translators: %s is class member variable name */
129+
__( 'Unknown property %s.', 'optimization-detective' ),
130+
__CLASS__ . '::$' . $name
131+
)
132+
)
133+
);
134+
}
135+
}
136+
}

plugins/optimization-detective/load.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ class_alias( OD_URL_Metric_Group_Collection::class, 'OD_URL_Metrics_Group_Collec
121121
require_once __DIR__ . '/detection.php';
122122

123123
// Optimization logic.
124+
require_once __DIR__ . '/class-od-template-optimization-context.php';
124125
require_once __DIR__ . '/class-od-link-collection.php';
125126
require_once __DIR__ . '/class-od-tag-visitor-registry.php';
126127
require_once __DIR__ . '/class-od-visited-tag-state.php';

plugins/optimization-detective/optimization.php

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,10 @@ static function () use ( $reasons ): void {
137137
* @return non-empty-string|mixed Passed-through template.
138138
*/
139139
function od_add_template_output_buffer_filter( $template ) {
140-
$slug = od_get_url_metrics_slug( od_get_normalized_query_vars() );
141-
$post = OD_URL_Metrics_Post_Type::get_post( $slug );
142-
$post_id = $post instanceof WP_Post && $post->ID > 0 ? $post->ID : null;
140+
$query_vars = od_get_normalized_query_vars();
141+
$slug = od_get_url_metrics_slug( $query_vars );
142+
$post = OD_URL_Metrics_Post_Type::get_post( $slug );
143+
$post_id = $post instanceof WP_Post && $post->ID > 0 ? $post->ID : null;
143144

144145
$tag_visitor_registry = new OD_Tag_Visitor_Registry();
145146

@@ -167,13 +168,20 @@ function od_add_template_output_buffer_filter( $template ) {
167168
* Fires when Optimization Detective is initialized to optimize the current response.
168169
*
169170
* @since n.e.x.t
170-
* @todo The parameters should be put into a context object as is done with other such actions.
171171
*
172-
* @param OD_URL_Metric_Group_Collection $group_collection URL Metric group collection.
173-
* @param OD_Tag_Visitor_Registry $tag_visitor_registry Tag visitor registry.
174-
* @param WP_Post|null $post The od_url_metrics post if it exists.
172+
* @param OD_Template_Optimization_Context $context Template optimization context.
175173
*/
176-
do_action( 'od_start_template_optimization', $group_collection, $tag_visitor_registry, $post );
174+
do_action(
175+
'od_start_template_optimization',
176+
new OD_Template_Optimization_Context(
177+
$group_collection,
178+
$tag_visitor_registry,
179+
$post_id,
180+
$query_vars,
181+
$slug,
182+
$current_etag
183+
)
184+
);
177185

178186
$callback = static function ( string $buffer ) use ( $tag_visitor_registry, $group_collection, $slug, $post_id ): string {
179187
return od_optimize_template_output_buffer(

plugins/optimization-detective/storage/data.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ function od_get_current_theme_template( ?string $template ) {
158158
}
159159
}
160160
if ( isset( $template ) ) {
161-
return basename( $template );
161+
return basename( $template ); // TODO: Why basename here?
162162
}
163163
return null;
164164
}

0 commit comments

Comments
 (0)