From 3bf49dabfb11339af3b379a8cabcf36d9c89607a Mon Sep 17 00:00:00 2001 From: Michael Lee Date: Thu, 18 Jan 2024 12:16:48 +0100 Subject: [PATCH 1/8] Exclude youtube thumbnail from lazyload --- inc/Dependencies/RocketLazyload/Iframe.php | 25 +++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/inc/Dependencies/RocketLazyload/Iframe.php b/inc/Dependencies/RocketLazyload/Iframe.php index d5143f3966..81bec8104b 100644 --- a/inc/Dependencies/RocketLazyload/Iframe.php +++ b/inc/Dependencies/RocketLazyload/Iframe.php @@ -49,7 +49,7 @@ public function lazyloadIframes( $html, $buffer, $args = [] ) { continue; } - if ( $args['youtube'] ) { + if ( $args['youtube'] && ! $this->is_youtube_excluded( $iframe ) ) { $iframe_lazyload = $this->replaceYoutubeThumbnail( $iframe ); } @@ -234,4 +234,27 @@ public function cleanYoutubeUrl( $url ) { return $scheme . $host . $path; } + + /** + * Checks if a youtube thumbnail is excluded from lazyload. + * + * @param array $iframe Array of matched patterns. + * @return boolean + */ + private function is_youtube_excluded( array $iframe ): bool { + /** + * 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', [] ); + + foreach ( $excluded_patterns as $excluded_pattern ) { + if ( strpos( $iframe[0], $excluded_pattern ) !== false ) { + return true; + } + } + + return false; + } } From e183b654d918fee82bf15a63934335d5dc89bc1a Mon Sep 17 00:00:00 2001 From: Michael Lee Date: Tue, 23 Jan 2024 15:00:51 +0100 Subject: [PATCH 2/8] Guard filter return --- inc/Dependencies/RocketLazyload/Iframe.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/inc/Dependencies/RocketLazyload/Iframe.php b/inc/Dependencies/RocketLazyload/Iframe.php index 81bec8104b..b20819ff83 100644 --- a/inc/Dependencies/RocketLazyload/Iframe.php +++ b/inc/Dependencies/RocketLazyload/Iframe.php @@ -249,6 +249,10 @@ private function is_youtube_excluded( array $iframe ): bool { */ $excluded_patterns = apply_filters( 'rocket_lazyload_exclude_youtube_thumbnail', [] ); + if ( ! is_array( $excluded_patterns ) ) { + return false; + } + foreach ( $excluded_patterns as $excluded_pattern ) { if ( strpos( $iframe[0], $excluded_pattern ) !== false ) { return true; From 908b20c40605fe5165eea2f568339ce86d74cd08 Mon Sep 17 00:00:00 2001 From: Michael Lee Date: Fri, 26 Jan 2024 19:18:20 +0100 Subject: [PATCH 3/8] Removed filter and method --- inc/Dependencies/RocketLazyload/Iframe.php | 29 +--------------------- 1 file changed, 1 insertion(+), 28 deletions(-) diff --git a/inc/Dependencies/RocketLazyload/Iframe.php b/inc/Dependencies/RocketLazyload/Iframe.php index b20819ff83..d5143f3966 100644 --- a/inc/Dependencies/RocketLazyload/Iframe.php +++ b/inc/Dependencies/RocketLazyload/Iframe.php @@ -49,7 +49,7 @@ public function lazyloadIframes( $html, $buffer, $args = [] ) { continue; } - if ( $args['youtube'] && ! $this->is_youtube_excluded( $iframe ) ) { + if ( $args['youtube'] ) { $iframe_lazyload = $this->replaceYoutubeThumbnail( $iframe ); } @@ -234,31 +234,4 @@ public function cleanYoutubeUrl( $url ) { return $scheme . $host . $path; } - - /** - * Checks if a youtube thumbnail is excluded from lazyload. - * - * @param array $iframe Array of matched patterns. - * @return boolean - */ - private function is_youtube_excluded( array $iframe ): bool { - /** - * 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 ) ) { - return false; - } - - foreach ( $excluded_patterns as $excluded_pattern ) { - if ( strpos( $iframe[0], $excluded_pattern ) !== false ) { - return true; - } - } - - return false; - } } From 8eb7c82027ea0642a2bd0154ea94ef9482588be3 Mon Sep 17 00:00:00 2001 From: Michael Lee Date: Fri, 26 Jan 2024 19:29:47 +0100 Subject: [PATCH 4/8] Added exclusions to dynamic script --- inc/Dependencies/RocketLazyload/Assets.php | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/inc/Dependencies/RocketLazyload/Assets.php b/inc/Dependencies/RocketLazyload/Assets.php index c388ae6a0d..53ddc57c36 100644 --- a/inc/Dependencies/RocketLazyload/Assets.php +++ b/inc/Dependencies/RocketLazyload/Assets.php @@ -267,7 +267,15 @@ 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', [] ); + $excluded_patterns = wp_json_encode( $excluded_patterns ); + + return ""; } /** From a91482a9b953dbf4563032272878e737ec24b654 Mon Sep 17 00:00:00 2001 From: Michael Lee Date: Fri, 26 Jan 2024 19:30:45 +0100 Subject: [PATCH 5/8] Register new event --- inc/Engine/Media/Lazyload/Subscriber.php | 1 + 1 file changed, 1 insertion(+) diff --git a/inc/Engine/Media/Lazyload/Subscriber.php b/inc/Engine/Media/Lazyload/Subscriber.php index 3f4ff83cd2..d8bb7e6a35 100644 --- a/inc/Engine/Media/Lazyload/Subscriber.php +++ b/inc/Engine/Media/Lazyload/Subscriber.php @@ -89,6 +89,7 @@ public static function get_subscribed_events() { 'rocket_lazyload_excluded_attributes' => 'add_exclusions', 'rocket_lazyload_excluded_src' => 'add_exclusions', 'rocket_lazyload_iframe_excluded_patterns' => 'add_exclusions', + 'rocket_lazyload_exclude_youtube_thumbnail' => 'add_exclusions', ]; } From 3ea9ddd1d9c8a32b807df40896aeefd96d57cdfc Mon Sep 17 00:00:00 2001 From: Michael Lee Date: Fri, 26 Jan 2024 19:35:04 +0100 Subject: [PATCH 6/8] Fixed phpcs --- inc/Engine/Media/Lazyload/Subscriber.php | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/inc/Engine/Media/Lazyload/Subscriber.php b/inc/Engine/Media/Lazyload/Subscriber.php index d8bb7e6a35..8532129706 100644 --- a/inc/Engine/Media/Lazyload/Subscriber.php +++ b/inc/Engine/Media/Lazyload/Subscriber.php @@ -75,20 +75,20 @@ public function __construct( Options_Data $options, Assets $assets, Image $image */ public static function get_subscribed_events() { return [ - 'wp_footer' => [ + 'wp_footer' => [ [ 'insert_lazyload_script', PHP_INT_MAX ], [ 'insert_youtube_thumbnail_script', PHP_INT_MAX ], ], - 'wp_head' => [ 'insert_nojs_style', PHP_INT_MAX ], - 'wp_enqueue_scripts' => [ 'insert_youtube_thumbnail_style', PHP_INT_MAX ], - 'rocket_buffer' => [ 'lazyload', 18 ], - 'rocket_lazyload_html' => 'lazyload_responsive', - 'init' => 'lazyload_smilies', - 'wp' => 'deactivate_lazyload_on_specific_posts', - 'wp_lazy_loading_enabled' => [ 'maybe_disable_core_lazyload', 10, 2 ], - 'rocket_lazyload_excluded_attributes' => 'add_exclusions', - 'rocket_lazyload_excluded_src' => 'add_exclusions', - 'rocket_lazyload_iframe_excluded_patterns' => 'add_exclusions', + 'wp_head' => [ 'insert_nojs_style', PHP_INT_MAX ], + 'wp_enqueue_scripts' => [ 'insert_youtube_thumbnail_style', PHP_INT_MAX ], + 'rocket_buffer' => [ 'lazyload', 18 ], + 'rocket_lazyload_html' => 'lazyload_responsive', + 'init' => 'lazyload_smilies', + 'wp' => 'deactivate_lazyload_on_specific_posts', + 'wp_lazy_loading_enabled' => [ 'maybe_disable_core_lazyload', 10, 2 ], + 'rocket_lazyload_excluded_attributes' => 'add_exclusions', + 'rocket_lazyload_excluded_src' => 'add_exclusions', + 'rocket_lazyload_iframe_excluded_patterns' => 'add_exclusions', 'rocket_lazyload_exclude_youtube_thumbnail' => 'add_exclusions', ]; } From 6b0127d77d8b18549d0b4a5238627c6ac96d0b40 Mon Sep 17 00:00:00 2001 From: Michael Lee Date: Tue, 13 Feb 2024 15:34:04 +0100 Subject: [PATCH 7/8] Guarded filter from null --- inc/Dependencies/RocketLazyload/Assets.php | 5 +++++ inc/Engine/Media/Lazyload/Subscriber.php | 6 +++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/inc/Dependencies/RocketLazyload/Assets.php b/inc/Dependencies/RocketLazyload/Assets.php index 53ddc57c36..7033584223 100644 --- a/inc/Dependencies/RocketLazyload/Assets.php +++ b/inc/Dependencies/RocketLazyload/Assets.php @@ -273,6 +273,11 @@ public function getYoutubeThumbnailScript( $args = [] ) { * @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 ""; diff --git a/inc/Engine/Media/Lazyload/Subscriber.php b/inc/Engine/Media/Lazyload/Subscriber.php index 8532129706..d26db2678b 100644 --- a/inc/Engine/Media/Lazyload/Subscriber.php +++ b/inc/Engine/Media/Lazyload/Subscriber.php @@ -456,7 +456,11 @@ public function maybe_disable_core_lazyload( $value, $tag_name ) { * @param array $exclusions Array of excluded patterns. * @return array */ - public function add_exclusions( array $exclusions ): array { + public function add_exclusions( $exclusions ): array { + if ( ! is_array( $exclusions ) ) { + $exclusions = []; + } + $exclude_lazyload = $this->options->get( 'exclude_lazyload', [] ); if ( empty( $exclude_lazyload ) ) { From ec991c1b41f106cbe15f77d6e2230c16aee06c6e Mon Sep 17 00:00:00 2001 From: Michael Lee Date: Tue, 13 Feb 2024 15:37:50 +0100 Subject: [PATCH 8/8] phpcs fix --- inc/Engine/Media/Lazyload/Subscriber.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inc/Engine/Media/Lazyload/Subscriber.php b/inc/Engine/Media/Lazyload/Subscriber.php index d26db2678b..50ddd59197 100644 --- a/inc/Engine/Media/Lazyload/Subscriber.php +++ b/inc/Engine/Media/Lazyload/Subscriber.php @@ -460,7 +460,7 @@ public function add_exclusions( $exclusions ): array { if ( ! is_array( $exclusions ) ) { $exclusions = []; } - + $exclude_lazyload = $this->options->get( 'exclude_lazyload', [] ); if ( empty( $exclude_lazyload ) ) {