Skip to content

Commit 554a800

Browse files
authored
Merge pull request #949 from Codeinwp/fix/compats
Fix/compats
2 parents 8716dd7 + 8cf77c6 commit 554a800

File tree

6 files changed

+41
-20
lines changed

6 files changed

+41
-20
lines changed

inc/compatibilities/beaver_builder.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,18 @@ function ( $all_watchers ) {
3232
return $all_watchers;
3333
}
3434
);
35-
add_filter( 'fl_builder_render_css', [ Optml_Main::instance()->manager, 'replace_content' ], PHP_INT_MAX, 1 );
36-
add_filter( 'fl_builder_render_js', [ Optml_Main::instance()->manager, 'replace_content' ], PHP_INT_MAX, 1 );
35+
add_filter( 'fl_builder_render_css', [ $this, 'replace_static_content' ], PHP_INT_MAX, 1 );
36+
add_filter( 'fl_builder_render_js', [ $this, 'replace_static_content' ], PHP_INT_MAX, 1 );
37+
}
38+
39+
/**
40+
* Replace urls in static content.
41+
*
42+
* @param string $content Content to replace.
43+
*
44+
* @return string Altered content.
45+
*/
46+
public function replace_static_content( $content ) {
47+
return Optml_Main::instance()->manager->replace_content( $content, true );
3748
}
3849
}

inc/compatibilities/divi_builder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public function optimize_divi_static_files( $page_resource ) {
5656
$data = $page_resource->get_data( 'file' );
5757

5858
if ( ! empty( $data ) ) {
59-
$data = Optml_Main::instance()->manager->replace_content( $data );
59+
$data = Optml_Main::instance()->manager->replace_content( $data, true );
6060
ET_Core_PageResource::$wpfs->put_contents( $page_resource->path, $data, 0644 );
6161
}
6262
}

inc/compatibilities/elementor_builder_late.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public function replace_meta( $metadata, $object_id, $meta_key, $single ) {
4848
return $metadata;
4949
}
5050

51-
return Optml_Main::instance()->manager->replace_content( $current_meta );
51+
return Optml_Main::instance()->manager->replace_content( $current_meta, true );
5252
}
5353

5454
// Return original if the check does not pass

inc/compatibilities/facetwp.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public function api_replacement_filter( $output, $data ) {
3939
$output = json_decode( $output );
4040

4141
if ( isset( $output->template ) ) {
42-
$output->template = Optml_Main::instance()->manager->replace_content( $output->template );
42+
$output->template = Optml_Main::instance()->manager->replace_content( $output->template, true );
4343
}
4444

4545
// Ignore invalid UTF-8 characters in PHP 7.2+
@@ -48,7 +48,7 @@ public function api_replacement_filter( $output, $data ) {
4848
} else {
4949
$output = json_encode( $output, JSON_INVALID_UTF8_IGNORE );
5050
}
51-
$output = Optml_Main::instance()->manager->replace_content( $output );
51+
$output = Optml_Main::instance()->manager->replace_content( $output, true );
5252
return $output;
5353
}
5454
/**
@@ -65,7 +65,7 @@ public function api_replacement_action( $output ) {
6565
if ( ! isset( $output['template'] ) || ! function_exists( 'FWP' ) ) {
6666
return;
6767
}
68-
$output['template'] = Optml_Main::instance()->manager->replace_content( $output['template'] );
68+
$output['template'] = Optml_Main::instance()->manager->replace_content( $output['template'], true );
6969
FWP()->request->output = $output;
7070
}
7171
/**

inc/compatibilities/yith_quick_view.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,12 @@ public function should_load() {
2323
*/
2424
public function register() {
2525
Optml_Url_Replacer::instance()->init();
26-
add_filter( 'woocommerce_single_product_image_thumbnail_html', [ Optml_Main::instance()->manager, 'replace_content' ] );
26+
add_filter(
27+
'woocommerce_single_product_image_thumbnail_html',
28+
function ( $html ) {
29+
return Optml_Main::instance()->manager->replace_content( $html, true );
30+
}
31+
);
2732
}
2833

2934
/**

inc/manager.php

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

Comments
 (0)