| Server IP : 68.183.124.220 / Your IP : 216.73.217.137 Web Server : Apache/2.4.18 (Ubuntu) System : Linux Sandbox-A 4.4.0-210-generic #242-Ubuntu SMP Fri Apr 16 09:57:56 UTC 2021 x86_64 User : gavin ( 1000) PHP Version : 7.0.33-0ubuntu0.16.04.16 Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority, MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : ON | Sudo : ON | Pkexec : ON Directory : /var/www/html/wp-content/plugins/rocket-lazy-load/src/Dependencies/RocketLazyload/ |
Upload File : |
<?php
/**
* Handles lazyloading of iframes
*
* @package RocketLazyload
*/
namespace RocketLazyLoadPlugin\Dependencies\RocketLazyload;
/**
* A class to provide the methods needed to lazyload iframes in WP Rocket and Lazyload by WP Rocket
*/
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.
* @return string
*/
public function lazyloadIframes($html, $buffer, $args = [])
{
$defaults = [
'youtube' => false,
];
$args = wp_parse_args($args, $defaults);
if (! preg_match_all('@<iframe(?<atts>\s.+)>.*</iframe>@iUs', $buffer, $iframes, PREG_SET_ORDER) ) {
return $html;
}
$iframes = array_unique($iframes, SORT_REGULAR);
foreach ($iframes as $iframe) {
if ($this->isIframeExcluded($iframe)) {
continue;
}
// Given the previous regex pattern, $iframe['atts'] starts with a whitespace character.
if (! preg_match('@\ssrc\s*=\s*(\'|")(?<src>.*)\1@iUs', $iframe['atts'], $atts)) {
continue;
}
$iframe['src'] = trim($atts['src']);
if ('' === $iframe['src']) {
continue;
}
if ($args['youtube']) {
$iframe_lazyload = $this->replaceYoutubeThumbnail($iframe);
}
if (empty($iframe_lazyload)) {
$iframe_lazyload = $this->replaceIframe($iframe);
}
$html = str_replace($iframe[0], $iframe_lazyload, $html);
unset($iframe_lazyload);
}
return $html;
}
/**
* Checks if the provided iframe is excluded from lazyload
*
* @param array $iframe Array of matched patterns.
* @return boolean
*/
public function isIframeExcluded($iframe)
{
foreach ($this->getExcludedPatterns() as $excluded_pattern) {
if (strpos($iframe[0], $excluded_pattern) !== false) {
return true;
}
}
return false;
}
/**
* Gets patterns excluded from lazyload for iframes
*
* @since 2.1.1
*
* @return array
*/
private function getExcludedPatterns()
{
/**
* Filters the patterns excluded from lazyload for iframes
*
* @since 2.1.1
*
* @param array $excluded_patterns Array of excluded patterns.
*/
return apply_filters(
'rocket_lazyload_iframe_excluded_patterns',
[
'gform_ajax_frame',
'data-no-lazy=',
'recaptcha/api/fallback',
'loading="eager"',
]
);
}
/**
* Applies lazyload on the iframe provided
*
* @param array $iframe Array of matched elements.
* @return string
*/
private function replaceIframe($iframe)
{
/**
* Filter the LazyLoad placeholder on src attribute
*
* @since 1.0
*
* @param string $placeholder placeholder that will be printed.
*/
$placeholder = apply_filters('rocket_lazyload_placeholder', 'about:blank');
$placeholder_atts = str_replace($iframe['src'], $placeholder, $iframe['atts']);
$iframe_lazyload = str_replace($iframe['atts'], $placeholder_atts . ' data-rocket-lazyload="fitvidscompatible" data-lazy-src="' . esc_url($iframe['src']) . '"', $iframe[0]);
if (! preg_match('@\sloading\s*=\s*(\'|")(?:lazy|auto)\1@i', $iframe_lazyload)) {
$iframe_lazyload = str_replace('<iframe', '<iframe loading="lazy"', $iframe_lazyload);
}
/**
* Filter the LazyLoad HTML output on iframes
*
* @since 1.0
*
* @param array $html Output that will be printed.
*/
$iframe_lazyload = apply_filters('rocket_lazyload_iframe_html', $iframe_lazyload);
$iframe_lazyload .= '<noscript>' . $iframe[0] . '</noscript>';
return $iframe_lazyload;
}
/**
* Replaces the iframe provided by the Youtube thumbnail
*
* @param array $iframe Array of matched elements.
* @return bool|string
*/
private function replaceYoutubeThumbnail($iframe)
{
$youtube_id = $this->getYoutubeIDFromURL($iframe['src']);
if (! $youtube_id) {
return false;
}
$query = wp_parse_url(htmlspecialchars_decode($iframe['src']), PHP_URL_QUERY);
/**
* Filter the LazyLoad HTML output on Youtube iframes
*
* @since 2.11
*
* @param array $html Output that will be printed.
*/
$youtube_lazyload = apply_filters('rocket_lazyload_youtube_html', '<div class="rll-youtube-player" data-id="' . esc_attr($youtube_id) . '" data-query="' . esc_attr($query) . '"></div>');
$youtube_lazyload .= '<noscript>' . $iframe[0] . '</noscript>';
return $youtube_lazyload;
}
/**
* Gets the Youtube ID from the URL provided
*
* @param string $url URL to search.
* @return bool|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;
}
// exclude playlist.
if ('videoseries' === $matches[1]) {
return false;
}
return $matches[1];
}
}