From e2678285fcbbe4e6cc46b165420da6f77169da94 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABl=20Robin?= Date: Tue, 28 Jan 2025 03:57:09 +0100 Subject: [PATCH 01/17] Closes #6661: safeguard exclusions for non valid path --- .../AboveTheFold/Frontend/Controller.php | 16 ++++++++-- .../Frontend/Controller/addExclusions.php | 32 +++++++++++++++++++ 2 files changed, 45 insertions(+), 3 deletions(-) diff --git a/inc/Engine/Media/AboveTheFold/Frontend/Controller.php b/inc/Engine/Media/AboveTheFold/Frontend/Controller.php index 530a525e48..13ca6de64d 100644 --- a/inc/Engine/Media/AboveTheFold/Frontend/Controller.php +++ b/inc/Engine/Media/AboveTheFold/Frontend/Controller.php @@ -227,12 +227,22 @@ private function get_path_for_exclusion( array $exclusions ): array { $exclusions = array_map( function ( $exclusion ) { $exclusion = wp_parse_url( $exclusion ); - return ltrim( $exclusion['path'], '/' ); + if ( isset( $exclusion['path'] ) && is_string( $exclusion['path'] ) ) { + return ltrim( $exclusion['path'], '/' ); + } + // Return empty string if not set. + return null; }, $exclusions - ); + ); - return $exclusions; + // Filter out null values. + return array_filter( + $exclusions, + function ( $value ) { + return $value !== null; + } + ); } /** 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..bd5382139d 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,36 @@ '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', + ], + ], ]; From bb23294a9a7bb93d1eb9e39db96775df37451a1c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABl=20Robin?= Date: Tue, 28 Jan 2025 04:01:38 +0100 Subject: [PATCH 02/17] Delete useless condition --- inc/Engine/Media/AboveTheFold/Frontend/Controller.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/inc/Engine/Media/AboveTheFold/Frontend/Controller.php b/inc/Engine/Media/AboveTheFold/Frontend/Controller.php index 13ca6de64d..2e51f355c1 100644 --- a/inc/Engine/Media/AboveTheFold/Frontend/Controller.php +++ b/inc/Engine/Media/AboveTheFold/Frontend/Controller.php @@ -227,7 +227,7 @@ private function get_path_for_exclusion( array $exclusions ): array { $exclusions = array_map( function ( $exclusion ) { $exclusion = wp_parse_url( $exclusion ); - if ( isset( $exclusion['path'] ) && is_string( $exclusion['path'] ) ) { + if ( isset( $exclusion['path'] ) ) { return ltrim( $exclusion['path'], '/' ); } // Return empty string if not set. @@ -242,7 +242,7 @@ function ( $exclusion ) { function ( $value ) { return $value !== null; } - ); + ); } /** From 6b5cdb3f30e1a7b5ada6cfb5db16567a72d8ab61 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABl=20Robin?= Date: Tue, 28 Jan 2025 06:30:03 +0100 Subject: [PATCH 03/17] fix phpcs --- inc/Engine/Media/AboveTheFold/Frontend/Controller.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inc/Engine/Media/AboveTheFold/Frontend/Controller.php b/inc/Engine/Media/AboveTheFold/Frontend/Controller.php index 2e51f355c1..479edd69fc 100644 --- a/inc/Engine/Media/AboveTheFold/Frontend/Controller.php +++ b/inc/Engine/Media/AboveTheFold/Frontend/Controller.php @@ -240,7 +240,7 @@ function ( $exclusion ) { return array_filter( $exclusions, function ( $value ) { - return $value !== null; + return null !== $value; } ); } From cade892206d85e79d2b8c4705e5f9d66d4c6a245 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABl=20Robin?= Date: Tue, 11 Feb 2025 02:55:16 +0100 Subject: [PATCH 04/17] Fix several notice/errors --- .../AboveTheFold/Frontend/Controller.php | 17 +++- .../Frontend/Controller/addExclusions.php | 87 +++++++++++++++++++ 2 files changed, 102 insertions(+), 2 deletions(-) diff --git a/inc/Engine/Media/AboveTheFold/Frontend/Controller.php b/inc/Engine/Media/AboveTheFold/Frontend/Controller.php index 479edd69fc..e2f8d1d2d9 100644 --- a/inc/Engine/Media/AboveTheFold/Frontend/Controller.php +++ b/inc/Engine/Media/AboveTheFold/Frontend/Controller.php @@ -90,6 +90,14 @@ private function preload_lcp( $html, $row ) { $lcp = json_decode( $row->lcp ); + $isEmptyLcp = ( is_object( $lcp ) && empty( get_object_vars( $lcp ) ) ) + || ( is_array( $lcp ) && empty( $lcp_decoded ) ) + || $lcp === null; + + if ( $isEmptyLcp ) { + return $html; + } + $preload .= $this->preload_tag( $lcp ); $replace = preg_replace( '#' . $title . '#', $preload, $html, 1 ); @@ -198,8 +206,13 @@ public function add_exclusions( $exclusions ): array { return $exclusions; } - if ( $row->lcp && 'not found' !== $row->lcp ) { - $lcp = $this->generate_lcp_link_tag_with_sources( json_decode( $row->lcp ) ); + $lcp_decoded = json_decode( $row->lcp ); + + $isEmptyLcp = ( is_object( $lcp_decoded ) && empty( get_object_vars( $lcp_decoded ) ) ) + || ( is_array( $lcp_decoded ) && empty( $lcp_decoded ) ) + || $lcp_decoded === null; + if ( $row->lcp && 'not found' !== $row->lcp && ! $isEmptyLcp ) { + $lcp = $this->generate_lcp_link_tag_with_sources( $lcp_decoded ); $lcp = $lcp['sources']; $lcp = $this->get_path_for_exclusion( $lcp ); } 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 bd5382139d..e00d91f39f 100644 --- a/tests/Fixtures/inc/Engine/Media/AboveTheFold/Frontend/Controller/addExclusions.php +++ b/tests/Fixtures/inc/Engine/Media/AboveTheFold/Frontend/Controller/addExclusions.php @@ -109,4 +109,91 @@ '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', + ], + ], ]; From a86c1f19054905d5db1f7ad42d382e71325a9fd9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABl=20Robin?= Date: Tue, 11 Feb 2025 03:00:58 +0100 Subject: [PATCH 05/17] Fix linters --- .../Media/AboveTheFold/Frontend/Controller.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/inc/Engine/Media/AboveTheFold/Frontend/Controller.php b/inc/Engine/Media/AboveTheFold/Frontend/Controller.php index e2f8d1d2d9..b5e1d5f25b 100644 --- a/inc/Engine/Media/AboveTheFold/Frontend/Controller.php +++ b/inc/Engine/Media/AboveTheFold/Frontend/Controller.php @@ -90,11 +90,11 @@ private function preload_lcp( $html, $row ) { $lcp = json_decode( $row->lcp ); - $isEmptyLcp = ( is_object( $lcp ) && empty( get_object_vars( $lcp ) ) ) - || ( is_array( $lcp ) && empty( $lcp_decoded ) ) - || $lcp === null; + $is_non_valid_lcp = ( is_object( $lcp ) && empty( get_object_vars( $lcp ) ) ) + || is_array( $lcp ) + || null === $lcp; - if ( $isEmptyLcp ) { + if ( $is_non_valid_lcp ) { return $html; } @@ -208,10 +208,10 @@ public function add_exclusions( $exclusions ): array { $lcp_decoded = json_decode( $row->lcp ); - $isEmptyLcp = ( is_object( $lcp_decoded ) && empty( get_object_vars( $lcp_decoded ) ) ) - || ( is_array( $lcp_decoded ) && empty( $lcp_decoded ) ) - || $lcp_decoded === null; - if ( $row->lcp && 'not found' !== $row->lcp && ! $isEmptyLcp ) { + $is_non_valid_lcp = ( is_object( $lcp_decoded ) && empty( get_object_vars( $lcp_decoded ) ) ) + || ( is_array( $lcp_decoded ) ) + || null === $lcp_decoded; + if ( $row->lcp && 'not found' !== $row->lcp && ! $is_non_valid_lcp ) { $lcp = $this->generate_lcp_link_tag_with_sources( $lcp_decoded ); $lcp = $lcp['sources']; $lcp = $this->get_path_for_exclusion( $lcp ); From 75550def3df775d397299eacc891acb8ebc28aac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABl=20Robin?= Date: Tue, 11 Feb 2025 03:17:29 +0100 Subject: [PATCH 06/17] Add condition to check if viewport is valid --- inc/Engine/Media/AboveTheFold/Frontend/Controller.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/inc/Engine/Media/AboveTheFold/Frontend/Controller.php b/inc/Engine/Media/AboveTheFold/Frontend/Controller.php index b5e1d5f25b..bc99dbffb4 100644 --- a/inc/Engine/Media/AboveTheFold/Frontend/Controller.php +++ b/inc/Engine/Media/AboveTheFold/Frontend/Controller.php @@ -217,7 +217,12 @@ public function add_exclusions( $exclusions ): array { $lcp = $this->get_path_for_exclusion( $lcp ); } - if ( $row->viewport && 'not found' !== $row->viewport ) { + $viewport_decoded = json_decode( $row->viewport ); + $is_non_valid_viewport = ( is_object( $viewport_decoded ) && empty( get_object_vars( $viewport_decoded ) ) ) + || ( is_array( $viewport_decoded ) ) + || null === $viewport_decoded; + + if ( $row->viewport && 'not found' !== $row->viewport && ! $is_non_valid_viewport ) { $atf = $this->get_atf_sources( json_decode( $row->viewport ) ); $atf = $this->get_path_for_exclusion( $atf ); } From 31a2ded7e74f11d5a0a90a7737a35132531ce2dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABl=20Robin?= Date: Tue, 11 Feb 2025 03:22:06 +0100 Subject: [PATCH 07/17] Fix condition --- inc/Engine/Media/AboveTheFold/Frontend/Controller.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/inc/Engine/Media/AboveTheFold/Frontend/Controller.php b/inc/Engine/Media/AboveTheFold/Frontend/Controller.php index bc99dbffb4..6442f3ba3e 100644 --- a/inc/Engine/Media/AboveTheFold/Frontend/Controller.php +++ b/inc/Engine/Media/AboveTheFold/Frontend/Controller.php @@ -91,7 +91,7 @@ private function preload_lcp( $html, $row ) { $lcp = json_decode( $row->lcp ); $is_non_valid_lcp = ( is_object( $lcp ) && empty( get_object_vars( $lcp ) ) ) - || is_array( $lcp ) + || ( is_array( $lcp ) && empty( $lcp ) ) || null === $lcp; if ( $is_non_valid_lcp ) { @@ -209,7 +209,7 @@ public function add_exclusions( $exclusions ): array { $lcp_decoded = json_decode( $row->lcp ); $is_non_valid_lcp = ( is_object( $lcp_decoded ) && empty( get_object_vars( $lcp_decoded ) ) ) - || ( is_array( $lcp_decoded ) ) + || ( is_array( $lcp_decoded ) && empty( $lcp_decoded ) ) || null === $lcp_decoded; if ( $row->lcp && 'not found' !== $row->lcp && ! $is_non_valid_lcp ) { $lcp = $this->generate_lcp_link_tag_with_sources( $lcp_decoded ); @@ -219,7 +219,7 @@ public function add_exclusions( $exclusions ): array { $viewport_decoded = json_decode( $row->viewport ); $is_non_valid_viewport = ( is_object( $viewport_decoded ) && empty( get_object_vars( $viewport_decoded ) ) ) - || ( is_array( $viewport_decoded ) ) + || ( is_array( $viewport_decoded ) && empty( $viewport_decoded ) ) || null === $viewport_decoded; if ( $row->viewport && 'not found' !== $row->viewport && ! $is_non_valid_viewport ) { From 85352882c0f1a523d1b92c283237e7f5c32ec156 Mon Sep 17 00:00:00 2001 From: Khadreal Date: Wed, 12 Feb 2025 10:44:43 +0100 Subject: [PATCH 08/17] Review modifications --- .../AboveTheFold/Frontend/Controller.php | 51 ++++++++++--------- 1 file changed, 26 insertions(+), 25 deletions(-) diff --git a/inc/Engine/Media/AboveTheFold/Frontend/Controller.php b/inc/Engine/Media/AboveTheFold/Frontend/Controller.php index 6442f3ba3e..c12cb254ad 100644 --- a/inc/Engine/Media/AboveTheFold/Frontend/Controller.php +++ b/inc/Engine/Media/AboveTheFold/Frontend/Controller.php @@ -206,21 +206,16 @@ public function add_exclusions( $exclusions ): array { return $exclusions; } - $lcp_decoded = json_decode( $row->lcp ); + $lcp_decoded = json_decode( $row->lcp ); + $is_non_valid_lcp = $this->is_non_valid_data( $row->lcp ); - $is_non_valid_lcp = ( is_object( $lcp_decoded ) && empty( get_object_vars( $lcp_decoded ) ) ) - || ( is_array( $lcp_decoded ) && empty( $lcp_decoded ) ) - || null === $lcp_decoded; if ( $row->lcp && 'not found' !== $row->lcp && ! $is_non_valid_lcp ) { $lcp = $this->generate_lcp_link_tag_with_sources( $lcp_decoded ); $lcp = $lcp['sources']; $lcp = $this->get_path_for_exclusion( $lcp ); } - $viewport_decoded = json_decode( $row->viewport ); - $is_non_valid_viewport = ( is_object( $viewport_decoded ) && empty( get_object_vars( $viewport_decoded ) ) ) - || ( is_array( $viewport_decoded ) && empty( $viewport_decoded ) ) - || null === $viewport_decoded; + $is_non_valid_viewport = $this->is_non_valid_data( $row->viewport ); if ( $row->viewport && 'not found' !== $row->viewport && ! $is_non_valid_viewport ) { $atf = $this->get_atf_sources( json_decode( $row->viewport ) ); @@ -235,6 +230,21 @@ 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_non_valid_data( string $data ): bool { + $decode_data = json_decode( $data ); + + return ( is_object( $decode_data ) && empty( get_object_vars( $decode_data ) ) ) + || ( is_array( $decode_data ) && empty( $decode_data ) ) + || null === $decode_data; + } + /** * Get only the url path to exclude. * @@ -242,25 +252,16 @@ 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 ); - if ( isset( $exclusion['path'] ) ) { - return ltrim( $exclusion['path'], '/' ); - } - // Return empty string if not set. - return null; - }, - $exclusions - ); + $sanitized_exclusions = []; - // Filter out null values. - return array_filter( - $exclusions, - function ( $value ) { - return null !== $value; + foreach ( $exclusions as $exclusion ) { + $parsed = wp_parse_url( $exclusion ); + if ( isset( $parsed['path'] ) ) { + $sanitized_exclusions[] = ltrim( $exclusion['path'], '/' ); } - ); + } + + return $sanitized_exclusions; } /** From 8bde35fbd647f3e14a23966d7102654141cedcc0 Mon Sep 17 00:00:00 2001 From: Khadreal Date: Wed, 12 Feb 2025 11:22:06 +0100 Subject: [PATCH 09/17] Minor modification, fix typo --- inc/Engine/Media/AboveTheFold/Frontend/Controller.php | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/inc/Engine/Media/AboveTheFold/Frontend/Controller.php b/inc/Engine/Media/AboveTheFold/Frontend/Controller.php index c12cb254ad..286e82d30c 100644 --- a/inc/Engine/Media/AboveTheFold/Frontend/Controller.php +++ b/inc/Engine/Media/AboveTheFold/Frontend/Controller.php @@ -88,16 +88,14 @@ private function preload_lcp( $html, $row ) { $title = $matches[0]; $preload = $title; - $lcp = json_decode( $row->lcp ); - - $is_non_valid_lcp = ( is_object( $lcp ) && empty( get_object_vars( $lcp ) ) ) - || ( is_array( $lcp ) && empty( $lcp ) ) - || null === $lcp; + $is_non_valid_lcp = $this->is_non_valid_data( $row->lcp ); if ( $is_non_valid_lcp ) { return $html; } + $lcp = json_decode( $row->lcp ); + $preload .= $this->preload_tag( $lcp ); $replace = preg_replace( '#' . $title . '#', $preload, $html, 1 ); @@ -257,7 +255,7 @@ private function get_path_for_exclusion( array $exclusions ): array { foreach ( $exclusions as $exclusion ) { $parsed = wp_parse_url( $exclusion ); if ( isset( $parsed['path'] ) ) { - $sanitized_exclusions[] = ltrim( $exclusion['path'], '/' ); + $sanitized_exclusions[] = ltrim( $parsed['path'], '/' ); } } From 0fe2a70797824dfa80c9747c1f46f685513e23f0 Mon Sep 17 00:00:00 2001 From: WordPressFan Date: Wed, 12 Feb 2025 12:35:42 +0200 Subject: [PATCH 10/17] very minor changes --- inc/Engine/Media/AboveTheFold/Frontend/Controller.php | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/inc/Engine/Media/AboveTheFold/Frontend/Controller.php b/inc/Engine/Media/AboveTheFold/Frontend/Controller.php index 286e82d30c..fe08586de1 100644 --- a/inc/Engine/Media/AboveTheFold/Frontend/Controller.php +++ b/inc/Engine/Media/AboveTheFold/Frontend/Controller.php @@ -88,9 +88,7 @@ private function preload_lcp( $html, $row ) { $title = $matches[0]; $preload = $title; - $is_non_valid_lcp = $this->is_non_valid_data( $row->lcp ); - - if ( $is_non_valid_lcp ) { + if ( $this->is_non_valid_data( $row->lcp ) ) { return $html; } @@ -204,11 +202,8 @@ public function add_exclusions( $exclusions ): array { return $exclusions; } - $lcp_decoded = json_decode( $row->lcp ); - $is_non_valid_lcp = $this->is_non_valid_data( $row->lcp ); - - if ( $row->lcp && 'not found' !== $row->lcp && ! $is_non_valid_lcp ) { - $lcp = $this->generate_lcp_link_tag_with_sources( $lcp_decoded ); + if ( $row->lcp && 'not found' !== $row->lcp && ! $this->is_non_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 ); } From 520c19903dd4db4ec349f44acee3470628e3c802 Mon Sep 17 00:00:00 2001 From: WordPressFan Date: Wed, 12 Feb 2025 12:36:58 +0200 Subject: [PATCH 11/17] very minor changes 2 --- inc/Engine/Media/AboveTheFold/Frontend/Controller.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/inc/Engine/Media/AboveTheFold/Frontend/Controller.php b/inc/Engine/Media/AboveTheFold/Frontend/Controller.php index fe08586de1..cc3cfee4a8 100644 --- a/inc/Engine/Media/AboveTheFold/Frontend/Controller.php +++ b/inc/Engine/Media/AboveTheFold/Frontend/Controller.php @@ -208,9 +208,7 @@ public function add_exclusions( $exclusions ): array { $lcp = $this->get_path_for_exclusion( $lcp ); } - $is_non_valid_viewport = $this->is_non_valid_data( $row->viewport ); - - if ( $row->viewport && 'not found' !== $row->viewport && ! $is_non_valid_viewport ) { + if ( $row->viewport && 'not found' !== $row->viewport && ! $this->is_non_valid_data( $row->viewport ) ) { $atf = $this->get_atf_sources( json_decode( $row->viewport ) ); $atf = $this->get_path_for_exclusion( $atf ); } From a73448d77c28b6ba105381d99048e4798cec0645 Mon Sep 17 00:00:00 2001 From: Khadreal Date: Wed, 12 Feb 2025 12:14:35 +0100 Subject: [PATCH 12/17] refactor logic --- inc/Engine/Media/AboveTheFold/Frontend/Controller.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/inc/Engine/Media/AboveTheFold/Frontend/Controller.php b/inc/Engine/Media/AboveTheFold/Frontend/Controller.php index cc3cfee4a8..b7ad6b17f3 100644 --- a/inc/Engine/Media/AboveTheFold/Frontend/Controller.php +++ b/inc/Engine/Media/AboveTheFold/Frontend/Controller.php @@ -229,11 +229,9 @@ public function add_exclusions( $exclusions ): array { * @return bool */ private function is_non_valid_data( string $data ): bool { - $decode_data = json_decode( $data ); + $decode_data = json_decode( $data, true ); - return ( is_object( $decode_data ) && empty( get_object_vars( $decode_data ) ) ) - || ( is_array( $decode_data ) && empty( $decode_data ) ) - || null === $decode_data; + return empty( $decode_data ); } /** From 32ed62a10de847c90996a34adbef46b393d3db8a Mon Sep 17 00:00:00 2001 From: WordPressFan Date: Wed, 12 Feb 2025 15:30:55 +0200 Subject: [PATCH 13/17] enhance the line --- inc/Engine/Media/AboveTheFold/Frontend/Controller.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/inc/Engine/Media/AboveTheFold/Frontend/Controller.php b/inc/Engine/Media/AboveTheFold/Frontend/Controller.php index b7ad6b17f3..05533571ff 100644 --- a/inc/Engine/Media/AboveTheFold/Frontend/Controller.php +++ b/inc/Engine/Media/AboveTheFold/Frontend/Controller.php @@ -229,9 +229,7 @@ public function add_exclusions( $exclusions ): array { * @return bool */ private function is_non_valid_data( string $data ): bool { - $decode_data = json_decode( $data, true ); - - return empty( $decode_data ); + return empty( json_decode( $data, true ) ); } /** From 0500270940d618cef127e3fb8e1da9b20d0e06ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABl=20Robin?= Date: Wed, 12 Feb 2025 15:42:54 +0100 Subject: [PATCH 14/17] Rename function --- inc/Engine/Media/AboveTheFold/Frontend/Controller.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/inc/Engine/Media/AboveTheFold/Frontend/Controller.php b/inc/Engine/Media/AboveTheFold/Frontend/Controller.php index 05533571ff..644f3c0ede 100644 --- a/inc/Engine/Media/AboveTheFold/Frontend/Controller.php +++ b/inc/Engine/Media/AboveTheFold/Frontend/Controller.php @@ -88,7 +88,7 @@ private function preload_lcp( $html, $row ) { $title = $matches[0]; $preload = $title; - if ( $this->is_non_valid_data( $row->lcp ) ) { + if ( ! $this->is_valid_data( $row->lcp ) ) { return $html; } @@ -202,13 +202,13 @@ public function add_exclusions( $exclusions ): array { return $exclusions; } - if ( $row->lcp && 'not found' !== $row->lcp && ! $this->is_non_valid_data( $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 && ! $this->is_non_valid_data( $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 ); } @@ -228,8 +228,8 @@ public function add_exclusions( $exclusions ): array { * * @return bool */ - private function is_non_valid_data( string $data ): bool { - return empty( json_decode( $data, true ) ); + private function is_valid_data( string $data ): bool { + return ! empty( json_decode( $data, true ) ); } /** From 35585ab1d8e22ed7fc5776823646f35955c98ede Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABl=20Robin?= Date: Wed, 12 Feb 2025 15:45:55 +0100 Subject: [PATCH 15/17] Refactor foreach get path exclusion --- inc/Engine/Media/AboveTheFold/Frontend/Controller.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/inc/Engine/Media/AboveTheFold/Frontend/Controller.php b/inc/Engine/Media/AboveTheFold/Frontend/Controller.php index 644f3c0ede..fa7fa4e92b 100644 --- a/inc/Engine/Media/AboveTheFold/Frontend/Controller.php +++ b/inc/Engine/Media/AboveTheFold/Frontend/Controller.php @@ -242,9 +242,9 @@ private function get_path_for_exclusion( array $exclusions ): array { $sanitized_exclusions = []; foreach ( $exclusions as $exclusion ) { - $parsed = wp_parse_url( $exclusion ); - if ( isset( $parsed['path'] ) ) { - $sanitized_exclusions[] = ltrim( $parsed['path'], '/' ); + $path = wp_parse_url( $exclusion, PHP_URL_PATH ); + if ( isset( $path ) ) { + $sanitized_exclusions[] = ltrim( $path, '/' ); } } From 1172af7c5d6a90c81757ad3518513b1321539df2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABl=20Robin?= Date: Wed, 12 Feb 2025 15:49:58 +0100 Subject: [PATCH 16/17] Fix condition on $path --- inc/Engine/Media/AboveTheFold/Frontend/Controller.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inc/Engine/Media/AboveTheFold/Frontend/Controller.php b/inc/Engine/Media/AboveTheFold/Frontend/Controller.php index fa7fa4e92b..e60307fea0 100644 --- a/inc/Engine/Media/AboveTheFold/Frontend/Controller.php +++ b/inc/Engine/Media/AboveTheFold/Frontend/Controller.php @@ -243,7 +243,7 @@ private function get_path_for_exclusion( array $exclusions ): array { foreach ( $exclusions as $exclusion ) { $path = wp_parse_url( $exclusion, PHP_URL_PATH ); - if ( isset( $path ) ) { + if ( isset( $path ) && $path ) { $sanitized_exclusions[] = ltrim( $path, '/' ); } } From 8793b1fb7dba464f3b15640e99bdc9e8f51fce35 Mon Sep 17 00:00:00 2001 From: WordPressFan Date: Wed, 12 Feb 2025 18:50:59 +0200 Subject: [PATCH 17/17] handle the case where path equals / in exclusions --- .../Media/AboveTheFold/Frontend/Controller.php | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/inc/Engine/Media/AboveTheFold/Frontend/Controller.php b/inc/Engine/Media/AboveTheFold/Frontend/Controller.php index e60307fea0..6494ccf6d2 100644 --- a/inc/Engine/Media/AboveTheFold/Frontend/Controller.php +++ b/inc/Engine/Media/AboveTheFold/Frontend/Controller.php @@ -242,10 +242,18 @@ private function get_path_for_exclusion( array $exclusions ): array { $sanitized_exclusions = []; foreach ( $exclusions as $exclusion ) { + if ( empty( $exclusion ) ) { + continue; + } + $path = wp_parse_url( $exclusion, PHP_URL_PATH ); - if ( isset( $path ) && $path ) { - $sanitized_exclusions[] = ltrim( $path, '/' ); + $path = ! empty( $path ) ? ltrim( $path, '/' ) : ''; + + if ( empty( $path ) ) { + continue; } + + $sanitized_exclusions[] = $path; } return $sanitized_exclusions;