diff --git a/inc/Engine/Media/AboveTheFold/Frontend/Controller.php b/inc/Engine/Media/AboveTheFold/Frontend/Controller.php index 530a525e48..6494ccf6d2 100644 --- a/inc/Engine/Media/AboveTheFold/Frontend/Controller.php +++ b/inc/Engine/Media/AboveTheFold/Frontend/Controller.php @@ -88,6 +88,10 @@ private function preload_lcp( $html, $row ) { $title = $matches[0]; $preload = $title; + if ( ! $this->is_valid_data( $row->lcp ) ) { + return $html; + } + $lcp = json_decode( $row->lcp ); $preload .= $this->preload_tag( $lcp ); @@ -198,13 +202,13 @@ public function add_exclusions( $exclusions ): array { return $exclusions; } - if ( $row->lcp && 'not found' !== $row->lcp ) { + if ( $row->lcp && 'not found' !== $row->lcp && $this->is_valid_data( $row->lcp ) ) { $lcp = $this->generate_lcp_link_tag_with_sources( json_decode( $row->lcp ) ); $lcp = $lcp['sources']; $lcp = $this->get_path_for_exclusion( $lcp ); } - if ( $row->viewport && 'not found' !== $row->viewport ) { + if ( $row->viewport && 'not found' !== $row->viewport && $this->is_valid_data( $row->viewport ) ) { $atf = $this->get_atf_sources( json_decode( $row->viewport ) ); $atf = $this->get_path_for_exclusion( $atf ); } @@ -217,6 +221,17 @@ public function add_exclusions( $exclusions ): array { return $exclusions; } + /** + * Check if lcp/viewport is valid + * + * @param string $data The lcp/viewport data. + * + * @return bool + */ + private function is_valid_data( string $data ): bool { + return ! empty( json_decode( $data, true ) ); + } + /** * Get only the url path to exclude. * @@ -224,15 +239,24 @@ public function add_exclusions( $exclusions ): array { * @return array */ private function get_path_for_exclusion( array $exclusions ): array { - $exclusions = array_map( - function ( $exclusion ) { - $exclusion = wp_parse_url( $exclusion ); - return ltrim( $exclusion['path'], '/' ); - }, - $exclusions - ); + $sanitized_exclusions = []; - return $exclusions; + foreach ( $exclusions as $exclusion ) { + if ( empty( $exclusion ) ) { + continue; + } + + $path = wp_parse_url( $exclusion, PHP_URL_PATH ); + $path = ! empty( $path ) ? ltrim( $path, '/' ) : ''; + + if ( empty( $path ) ) { + continue; + } + + $sanitized_exclusions[] = $path; + } + + return $sanitized_exclusions; } /** diff --git a/tests/Fixtures/inc/Engine/Media/AboveTheFold/Frontend/Controller/addExclusions.php b/tests/Fixtures/inc/Engine/Media/AboveTheFold/Frontend/Controller/addExclusions.php index fc609905eb..e00d91f39f 100644 --- a/tests/Fixtures/inc/Engine/Media/AboveTheFold/Frontend/Controller/addExclusions.php +++ b/tests/Fixtures/inc/Engine/Media/AboveTheFold/Frontend/Controller/addExclusions.php @@ -77,4 +77,123 @@ 'foobar.jpg', ], ], + 'testShouldReturnEmptyStringWhenUrlNotValid' => [ + 'config' => [ + 'filter' => true, + 'wp' => (object) [ + 'request' => '', + ], + 'url' => 'http://example.org', + 'is_mobile' => false, + 'row' => (object) [ + 'lcp' => json_encode( (object) [ + 'type' => 'img', + 'src' => ':', + ] ), + 'viewport' => json_encode( [ + 0 => (object) [ + 'type' => 'img', + 'src' => 'https://example.com/foobar.jpg', + ], + ] ), + ], + 'cache_mobile' => 0, + 'do_caching_mobile_files' => 0, + 'wp_is_mobile' => false, + ], + 'exclusions' => [ + 'foo', + ], + 'expected' => [ + 'foo', + 'foobar.jpg', + ], + ], + 'testShouldReturnEmptyLcpNotValid' => [ + 'config' => [ + 'filter' => true, + 'wp' => (object) [ + 'request' => '', + ], + 'url' => 'http://example.org', + 'is_mobile' => false, + 'row' => (object) [ + 'lcp' => '[]', + 'viewport' => json_encode( [ + 0 => (object) [ + 'type' => 'img', + 'src' => 'https://example.com/foobar.jpg', + ], + ] ), + ], + 'cache_mobile' => 0, + 'do_caching_mobile_files' => 0, + 'wp_is_mobile' => false, + ], + 'exclusions' => [ + 'foo', + ], + 'expected' => [ + 'foo', + 'foobar.jpg', + ], + ], + 'testShouldReturnEmptyLcpNotValid2' => [ + 'config' => [ + 'filter' => true, + 'wp' => (object) [ + 'request' => '', + ], + 'url' => 'http://example.org', + 'is_mobile' => false, + 'row' => (object) [ + 'lcp' => 'null', + 'viewport' => json_encode( [ + 0 => (object) [ + 'type' => 'img', + 'src' => 'https://example.com/foobar.jpg', + ], + ] ), + ], + 'cache_mobile' => 0, + 'do_caching_mobile_files' => 0, + 'wp_is_mobile' => false, + ], + 'exclusions' => [ + 'foo', + ], + 'expected' => [ + 'foo', + 'foobar.jpg', + ], + ], + 'testShouldReturnEmptyLcpNotValid3' => [ + 'config' => [ + 'filter' => true, + 'wp' => (object) [ + 'request' => '', + ], + 'url' => 'http://example.org', + 'is_mobile' => false, + 'row' => (object) [ + 'lcp' => '{}', + 'viewport' => json_encode( [ + 0 => (object) [ + 'type' => 'img', + 'src' => 'https://example.com/foobar.jpg', + ], + ] ), + ], + 'cache_mobile' => 0, + 'do_caching_mobile_files' => 0, + 'wp_is_mobile' => false, + ], + 'exclusions' => [ + 'foo', + ], + 'expected' => [ + 'foo', + 'foobar.jpg', + ], + ], ];