-
Notifications
You must be signed in to change notification settings - Fork 7
feat: Add LazyLoadImages toggle to Carousel block with per-slide override option #100
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
base: develop
Are you sure you want to change the base?
Changes from all commits
c414e58
23b7eae
5d56268
c8ebe4e
f42c33b
69a0ac6
2cc106d
95c1f8f
ed1ffcf
a9e9484
6412c16
1e4944d
a24a33d
01d683a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,6 +10,8 @@ | |
| namespace Rt_Carousel; | ||
|
|
||
| use Rt_Carousel\Traits\Singleton; | ||
| use WP_Block; | ||
| use WP_HTML_Tag_Processor; | ||
|
|
||
| // Exit if accessed directly. | ||
| if ( ! defined( 'ABSPATH' ) ) { | ||
|
|
@@ -41,6 +43,7 @@ protected function setup_hooks(): void { | |
| add_action( 'init', [ $this, 'register_block_patterns' ] ); | ||
| add_action( 'admin_notices', [ $this, 'legacy_plugin_notice' ] ); | ||
| add_action( 'network_admin_notices', [ $this, 'legacy_plugin_notice' ] ); | ||
| add_filter( 'render_block_rt-carousel/carousel', [ $this, 'handle_lazy_load_images' ], 16, 3 ); | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -260,4 +263,57 @@ private function load_patterns_from_disk(): array { | |
|
|
||
| return $data; | ||
| } | ||
|
|
||
| /** | ||
| * Add loading="lazy" to images in carousel slides. | ||
| * | ||
| * @param string $block_content The block content. | ||
| * @param array $parsed_block The parsed block. | ||
| * @param \WP_Block $instance The block instance. | ||
| * | ||
| * @return string Modified block content. | ||
| */ | ||
| public function handle_lazy_load_images( string $block_content, array $parsed_block, WP_Block $instance ): string { | ||
| // $instance was added in WP 5.9.0, if it's not available, return the block content unmodified. | ||
| if ( ! $instance ) { | ||
| return $block_content; | ||
| } | ||
|
|
||
| // Bail early if the lazyLoadImages setting is not set. | ||
| if ( ! isset( $instance->attributes['lazyLoadImages'] ) ) { | ||
| return $block_content; | ||
| } | ||
|
|
||
| $lazy_load = (bool) $instance->attributes['lazyLoadImages']; | ||
|
|
||
| // If lazy loading is disabled, return as-is. | ||
| if ( ! $lazy_load ) { | ||
| return $block_content; | ||
| } | ||
|
|
||
| // Use WP_HTML_Tag_Processor to add loading="lazy" to <img> tags. | ||
| $processor = new WP_HTML_Tag_Processor( $block_content ); | ||
| $slide_index = 0; | ||
|
|
||
| while ( $processor->next_tag( ) ) { | ||
| $tag = $processor->get_tag(); | ||
|
|
||
| // Keep a track of the slide index to determine if an image is in the first slide or subsequent slides. | ||
| if ( 'DIV' === $tag && $processor->has_class( 'embla__slide' ) ) { | ||
| $slide_index++; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use pre-increment instead: ++$slide_index. |
||
| } | ||
|
|
||
| // If it's the first slide, set loading="lazy". For subsequent slides, set loading="eager" and fetchpriority="high". | ||
| if ( 'IMG' === $tag && null === $processor->get_attribute( 'loading' ) ) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use early exist |
||
| if ( 1 === $slide_index ) { | ||
| $processor->set_attribute( 'loading', 'eager' ); | ||
| $processor->set_attribute( 'fetchpriority', 'high' ); | ||
| } else { | ||
| $processor->set_attribute( 'loading', 'lazy' ); | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor PHPCS note — there's a trailing blank line before the closing Suggestion: while ( $processor->next_tag( 'img' ) ) {
if ( null === $processor->get_attribute( 'loading' ) ) {
$processor->set_attribute( 'loading', 'lazy' );
}
} |
||
| return $processor->get_updated_html(); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
space after opening parathesis