Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #6960 Beacon script can be injected multiple times when there are multiple </body> in the page. #7231

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
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,
],
],
];
Loading