return $data;
}
if ( ! preg_match( '/]*>/i', $post->post_content, $img_match ) ) {
if ( ! preg_match_all( '/
]*>/i', $post->post_content, $img_matches ) ) {
return $data;
}
$img = $img_match[0];
$fallback_img = '';
foreach ( $img_matches[0] as $img ) {
if ( fl_is_lcp_candidate_img( $img ) ) {
$data = fl_extract_img_data( $img );
if ( ! empty( $data['id'] ) || ! empty( $data['src'] ) ) {
return $data;
}
}
if ( ! $fallback_img && fl_is_non_avatar_content_img( $img ) ) {
$fallback_img = $img;
}
}
if ( $fallback_img ) {
return fl_extract_img_data( $fallback_img );
}
return $data;
}
function fl_extract_img_data( $img ) {
$data = array(
'id' => 0,
'src' => '',
);
if ( preg_match( '/wp-image-([0-9]+)/i', $img, $id_match ) ) {
$data['id'] = absint( $id_match[1] );
}
return $data;
}
function fl_img_get_attr( $html, $name ) {
if ( preg_match( '/\s' . preg_quote( $name, '/' ) . '=(["\'])(.*?)\1/i', $html, $m ) ) {
return html_entity_decode( $m[2] );
}
return '';
}
function fl_is_non_avatar_content_img( $img ) {
$class = strtolower( fl_img_get_attr( $img, 'class' ) );
$src = strtolower( fl_img_get_attr( $img, 'src' ) );
$style = strtolower( fl_img_get_attr( $img, 'style' ) );
if ( ! $src || 0 === stripos( $src, 'data:' ) ) {
return false;
}
$blocked = array(
'avatar',
'author',
'entry-author',
'profile',
'ceris-img-logo',
'wmu-preview-img',
);
foreach ( $blocked as $needle ) {
if ( false !== strpos( $class, $needle ) || false !== strpos( $src, $needle ) ) {
return false;
}
}
if ( false !== strpos( $style, 'border-radius: 50%' ) || false !== strpos( $style, 'border-radius:50%' ) ) {
return false;
}
return true;
}
function fl_is_lcp_candidate_img( $img ) {
if ( ! fl_is_non_avatar_content_img( $img ) ) {
return false;
}
return (bool) preg_match( '/\bwp-image-[0-9]+\b/i', $img );
}
function fl_first_content_image_id( $post ) {
$data = fl_first_content_image_data( $post );
$post = get_queried_object();
$data = fl_first_content_image_data( $post );
$id = get_post_thumbnail_id( $post ) ?: $data['id'];
$id = $data['id'] ?: get_post_thumbnail_id( $post );
$src = $data['src'];
if ( ! $src && $id ) {
return $content;
}
$first = true;
$lcp_done = false;
return preg_replace_callback( '/
]*>/i', function ( $m ) use ( &$first ) {
return preg_replace_callback( '/
]*>/i', function ( $m ) use ( &$lcp_done ) {
$img = $m[0];
$img = fl_img_set_attr( $img, 'decoding', 'async' );
if ( $first ) {
if ( ! $lcp_done && fl_is_lcp_candidate_img( $img ) ) {
$img = fl_img_set_attr( $img, 'data-no-lazy', '1' );
$img = fl_img_set_attr( $img, 'loading', 'eager' );
$img = fl_img_set_attr( $img, 'fetchpriority', 'high' );
$first = false;
$lcp_done = true;
} else {
$img = fl_img_set_attr( $img, 'loading', 'lazy' );
$img = fl_img_set_attr( $img, 'fetchpriority', 'auto' );