@@ -400,29 +400,34 @@ public function regular_tag_replace( $new_tag, $original_url, $new_url, $optml_a
400400 $ new_tag = $ is_slashed ? str_replace ( 'loading=\"lazy\" ' , 'loading=\"eager\" ' , $ new_tag ) : str_replace ( 'loading="lazy" ' , 'loading="eager" ' , $ new_tag );
401401 }
402402 }
403- if ( $ this ->settings ->is_lazyload_type_viewport () && Optml_Manager::instance ()->page_profiler ->is_in_all_viewports ( $ this ->get_id_by_url ( $ original_url ) ) ) {
404- if ( OPTML_DEBUG ) {
405- do_action ( 'optml_log ' , 'Adding preload priority for image ' . $ original_url . '| ' . $ this ->get_id_by_url ( $ original_url ) );
406- }
407- $ new_tag = preg_replace ( '/<img/im ' , $ is_slashed ? '<img fetchpriority=\"high\" ' : '<img fetchpriority="high" ' , $ new_tag );
408- // collect ID for preload.
409- Links::add_id ( $ this ->get_id_by_url ( $ original_url ), 'high ' );
410- }
411- if ( $ this ->settings ->is_lazyload_type_viewport () && Optml_Manager::instance ()->page_profiler ->is_lcp_image_in_all_viewports ( $ this ->get_id_by_url ( $ original_url ) ) ) {
403+
404+ if ( $ this ->settings ->is_lazyload_type_viewport () ) {
405+ $ is_in_all_viewports = Optml_Manager::instance ()->page_profiler ->is_in_all_viewports ( $ this ->get_id_by_url ( $ original_url ) );
406+ $ is_lcp_image = Optml_Manager::instance ()->page_profiler ->is_lcp_image_in_all_viewports ( $ this ->get_id_by_url ( $ original_url ) );
407+
412408 if ( OPTML_DEBUG ) {
413- do_action ( 'optml_log ' , 'Adding preload image is LCP ' . $ original_url . '| ' . $ this ->get_id_by_url ( $ original_url ) );
409+ if ( $ is_in_all_viewports ) {
410+ do_action ( 'optml_log ' , 'Adding preload priority for image ' . $ original_url . '| ' . $ this ->get_id_by_url ( $ original_url ) );
411+ } elseif ( $ is_lcp_image ) {
412+ do_action ( 'optml_log ' , 'Adding preload image is LCP ' . $ original_url . '| ' . $ this ->get_id_by_url ( $ original_url ) );
413+ }
414414 }
415415
416- $ new_tag = preg_replace ( '/<img/im ' , $ is_slashed ? '<img fetchpriority=\"high\" ' : '<img fetchpriority="high" ' , $ new_tag );
417- Links::add_id ( $ this ->get_id_by_url ( $ original_url ), 'high ' );
418- }
419- // // If the image is between the first images we add the fetchpriority attribute to improve the LCP.
420- if ( $ this ->settings ->is_lazyload_type_fixed () && self ::$ lazyload_skipped_images < Optml_Lazyload_Replacer::get_skip_lazyload_limit () ) {
421- if ( strpos ( $ new_tag , 'fetchpriority= ' ) === false ) {
416+ if ( $ is_in_all_viewports || $ is_lcp_image ) {
422417 $ new_tag = preg_replace ( '/<img/im ' , $ is_slashed ? '<img fetchpriority=\"high\" ' : '<img fetchpriority="high" ' , $ new_tag );
418+ Links::add_id ( $ this ->get_id_by_url ( $ original_url ), 'high ' ); // collect ID for preload.
423419 }
424420 }
425421
422+ // // If the image is between the first images we add the fetchpriority attribute to improve the LCP.
423+ if (
424+ $ this ->settings ->is_lazyload_type_fixed () &&
425+ self ::$ lazyload_skipped_images < Optml_Lazyload_Replacer::get_skip_lazyload_limit () &&
426+ false === strpos ( $ new_tag , 'fetchpriority= ' )
427+ ) {
428+ $ new_tag = preg_replace ( '/<img/im ' , $ is_slashed ? '<img fetchpriority=\"high\" ' : '<img fetchpriority="high" ' , $ new_tag );
429+ }
430+
426431 ++self ::$ lazyload_skipped_images ;
427432 return preg_replace ( $ pattern , $ replace , $ new_tag );
428433 }
0 commit comments