MAX_FILE_SIZE) { return $html . "\n" . ""; } return $html . "\n" . ""; } // Replace attributes (src, srcset, data-src, etc) foreach ($this->tags as $tag) { $elems = $dom->find($tag); foreach ($elems as $elem) { $attributes = $elem->getAllAttributes(); foreach ($attributes as $attr_name => $attr_value) { if ($this->is_image_attribute($attr_name)) { $elem->setAttribute($attr_name, $this->handle_attribute($attr_value)); } } } } return $dom->save(); } /** * Append image urls with `.webp` extension * * @param strinng $url - Image URL * @return string */ private function replace_url($url) { return $url . '.webp'; } /** * If webp version for supplied image url is available then replace extension * * @param string $url - URL of image * @return string */ private function maybe_replace_url($url) { if ($this->is_webp_version_available($url)) { $url = $this->replace_url($url); } return $url; } /** * Modifies src attribute value, if needed * * @param string $attr_value * @return string */ private function handle_src($attr_value) { return $this->maybe_replace_url($attr_value); } /** * Modified `srcset` attribute value, if needed * * @param string $attr_value * @return string */ private function handle_srcset($attr_value) { // $attr_value is ie: $srcset_arr = explode(',', $attr_value); foreach ($srcset_arr as $i => $srcset_entry) { // $srcset_entry is ie "image.jpg 520w", but can also lack width, ie just "image.jpg" // it can also be ie "image.jpg 2x" $srcset_entry = trim($srcset_entry); $entry_parts = preg_split('/\s+/', $srcset_entry, 2); if (count($entry_parts) == 2) { list($src, $descriptors) = $entry_parts; } else { $src = $srcset_entry; $descriptors = null; } $url = $this->maybe_replace_url($src); $srcset_arr[$i] = $url . (isset($descriptors) ? ' ' . $descriptors : ''); } return implode(', ', $srcset_arr); } /** * Decides whether given value is a `srcset` or not * * @return bool */ private function looks_like_srcset($value) { if (preg_match('#\s\d*(w|x)#', $value)) { return true; } return false; } /** * Handle attribute value based on attribute name, src or srcset * * @return string */ private function handle_attribute($value) { if ($this->looks_like_srcset($value)) { return $this->handle_srcset($value); } return $this->handle_src($value); } /** * Decide whether given attribute name is an image attribute or not * * @return bool */ private function is_image_attribute($attr_name) { return preg_match('#^(src|srcset|poster|(data-[^=]*(lazy|small|slide|img|large|src|thumb|source|set|bg-url)[^=]*))$#i', $attr_name); } /** * Does webp image file exists * * @param string $url * @return boolean */ private function is_webp_version_available($url) { $filename = WP_Optimize_Utils::get_file_path($url); if (empty($filename)) return false; return file_exists($filename . '.webp'); } } endif;