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 tests and fix bug with adding links at finish action
  • Loading branch information
westonruter committed Mar 14, 2025
commit 38a4c6b61aef92a0a0f450df4486a9eed7cd92d7
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
* @since n.e.x.t
*
* @property-read OD_URL_Metric_Group_Collection $url_metric_group_collection URL Metric group collection.
* @property-read OD_Tag_Visitor_Registry $tag_visitor_registry Tag visitor registry.
* @property-read positive-int|null $url_metrics_id ID for the od_url_metrics post which provided the URL Metrics in the collection.
* @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.
Expand Down
19 changes: 10 additions & 9 deletions plugins/optimization-detective/optimization.php
Original file line number Diff line number Diff line change
Expand Up @@ -349,15 +349,6 @@ function od_optimize_template_output_buffer( string $buffer ): string {
$visited_tag_state->reset();
} while ( $processor->next_tag( array( 'tag_closers' => 'skip' ) ) );

// Send any preload links in a Link response header and in a LINK tag injected at the end of the HEAD.
if ( count( $link_collection ) > 0 ) {
$response_header_links = $link_collection->get_response_header();
if ( ! is_null( $response_header_links ) && ! headers_sent() ) {
header( $response_header_links, false );
}
$processor->append_head_html( $link_collection->get_html() );
}

// Inject detection script.
// TODO: When optimizing above, if we find that there is a stored LCP element but it fails to match, it should perhaps set $needs_detection to true and send the request with an override nonce. However, this would require backtracking and adding the data-od-xpath attributes.
if ( $needs_detection ) {
Expand All @@ -373,5 +364,15 @@ function od_optimize_template_output_buffer( string $buffer ): string {
*/
do_action( 'od_finish_template_optimization', $template_optimization_context );

// Send any preload links in a Link response header and in a LINK tag injected at the end of the HEAD.
// Additional links may have been added at the od_finish_template_optimization action, so this must come after.
if ( count( $link_collection ) > 0 ) {
$response_header_links = $link_collection->get_response_header();
if ( ! is_null( $response_header_links ) && ! headers_sent() ) {
header( $response_header_links, false );
}
$processor->append_head_html( $link_collection->get_html() );
}

return $processor->get_updated_html();
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

66 changes: 66 additions & 0 deletions plugins/optimization-detective/tests/test-optimization.php
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,10 @@ public function data_provider_test_od_optimize_template_output_buffer(): array {
* @covers OD_Visited_Tag_State::is_tag_tracked
* @covers OD_Visited_Tag_State::reset
* @covers OD_HTML_Tag_Processor::is_admin_bar
* @covers OD_Template_Optimization_Context::__construct
* @covers OD_Template_Optimization_Context::__get
* @covers OD_Template_Optimization_Context::append_head_html
* @covers OD_Template_Optimization_Context::append_body_html
*
* @dataProvider data_provider_test_od_optimize_template_output_buffer
*
Expand All @@ -385,6 +389,18 @@ public function data_provider_test_od_optimize_template_output_buffer(): array {
* @noinspection PhpDocMissingThrowsInspection
*/
public function test_od_optimize_template_output_buffer( string $directory ): void {
$this->assertSame( 0, did_action( 'od_register_tag_visitors' ) );
$this->assertSame( 0, did_action( 'od_start_template_optimization' ) );
$this->assertSame( 0, did_action( 'od_finish_template_optimization' ) );

$did_initialize = false;
add_action(
'od_register_tag_visitors',
static function () use ( &$did_initialize ): void {
$did_initialize = true;
}
);

add_action(
'od_register_tag_visitors',
function ( OD_Tag_Visitor_Registry $tag_visitor_registry ): void {
Expand Down Expand Up @@ -431,6 +447,56 @@ function ( OD_Tag_Visitor_Context $context ): void {
}
);

$template_optimization_context = null;
add_action(
'od_start_template_optimization',
function ( OD_Template_Optimization_Context $context ) use ( &$template_optimization_context ): void {
$this->assertInstanceOf( OD_URL_Metric_Group_Collection::class, $context->url_metric_group_collection );
$this->assertTrue( is_int( $context->url_metrics_id ) || is_null( $context->url_metrics_id ) );
$this->assertIsArray( $context->normalized_query_vars );
$this->assertIsString( $context->url_metrics_slug );
$this->assertSame( od_get_url_metrics_slug( $context->normalized_query_vars ), $context->url_metrics_slug );
if ( is_int( $context->url_metrics_id ) ) {
$post = get_post( $context->url_metrics_id );
$this->assertInstanceOf( WP_Post::class, $post );
$this->assertSame( $post->post_name, $context->url_metrics_slug );
}
$this->assertInstanceOf( OD_Link_Collection::class, $context->link_collection );

$error = null;
$value = '';
try {
$value = $context->__get( 'invalid_param' );
} catch ( Error $e ) {
$error = $e;
}
$this->assertInstanceOf( Error::class, $error );
$this->assertSame( '', $value );

$context->append_head_html( "<!-- Inserted at od_start_template_optimization action. -->\n" );

$template_optimization_context = $context;
}
);

add_action(
'od_finish_template_optimization',
function ( OD_Template_Optimization_Context $context ) use ( &$template_optimization_context ): void {
$this->assertSame( $template_optimization_context, $context );
$context->link_collection->add_link(
array(
'rel' => 'preconnect',
'href' => 'https://inserted-at-finish-template-optimization-action.example.net/',
)
);
$context->append_body_html( "<!-- Inserted at od_finish_template_optimization action. -->\n" );
}
);

$this->assert_snapshot_equals( $directory );

$this->assertSame( $did_initialize ? 1 : 0, did_action( 'od_register_tag_visitors' ) );
$this->assertSame( $did_initialize ? 1 : 0, did_action( 'od_start_template_optimization' ) );
$this->assertSame( $did_initialize ? 1 : 0, did_action( 'od_finish_template_optimization' ) );
}
}