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
Add link collection to context
  • Loading branch information
westonruter committed Mar 13, 2025
commit 51feaad38010ae0113adeb0a782c0b4ed4328c66
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
* @property-read array<string, mixed> $normalized_query_vars Normalized query vars.
* @property-read non-empty-string $url_metrics_slug Slug for the od_url_metrics post.
* @property-read non-empty-string $current_etag Current ETag.
* @property-read OD_Link_Collection $link_collection Link collection.
*/
final class OD_Template_Optimization_Context {

Expand Down Expand Up @@ -76,6 +77,14 @@ final class OD_Template_Optimization_Context {
*/
private $current_etag;

/**
* Link collection.
*
* @since n.e.x.t
* @var OD_Link_Collection
*/
private $link_collection;

/**
* Constructor.
*
Expand All @@ -87,14 +96,16 @@ final class OD_Template_Optimization_Context {
* @param array<string, mixed> $normalized_query_vars Normalized query vars.
* @param non-empty-string $url_metrics_slug Slug for the od_url_metrics post.
* @param non-empty-string $current_etag Current ETag.
* @param OD_Link_Collection $link_collection Link collection.
*/
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 ) {
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 ) {
$this->url_metric_group_collection = $url_metric_group_collection;
$this->tag_visitor_registry = $tag_visitor_registry;
$this->url_metrics_id = $url_metrics_id;
$this->normalized_query_vars = $normalized_query_vars;
$this->url_metrics_slug = $url_metrics_slug;
$this->current_etag = $current_etag;
$this->link_collection = $link_collection;
}

/**
Expand All @@ -121,6 +132,8 @@ public function __get( string $name ) {
return $this->url_metrics_slug;
case 'current_etag':
return $this->current_etag;
case 'link_collection':
return $this->link_collection;
default:
throw new Error(
esc_html(
Expand Down
11 changes: 7 additions & 4 deletions plugins/optimization-detective/optimization.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ function od_add_template_output_buffer_filter( $template ) {
od_get_url_metrics_breakpoint_sample_size(),
od_get_url_metric_freshness_ttl()
);
$link_collection = new OD_Link_Collection();

/**
* Fires when Optimization Detective is initialized to optimize the current response.
Expand All @@ -179,15 +180,17 @@ function od_add_template_output_buffer_filter( $template ) {
$post_id,
$query_vars,
$slug,
$current_etag
$current_etag,
$link_collection
)
);

$callback = static function ( string $buffer ) use ( $tag_visitor_registry, $group_collection, $slug, $post_id ): string {
$callback = static function ( string $buffer ) use ( $tag_visitor_registry, $group_collection, $link_collection, $slug, $post_id ): string {
return od_optimize_template_output_buffer(
$buffer,
$tag_visitor_registry,
$group_collection,
$link_collection,
$slug,
$post_id
);
Expand Down Expand Up @@ -304,11 +307,12 @@ function od_is_response_html_content_type(): bool {
* @param string $buffer Template output buffer.
* @param OD_Tag_Visitor_Registry $tag_visitor_registry Tag visitor registry.
* @param OD_URL_Metric_Group_Collection $group_collection URL Metric group collection.
* @param OD_Link_Collection $link_collection Link collection.
* @param non-empty-string $slug Slug.
* @param positive-int|null $post_id The ID for the od_url_metric post if it exists.
* @return string Filtered template output buffer.
*/
function od_optimize_template_output_buffer( string $buffer, OD_Tag_Visitor_Registry $tag_visitor_registry, OD_URL_Metric_Group_Collection $group_collection, string $slug, ?int $post_id ): string {
function od_optimize_template_output_buffer( string $buffer, OD_Tag_Visitor_Registry $tag_visitor_registry, OD_URL_Metric_Group_Collection $group_collection, OD_Link_Collection $link_collection, string $slug, ?int $post_id ): string {

// If the content-type is not HTML or the output does not start with '<', then abort since the buffer is definitely not HTML.
if (
Expand All @@ -328,7 +332,6 @@ function od_optimize_template_output_buffer( string $buffer, OD_Tag_Visitor_Regi
return $buffer;
}

$link_collection = new OD_Link_Collection();
$visited_tag_state = new OD_Visited_Tag_State();
$tag_visitor_context = new OD_Tag_Visitor_Context(
$processor,
Expand Down