ad');\"" : "") . ">";
if($line['as'] == 'image') {
array_unshift(self::$preloads_ready, $preload);
}
else {
self::$preloads_ready[] = $preload;
}
}
}
if(!empty(self::$preloads_ready)) {
$preloads_string = "";
foreach(apply_filters('perfmatters_preloads_ready', self::$preloads_ready) as $preload) {
$preloads_string.= $preload;
}
$pos = strpos($html, '');
if($pos !== false) {
$html = substr_replace($html, '' . $preloads_string, $pos, 8);
}
}
return $html;
}
//add critical image preloads
public static function add_critical_image_preloads(&$html, $clean_html) {
//exclude images from preloading by parent selector
$parent_exclusions = apply_filters('perfmatters_critical_image_parent_exclusions', array());
if(!empty($parent_exclusions)) {
//match all selectors
preg_match_all('#<(div|section|figure)(\s[^>]*?(' . implode('|', $parent_exclusions) . ').*?)>.*?<\/\g1>#is', $clean_html, $selectors, PREG_SET_ORDER);
if(!empty($selectors)) {
foreach($selectors as $selector) {
//match all img tags
preg_match_all('#]+?)\/?>#is', $selector[0], $images, PREG_SET_ORDER);
if(!empty($images)) {
//loop through images
foreach($images as $image) {
//remove image from current working clean html
$clean_html = str_replace($image[0], '', $clean_html);
}
}
}
}
}
//match all image formats
preg_match_all('#(]+?)\/?>(?><\/picture>)?#is', $clean_html, $matches, PREG_SET_ORDER);
if(!empty($matches)) {
$exclusions = apply_filters('perfmatters_critical_image_exclusions', array(
';base64',
'w3.org',
'data-perfmatters-skip-preload'
));
$count = 0;
foreach($matches as $match) {
if($count >= self::$critical_images) {
break;
}
if(strpos($match[0], 'secure.gravatar.com') !== false) {
continue;
}
if(!empty($exclusions) && is_array($exclusions)) {
foreach($exclusions as $exclusion) {
if(strpos($match[0], $exclusion) !== false) {
continue 2;
}
}
}
//picture tag
if(!empty($match[1])) {
preg_match('#