Skip to content

Commit afb587a

Browse files
chore: update functions
1 parent 70ba497 commit afb587a

File tree

4 files changed

+40
-32
lines changed

4 files changed

+40
-32
lines changed

assets/src/dashboard/parts/connected/settings/Lazyload.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ const Lazyload = ({ settings, setSettings, setCanSave }) => {
127127
setting.delete( slug );
128128
}
129129
console.log( setting );
130-
updateValue( 'lazyload_type', Array.from( setting ).join( '|' ) );
130+
updateValue( 'lazyload_type', Array.from( setting ).toSorted().join( '|' ) );
131131
},
132132
[ settings?.lazyload_type ]
133133
);

inc/lazyload_replacer.php

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -449,22 +449,25 @@ public function can_lazyload_for( $url, $tag = '' ) {
449449
if ( defined( 'OPTML_DISABLE_PNG_LAZYLOAD' ) && OPTML_DISABLE_PNG_LAZYLOAD ) {
450450
return $type['ext'] !== 'png';
451451
}
452-
if ( $this->settings->is_lazyload_type_viewport() && Optml_Manager::instance()->page_profiler->is_in_all_viewports( $this->get_id_by_url( $url ) ) ) {
453-
if ( OPTML_DEBUG ) {
454-
do_action( 'optml_log', 'Lazyload skipped image is in all viewports ' . $url . '|' . $this->get_id_by_url( $url ) );
455-
}
456-
// collect ID for preload.
457-
Links::add_id( $this->get_id_by_url( $url ), 'high' );
458-
return false;
459-
}
460-
if ( $this->settings->is_lazyload_type_viewport() && Optml_Manager::instance()->page_profiler->is_lcp_image_in_all_viewports( $this->get_id_by_url( $url ) ) ) {
452+
453+
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+
461457
if ( OPTML_DEBUG ) {
462-
do_action( 'optml_log', 'Lazyload skipped image is LCP ' . $url . '|' . $this->get_id_by_url( $url ) );
458+
if ( $is_in_all_viewports ) {
459+
do_action( 'optml_log', 'Lazyload skipped image is in all viewports ' . $url . '|' . $this->get_id_by_url( $url ) );
460+
} elseif ( $is_lcp_image ) {
461+
do_action( 'optml_log', 'Lazyload skipped image is LCP ' . $url . '|' . $this->get_id_by_url( $url ) );
462+
}
463463
}
464464

465-
Links::add_id( $this->get_id_by_url( $url ), 'high' );
466-
return false;
465+
if ( $is_in_all_viewports || $is_lcp_image ) {
466+
Links::add_id( $this->get_id_by_url( $url ), 'high' ); // collect ID for preload.
467+
return false;
468+
}
467469
}
470+
468471
if ( $this->settings->is_lazyload_type_fixed() && Optml_Tag_Replacer::$lazyload_skipped_images < self::get_skip_lazyload_limit() ) {
469472
return false;
470473
}

inc/settings.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ public function parse_settings( $new_settings ) {
292292
$sanitized_value = is_string( $value ) ? sanitize_text_field( $value ) : '';
293293
break;
294294
case 'lazyload_type':
295-
$sanitized_value = $this->to_map_values( $value, [ 'fixed', 'viewport', 'all' ], 'fixed' );
295+
$sanitized_value = $this->to_map_values( $value, [ 'fixed', 'viewport', 'all', 'fixed|viewport' ], 'fixed' );
296296
break;
297297
case 'compression_mode':
298298
$sanitized_value = $this->to_map_values( $value, [ 'speed_optimized', 'quality_optimized', 'custom' ], 'custom' );
@@ -796,7 +796,7 @@ public function clear_cache( $type = '' ) {
796796
* @return bool
797797
*/
798798
public function is_lazyload_type_viewport() {
799-
return $this->get( 'lazyload_type' ) === 'viewport';
799+
return false !== strpos( $this->get( 'lazyload_type' ), 'viewport' );
800800
}
801801

802802
/**
@@ -814,7 +814,7 @@ public function is_lazyload_type_all() {
814814
* @return bool
815815
*/
816816
public function is_lazyload_type_fixed() {
817-
return $this->get( 'lazyload_type' ) === 'fixed';
817+
return false !== strpos( $this->get( 'lazyload_type' ), 'fixed' );
818818
}
819819
/**
820820
* Utility to check if offload is enabled.

inc/tag_replacer.php

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)