diff --git a/inc/Engine/Common/PerformanceHints/Frontend/Processor.php b/inc/Engine/Common/PerformanceHints/Frontend/Processor.php index 7988bdc3bf..b48704c50e 100644 --- a/inc/Engine/Common/PerformanceHints/Frontend/Processor.php +++ b/inc/Engine/Common/PerformanceHints/Frontend/Processor.php @@ -191,8 +191,14 @@ private function inject_beacon( $html, $url, $is_mobile ): string { // Create the script tag. $script_tag = ""; // phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedScript $last_body_tag_position = strrpos( $html, '
+' ); - // 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 . '', $last_body_tag_position, 7 ); + + if ( false !== $last_body_tag_position ) { + // 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 . '', $last_body_tag_position, 7 ); + } else { + // Append to the end of html if is not found. + $html .= $inline_script . $script_tag; + } return $html; } diff --git a/tests/Fixtures/inc/Engine/Common/PerformanceHints/Frontend/Subscriber/HTML/no_closing_body_tag_input.html b/tests/Fixtures/inc/Engine/Common/PerformanceHints/Frontend/Subscriber/HTML/no_closing_body_tag_input.html new file mode 100644 index 0000000000..ae1390a63d --- /dev/null +++ b/tests/Fixtures/inc/Engine/Common/PerformanceHints/Frontend/Subscriber/HTML/no_closing_body_tag_input.html @@ -0,0 +1,6 @@ + +
+
+ +