Skip to content

Commit 06aa954

Browse files
chore: conditions checks
1 parent f5f5a63 commit 06aa954

File tree

4 files changed

+509
-161
lines changed

4 files changed

+509
-161
lines changed

inc/lazyload_replacer.php

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -450,25 +450,33 @@ public function can_lazyload_for( $url, $tag = '' ) {
450450
return $type['ext'] !== 'png';
451451
}
452452

453+
$no_viewport_data_available = true;
454+
453455
if ( $this->settings->is_lazyload_type_viewport() ) {
454-
$is_in_all_viewports = Optml_Manager::instance()->page_profiler->is_in_all_viewports( $this->get_id_by_url( $url ) );
455-
$is_lcp_image = Optml_Manager::instance()->page_profiler->is_lcp_image_in_all_viewports( $this->get_id_by_url( $url ) );
456+
$image_id = $this->get_id_by_url( $url );
457+
$is_in_all_viewports = Optml_Manager::instance()->page_profiler->is_in_all_viewports( $image_id );
458+
$is_lcp_image = Optml_Manager::instance()->page_profiler->is_lcp_image_in_all_viewports( $image_id );
459+
$no_viewport_data_available = ! Optml_Manager::instance()->page_profiler->check_data_availability();
456460

457461
if ( OPTML_DEBUG ) {
458462
if ( $is_in_all_viewports ) {
459-
do_action( 'optml_log', 'Lazyload skipped image is in all viewports ' . $url . '|' . $this->get_id_by_url( $url ) );
463+
do_action( 'optml_log', 'Lazyload skipped image is in all viewports ' . $url . '|' . $image_id );
460464
} elseif ( $is_lcp_image ) {
461-
do_action( 'optml_log', 'Lazyload skipped image is LCP ' . $url . '|' . $this->get_id_by_url( $url ) );
465+
do_action( 'optml_log', 'Lazyload skipped image is LCP ' . $url . '|' . $image_id );
462466
}
463467
}
464468

465469
if ( $is_in_all_viewports || $is_lcp_image ) {
466-
Links::add_id( $this->get_id_by_url( $url ), 'high' ); // collect ID for preload.
470+
Links::add_id( $image_id, 'high' ); // collect ID for preload.
467471
return false;
468472
}
469473
}
470474

471-
if ( $this->settings->is_lazyload_type_fixed() && Optml_Tag_Replacer::$lazyload_skipped_images < self::get_skip_lazyload_limit() ) {
475+
if (
476+
$no_viewport_data_available &&
477+
$this->settings->is_lazyload_type_fixed() &&
478+
Optml_Tag_Replacer::$lazyload_skipped_images < self::get_skip_lazyload_limit()
479+
) {
472480
return false;
473481
}
474482
return true;

inc/tag_replacer.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -401,26 +401,31 @@ public function regular_tag_replace( $new_tag, $original_url, $new_url, $optml_a
401401
}
402402
}
403403

404+
$no_viewport_data_available = true;
405+
404406
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+
$image_id = $this->get_id_by_url( $original_url );
408+
$is_in_all_viewports = Optml_Manager::instance()->page_profiler->is_in_all_viewports( $image_id );
409+
$is_lcp_image = Optml_Manager::instance()->page_profiler->is_lcp_image_in_all_viewports( $image_id );
410+
$no_viewport_data_available = ! Optml_Manager::instance()->page_profiler->check_data_availability();
407411

408412
if ( OPTML_DEBUG ) {
409413
if ( $is_in_all_viewports ) {
410-
do_action( 'optml_log', 'Adding preload priority for image ' . $original_url . '|' . $this->get_id_by_url( $original_url ) );
414+
do_action( 'optml_log', 'Adding preload priority for image ' . $original_url . '|' . $image_id );
411415
} elseif ( $is_lcp_image ) {
412-
do_action( 'optml_log', 'Adding preload image is LCP ' . $original_url . '|' . $this->get_id_by_url( $original_url ) );
416+
do_action( 'optml_log', 'Adding preload image is LCP ' . $original_url . '|' . $image_id );
413417
}
414418
}
415419

416420
if ( $is_in_all_viewports || $is_lcp_image ) {
417421
$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.
422+
Links::add_id( $image_id, 'high' ); // collect ID for preload.
419423
}
420424
}
421425

422-
// // If the image is between the first images we add the fetchpriority attribute to improve the LCP.
426+
// If the image is between the first images we add the fetchpriority attribute to improve the LCP.
423427
if (
428+
$no_viewport_data_available &&
424429
$this->settings->is_lazyload_type_fixed() &&
425430
self::$lazyload_skipped_images < Optml_Lazyload_Replacer::get_skip_lazyload_limit() &&
426431
false === strpos( $new_tag, 'fetchpriority=' )

inc/v2/PageProfiler/Profile.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,4 +379,22 @@ public static function get_active_devices(): array {
379379
self::DEVICE_TYPE_DESKTOP,
380380
];
381381
}
382+
383+
/**
384+
* Check if there is any profile data available for the current profile.
385+
*
386+
* @return bool
387+
*/
388+
public function check_data_availability(): bool {
389+
$has_data = false;
390+
391+
foreach ( self::get_active_devices() as $device ) {
392+
if ( empty( self::$current_profile_data[ $device ] ) ) {
393+
$has_data = true;
394+
break;
395+
}
396+
}
397+
398+
return $has_data;
399+
}
382400
}

0 commit comments

Comments
 (0)