Skip to content

Commit ed3aa4c

Browse files
committed
Finalize the tag registry after the od_register_tag_visitors action has fired
1 parent 95e5388 commit ed3aa4c

File tree

2 files changed

+52
-3
lines changed

2 files changed

+52
-3
lines changed

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

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,43 @@ 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+
3553
/**
3654
* Registers a tag visitor.
3755
*
3856
* @since 0.3.0
57+
* @since n.e.x.t Returns boolean for whether registration is successful. Returns false if registry is finalized.
3958
*
4059
* @phpstan-param TagVisitorCallback $tag_visitor_callback
4160
*
4261
* @param non-empty-string $id Identifier for the tag visitor.
4362
* @param callable $tag_visitor_callback Tag visitor callback.
63+
* @return bool Whether a tag visitor was registered.
4464
*/
45-
public function register( string $id, callable $tag_visitor_callback ): void {
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+
}
4670
$this->visitors[ $id ] = $tag_visitor_callback;
71+
return true;
4772
}
4873

4974
/**
@@ -77,11 +102,16 @@ public function get_registered( string $id ): ?callable {
77102
* Unregisters a tag visitor.
78103
*
79104
* @since 0.3.0
105+
* @since n.e.x.t Returns false if the registry is finalized.
80106
*
81107
* @param non-empty-string $id Identifier for the tag visitor.
82108
* @return bool Whether a tag visitor was unregistered.
83109
*/
84110
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+
}
85115
if ( ! $this->is_registered( $id ) ) {
86116
return false;
87117
}
@@ -110,4 +140,19 @@ public function getIterator(): ArrayIterator {
110140
public function count(): int {
111141
return count( $this->visitors );
112142
}
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+
}
113158
}

plugins/optimization-detective/optimization.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,17 +153,21 @@ function od_add_template_output_buffer_filter( $template ) {
153153
*/
154154
do_action( 'od_register_tag_visitors', $tag_visitor_registry );
155155

156+
// Prevent modification of the tag visitor registry since doing so would invalidate the etag.
157+
$tag_visitor_registry->finalize();
158+
156159
global $wp_the_query;
157160
$current_theme_template = od_get_current_theme_template( is_string( $template ) ? $template : null );
158161
$current_etag = od_get_current_url_metrics_etag( $tag_visitor_registry, $wp_the_query, $current_theme_template );
159162
$group_collection = new OD_URL_Metric_Group_Collection(
160163
$post instanceof WP_Post ? OD_URL_Metrics_Post_Type::get_url_metrics_from_post( $post ) : array(),
161164
$current_etag,
165+
// TODO: Add the following values to the context as well.
162166
od_get_breakpoint_max_widths(),
163167
od_get_url_metrics_breakpoint_sample_size(),
164168
od_get_url_metric_freshness_ttl()
165-
);
166-
$link_collection = new OD_Link_Collection();
169+
); // TODO: Also finalize the collection?
170+
$link_collection = new OD_Link_Collection();
167171

168172
$context = new OD_Template_Optimization_Context(
169173
$group_collection,

0 commit comments

Comments
 (0)