Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Finalize the tag registry after the od_register_tag_visitors action h…
…as fired
  • Loading branch information
westonruter committed Mar 13, 2025
commit 8bcfc13c88131515de70e6618f9daf074f93e698
47 changes: 46 additions & 1 deletion plugins/optimization-detective/class-od-tag-visitor-registry.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,43 @@ final class OD_Tag_Visitor_Registry implements Countable, IteratorAggregate {
*/
private $visitors = array();

/**
* Whether finalized.
*
* @since n.e.x.t
* @var bool
*/
private $is_finalized = false;

/**
* Finalizes the registry to prevent further modifications.
*
* @since n.e.x.t
* @access private
*/
public function finalize(): void {
$this->is_finalized = true;
}

/**
* Registers a tag visitor.
*
* @since 0.3.0
* @since n.e.x.t Returns boolean for whether registration is successful. Returns false if registry is finalized.
*
* @phpstan-param TagVisitorCallback $tag_visitor_callback
*
* @param non-empty-string $id Identifier for the tag visitor.
* @param callable $tag_visitor_callback Tag visitor callback.
* @return bool Whether a tag visitor was registered.
*/
public function register( string $id, callable $tag_visitor_callback ): void {
public function register( string $id, callable $tag_visitor_callback ): bool {
if ( $this->is_finalized ) {
_doing_it_wrong( __METHOD__, esc_html( $this->get_finalized_message() ), 'optimization-detective 1.0.0' );
return false;
}
$this->visitors[ $id ] = $tag_visitor_callback;
return true;
}

/**
Expand Down Expand Up @@ -77,11 +102,16 @@ public function get_registered( string $id ): ?callable {
* Unregisters a tag visitor.
*
* @since 0.3.0
* @since n.e.x.t Returns false if the registry is finalized.
*
* @param non-empty-string $id Identifier for the tag visitor.
* @return bool Whether a tag visitor was unregistered.
*/
public function unregister( string $id ): bool {
if ( $this->is_finalized ) {
_doing_it_wrong( __METHOD__, esc_html( $this->get_finalized_message() ), 'optimization-detective 1.0.0' );
return false;
}
if ( ! $this->is_registered( $id ) ) {
return false;
}
Expand Down Expand Up @@ -110,4 +140,19 @@ public function getIterator(): ArrayIterator {
public function count(): int {
return count( $this->visitors );
}

/**
* Gets the finalized message when attempting to mutate the registry after the od_register_tag_visitors action.
*
* @since n.e.x.t
*
* @return string Message.
*/
private function get_finalized_message(): string {
return sprintf(
/* translators: %s is the od_register_tag_visitors action */
__( 'The tag visitor registry has already been finalized. This method must be called during the %s action.', 'optimization-detective' ),
'od_register_tag_visitors'
);
}
}
8 changes: 6 additions & 2 deletions plugins/optimization-detective/optimization.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,17 +153,21 @@ function od_add_template_output_buffer_filter( $template ) {
*/
do_action( 'od_register_tag_visitors', $tag_visitor_registry );

// Prevent modification of the tag visitor registry since doing so would invalidate the etag.
$tag_visitor_registry->finalize();

global $wp_the_query;
$current_theme_template = od_get_current_theme_template( is_string( $template ) ? $template : null );
$current_etag = od_get_current_url_metrics_etag( $tag_visitor_registry, $wp_the_query, $current_theme_template );
$group_collection = new OD_URL_Metric_Group_Collection(
$post instanceof WP_Post ? OD_URL_Metrics_Post_Type::get_url_metrics_from_post( $post ) : array(),
$current_etag,
// TODO: Add the following values to the context as well.
od_get_breakpoint_max_widths(),
od_get_url_metrics_breakpoint_sample_size(),
od_get_url_metric_freshness_ttl()
);
$link_collection = new OD_Link_Collection();
); // TODO: Also finalize the collection?
$link_collection = new OD_Link_Collection();

$context = new OD_Template_Optimization_Context(
$group_collection,
Expand Down