@@ -409,15 +409,16 @@ public function register_after_setup() {
409409 * Filter raw HTML content for urls.
410410 *
411411 * @param string $html HTML to filter.
412+ * @param bool $partial If this is a partial content replacement and not a full page. It matters when we are are doing full page optimization like viewport lazyload.
412413 *
413414 * @return mixed Filtered content.
414415 */
415- public function replace_content ( $ html ) {
416+ public function replace_content ( $ html, $ partial = false ) {
416417
417418 if ( defined ( 'REST_REQUEST ' ) && REST_REQUEST && is_user_logged_in () && ( apply_filters ( 'optml_force_replacement ' , false ) !== true ) ) {
418419 return $ html ;
419420 }
420- if ( $ this ->settings ->is_lazyload_type_viewport () ) {
421+ if ( $ this ->settings ->is_lazyload_type_viewport () && ! $ partial ) {
421422 $ profile_id = Profile::generate_id ( $ html );
422423 // We disable the optimizer for logged in users.
423424 if ( ! is_user_logged_in () || ! apply_filters ( 'optml_force_page_profiler ' , false ) !== true ) {
@@ -442,7 +443,9 @@ public function replace_content( $html ) {
442443 Profile::set_current_profile_id ( $ profile_id );
443444 $ this ->page_profiler ->set_current_profile_data ();
444445 }
445- $ html = $ this ->add_html_class ( $ html );
446+ if ( ! $ partial ) {
447+ $ html = $ this ->add_html_class ( $ html );
448+ }
446449
447450 $ html = $ this ->process_images_from_content ( $ html );
448451
@@ -457,7 +460,7 @@ public function replace_content( $html ) {
457460 }
458461 }
459462
460- if ( $ this ->settings ->is_lazyload_type_viewport () ) {
463+ if ( $ this ->settings ->is_lazyload_type_viewport () && ! $ partial ) {
461464 $ personalized_bg_css = Lazyload::get_current_personalized_css ();
462465 if ( OPTML_DEBUG ) {
463466 do_action ( 'optml_log ' , 'viewport_bgselectorsdata: ' . print_r ( $ personalized_bg_css , true ) );
@@ -475,22 +478,24 @@ public function replace_content( $html ) {
475478 }
476479 }
477480 }
478- // WE need this last since during bg personalized CSS we collect preload urls
479- if ( Links::get_links_count () > 0 ) {
480- if ( OPTML_DEBUG ) {
481- do_action ( 'optml_log ' , 'preload_links: ' . print_r ( Links::get_links (), true ) );
481+ if ( ! $ partial ) {
482+ // WE need this last since during bg personalized CSS we collect preload urls
483+ if ( Links::get_links_count () > 0 ) {
484+ if ( OPTML_DEBUG ) {
485+ do_action ( 'optml_log ' , 'preload_links: ' . print_r ( Links::get_links (), true ) );
486+ }
487+ $ html = str_replace ( Optml_Admin::get_preload_links_placeholder (), Links::get_links_html (), $ html );
488+ } else {
489+ $ html = str_replace ( Optml_Admin::get_preload_links_placeholder (), '' , $ html );
482490 }
483- $ html = str_replace ( Optml_Admin::get_preload_links_placeholder (), Links::get_links_html (), $ html );
484- } else {
485- $ html = str_replace ( Optml_Admin::get_preload_links_placeholder (), '' , $ html );
486491 }
487492
488493 $ html = apply_filters ( 'optml_url_pre_process ' , $ html );
489494
490495 $ html = $ this ->process_urls_from_content ( $ html );
491496
492497 $ html = apply_filters ( 'optml_url_post_process ' , $ html );
493- if ( $ this ->settings ->is_lazyload_type_viewport () ) {
498+ if ( $ this ->settings ->is_lazyload_type_viewport () && ! $ partial ) {
494499 Profile::reset_current_profile ();
495500 }
496501 return $ html ;
0 commit comments