Skip to content

Commit

Permalink
Backport change from wprocket
Browse files Browse the repository at this point in the history
  • Loading branch information
jeawhanlee committed Feb 14, 2024
1 parent 5ec5c1f commit 565da9e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 25 deletions.
15 changes: 14 additions & 1 deletion src/Assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,20 @@ public function getYoutubeThumbnailScript( $args = [] ) {
}
}

return "<script>function lazyLoadThumb(e,alt){var t='{$image}',a='<button class=\"play\" aria-label=\"play Youtube video\"></button>';t=t.replace('alt=\"\"','alt=\"'+alt+'\"');return t.replace(\"ID\",e)+a}function lazyLoadYoutubeIframe(){var e=document.createElement(\"iframe\"),t=\"ID?autoplay=1\";t+=0===this.parentNode.dataset.query.length?'':'&'+this.parentNode.dataset.query;e.setAttribute(\"src\",t.replace(\"ID\",this.parentNode.dataset.src)),e.setAttribute(\"frameborder\",\"0\"),e.setAttribute(\"allowfullscreen\",\"1\"),e.setAttribute(\"allow\", \"accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture\"),this.parentNode.parentNode.replaceChild(e,this.parentNode)}document.addEventListener(\"DOMContentLoaded\",function(){var e,t,p,a=document.getElementsByClassName(\"rll-youtube-player\");for(t=0;t<a.length;t++)e=document.createElement(\"div\"),e.setAttribute(\"data-id\",a[t].dataset.id),e.setAttribute(\"data-query\", a[t].dataset.query),e.setAttribute(\"data-src\", a[t].dataset.src),e.innerHTML=lazyLoadThumb(a[t].dataset.id,a[t].dataset.alt),a[t].appendChild(e),p=e.querySelector('.play'),p.onclick=lazyLoadYoutubeIframe});</script>";
/**
* 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 "<script>function lazyLoadThumb(e,alt,l){var t='{$image}',a='<button class=\"play\" aria-label=\"play Youtube video\"></button>';if(l){t=t.replace('data-lazy-','');t=t.replace('loading=\"lazy\"','');t=t.replace(/<noscript>.*?<\/noscript>/g,'');}t=t.replace('alt=\"\"','alt=\"'+alt+'\"');return t.replace(\"ID\",e)+a}function lazyLoadYoutubeIframe(){var e=document.createElement(\"iframe\"),t=\"ID?autoplay=1\";t+=0===this.parentNode.dataset.query.length?\"\":\"&\"+this.parentNode.dataset.query;e.setAttribute(\"src\",t.replace(\"ID\",this.parentNode.dataset.src)),e.setAttribute(\"frameborder\",\"0\"),e.setAttribute(\"allowfullscreen\",\"1\"),e.setAttribute(\"allow\",\"accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture\"),this.parentNode.parentNode.replaceChild(e,this.parentNode)}document.addEventListener(\"DOMContentLoaded\",function(){var exclusions={$excluded_patterns};var e,t,p,u,l,a=document.getElementsByClassName(\"rll-youtube-player\");for(t=0;t<a.length;t++)(e=document.createElement(\"div\")),(u='{$image_url}'),(u=u.replace('ID',a[t].dataset.id)),(l=exclusions.some(exclusion=>u.includes(exclusion))),e.setAttribute(\"data-id\",a[t].dataset.id),e.setAttribute(\"data-query\",a[t].dataset.query),e.setAttribute(\"data-src\",a[t].dataset.src),(e.innerHTML=lazyLoadThumb(a[t].dataset.id,a[t].dataset.alt,l)),a[t].appendChild(e),(p=e.querySelector(\".play\")),(p.onclick=lazyLoadYoutubeIframe)});</script>";
}

/**
Expand Down
25 changes: 1 addition & 24 deletions src/Iframe.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}

Expand Down Expand Up @@ -234,27 +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', [] );

foreach ( $excluded_patterns as $excluded_pattern ) {
if ( strpos( $iframe[0], $excluded_pattern ) !== false ) {
return true;
}
}

return false;
}
}

0 comments on commit 565da9e

Please sign in to comment.