Skip to content
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

Closes #5723 Add a filter to enable/disable the noscript tag of lazyloaded images #6824

Merged
merged 3 commits into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions inc/Dependencies/RocketLazyload/Assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class Assets {
/**
* Inserts the lazyload script in the HTML
*
* @param array $args Array of arguments to populate the lazyload script tag.
* @param array<string> $args Array of arguments to populate the lazyload script tag.
* @return void
*/
public function insertLazyloadScript( $args = [] ) {
Expand All @@ -27,7 +27,7 @@ public function insertLazyloadScript( $args = [] ) {
/**
* Gets the inline lazyload script configuration
*
* @param array $args Array of arguments to populate the lazyload script options.
* @param array<string, int> $args Array of arguments to populate the lazyload script options.
* @return string
*/
public function getInlineLazyloadScript( $args = [] ) {
Expand Down Expand Up @@ -177,7 +177,7 @@ class_loaded: "lazyloaded",
/**
* Returns the lazyload inline script
*
* @param array $args Array of arguments to populate the lazyload script options.
* @param array<string> $args Array of arguments to populate the lazyload script options.
* @return string
*/
public function getLazyloadScript( $args = [] ) {
Expand All @@ -194,15 +194,15 @@ public function getLazyloadScript( $args = [] ) {
*
* @since 2.2.6
*
* @param $script_tag HTML tag for the lazyload script.
* @param string $script_tag HTML tag for the lazyload script.
*/
return apply_filters( 'rocket_lazyload_script_tag', '<script data-no-minify="1" async src="' . $args['base_url'] . $args['version'] . '/lazyload' . $min . '.js"></script>' ); // phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedScript
}

/**
* Inserts in the HTML the script to replace the Youtube thumbnail by the iframe.
*
* @param array $args Array of arguments to populate the script options.
* @param array<string, bool> $args Array of arguments to populate the script options.
* @return void
*/
public function insertYoutubeThumbnailScript( $args = [] ) {
Expand All @@ -212,7 +212,7 @@ public function insertYoutubeThumbnailScript( $args = [] ) {
/**
* Returns the Youtube Thumbnail inline script
*
* @param array $args Array of arguments to populate the script options.
* @param array<string, bool> $args Array of arguments to populate the script options.
* @return string
*/
public function getYoutubeThumbnailScript( $args = [] ) {
Expand Down Expand Up @@ -286,7 +286,7 @@ public function getYoutubeThumbnailScript( $args = [] ) {
/**
* Inserts the CSS to style the Youtube thumbnail container
*
* @param array $args Array of arguments to populate the CSS.
* @param array<string, bool> $args Array of arguments to populate the CSS.
* @return void
*/
public function insertYoutubeThumbnailCSS( $args = [] ) {
Expand All @@ -298,7 +298,7 @@ public function insertYoutubeThumbnailCSS( $args = [] ) {
/**
* Returns the CSS for the Youtube Thumbnail
*
* @param array $args Array of arguments to populate the CSS.
* @param array<string, bool> $args Array of arguments to populate the CSS.
* @return string
*/
public function getYoutubeThumbnailCSS( $args = [] ) {
Expand All @@ -320,6 +320,8 @@ public function getYoutubeThumbnailCSS( $args = [] ) {

/**
* Inserts the CSS needed when Javascript is not enabled to keep the display correct
*
* @return void
*/
public function insertNoJSCSS() {
echo $this->getNoJSCSS(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
Expand Down
40 changes: 25 additions & 15 deletions inc/Dependencies/RocketLazyload/Iframe.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ class Iframe {
/**
* Finds iframes in the HTML provided and call the methods to lazyload them
*
* @param string $html Original HTML.
* @param string $buffer Content to parse.
* @param array $args Array of arguments to use.
* @param string $html Original HTML.
* @param string $buffer Content to parse.
* @param array<bool> $args Array of arguments to use.
*
* @return string
*/
public function lazyloadIframes( $html, $buffer, $args = [] ) {
Expand Down Expand Up @@ -49,6 +50,8 @@ public function lazyloadIframes( $html, $buffer, $args = [] ) {
continue;
}

$iframe_lazyload = '';

if ( $args['youtube'] ) {
$iframe_lazyload = $this->replaceYoutubeThumbnail( $iframe );
}
Expand All @@ -68,7 +71,7 @@ public function lazyloadIframes( $html, $buffer, $args = [] ) {
/**
* Checks if the provided iframe is excluded from lazyload
*
* @param array $iframe Array of matched patterns.
* @param array<string> $iframe Array of matched patterns.
* @return boolean
*/
public function isIframeExcluded( $iframe ) {
Expand All @@ -87,7 +90,7 @@ public function isIframeExcluded( $iframe ) {
*
* @since 2.1.1
*
* @return array
* @return array<string>
*/
private function getExcludedPatterns() {
/**
Expand All @@ -114,7 +117,8 @@ private function getExcludedPatterns() {
/**
* Applies lazyload on the iframe provided
*
* @param array $iframe Array of matched elements.
* @param array<string> $iframe Array of matched elements.
*
* @return string
*/
private function replaceIframe( $iframe ) {
Expand All @@ -139,7 +143,7 @@ private function replaceIframe( $iframe ) {
*
* @since 1.0
*
* @param array $html Output that will be printed.
* @param string $html Output that will be printed.
*/
$iframe_lazyload = apply_filters( 'rocket_lazyload_iframe_html', $iframe_lazyload );
$iframe_lazyload .= '<noscript>' . $iframe[0] . '</noscript>';
Expand All @@ -150,18 +154,23 @@ private function replaceIframe( $iframe ) {
/**
* Replaces the iframe provided by the Youtube thumbnail
*
* @param array $iframe Array of matched elements.
* @return bool|string
* @param array<string> $iframe Array of matched elements.
*
* @return string
*/
private function replaceYoutubeThumbnail( $iframe ) {
$youtube_id = $this->getYoutubeIDFromURL( $iframe['src'] );

if ( ! $youtube_id ) {
return false;
if ( '' === $youtube_id ) {
return '';
}

$query = wp_parse_url( htmlspecialchars_decode( $iframe['src'] ), PHP_URL_QUERY );

if ( ! is_string( $query ) ) {
$query = '';
}

$youtube_url = $this->changeYoutubeUrlForYoutuDotBe( $iframe['src'] );
$youtube_url = $this->cleanYoutubeUrl( $iframe['src'] );

Expand All @@ -173,7 +182,7 @@ private function replaceYoutubeThumbnail( $iframe ) {
*
* @since 2.11
*
* @param array $html Output that will be printed.
* @param string $html Output that will be printed.
*/
$youtube_lazyload = apply_filters( 'rocket_lazyload_youtube_html', '<div class="rll-youtube-player" data-src="' . esc_attr( $youtube_url ) . '" data-id="' . esc_attr( $youtube_id ) . '" data-query="' . esc_attr( $query ) . '" data-alt="' . esc_attr( $title ) . '"></div>' );
$youtube_lazyload .= '<noscript>' . $iframe[0] . '</noscript>';
Expand All @@ -185,19 +194,20 @@ private function replaceYoutubeThumbnail( $iframe ) {
* Gets the Youtube ID from the URL provided
*
* @param string $url URL to search.
* @return bool|string
*
* @return string
*/
public function getYoutubeIDFromURL( $url ) {
$pattern = '#^(?:https?:)?(?://)?(?:www\.)?(?:youtu\.be|youtube\.com|youtube-nocookie\.com)/(?:embed/|v/|watch/?\?v=)?([\w-]{11})#iU';
$result = preg_match( $pattern, $url, $matches );

if ( ! $result ) {
return false;
return '';
}

// exclude playlist.
if ( 'videoseries' === $matches[1] ) {
return false;
return '';
}

return $matches[1];
Expand Down
Loading
Loading