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('#]+?image\/(webp|avif)[^>]+?)\/?>#is', $match[0], $source); if(!empty($source)) { if(self::generate_critical_image_preload($source[1])) { $new_picture = str_replace(''; //mark src used and return if(!empty($src)) { self::$used_srcs[] = $src; } return true; } } private static function location_check($locations) { $location_match = false; $exploded_locations = explode(',', $locations); $trimmed_locations = array_map('trim', $exploded_locations); //single post exclusion if(is_singular()) { global $post; if(in_array($post->ID, $trimmed_locations)) { $location_match = true; } } //posts page exclusion elseif(is_home() && in_array('blog', $trimmed_locations)) { $location_match = true; } elseif(is_archive()) { //woocommerce shop check if(function_exists('is_shop') && is_shop()) { if(in_array(wc_get_page_id('shop'), $trimmed_locations)) { $location_match = true; } } } return $location_match; } public static function disable_core_fetch($loading_attrs) { unset($loading_attrs['fetchpriority']); return $loading_attrs; } }