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

Closes #6591: LCP/ATF data is malformed when no LCP is found by the beacon #6596

35 changes: 18 additions & 17 deletions assets/js/lcp-beacon.js
Original file line number Diff line number Diff line change
@@ -167,7 +167,10 @@ function main() {
});

if ( true === lcp_data_response.success ) {
console.log('Bailing out because data is already available');
if (rocket_lcp_data.debug) {
console.log('Bailing out because data is already available');
}

return;
}

@@ -178,7 +181,10 @@ function main() {
if ( ( rocket_lcp_data.is_mobile && ( screenWidth > rocket_lcp_data.width_threshold || screenHeight > rocket_lcp_data.height_threshold ) ) ||
( ! rocket_lcp_data.is_mobile && ( screenWidth < rocket_lcp_data.width_threshold || screenHeight < rocket_lcp_data.height_threshold ) ) )
{
console.log('Bailing out because screen size is not acceptable');
if (rocket_lcp_data.debug) {
console.log('Bailing out because screen size is not acceptable');
}

return;
}

@@ -193,7 +199,9 @@ function main() {
label: "lcp",
}];
} else {
console.log("No LCP candidate found.");
if (rocket_lcp_data.debug) {
console.log("No LCP candidate found.");
}
}

above_the_fold_images.forEach(({ element, elementInfo }) => {
@@ -206,14 +214,6 @@ function main() {

let performance_images_json = JSON.stringify(performance_images);
window.performance_images_json = performance_images_json;
const payload = {
action: 'rocket_lcp',
rocket_lcp_nonce: rocket_lcp_data.nonce,
url: rocket_lcp_data.url,
is_mobile: rocket_lcp_data.is_mobile,
images: performance_images_json,
status: 'success'
};

const data = new FormData();
data.append('action', 'rocket_lcp');
@@ -235,21 +235,22 @@ function main() {
.then((data) => {
const beaconscript = document.querySelector('[data-name="wpr-lcp-beacon"]');
beaconscript.setAttribute('beacon-completed', 'true');
console.log(data);

if (rocket_lcp_data.debug) {
console.log(data);
}
})
.catch((error) => {
console.error(error);
if (rocket_lcp_data.debug) {
console.error(error);
}
});
}

if (document.readyState !== 'loading') {
console.time("extract");
setTimeout(main, 500);
console.timeEnd("extract");
} else {
document.addEventListener("DOMContentLoaded", function () {
console.time("extract");
setTimeout(main, 500);
console.timeEnd("extract");
});
}
2 changes: 1 addition & 1 deletion assets/js/lcp-beacon.js.min.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion assets/js/lcp-beacon.min.js
14 changes: 7 additions & 7 deletions inc/Engine/Media/AboveTheFold/AJAX/Controller.php
Original file line number Diff line number Diff line change
@@ -54,12 +54,12 @@ public function add_lcp_data() {
$keys = [ 'bg_set', 'src' ];

foreach ( $images as $image ) {
if ( 'lcp' === $image->label && 'not found' === $lcp ) {
$lcp = $this->create_object( $image, $keys );
} elseif ( 'above-the-fold' === $image->label ) {
$viewport_image = $this->create_object( $image, $keys );
if ( null !== $viewport_image ) {
$viewport[] = $viewport_image;
if ( isset( $image->type ) ) {
$object = $this->create_object( $image, $keys );
if ( 'lcp' === $image->label && null !== $object ) {
$lcp = $object;
} elseif ( 'above-the-fold' === $image->label && null !== $object ) {
$viewport[] = $object;
}
}
}
@@ -75,7 +75,7 @@ public function add_lcp_data() {
'url' => $url,
'is_mobile' => $is_mobile,
'status' => 'completed',
'lcp' => wp_json_encode( $lcp ),
'lcp' => ( is_array( $lcp ) || is_object( $lcp ) ) ? wp_json_encode( $lcp ) : $lcp,
'viewport' => wp_json_encode( $viewport ),
'last_accessed' => current_time( 'mysql', true ),
];
1 change: 1 addition & 0 deletions inc/Engine/Media/AboveTheFold/Frontend/Controller.php
Original file line number Diff line number Diff line change
@@ -389,6 +389,7 @@ public function inject_beacon( $html, $url, $is_mobile ): string {
'elements' => $this->lcp_atf_elements(),
'width_threshold' => $width_threshold,
'height_threshold' => $height_threshold,
'debug' => rocket_get_constant( 'WP_ROCKET_DEBUG' ),
];

$inline_script = '<script>var rocket_lcp_data = ' . wp_json_encode( $data ) . '</script>';
Original file line number Diff line number Diff line change
@@ -783,14 +783,14 @@ function _isWebView_Android(options) {
<p>In just a bit here, you should see the text start to wrap below the right aligned image and settle in nicely. There should still be plenty of room and everything should be sitting pretty. Yeah&#8230; Just like that. It never felt so good to be right.</p>
<p>And that&rsquo;s a wrap, yo! You survived the tumultuous waters of alignment. Image alignment achievement unlocked!</p>
<figure class="post-thumbnail">
<img width="1568" height="1046" src="http://cdn.example.org/wp-content/uploads/2018/03/sticker-mule-189122-unsplash-1568x1046.jpg" class="attachment-post-thumbnail size-post-thumbnail wp-post-image" alt="" srcset="http://cdn.example.org/wp-content/uploads/2018/03/sticker-mule-189122-unsplash-1568x1046.jpg 1568w, http://cdn.example.org/wp-content/uploads/2018/03/sticker-mule-189122-unsplash-300x200.jpg 300w, http://cdn.example.org/wp-content/uploads/2018/03/sticker-mule-189122-unsplash-768x513.jpg 768w, http://cdn.example.org/wp-content/uploads/2018/03/sticker-mule-189122-unsplash-1024x683.jpg 1024w" sizes="(max-width: 1568px) 100vw, 1568px" />
<img width="1568" height="1046" src="http://cdn.example.org/wp-content/uploads/2018/03/sticker-mule-189122-unsplash-1568x1046.jpg" class="attachment-post-thumbnail size-post-thumbnail wp-post-image" alt="" srcset="http://example.org/wp-content/uploads/2018/03/sticker-mule-189122-unsplash-1568x1046.jpg 1568w, http://example.org/wp-content/uploads/2018/03/sticker-mule-189122-unsplash-300x200.jpg 300w, http://example.org/wp-content/uploads/2018/03/sticker-mule-189122-unsplash-768x513.jpg 768w, http://example.org/wp-content/uploads/2018/03/sticker-mule-189122-unsplash-1024x683.jpg 1024w" sizes="(max-width: 1568px) 100vw, 1568px" />
</figure>
<figure class="post-thumbnail">
<img width="1568" height="1046" src="http://cdn.example.org/wp-content/uploads/2018/03/sticker-mule-189122-unsplash-1568x1046.jpg" class="attachment-post-thumbnail size-post-thumbnail wp-post-image" alt="" srcset="http://cdn.example.org/wp-content/uploads/2018/03/sticker-mule-189122-unsplash-1568x1046.jpg 1568w, http://cdn.example.org/wp-content/uploads/2018/03/sticker-mule-189122-unsplash-300x200.jpg 300w, http://cdn.example.org/wp-content/uploads/2018/03/sticker-mule-189122-unsplash-768x513.jpg 768w, http://cdn.example.org/wp-content/uploads/2018/03/sticker-mule-189122-unsplash-1024x683.jpg 1024w, http://cdn.example.org/wp-content/uploads/2018/03/sticker-mule-189122-unsplash-1568x1046.jpg 1568w" sizes="(max-width: 1568px) 100vw, 1568px" />
<img width="1568" height="1046" src="http://cdn.example.org/wp-content/uploads/2018/03/sticker-mule-189122-unsplash-1568x1046.jpg" class="attachment-post-thumbnail size-post-thumbnail wp-post-image" alt="" srcset="http://example.org/wp-content/uploads/2018/03/sticker-mule-189122-unsplash-1568x1046.jpg 1568w, http://example.org/wp-content/uploads/2018/03/sticker-mule-189122-unsplash-300x200.jpg 300w, http://example.org/wp-content/uploads/2018/03/sticker-mule-189122-unsplash-768x513.jpg 768w, http://example.org/wp-content/uploads/2018/03/sticker-mule-189122-unsplash-1024x683.jpg 1024w, http://example.org/wp-content/uploads/2018/03/sticker-mule-189122-unsplash-1568x1046.jpg 1568w" sizes="(max-width: 1568px) 100vw, 1568px" />
</figure>
<picture>
<source srcset="http://cdn.example.org/images/site/logo/logo-acommeassure.webp" type="image/webp">
<source srcset="http://cdn.example.org/images/site/logo/logo-acommeassure.png 100w" type="image/png">
<source srcset="/images/site/logo/logo-acommeassure.png 100w" type="image/png">
<img
width="300"
height="60"
@@ -1033,7 +1033,5 @@ class="logoSite show-for-large"
<script type='text/javascript' src='http://cdn.example.org/wp-content/themes/storefront/assets/js/vendor/pep.min.js?ver=0.4.3'></script>
<script type='text/javascript' src='http://cdn.example.org/wp-includes/js/wp-embed.min.js?ver=5.2.2'></script>
<script src="http://cdn.example.org/wp-content/plugins/test/script.js"></script>
<script>var rocket_lcp_data = {"ajax_url":"http:\/\/example.org\/wp-admin\/admin-ajax.php","nonce":"<?php echo wp_create_nonce( 'rocket_lcp' ) ?>","url":"http:\/\/example.org","is_mobile":false,"elements":"img, video, picture, p, main, div, li, svg","width_threshold":1600,"height_threshold":700}</script>
<script data-name="wpr-lcp-beacon" src='http://cdn.example.org/wp-content/plugins/wp-rocket/assets/js/lcp-beacon.min.js' async></script>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -718,8 +718,5 @@
<script type='text/javascript' src='http://cdn.example.org/blog/wp-includes/js/comment-reply.min.js?ver=5.2.2'></script>
<script type='text/javascript' src='http://cdn.example.org/blog/wp-content/themes/storefront/assets/js/vendor/pep.min.js?ver=0.4.3'></script>
<script type='text/javascript' src='http://cdn.example.org/blog/wp-includes/js/wp-embed.min.js?ver=5.2.2'></script>
<script>var rocket_lcp_data = {"ajax_url":"http:\/\/example.org\/wp-admin\/admin-ajax.php","nonce":"<?php echo wp_create_nonce( 'rocket_lcp' ) ?>","url":"http:\/\/example.org\/blog","is_mobile":false,"elements":"img, video, picture, p, main, div, li, svg","width_threshold":1600,"height_threshold":700}</script>
<script data-name="wpr-lcp-beacon" src='http://example.org/wp-content/plugins/wp-rocket/assets/js/lcp-beacon.min.js' async></script>

</body>
</html>
11 changes: 2 additions & 9 deletions tests/Fixtures/inc/Engine/CDN/Subscriber/rewrite.php
Original file line number Diff line number Diff line change
@@ -1,21 +1,14 @@
<?php
ob_start(); // Start output buffering
require 'HTML/siteURL/rewrite.php';; // Execute the PHP code in example.php
$default_site_url_expected = ob_get_clean();

ob_start();
require 'HTML/siteURLWithPath/rewrite.php';
$default_site_url_with_path_expected = ob_get_clean();

return [
'testDefaultSiteURL' => [
'site_url' => 'http://example.org',
'original' => file_get_contents( WP_ROCKET_TESTS_FIXTURES_DIR . '/inc/Engine/CDN/Subscriber/HTML/siteURL/original.html' ),
'expected' => $default_site_url_expected,
'expected' => file_get_contents( WP_ROCKET_TESTS_FIXTURES_DIR . '/inc/Engine/CDN/Subscriber/HTML/siteURL/expected.html' ),
],
'testSiteURLWithPath' => [
'site_url' => 'http://example.org/blog',
'original' => file_get_contents( WP_ROCKET_TESTS_FIXTURES_DIR . '/inc/Engine/CDN/Subscriber/HTML/siteURLWithPath/original.html' ),
'expected' => $default_site_url_with_path_expected,
'expected' => file_get_contents( WP_ROCKET_TESTS_FIXTURES_DIR . '/inc/Engine/CDN/Subscriber/HTML/siteURLWithPath/expected.html' ),
],
];
Original file line number Diff line number Diff line change
@@ -3,5 +3,5 @@
<title>Test</title>
</head>
<body>
<script>var rocket_lcp_data = {"ajax_url":"http:\/\/example.org\/wp-admin\/admin-ajax.php","nonce":"96ac96b69e","url":"http:\/\/example.org","is_mobile":false,"elements":"img, video, picture, p, main, div, li, svg","width_threshold":1600,"height_threshold":700}</script><script data-name="wpr-lcp-beacon" src='http://example.org/wp-content/plugins/wp-rocket/assets/js/lcp-beacon.min.js' async></script></body>
<script>var rocket_lcp_data = {"ajax_url":"http:\/\/example.org\/wp-admin\/admin-ajax.php","nonce":"96ac96b69e","url":"http:\/\/example.org","is_mobile":false,"elements":"img, video, picture, p, main, div, li, svg","width_threshold":1600,"height_threshold":700,"debug":false}</script><script data-name="wpr-lcp-beacon" src='http://example.org/wp-content/plugins/wp-rocket/assets/js/lcp-beacon.min.js' async></script></body>
</html>
Original file line number Diff line number Diff line change
@@ -240,4 +240,17 @@
],
'expected' => $html_output_with_img_lcp,
],
'shouldNotDoAnythingIfNoLcp' => [
'config' => [
'html' => $html_input,
'row' => [
'status' => 'completed',
'url' => 'http://example.org',
'lcp' => 'not found',
'viewport' => json_encode( [
] ),
],
],
'expected' => $html_output,
],
];
Original file line number Diff line number Diff line change
@@ -38,8 +38,6 @@
<link data-minify="1" rel="stylesheet" href="http://example.org/wp-content/cache/min/1/font-awesome/4.7.0/css/font-awesome.min.css?ver={{mtime}}">
</head>
<body>
<script>var rocket_lcp_data = {"ajax_url":"http:\/\/example.org\/wp-admin\/admin-ajax.php","nonce":"'.wp_create_nonce( 'rocket_lcp' ).'","url":"http:\/\/example.org","is_mobile":false,"elements":"img, video, picture, p, main, div, li, svg","width_threshold":1600,"height_threshold":700}</script>
<script data-name="wpr-lcp-beacon" src=\'http://example.org/wp-content/plugins/wp-rocket/assets/js/lcp-beacon.min.js\' async></script>
</body>
</html>',
'files' => [
@@ -84,8 +82,6 @@
<link data-minify="1" rel="stylesheet" href="http://example.org/wp-content/cache/min/1/ajax/libs/font-awesome/5.15.2/css/fontawesome.css?ver={{mtime}}">
</head>
<body>
<script>var rocket_lcp_data = {"ajax_url":"http:\/\/example.org\/wp-admin\/admin-ajax.php","nonce":"'.wp_create_nonce( 'rocket_lcp' ).'","url":"http:\/\/example.org","is_mobile":false,"elements":"img, video, picture, p, main, div, li, svg","width_threshold":1600,"height_threshold":700}</script>
<script data-name="wpr-lcp-beacon" src=\'http://example.org/wp-content/plugins/wp-rocket/assets/js/lcp-beacon.min.js\' async></script>
</body>
</html>',
'files' => [
@@ -128,8 +124,6 @@
<link data-minify="1" rel="stylesheet" href="http://example.org/wp-content/cache/min/1/font-awesome/4.7.0/css/font-awesome.min.css?ver={{mtime}}">
</head>
<body>
<script>var rocket_lcp_data = {"ajax_url":"http:\/\/example.org\/wp-admin\/admin-ajax.php","nonce":"'.wp_create_nonce( 'rocket_lcp' ).'","url":"http:\/\/example.org","is_mobile":false,"elements":"img, video, picture, p, main, div, li, svg","width_threshold":1600,"height_threshold":700}</script>
<script data-name="wpr-lcp-beacon" src=\'http://example.org/wp-content/plugins/wp-rocket/assets/js/lcp-beacon.min.js\' async></script>
</body>
</html>',
'files' => [
@@ -170,13 +164,11 @@
<title>Sample Page</title>
<link data-minify="1" rel="stylesheet" href="https://123456.rocketcdn.me/wp-content/cache/min/1/wp-content/themes/twentytwenty/style.css?ver={{mtime}}" type="text/css" media="all">
<link data-minify="1" rel="stylesheet" href="https://123456.rocketcdn.me/wp-content/cache/min/1/wp-content/plugins/hello-dolly/style.css?ver={{mtime}}">
<link rel="stylesheet" href="https://123456.rocketcdn.me/wp-includes/css/dashicons.min.css">
<link rel="stylesheet" href="http://example.org/wp-includes/css/dashicons.min.css">
<link data-minify="1" rel="stylesheet" href="https://123456.rocketcdn.me/wp-content/cache/min/1/wp-content/themes/twentytwenty/style-font-face.min.css?ver={{mtime}}">
</head>
<body>
<script>var rocket_lcp_data = {"ajax_url":"http:\/\/example.org\/wp-admin\/admin-ajax.php","nonce":"'.wp_create_nonce( 'rocket_lcp' ).'","url":"http:\/\/example.org","is_mobile":false,"elements":"img, video, picture, p, main, div, li, svg","width_threshold":1600,"height_threshold":700}</script>
<script data-name="wpr-lcp-beacon" src=\'https://123456.rocketcdn.me/wp-content/plugins/wp-rocket/assets/js/lcp-beacon.min.js\' async></script>
</body>
</html>',
'files' => [
@@ -224,8 +216,6 @@
</head>
<body>
<script>var rocket_lcp_data = {"ajax_url":"http:\/\/example.org\/wp-admin\/admin-ajax.php","nonce":"'.wp_create_nonce( 'rocket_lcp' ).'","url":"http:\/\/example.org","is_mobile":false,"elements":"img, video, picture, p, main, div, li, svg","width_threshold":1600,"height_threshold":700}</script>
<script data-name="wpr-lcp-beacon" src=\'https://123456.rocketcdn.me/wp-content/plugins/wp-rocket/assets/js/lcp-beacon.min.js\' async></script>
</body>
</html>',
'files' => [
@@ -270,8 +260,6 @@
</head>
<body>
<script>var rocket_lcp_data = {"ajax_url":"http:\/\/example.org\/wp-admin\/admin-ajax.php","nonce":"'.wp_create_nonce( 'rocket_lcp' ).'","url":"http:\/\/example.org","is_mobile":false,"elements":"img, video, picture, p, main, div, li, svg","width_threshold":1600,"height_threshold":700}</script>
<script data-name="wpr-lcp-beacon" src=\'https://123456.rocketcdn.me/cdnpath/wp-content/plugins/wp-rocket/assets/js/lcp-beacon.min.js\' async></script>
</body>
</html>',
'files' => [
Original file line number Diff line number Diff line change
@@ -43,8 +43,6 @@
<script data-minify="1" type="text/javascript" src="http://example.org/wp-content/cache/min/1/ajax/libs/twitter-bootstrap/4.5.0/js/bootstrap.js?ver={{mtime}}"></script>
</head>
<body>
<script>var rocket_lcp_data = {"ajax_url":"http:\/\/example.org\/wp-admin\/admin-ajax.php","nonce":"'.wp_create_nonce( 'rocket_lcp' ).'","url":"http:\/\/example.org","is_mobile":false,"elements":"img, video, picture, p, main, div, li, svg","width_threshold":1600,"height_threshold":700}</script>
<script data-name="wpr-lcp-beacon" src=\'http://example.org/wp-content/plugins/wp-rocket/assets/js/lcp-beacon.min.js\' async></script>
</body>
</html>',
'files' => [
@@ -88,8 +86,6 @@
<script data-minify="1" type="text/javascript" src="http://example.org/wp-content/cache/min/1/ajax/libs/twitter-bootstrap/4.5.1/js/bootstrap.js?ver={{mtime}}"></script>
</head>
<body>
<script>var rocket_lcp_data = {"ajax_url":"http:\/\/example.org\/wp-admin\/admin-ajax.php","nonce":"'.wp_create_nonce( 'rocket_lcp' ).'","url":"http:\/\/example.org","is_mobile":false,"elements":"img, video, picture, p, main, div, li, svg","width_threshold":1600,"height_threshold":700}</script>
<script data-name="wpr-lcp-beacon" src=\'http://example.org/wp-content/plugins/wp-rocket/assets/js/lcp-beacon.min.js\' async></script>
</body>
</html>',
'files' => [
@@ -125,8 +121,6 @@
<script type="text/javascript" src="https://cse.google.com/cse.js?cx=xxx:xxx"></script>
</head>
<body>
<script>var rocket_lcp_data = {"ajax_url":"http:\/\/example.org\/wp-admin\/admin-ajax.php","nonce":"'.wp_create_nonce( 'rocket_lcp' ).'","url":"http:\/\/example.org","is_mobile":false,"elements":"img, video, picture, p, main, div, li, svg","width_threshold":1600,"height_threshold":700}</script>
<script data-name="wpr-lcp-beacon" src=\'http://example.org/wp-content/plugins/wp-rocket/assets/js/lcp-beacon.min.js\' async></script>
</body>
</html>',
'files' => [],
@@ -157,11 +151,9 @@
<title>Sample Page</title>
<script data-minify="1" type="text/javascript" src="https://123456.rocketcdn.me/wp-content/cache/min/1/wp-content/themes/twentytwenty/assets/script.js?ver={{mtime}}"></script>
<script data-minify="1" type="text/javascript" src="https://123456.rocketcdn.me/wp-content/cache/min/1/wp-content/plugins/hello-dolly/script.js?ver={{mtime}}"></script>
<script type="text/javascript" src="https://123456.rocketcdn.me' . $jquery_path . '"></script>
<script type="text/javascript" src="http://example.org' . $jquery_path . '"></script>
</head>
<body>
<script>var rocket_lcp_data = {"ajax_url":"http:\/\/example.org\/wp-admin\/admin-ajax.php","nonce":"'.wp_create_nonce( 'rocket_lcp' ).'","url":"http:\/\/example.org","is_mobile":false,"elements":"img, video, picture, p, main, div, li, svg","width_threshold":1600,"height_threshold":700}</script>
<script data-name="wpr-lcp-beacon" src=\'https://123456.rocketcdn.me/wp-content/plugins/wp-rocket/assets/js/lcp-beacon.min.js\' async></script>
</body>
</html>',
'files' => [
@@ -201,8 +193,6 @@
<script type="text/javascript" src="https://123456.rocketcdn.me' . $jquery_path . '"></script>
</head>
<body>
<script>var rocket_lcp_data = {"ajax_url":"http:\/\/example.org\/wp-admin\/admin-ajax.php","nonce":"'.wp_create_nonce( 'rocket_lcp' ).'","url":"http:\/\/example.org","is_mobile":false,"elements":"img, video, picture, p, main, div, li, svg","width_threshold":1600,"height_threshold":700}</script>
<script data-name="wpr-lcp-beacon" src=\'https://123456.rocketcdn.me/wp-content/plugins/wp-rocket/assets/js/lcp-beacon.min.js\' async></script>
</body>
</html>',
'files' => [
@@ -238,11 +228,9 @@
<title>Sample Page</title>
<script data-minify="1" type="text/javascript" src="https://123456.rocketcdn.me/cdnpath/wp-content/cache/min/1/wp-content/themes/twentytwenty/assets/script.js?ver={{mtime}}"></script>
<script data-minify="1" type="text/javascript" src="https://123456.rocketcdn.me/cdnpath/wp-content/cache/min/1/wp-content/plugins/hello-dolly/script.js?ver={{mtime}}"></script>
<script type="text/javascript" src="https://123456.rocketcdn.me/cdnpath' . $jquery_path . '"></script>
<script type="text/javascript" src="http://example.org' . $jquery_path . '"></script>
</head>
<body>
<script>var rocket_lcp_data = {"ajax_url":"http:\/\/example.org\/wp-admin\/admin-ajax.php","nonce":"'.wp_create_nonce( 'rocket_lcp' ).'","url":"http:\/\/example.org","is_mobile":false,"elements":"img, video, picture, p, main, div, li, svg","width_threshold":1600,"height_threshold":700}</script>
<script data-name="wpr-lcp-beacon" src=\'https://123456.rocketcdn.me/cdnpath/wp-content/plugins/wp-rocket/assets/js/lcp-beacon.min.js\' async></script>
</body>
</html>',
'files' => [
@@ -287,9 +275,7 @@
</script>
</head>
<body>
<script>var rocket_lcp_data = {"ajax_url":"http:\/\/example.org\/wp-admin\/admin-ajax.php","nonce":"'.wp_create_nonce( 'rocket_lcp' ).'","url":"http:\/\/example.org","is_mobile":false,"elements":"img, video, picture, p, main, div, li, svg","width_threshold":1600,"height_threshold":700}</script>
<script data-name="wpr-lcp-beacon" src=\'http://example.org/wp-content/plugins/wp-rocket/assets/js/lcp-beacon.min.js\' async></script>
<script src="http://example.org/wp-content/cache/min/1/f819bcaed244d53d3b4ffc4c5cc0efdc.js" data-minify="1"></script>
<script src="http://example.org/wp-content/cache/min/1/f819bcaed244d53d3b4ffc4c5cc0efdc.js" data-minify="1"></script>
</body>
</html>',
'files' => [
@@ -333,9 +319,7 @@
</script>
</head>
<body>
<script>var rocket_lcp_data = {"ajax_url":"http:\/\/example.org\/wp-admin\/admin-ajax.php","nonce":"'.wp_create_nonce( 'rocket_lcp' ).'","url":"http:\/\/example.org","is_mobile":false,"elements":"img, video, picture, p, main, div, li, svg","width_threshold":1600,"height_threshold":700}</script>
<script data-name="wpr-lcp-beacon" src=\'https://123456.rocketcdn.me/wp-content/plugins/wp-rocket/assets/js/lcp-beacon.min.js\' async></script>
<script src="https://123456.rocketcdn.me/wp-content/cache/min/1/f819bcaed244d53d3b4ffc4c5cc0efdc.js" data-minify="1"></script>
<script src="https://123456.rocketcdn.me/wp-content/cache/min/1/f819bcaed244d53d3b4ffc4c5cc0efdc.js" data-minify="1"></script>
</body>
</html>',
'files' => [
@@ -375,15 +359,13 @@
'html' => '<html>
<head>
<title>Sample Page</title>
<script type="text/javascript" src="http://example.org' . $jquery_path . '" defer></script>
<script type="text/javascript" src="http://example.org' . $jquery_path . '"></script>
<script>
nonce = "nonce";
</script>
</head>
<body>
<script>var rocket_lcp_data = {"ajax_url":"http:\/\/example.org\/wp-admin\/admin-ajax.php","nonce":"'.wp_create_nonce( 'rocket_lcp' ).'","url":"http:\/\/example.org","is_mobile":false,"elements":"img, video, picture, p, main, div, li, svg","width_threshold":1600,"height_threshold":700}</script>
<script data-name="wpr-lcp-beacon" src=\'http://example.org/wp-content/plugins/wp-rocket/assets/js/lcp-beacon.min.js\' async></script>
<script src="http://example.org/wp-content/cache/min/1/1100e4606ab35f45752eb8c3c8da0427.js" data-minify="1" defer></script>
<script src="http://example.org/wp-content/cache/min/1/1100e4606ab35f45752eb8c3c8da0427.js" data-minify="1"></script>
</body>
</html>',
'files' => [
@@ -424,16 +406,14 @@
'html' => '<html>
<head>
<title>Sample Page</title>
<script type="text/javascript" src="http://example.org' . $jquery_path . '" defer></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.js" defer></script>
<script type="text/javascript" src="http://example.org' . $jquery_path . '"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.js"></script>
<script>
nonce = "nonce";
</script>
</head>
<body>
<script>var rocket_lcp_data = {"ajax_url":"http:\/\/example.org\/wp-admin\/admin-ajax.php","nonce":"'.wp_create_nonce( 'rocket_lcp' ).'","url":"http:\/\/example.org","is_mobile":false,"elements":"img, video, picture, p, main, div, li, svg","width_threshold":1600,"height_threshold":700}</script>
<script data-name="wpr-lcp-beacon" src=\'http://example.org/wp-content/plugins/wp-rocket/assets/js/lcp-beacon.min.js\' async></script>
<script src="http://example.org/wp-content/cache/min/1/1100e4606ab35f45752eb8c3c8da0427.js" data-minify="1" defer></script>
<script src="http://example.org/wp-content/cache/min/1/1100e4606ab35f45752eb8c3c8da0427.js" data-minify="1"></script>
</body>
</html>',
'files' => [
@@ -473,15 +453,13 @@
'html' => '<html>
<head>
<title>Sample Page</title>
<script type="text/javascript" src="https://123456.rocketcdn.me' . $jquery_path . '" defer></script>
<script type="text/javascript" src="http://example.org' . $jquery_path . '"></script>
<script>
nonce = "nonce";
</script>
</head>
<body>
<script>var rocket_lcp_data = {"ajax_url":"http:\/\/example.org\/wp-admin\/admin-ajax.php","nonce":"'.wp_create_nonce( 'rocket_lcp' ).'","url":"http:\/\/example.org","is_mobile":false,"elements":"img, video, picture, p, main, div, li, svg","width_threshold":1600,"height_threshold":700}</script>
<script data-name="wpr-lcp-beacon" src=\'https://123456.rocketcdn.me/wp-content/plugins/wp-rocket/assets/js/lcp-beacon.min.js\' async></script>
<script src="https://123456.rocketcdn.me/wp-content/cache/min/1/1100e4606ab35f45752eb8c3c8da0427.js" data-minify="1" defer></script>
<script src="https://123456.rocketcdn.me/wp-content/cache/min/1/1100e4606ab35f45752eb8c3c8da0427.js" data-minify="1"></script>
</body>
</html>',
'files' => [
@@ -522,16 +500,14 @@
'html' => '<html>
<head>
<title>Sample Page</title>
<script type="text/javascript" src="https://123456.rocketcdn.me' . $jquery_path . '" defer></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.js" defer></script>
<script type="text/javascript" src="http://example.org' . $jquery_path . '"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.js"></script>
<script>
nonce = "nonce";
</script>
</head>
<body>
<script>var rocket_lcp_data = {"ajax_url":"http:\/\/example.org\/wp-admin\/admin-ajax.php","nonce":"'.wp_create_nonce( 'rocket_lcp' ).'","url":"http:\/\/example.org","is_mobile":false,"elements":"img, video, picture, p, main, div, li, svg","width_threshold":1600,"height_threshold":700}</script>
<script data-name="wpr-lcp-beacon" src=\'https://123456.rocketcdn.me/wp-content/plugins/wp-rocket/assets/js/lcp-beacon.min.js\' async></script>
<script src="https://123456.rocketcdn.me/wp-content/cache/min/1/1100e4606ab35f45752eb8c3c8da0427.js" data-minify="1" defer></script>
<script src="https://123456.rocketcdn.me/wp-content/cache/min/1/1100e4606ab35f45752eb8c3c8da0427.js" data-minify="1"></script>
</body>
</html>',
'files' => [
@@ -576,9 +552,7 @@
</script>
</head>
<body>
<script>var rocket_lcp_data = {"ajax_url":"http:\/\/example.org\/wp-admin\/admin-ajax.php","nonce":"'.wp_create_nonce( 'rocket_lcp' ).'","url":"http:\/\/example.org","is_mobile":false,"elements":"img, video, picture, p, main, div, li, svg","width_threshold":1600,"height_threshold":700}</script>
<script data-name="wpr-lcp-beacon" src=\'https://123456.rocketcdn.me/wp-content/plugins/wp-rocket/assets/js/lcp-beacon.min.js\' async></script>
<script src="https://123456.rocketcdn.me/wp-content/cache/min/1/f819bcaed244d53d3b4ffc4c5cc0efdc.js" data-minify="1"></script>
<script src="https://123456.rocketcdn.me/wp-content/cache/min/1/f819bcaed244d53d3b4ffc4c5cc0efdc.js" data-minify="1"></script>
</body>
</html>',
'files' => [
@@ -624,9 +598,7 @@
</script>
</head>
<body>
<script>var rocket_lcp_data = {"ajax_url":"http:\/\/example.org\/wp-admin\/admin-ajax.php","nonce":"'.wp_create_nonce( 'rocket_lcp' ).'","url":"http:\/\/example.org","is_mobile":false,"elements":"img, video, picture, p, main, div, li, svg","width_threshold":1600,"height_threshold":700}</script>
<script data-name="wpr-lcp-beacon" src=\'https://123456.rocketcdn.me/cdnpath/wp-content/plugins/wp-rocket/assets/js/lcp-beacon.min.js\' async></script>
<script src="https://123456.rocketcdn.me/cdnpath/wp-content/cache/min/1/f819bcaed244d53d3b4ffc4c5cc0efdc.js" data-minify="1"></script>
<script src="https://123456.rocketcdn.me/cdnpath/wp-content/cache/min/1/f819bcaed244d53d3b4ffc4c5cc0efdc.js" data-minify="1"></script>
</body>
</html>',
'files' => [
Original file line number Diff line number Diff line change
@@ -7,7 +7,6 @@
'</head><body></body></html>',
'expected' => '<html><head><title>Sample Page</title>' .
'</head><body>' .
'<script>var rocket_lcp_data = {"ajax_url":"http:\/\/example.org\/wp-admin\/admin-ajax.php","nonce":"'.wp_create_nonce( 'rocket_lcp' ).'","url":"http:\/\/example.org","is_mobile":false,"elements":"img, video, picture, p, main, div, li, svg","width_threshold":1600,"height_threshold":700}</script><script data-name="wpr-lcp-beacon" src=\'http://example.org/wp-content/plugins/wp-rocket/assets/js/lcp-beacon.min.js\' async></script>' .
"</body></html>\n" . '<!-- This website is like a Rocket, isn\'t it? Performance optimized by WP Rocket. Learn more: https://wp-rocket.me' . ' -->',
],
'testWPEngineAddFootprintWithWhitelabel' => [
@@ -16,14 +15,13 @@
'</head><body></body></html>',
'expected' => '<html><head><title>Sample Page</title>' .
'</head><body>' .
'<script>var rocket_lcp_data = {"ajax_url":"http:\/\/example.org\/wp-admin\/admin-ajax.php","nonce":"'.wp_create_nonce( 'rocket_lcp' ).'","url":"http:\/\/example.org","is_mobile":false,"elements":"img, video, picture, p, main, div, li, svg","width_threshold":1600,"height_threshold":700}</script><script data-name="wpr-lcp-beacon" src=\'http://example.org/wp-content/plugins/wp-rocket/assets/js/lcp-beacon.min.js\' async></script>' .
"</body></html>\n" . '<!-- Optimized for great performance' . ' -->',
],
'testWPEngineAddFootprintNoHtmlShouldBailOut' => [
'white_label_footprint' => false,
'html' => '<html><head><title>Sample Page</title>' .
'</head><body></body>',
'expected' => '<html><head><title>Sample Page</title>' .
'</head><body><script>var rocket_lcp_data = {"ajax_url":"http:\/\/example.org\/wp-admin\/admin-ajax.php","nonce":"'.wp_create_nonce( 'rocket_lcp' ).'","url":"http:\/\/example.org","is_mobile":false,"elements":"img, video, picture, p, main, div, li, svg","width_threshold":1600,"height_threshold":700}</script><script data-name="wpr-lcp-beacon" src=\'http://example.org/wp-content/plugins/wp-rocket/assets/js/lcp-beacon.min.js\' async></script></body>',
'</head><body></body>',
],
];
Original file line number Diff line number Diff line change
@@ -20,8 +20,6 @@
<body>
<img src="https://example.org/wp-content/uploads/logo.jpg" data-height-percentage="54">
<img width="1" height="1" src="https://example.org/wp-content/uploads/my-picture.jpg">
<script>var rocket_lcp_data = {"ajax_url":"http:\/\/example.org\/wp-admin\/admin-ajax.php","nonce":"$nonce","url":"http:\/\/example.org\/http:\/\/example.org","is_mobile":false,"elements":"img, video, picture, p, main, div, li, svg","width_threshold":1600,"height_threshold":700}</script>
<script data-name="wpr-lcp-beacon" src='http://example.org/wp-content/plugins/wp-rocket/assets/js/lcp-beacon.min.js' async></script>
</body>
</html>
EXPECTEDHTML;
@@ -96,7 +94,7 @@
],
'html' => [
'original' => '<!doctype><html><body><img src="http://example.org/wp-content/uploads/logo.jpg" width="100" DATA-height-PERcenTAGE="54"></body></html>',
'expected' => '<!doctype><html><body><img src="http://example.org/wp-content/uploads/logo.jpg" width="100" DATA-height-PERcenTAGE="54"><script>var rocket_lcp_data = {"ajax_url":"http:\/\/example.org\/wp-admin\/admin-ajax.php","nonce":"'.$nonce.'","url":"http:\/\/example.org\/http:\/\/example.org","is_mobile":false,"elements":"img, video, picture, p, main, div, li, svg","width_threshold":1600,"height_threshold":700}</script><script data-name="wpr-lcp-beacon" src=\'http://example.org/wp-content/plugins/wp-rocket/assets/js/lcp-beacon.min.js\' async></script></body></html>',
'expected' => '<!doctype><html><body><img src="http://example.org/wp-content/uploads/logo.jpg" width="100" DATA-height-PERcenTAGE="54"></body></html>',
],
],
],
Original file line number Diff line number Diff line change
@@ -18,7 +18,6 @@
<body>
<img src="https://example.org/wp-content/uploads/logo.jpg" data-height-percentage="54">
<img width="1" height="1" src="https://example.org/wp-content/uploads/my-picture.jpg">
<script data-name="wpr-lcp-beacon" src='http://example.org/wp-content/plugins/wp-rocket/assets/js/lcp-beacon.min.js' async></script>
</body>
</html>
EXPECTEDHTML;
@@ -93,7 +92,7 @@
],
'html' => [
'original' => '<!doctype><html><body><img src="http://example.org/wp-content/uploads/logo.jpg" width="100" DATA-height-PERcenTAGE="54"></body></html>',
'expected' => '<!doctype><html><body><img src="http://example.org/wp-content/uploads/logo.jpg" width="100" DATA-height-PERcenTAGE="54"><script src=\'http://example.org/wp-content/plugins/wp-rocket/assets/js/lcp-beacon.min.js\' async></script></body></html>',
'expected' => '<!doctype><html><body><img src="http://example.org/wp-content/uploads/logo.jpg" width="100" DATA-height-PERcenTAGE="54"></body></html>',
],
],
],
24 changes: 16 additions & 8 deletions tests/Integration/inc/Engine/CDN/Subscriber/rewrite.php
Original file line number Diff line number Diff line change
@@ -4,30 +4,38 @@

/**
* Test class covering \WP_Rocket\Engine\CDN\Subscriber::rewrite
* @uses \WP_Rocket\Engine\CDN\CDN::rewrite
* @group CDN
* @group rewrite
*
* @uses \WP_Rocket\Engine\CDN\CDN::rewrite
* @group CDN
*/
class Test_Rewrite extends TestCase {
public function set_up() {
parent::set_up();

$this->unregisterAllCallbacksExcept( 'rocket_buffer', 'rewrite', 20 );
}

public function tear_down() {
remove_filter( 'content_url', [ $this, 'setContentURL' ] );
remove_filter( 'includes_url', [ $this, 'setIncludesURL' ] );

$this->restoreWpHook( 'rocket_buffer' );

parent::tear_down();
}

/**
* @dataProvider providerTestData
*/
public function testShouldRewriteURL( $home_url, $original, $expected ) {
$this->cnames = [
$this->cnames = [
'cdn.example.org',
];
$this->cdn_zone = [
'all'
$this->cdn_zone = [
'all',
];
$this->home_url = $home_url;
$this->content_url = "{$home_url}/wp-content/";
$this->home_url = $home_url;
$this->content_url = "{$home_url}/wp-content/";
$this->includes_url = "{$home_url}/wp-includes/";

add_filter( 'pre_get_rocket_option_cdn', [ $this, 'return_true' ] );
Original file line number Diff line number Diff line change
@@ -26,13 +26,17 @@ class Test_Process extends TestCase {
public function set_up() {
parent::set_up();

$this->unregisterAllCallbacksExcept( 'rocket_buffer', 'process', 16 );

add_filter( 'pre_get_rocket_option_minify_css', [ $this, 'return_true' ] );
add_filter( 'pre_get_rocket_option_minify_css_key', [ $this, 'return_key' ] );
}

public function tear_down() {
parent::tear_down();

$this->restoreWpHook( 'rocket_buffer' );

remove_filter( 'pre_get_rocket_option_minify_css', [ $this, 'return_true' ] );
remove_filter( 'pre_get_rocket_option_minify_css_key', [ $this, 'return_key' ] );

Original file line number Diff line number Diff line change
@@ -24,9 +24,17 @@ class Test_Process extends TestCase {

protected $path_to_test_data = '/inc/Engine/Optimization/Minify/JS/Subscriber/process.php';

public function set_up() {
parent::set_up();

$this->unregisterAllCallbacksExcept( 'rocket_buffer', 'process', 22 );
}

public function tear_down() {
parent::tear_down();

$this->restoreWpHook( 'rocket_buffer' );

remove_filter( 'pre_get_rocket_option_minify_js', [ $this, 'return_true' ] );
remove_filter( 'pre_get_rocket_option_minify_js_key', [ $this, 'return_key' ] );
remove_filter( 'pre_get_rocket_option_defer_all_js', [ $this, 'return_defer_all_js' ] );
Original file line number Diff line number Diff line change
@@ -12,6 +12,17 @@
* @group ThirdParty
*/
class Test_AddFootprint extends TestCase {
public function set_up() {
parent::set_up();

$this->unregisterAllCallbacksExcept( 'rocket_buffer', 'add_footprint', 50 );
}

public function tear_down() {
$this->restoreWpHook( 'rocket_buffer' );

parent::tear_down();
}

/**
* @dataProvider providerTestData
Original file line number Diff line number Diff line change
@@ -14,7 +14,7 @@ class Test_DisableImageDimensionsHeightPercentage extends WPThemeTestcase {
use DBTrait;

protected $path_to_test_data = '/inc/ThirdParty/Themes/Divi/Integration/disableImageDimensionsHeightPercentage.php';

private $container;
private $event;
private $subscriber;
@@ -36,12 +36,16 @@ public function set_up() {
$this->container = apply_filters( 'rocket_container', '' );
$this->event = $this->container->get( 'event_manager' );

$this->unregisterAllCallbacksExcept( 'rocket_buffer', 'specify_image_dimensions', 17 );

add_filter( 'rocket_specify_image_dimensions', '__return_true' );
}

public function tear_down() {
$this->event->remove_subscriber( $this->subscriber );

$this->restoreWpHook( 'rocket_buffer' );

remove_filter( 'rocket_specify_image_dimensions', '__return_true' );
unset( $GLOBALS['wp'] );


Unchanged files with check annotations Beta

protected $myrc4;
protected function readfile_chunked( $file, $retbytes=true, $myrc4, $cache_to_file='' ) {

Check warning on line 112 in tests/Fixtures/inc/ThirdParty/Plugins/PDFEmbedder/core/secure/uploads.php

GitHub Actions / WP latest with PHP 8.3 on ubuntu-latest.

Optional parameter $retbytes declared before required parameter $myrc4 is implicitly treated as a required parameter

Check warning on line 112 in tests/Fixtures/inc/ThirdParty/Plugins/PDFEmbedder/core/secure/uploads.php

GitHub Actions / WP latest with PHP 8.0 on ubuntu-latest.

Required parameter $myrc4 follows optional parameter $retbytes
$chunksize = 1024 * 1024;
$buffer = '';