Skip to content

Commit 0ebc797

Browse files
committed
refactor: update lazyload logic to conditionally load profiler based on user state and settings
1 parent 3d71060 commit 0ebc797

2 files changed

Lines changed: 4 additions & 3 deletions

File tree

inc/manager.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,8 @@ public function replace_content( $html, $partial = false ) {
420420
if ( defined( 'REST_REQUEST' ) && REST_REQUEST && is_user_logged_in() && ( apply_filters( 'optml_force_replacement', false ) !== true ) ) {
421421
return $html;
422422
}
423-
if ( $this->settings->is_lazyload_type_viewport() && ! $partial ) {
423+
$should_load_profiler = ! $partial && apply_filters( 'optml_page_profiler_disable', false ) === false;
424+
if ( $should_load_profiler ) {
424425
$profile_id = Profile::generate_id( $html );
425426
// We disable the optimizer for logged in users.
426427
if ( ! is_user_logged_in() || ! apply_filters( 'optml_force_page_profiler', false ) !== true ) {
@@ -497,7 +498,7 @@ public function replace_content( $html, $partial = false ) {
497498
$html = $this->process_urls_from_content( $html );
498499

499500
$html = apply_filters( 'optml_url_post_process', $html );
500-
if ( $this->settings->is_lazyload_type_viewport() && ! $partial ) {
501+
if ( $should_load_profiler ) {
501502
Profile::reset_current_profile();
502503
}
503504
return $html;

tests/test-lazyload.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ public function test_should_not_add_loading () {
407407
$replaced_content = Optml_Manager::instance()->replace_content( $content );
408408
$this->assertStringContainsString( 'i.optimole.com', $replaced_content );
409409
$this->assertStringContainsString( 'data-opt-src', $replaced_content );
410-
$this->assertEquals( 2, substr_count( $replaced_content, 'loading="lazy"' ) );
410+
$this->assertEquals( 0, substr_count( $replaced_content, 'loading="lazy"' ) );
411411
$this->assertStringContainsString( 'loading="eager"', $replaced_content );
412412
}
413413

0 commit comments

Comments
 (0)