Skip to content

Commit 6abee9c

Browse files
committed
Go back to initializing in footer but add action before/after optimization loop
1 parent a57391d commit 6abee9c

File tree

4 files changed

+107
-168
lines changed

4 files changed

+107
-168
lines changed

plugins/optimization-detective/class-od-tag-visitor-registry.php

Lines changed: 1 addition & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -32,43 +32,18 @@ final class OD_Tag_Visitor_Registry implements Countable, IteratorAggregate {
3232
*/
3333
private $visitors = array();
3434

35-
/**
36-
* Whether finalized.
37-
*
38-
* @since n.e.x.t
39-
* @var bool
40-
*/
41-
private $is_finalized = false;
42-
43-
/**
44-
* Finalizes the registry to prevent further modifications.
45-
*
46-
* @since n.e.x.t
47-
* @access private
48-
*/
49-
public function finalize(): void {
50-
$this->is_finalized = true;
51-
}
52-
5335
/**
5436
* Registers a tag visitor.
5537
*
5638
* @since 0.3.0
57-
* @since n.e.x.t Returns boolean for whether registration is successful. Returns false if registry is finalized.
5839
*
5940
* @phpstan-param TagVisitorCallback $tag_visitor_callback
6041
*
6142
* @param non-empty-string $id Identifier for the tag visitor.
6243
* @param callable $tag_visitor_callback Tag visitor callback.
63-
* @return bool Whether a tag visitor was registered.
6444
*/
65-
public function register( string $id, callable $tag_visitor_callback ): bool {
66-
if ( $this->is_finalized ) {
67-
_doing_it_wrong( __METHOD__, esc_html( $this->get_finalized_message() ), 'optimization-detective 1.0.0' );
68-
return false;
69-
}
45+
public function register( string $id, callable $tag_visitor_callback ): void {
7046
$this->visitors[ $id ] = $tag_visitor_callback;
71-
return true;
7247
}
7348

7449
/**
@@ -102,16 +77,11 @@ public function get_registered( string $id ): ?callable {
10277
* Unregisters a tag visitor.
10378
*
10479
* @since 0.3.0
105-
* @since n.e.x.t Returns false if the registry is finalized.
10680
*
10781
* @param non-empty-string $id Identifier for the tag visitor.
10882
* @return bool Whether a tag visitor was unregistered.
10983
*/
11084
public function unregister( string $id ): bool {
111-
if ( $this->is_finalized ) {
112-
_doing_it_wrong( __METHOD__, esc_html( $this->get_finalized_message() ), 'optimization-detective 1.0.0' );
113-
return false;
114-
}
11585
if ( ! $this->is_registered( $id ) ) {
11686
return false;
11787
}
@@ -140,19 +110,4 @@ public function getIterator(): ArrayIterator {
140110
public function count(): int {
141111
return count( $this->visitors );
142112
}
143-
144-
/**
145-
* Gets the finalized message when attempting to mutate the registry after the od_register_tag_visitors action.
146-
*
147-
* @since n.e.x.t
148-
*
149-
* @return string Message.
150-
*/
151-
private function get_finalized_message(): string {
152-
return sprintf(
153-
/* translators: %s is the od_register_tag_visitors action */
154-
__( 'The tag visitor registry has already been finalized. This method must be called during the %s action.', 'optimization-detective' ),
155-
'od_register_tag_visitors'
156-
);
157-
}
158113
}

plugins/optimization-detective/class-od-template-optimization-context.php

Lines changed: 42 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
// @codeCoverageIgnoreEnd
1414

1515
/**
16-
* Context for optimizing a template prior to rendering.
16+
* Context for optimizing a template.
1717
*
1818
* @since n.e.x.t
1919
*
@@ -22,7 +22,6 @@
2222
* @property-read positive-int|null $url_metrics_id ID for the od_url_metrics post which provided the URL Metrics in the collection.
2323
* @property-read array<string, mixed> $normalized_query_vars Normalized query vars.
2424
* @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.
2625
* @property-read OD_Link_Collection $link_collection Link collection.
2726
*/
2827
final class OD_Template_Optimization_Context {
@@ -36,12 +35,16 @@ final class OD_Template_Optimization_Context {
3635
private $url_metric_group_collection;
3736

3837
/**
39-
* Tag visitor registry.
38+
* HTML Tag Processor.
39+
*
40+
* This object is not directly exposed with an accessor property. This class exposes {@see self::append_head_html()}
41+
* and {@see self::append_body_html()} methods which wrap calls to the underlying
42+
* {@see OD_HTML_Tag_Processor::append_head_html()} and {@see OD_HTML_Tag_Processor::append_body_html()}.s
4043
*
4144
* @since n.e.x.t
42-
* @var OD_Tag_Visitor_Registry
45+
* @var OD_HTML_Tag_Processor
4346
*/
44-
private $tag_visitor_registry;
47+
private $processor;
4548

4649
/**
4750
* ID for the od_url_metrics post which provided the URL Metrics in the collection.
@@ -69,14 +72,6 @@ final class OD_Template_Optimization_Context {
6972
*/
7073
private $url_metrics_slug;
7174

72-
/**
73-
* Current ETag.
74-
*
75-
* @since n.e.x.t
76-
* @var non-empty-string
77-
*/
78-
private $current_etag;
79-
8075
/**
8176
* Link collection.
8277
*
@@ -90,22 +85,46 @@ final class OD_Template_Optimization_Context {
9085
*
9186
* @since n.e.x.t
9287
*
88+
* @param OD_HTML_Tag_Processor $processor HTML Tag Processor.
9389
* @param OD_URL_Metric_Group_Collection $url_metric_group_collection URL Metric group collection.
94-
* @param OD_Tag_Visitor_Registry $tag_visitor_registry Tag visitor registry.
95-
* @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.
90+
* @param OD_Link_Collection $link_collection Link collection.
9691
* @param array<string, mixed> $normalized_query_vars Normalized query vars.
9792
* @param non-empty-string $url_metrics_slug Slug for the od_url_metrics post.
98-
* @param non-empty-string $current_etag Current ETag.
99-
* @param OD_Link_Collection $link_collection Link collection.
93+
* @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.
10094
*/
101-
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, OD_Link_Collection $link_collection ) {
95+
public function __construct( OD_HTML_Tag_Processor $processor, OD_URL_Metric_Group_Collection $url_metric_group_collection, OD_Link_Collection $link_collection, array $normalized_query_vars, string $url_metrics_slug, ?int $url_metrics_id ) {
96+
$this->processor = $processor;
10297
$this->url_metric_group_collection = $url_metric_group_collection;
103-
$this->tag_visitor_registry = $tag_visitor_registry;
104-
$this->url_metrics_id = $url_metrics_id;
98+
$this->link_collection = $link_collection;
10599
$this->normalized_query_vars = $normalized_query_vars;
106100
$this->url_metrics_slug = $url_metrics_slug;
107-
$this->current_etag = $current_etag;
108-
$this->link_collection = $link_collection;
101+
$this->url_metrics_id = $url_metrics_id;
102+
}
103+
104+
/**
105+
* Append HTML to the HEAD.
106+
*
107+
* The provided HTML must be valid! No validation is performed.
108+
*
109+
* @since n.e.x.t
110+
*
111+
* @param non-empty-string $html HTML to inject.
112+
*/
113+
public function append_head_html( string $html ): void {
114+
$this->processor->append_head_html( $html );
115+
}
116+
117+
/**
118+
* Append HTML to the BODY.
119+
*
120+
* The provided HTML must be valid! No validation is performed.
121+
*
122+
* @since n.e.x.t
123+
*
124+
* @param non-empty-string $html HTML to inject.
125+
*/
126+
public function append_body_html( string $html ): void {
127+
$this->processor->append_body_html( $html );
109128
}
110129

111130
/**
@@ -119,9 +138,8 @@ public function __construct( OD_URL_Metric_Group_Collection $url_metric_group_co
119138
* @throws Error When property is unknown.
120139
*/
121140
public function __get( string $name ) {
141+
// Note: The $processor is intentionally not exposed.
122142
switch ( $name ) {
123-
case 'tag_visitor_registry':
124-
return $this->tag_visitor_registry;
125143
case 'url_metrics_id':
126144
return $this->url_metrics_id;
127145
case 'url_metric_group_collection':
@@ -130,8 +148,6 @@ public function __get( string $name ) {
130148
return $this->normalized_query_vars;
131149
case 'url_metrics_slug':
132150
return $this->url_metrics_slug;
133-
case 'current_etag':
134-
return $this->current_etag;
135151
case 'link_collection':
136152
return $this->link_collection;
137153
default:

0 commit comments

Comments
 (0)