#is', $in); // extract img tags and add lazyload attribs. if (preg_match_all('#]*src[^>]*>#Usmi', $out, $matches)) { foreach ($matches[0] as $tag) { $to_replace[$tag] = $this->add_lazyload_for_images($tag); } $out = str_replace(array_keys($to_replace), array_values($to_replace), $out); } // restore noscript tags. $out = twoptimizeBase::restore_marked_content('SCRIPT', $out); return $out; } public function filter_lazyload_iframes($in) { // only used is image optimization is NOT active but lazyload is. $to_replace = array(); // hide (no)script tags to avoid nesting noscript tags (as lazyloaded images add noscript). $out = twoptimizeBase::replace_contents_with_marker_if_exists('SCRIPT', '#is', $in); // extract img tags and add lazyload attribs. if (preg_match_all('#]*src[^>]*>#Usmi', $out, $matches)) { // only used is image optimization is NOT active but lazyload is. foreach ($matches[0] as $tag) { $to_replace[$tag] = $this->add_lazyload_for_iframe($tag); } $out = str_replace(array_keys($to_replace), array_values($to_replace), $out); } // restore noscript tags. $out = twoptimizeBase::restore_marked_content('SCRIPT', $out); return $out; } public function filter_lazyload_video($in) { // only used is image optimization is NOT active but lazyload is. $to_replace = array(); // hide (no)script tags to avoid nesting noscript tags (as lazyloaded images add noscript). $out = twoptimizeBase::replace_contents_with_marker_if_exists('SCRIPT', '#is', $in); // extract img tags and add lazyload attribs. if (preg_match_all('#]*>(.*?)#', $out, $matches)) { // only used is image optimization is NOT active but lazyload is. foreach ($matches[0] as $tag) { $to_replace[$tag] = $this->add_lazyload_for_video($tag); } $out = str_replace(array_keys($to_replace), array_values($to_replace), $out); } // restore noscript tags. $out = twoptimizeBase::restore_marked_content('SCRIPT', $out); return $out; } public function isExcluded($tag) { /* * keywords for popular sliders * exclude slider images * */ $slider_keywords = array( 'slider', 'soliloquy', ); foreach ($slider_keywords as $val) { if (strpos($tag, $val)) { return true; } } preg_match('@src="([^"]+)"@', $tag, $match); $src = array_pop($match); if (!isset($src) || $src === '') { return true; } $two_exclude_lazyload = get_option('two_exclude_lazyload'); if (isset($two_exclude_lazyload) && !empty($two_exclude_lazyload)) { $exclude_lazyload = explode(',', $two_exclude_lazyload); foreach ($exclude_lazyload as $name) { if (strpos($src, $name)) { return true; } } } return false; } public function add_lazyload_for_images($tag) { //this one is reverse, if image is excluded or option is disabled we add attribute if ($this->isImage($tag) && ($this->isExcluded($tag) || get_option('two_lazyload', 'off') === 'off')) { return str_replace(' src=', ' data-pagespeed-no-defer src=', $tag); } return $tag; } private function add_lazyload_for_iframe($tag) { if ($this->isIframe($tag) && !$this->isExcluded($tag) && get_option('two_iframe_lazyload', 'off') === 'on') { $noscriptTag = ''; // insert lazyload class. $tag = $this->inject_classes_in_tag($tag, 'lazyload'); return $noscriptTag . str_replace(' src=', ' src="" data-src=', $tag); } return $tag; } private function add_lazyload_for_video($tag) { if ($this->isVideo($tag) && !$this->isExcluded($tag) && get_option('two_video_lazyload', 'off') === 'on') { $noscriptTag = ''; // insert lazyload class. $tag = $this->inject_classes_in_tag($tag, 'lazyload'); // Get src from source tag preg_match_all('@src="([^"]+)"@', $tag, $match); $src = array_pop($match); // Get type from source tag preg_match_all('@type="([^"]+)"@', $tag, $match); $type = array_pop($match); // Get data-src attr accoording lib requirements $data_src = ''; $srcCount = count($src); for ($i = 0; $i < $srcCount; $i++) { $t = explode('.', $src[$i]); if (!isset($type[$i])) { $type[$i] = 'video/' . end($t); } $data_src .= $src[$i] . '|' . $type[$i] . ','; } $data_src = rtrim($data_src, ','); $tag = str_replace(']+>/i', ' ', $tag, -1, $count); $tag = str_replace(array(' srcset=', ' sizes='), array(' data-srcset=', ' data-sizes='), $tag); // add the noscript-tag from earlier. return $noscriptTag . $tag; } return $tag; } private function isImage($tag) { return strpos($tag, '