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 1 commit
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
9 changes: 7 additions & 2 deletions inc/Engine/Common/PerformanceHints/Frontend/Processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,14 @@ private function inject_beacon( $html, $url, $is_mobile ): string {

// Create the script tag.
$script_tag = "<script data-name=\"wpr-wpr-beacon\" src='{$script_url}' async></script>"; // phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedScript
$body_tag = strrpos( $html, '</body>' );

// Append the script tag just before the closing body tag.
return preg_replace( '/<\/body>/', $inline_script . $script_tag . '</body>', $html, 1 );
if ( false !== $body_tag ) {
jeawhanlee marked this conversation as resolved.
Show resolved Hide resolved
// 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>', $body_tag, 7 );
}

return $html;
}

/**
Expand Down
Khadreal marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,20 @@
<title>Test</title>
</head>
<body>
</body>
<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
Expand Up @@ -3,7 +3,20 @@
<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>
<body>
</body>
</html>
Loading