From efbc71e8cac44e2c946bdd93e7d5b91b4a56d576 Mon Sep 17 00:00:00 2001 From: Michael Lee <38788055+jeawhanlee@users.noreply.github.com> Date: Mon, 26 Feb 2024 10:52:28 +0100 Subject: [PATCH] Enhancement/exclude youtube thumbnail from lazyload (#116) * Exclude youtube thumbnail * Updated tests * Backport change from wprocket * Updated tests --- .../Unit/Assets/getYoutubeThumbnailScript.php | 46 +++++++++++++------ src/Assets.php | 15 +++++- 2 files changed, 45 insertions(+), 16 deletions(-) diff --git a/Tests/Unit/Assets/getYoutubeThumbnailScript.php b/Tests/Unit/Assets/getYoutubeThumbnailScript.php index e646f02..95b0f06 100644 --- a/Tests/Unit/Assets/getYoutubeThumbnailScript.php +++ b/Tests/Unit/Assets/getYoutubeThumbnailScript.php @@ -3,6 +3,7 @@ namespace RocketLazyload\Tests\Unit\Assets; use Brain\Monkey\Functions; +use Brain\Monkey\Filters; use RocketLazyload\Assets; use RocketLazyload\Tests\Unit\TestCase; @@ -27,7 +28,10 @@ protected function set_up() { * @param array $args An array of arguments to configure the inline script. * @param string $expected the expected HTML. */ - public function testShouldReturnYoutubeThumbnailScript( $args, $expected ) { + public function testShouldReturnYoutubeThumbnailScript( $args, $excluded, $expected ) { + Filters\expectApplied( 'rocket_lazyload_exclude_youtube_thumbnail' ) + ->andReturn( $excluded ); + $this->assertSame( $expected, $this->assets->getYoutubeThumbnailScript( $args ) @@ -43,37 +47,43 @@ public function youtubeDataProvider() { return [ [ [], - $this->add_element( '' ), + [], + $this->add_element( '', '[]', 'https://i.ytimg.com/vi/ID/hqdefault.jpg' ), ], [ [ 'resolution' => 'mqdefault', ], - $this->add_element( '' ), + [], + $this->add_element( '', '[]', 'https://i.ytimg.com/vi/ID/mqdefault.jpg' ), ], [ [ 'resolution' => 'sddefault', ], - $this->add_element( '' ), + [], + $this->add_element( '', '[]', 'https://i.ytimg.com/vi/ID/sddefault.jpg' ), ], [ [ 'resolution' => 'hqdefault', ], - $this->add_element( '' ), + [], + $this->add_element( '', '[]', 'https://i.ytimg.com/vi/ID/hqdefault.jpg' ), ], [ [ 'resolution' => 'maxresdefault', ], - $this->add_element( '' ), + [], + $this->add_element( '', '[]', 'https://i.ytimg.com/vi/ID/maxresdefault.jpg' ), ], [ [ 'resolution' => 'ultra', ], - $this->add_element( '' ), + [], + $this->add_element( '', '[]', 'https://i.ytimg.com/vi/ID/hqdefault.jpg' ), ], [ [ @@ -81,7 +91,8 @@ public function youtubeDataProvider() { 'lazy_image' => true, 'native' => false, ], - $this->add_element( '' ), + [], + $this->add_element( '', '[]', 'https://i.ytimg.com/vi/ID/hqdefault.jpg' ), ], [ [ @@ -89,7 +100,8 @@ public function youtubeDataProvider() { 'lazy_image' => true, 'native' => true, ], - $this->add_element( '' ), + [], + $this->add_element( '', '[]', 'https://i.ytimg.com/vi/ID/hqdefault.jpg' ), ], [ [ @@ -98,16 +110,19 @@ public function youtubeDataProvider() { 'native' => true, 'extension' => 'webp', ], - $this->add_element( '' ), + [], + $this->add_element( '', '[]', 'https://i.ytimg.com/vi_webp/ID/hqdefault.webp' ), ], [ [ 'resolution' => 'hqdefault', 'lazy_image' => true, 'native' => true, - 'extension' => 'webp', ], - "", + [ + 'https://i.ytimg.com/vi/12345/hqdefault.jpg', + ], + $this->add_element( '', '["https:\/\/i.ytimg.com\/vi\/12345\/hqdefault.jpg"]', 'https://i.ytimg.com/vi/ID/hqdefault.jpg' ), ], [ [ @@ -116,12 +131,13 @@ public function youtubeDataProvider() { 'native' => true, 'extension' => 'webp', ], - "" + [], + "" ], ]; } - private function add_element( $element ) { - return ""; + private function add_element( $element, $excluded_patterns, $image_url ) { + return ""; } } diff --git a/src/Assets.php b/src/Assets.php index adbddbd..8fcd3d5 100644 --- a/src/Assets.php +++ b/src/Assets.php @@ -267,7 +267,20 @@ public function getYoutubeThumbnailScript( $args = [] ) { } } - return ""; + /** + * Filters the patterns excluded from lazyload for youtube thumbnails. + * + * @param array $excluded_patterns Array of excluded patterns. + */ + $excluded_patterns = apply_filters( 'rocket_lazyload_exclude_youtube_thumbnail', [] ); + + if ( ! is_array( $excluded_patterns ) ) { + $excluded_patterns = []; + } + + $excluded_patterns = wp_json_encode( $excluded_patterns ); + + return ""; } /**