From 8cf77c6192417049499b6c0c6ba077030e8e7480 Mon Sep 17 00:00:00 2001 From: selul Date: Mon, 18 Aug 2025 12:27:40 +0300 Subject: [PATCH] fix compatibilities with viewport lazyload on. ref: https://secure.helpscout.net/conversation/3038781531/470527?viewId=2353196 --- inc/compatibilities/beaver_builder.php | 15 ++++++++-- inc/compatibilities/divi_builder.php | 2 +- .../elementor_builder_late.php | 2 +- inc/compatibilities/facetwp.php | 6 ++-- inc/compatibilities/yith_quick_view.php | 7 ++++- inc/manager.php | 29 +++++++++++-------- 6 files changed, 41 insertions(+), 20 deletions(-) diff --git a/inc/compatibilities/beaver_builder.php b/inc/compatibilities/beaver_builder.php index 96e757eea..ffc400b87 100644 --- a/inc/compatibilities/beaver_builder.php +++ b/inc/compatibilities/beaver_builder.php @@ -32,7 +32,18 @@ function ( $all_watchers ) { return $all_watchers; } ); - add_filter( 'fl_builder_render_css', [ Optml_Main::instance()->manager, 'replace_content' ], PHP_INT_MAX, 1 ); - add_filter( 'fl_builder_render_js', [ Optml_Main::instance()->manager, 'replace_content' ], PHP_INT_MAX, 1 ); + add_filter( 'fl_builder_render_css', [ $this, 'replace_static_content' ], PHP_INT_MAX, 1 ); + add_filter( 'fl_builder_render_js', [ $this, 'replace_static_content' ], PHP_INT_MAX, 1 ); + } + + /** + * Replace urls in static content. + * + * @param string $content Content to replace. + * + * @return string Altered content. + */ + public function replace_static_content( $content ) { + return Optml_Main::instance()->manager->replace_content( $content, true ); } } diff --git a/inc/compatibilities/divi_builder.php b/inc/compatibilities/divi_builder.php index bec392ca2..7caef7aa7 100644 --- a/inc/compatibilities/divi_builder.php +++ b/inc/compatibilities/divi_builder.php @@ -56,7 +56,7 @@ public function optimize_divi_static_files( $page_resource ) { $data = $page_resource->get_data( 'file' ); if ( ! empty( $data ) ) { - $data = Optml_Main::instance()->manager->replace_content( $data ); + $data = Optml_Main::instance()->manager->replace_content( $data, true ); ET_Core_PageResource::$wpfs->put_contents( $page_resource->path, $data, 0644 ); } } diff --git a/inc/compatibilities/elementor_builder_late.php b/inc/compatibilities/elementor_builder_late.php index ab3a2da9c..4759dcdf5 100644 --- a/inc/compatibilities/elementor_builder_late.php +++ b/inc/compatibilities/elementor_builder_late.php @@ -48,7 +48,7 @@ public function replace_meta( $metadata, $object_id, $meta_key, $single ) { return $metadata; } - return Optml_Main::instance()->manager->replace_content( $current_meta ); + return Optml_Main::instance()->manager->replace_content( $current_meta, true ); } // Return original if the check does not pass diff --git a/inc/compatibilities/facetwp.php b/inc/compatibilities/facetwp.php index 819304f38..8db809653 100644 --- a/inc/compatibilities/facetwp.php +++ b/inc/compatibilities/facetwp.php @@ -39,7 +39,7 @@ public function api_replacement_filter( $output, $data ) { $output = json_decode( $output ); if ( isset( $output->template ) ) { - $output->template = Optml_Main::instance()->manager->replace_content( $output->template ); + $output->template = Optml_Main::instance()->manager->replace_content( $output->template, true ); } // Ignore invalid UTF-8 characters in PHP 7.2+ @@ -48,7 +48,7 @@ public function api_replacement_filter( $output, $data ) { } else { $output = json_encode( $output, JSON_INVALID_UTF8_IGNORE ); } - $output = Optml_Main::instance()->manager->replace_content( $output ); + $output = Optml_Main::instance()->manager->replace_content( $output, true ); return $output; } /** @@ -65,7 +65,7 @@ public function api_replacement_action( $output ) { if ( ! isset( $output['template'] ) || ! function_exists( 'FWP' ) ) { return; } - $output['template'] = Optml_Main::instance()->manager->replace_content( $output['template'] ); + $output['template'] = Optml_Main::instance()->manager->replace_content( $output['template'], true ); FWP()->request->output = $output; } /** diff --git a/inc/compatibilities/yith_quick_view.php b/inc/compatibilities/yith_quick_view.php index 7537e27bd..d2ab58111 100644 --- a/inc/compatibilities/yith_quick_view.php +++ b/inc/compatibilities/yith_quick_view.php @@ -23,7 +23,12 @@ public function should_load() { */ public function register() { Optml_Url_Replacer::instance()->init(); - add_filter( 'woocommerce_single_product_image_thumbnail_html', [ Optml_Main::instance()->manager, 'replace_content' ] ); + add_filter( + 'woocommerce_single_product_image_thumbnail_html', + function ( $html ) { + return Optml_Main::instance()->manager->replace_content( $html, true ); + } + ); } /** diff --git a/inc/manager.php b/inc/manager.php index 5639dcca2..a73594435 100644 --- a/inc/manager.php +++ b/inc/manager.php @@ -409,15 +409,16 @@ public function register_after_setup() { * Filter raw HTML content for urls. * * @param string $html HTML to filter. + * @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. * * @return mixed Filtered content. */ - public function replace_content( $html ) { + public function replace_content( $html, $partial = false ) { if ( defined( 'REST_REQUEST' ) && REST_REQUEST && is_user_logged_in() && ( apply_filters( 'optml_force_replacement', false ) !== true ) ) { return $html; } - if ( $this->settings->is_lazyload_type_viewport() ) { + if ( $this->settings->is_lazyload_type_viewport() && ! $partial ) { $profile_id = Profile::generate_id( $html ); // We disable the optimizer for logged in users. if ( ! is_user_logged_in() || ! apply_filters( 'optml_force_page_profiler', false ) !== true ) { @@ -442,7 +443,9 @@ public function replace_content( $html ) { Profile::set_current_profile_id( $profile_id ); $this->page_profiler->set_current_profile_data(); } - $html = $this->add_html_class( $html ); + if ( ! $partial ) { + $html = $this->add_html_class( $html ); + } $html = $this->process_images_from_content( $html ); @@ -457,7 +460,7 @@ public function replace_content( $html ) { } } - if ( $this->settings->is_lazyload_type_viewport() ) { + if ( $this->settings->is_lazyload_type_viewport() && ! $partial ) { $personalized_bg_css = Lazyload::get_current_personalized_css(); if ( OPTML_DEBUG ) { do_action( 'optml_log', 'viewport_bgselectorsdata: ' . print_r( $personalized_bg_css, true ) ); @@ -475,14 +478,16 @@ public function replace_content( $html ) { } } } - // WE need this last since during bg personalized CSS we collect preload urls - if ( Links::get_links_count() > 0 ) { - if ( OPTML_DEBUG ) { - do_action( 'optml_log', 'preload_links: ' . print_r( Links::get_links(), true ) ); + if ( ! $partial ) { + // WE need this last since during bg personalized CSS we collect preload urls + if ( Links::get_links_count() > 0 ) { + if ( OPTML_DEBUG ) { + do_action( 'optml_log', 'preload_links: ' . print_r( Links::get_links(), true ) ); + } + $html = str_replace( Optml_Admin::get_preload_links_placeholder(), Links::get_links_html(), $html ); + } else { + $html = str_replace( Optml_Admin::get_preload_links_placeholder(), '', $html ); } - $html = str_replace( Optml_Admin::get_preload_links_placeholder(), Links::get_links_html(), $html ); - } else { - $html = str_replace( Optml_Admin::get_preload_links_placeholder(), '', $html ); } $html = apply_filters( 'optml_url_pre_process', $html ); @@ -490,7 +495,7 @@ public function replace_content( $html ) { $html = $this->process_urls_from_content( $html ); $html = apply_filters( 'optml_url_post_process', $html ); - if ( $this->settings->is_lazyload_type_viewport() ) { + if ( $this->settings->is_lazyload_type_viewport() && ! $partial ) { Profile::reset_current_profile(); } return $html;