Skip to content

Commit

Permalink
Fix #6960 Beacon script can be injected multiple times when there are…
Browse files Browse the repository at this point in the history
… multiple </body> in the page. (#7231)

Co-authored-by: Michael Lee <[email protected]>
  • Loading branch information
Khadreal and jeawhanlee authored Jan 27, 2025
1 parent 83f80bb commit 426d736
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 3 deletions.
8 changes: 5 additions & 3 deletions inc/Engine/Common/PerformanceHints/Frontend/Processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,10 +189,12 @@ private function inject_beacon( $html, $url, $is_mobile ): string {
$script_url = rocket_get_constant( 'WP_ROCKET_ASSETS_JS_URL' ) . 'wpr-beacon' . $min . '.js';

// Create the script tag.
$script_tag = "<script data-name=\"wpr-wpr-beacon\" src='{$script_url}' async></script>"; // phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedScript
$script_tag = "<script data-name=\"wpr-wpr-beacon\" src='{$script_url}' async></script>"; // phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedScript
$last_body_tag_position = strrpos( $html, '</body>' );
// Append the script tag just before the last closing body tag especially in cases where there's an iframe.
$html = substr_replace( $html, $inline_script . $script_tag . '</body>', $last_body_tag_position, 7 );

// Append the script tag just before the closing body tag.
return str_replace( '</body>', $inline_script . $script_tag . '</body>', $html );
return $html;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<html>
<head>
<title>Test</title>
</head>
<body>
<iframe srcdoc="
<!DOCTYPE html>
<html lang='en'>
<head>
<meta charset='UTF-8'>
<meta name='viewport' content='width=device-width, initial-scale=1.0'>
<title>Iframe Content</title>
</head>
<body>
<header>
<h1>Iframe Header</h1>
</header>
</body>
</html>
" width="600" height="400" style="border:none;"></iframe>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<html>
<head>
<title>Test</title>
</head>
<body>
<iframe srcdoc="
<!DOCTYPE html>
<html lang='en'>
<head>
<meta charset='UTF-8'>
<meta name='viewport' content='width=device-width, initial-scale=1.0'>
<title>Iframe Content</title>
</head>
<body>
<header>
<h1>Iframe Header</h1>
</header>
</body>
</html>
" width="600" height="400" style="border:none;"></iframe>
<script>var rocket_beacon_data = {"ajax_url":"http:\/\/example.org\/wp-admin\/admin-ajax.php","nonce":"96ac96b69e","url":"http:\/\/example.org","is_mobile":false,"width_threshold":1600,"height_threshold":700,"delay":500,"debug":false,"status":{"atf":true,"lrc":true},"elements":"img, video, picture, p, main, div, li, svg, section, header, span","lrc_threshold":1800}</script><script data-name="wpr-wpr-beacon" src='http://example.org/wp-content/plugins/wp-rocket/assets/js/wpr-beacon.min.js' async></script></body>
</html>
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php

$html_input = file_get_contents(__DIR__ . '/HTML/input.html');
$html_with_double_body = file_get_contents(__DIR__ . '/HTML/double_body_tag.html');
$html_with_double_body_output = file_get_contents(__DIR__ . '/HTML/output_double_body_tag.html');
$html_output = file_get_contents(__DIR__ . '/HTML/output.html');
$html_output_with_preload = file_get_contents(__DIR__ . '/HTML/output_w_preload.html');
$html_output_with_beacon = file_get_contents(__DIR__ . '/HTML/output_w_beacon.html');
Expand Down Expand Up @@ -615,5 +617,17 @@
],
'expected' => $html_output_with_beacon_and_only_lrc_opt,
],
'shouldNotDuplicateBeaconOnAPage' => [
'config' => [
'html' => $html_with_double_body,
'atf' => [
'row' => null,
],
'lrc' => [
'row' => null,
],
],
'expected' => $html_with_double_body_output,
],
],
];

0 comments on commit 426d736

Please sign in to comment.