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 #7265 fatal error with 3.18.1.2 #7267

Merged
merged 6 commits into from
Jan 31, 2025
Merged
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
10 changes: 8 additions & 2 deletions inc/Engine/Common/PerformanceHints/Frontend/Processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,14 @@
// Create the script tag.
$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 );

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 . '</body>', $last_body_tag_position, 7 );
} else {

Check notice on line 198 in inc/Engine/Common/PerformanceHints/Frontend/Processor.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

inc/Engine/Common/PerformanceHints/Frontend/Processor.php#L198

The method inject_beacon uses an else expression. Else clauses are basically not necessary and you can simplify the code by not using them.
// Append to the end of html if </body> is not found.
$html .= $inline_script . $script_tag;
}

return $html;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<html>
<head>
<title>Test</title>
</head>
<body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<html>
<head>
<title>Test</title>
</head>
<body>
</html>
<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>
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_input_without_closing_body_tag = file_get_contents(__DIR__ . '/HTML/no_closing_body_tag_input.html');
$html_input_without_closing_body_tag_output = file_get_contents(__DIR__ . '/HTML/no_closing_body_tag_output.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');
Expand Down Expand Up @@ -629,5 +631,17 @@
],
'expected' => $html_with_double_body_output,
],
'shouldAddBeaconWithoutClosingBodyTag' => [
'config' => [
'html' => $html_input_without_closing_body_tag,
'atf' => [
'row' => null,
],
'lrc' => [
'row' => null,
],
],
'expected' => $html_input_without_closing_body_tag_output,
],
],
];
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ public function testShouldReturnAsExpected( $config, $expected ) {
add_filter( 'pre_get_rocket_option_cache_logged_user', [ $this, 'get_cache_user' ] );

$this->assertSame(
$expected,
apply_filters( 'rocket_buffer', $config['html'] )
trim($expected),
trim(apply_filters( 'rocket_buffer', $config['html'] ))
);
}

Expand Down
Loading