Skip to content

Commit adbbd36

Browse files
committed
feat: enhance debug logging and improve content processing logic
1 parent 3b66c03 commit adbbd36

2 files changed

Lines changed: 23 additions & 5 deletions

File tree

inc/manager.php

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,9 @@ 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 ( OPTML_DEBUG ) {
424+
do_action( 'optml_log', 'in Viewport: ' . var_export( $partial, true ) . wp_debug_backtrace_summary() );
425+
}
423426
if ( $this->settings->is_lazyload_type_viewport() && ! $partial ) {
424427
$profile_id = Profile::generate_id( $html );
425428
// We disable the optimizer for logged in users.
@@ -594,11 +597,14 @@ public static function should_ignore_image_tags() {
594597
public static function parse_images_from_html( $content ) {
595598
$images = [];
596599

600+
if ( OPTML_DEBUG ) {
601+
do_action( 'optml_log', 'Content to parse images from: ' . $content );
602+
}
597603
$regex = '/(?:<a[^>]+?href=["|\'](?P<link_url>[^\s]+?)["|\'][^>]*?>\s*)?(?P<img_tag>(?:<noscript\s*>\s*)?<img[^>]*?\s?(?:' . implode( '|', array_merge( [ 'src' ], Optml_Tag_Replacer::possible_src_attributes() ) ) . ')=["\'\\\\]*?(?P<img_url>[' . Optml_Config::$chars . ']{10,}).*?>(?:\s*<\/noscript\s*>)?){1}(?:\s*<\/a>)?/ismu';
598604

599605
if ( preg_match_all( $regex, $content, $images, PREG_OFFSET_CAPTURE ) ) {
600606
if ( OPTML_DEBUG ) {
601-
do_action( 'optml_log', 'images parased: ' . print_r( $images, true ) );
607+
do_action( 'optml_log', 'Image tags parsed: ' . print_r( $images, true ) );
602608
}
603609
foreach ( $images as $key => $unused ) {
604610
// Simplify the output as much as possible, mostly for confirming test results.
@@ -646,8 +652,7 @@ public static function parse_images_from_html( $content ) {
646652
public function process_urls_from_content( $html ) {
647653
$extracted_urls = $this->extract_urls_from_content( $html );
648654
if ( OPTML_DEBUG ) {
649-
do_action( 'optml_log', 'matched urls' );
650-
do_action( 'optml_log', $extracted_urls );
655+
do_action( 'optml_log', 'Extracted image urls from content: ' . print_r( $extracted_urls, true ) );
651656
}
652657
return $this->do_url_replacement( $html, $extracted_urls );
653658
}
@@ -759,7 +764,20 @@ public function process_template_redirect_content() {
759764
remove_filter( 'the_content', [ $this, 'process_images_from_content' ], PHP_INT_MAX );
760765

761766
ob_start(
762-
[ &$this, 'replace_content' ]
767+
function ( $content ) {
768+
/*
769+
* Wrap the call to replace_content() so that PHP’s output-buffering system
770+
* does not pass its own second argument ($phase bitmask) to our method.
771+
*
772+
* replace_content() expects the second parameter to be a boolean $partial,
773+
* indicating whether the content is a partial replacement (e.g. for
774+
* viewport lazy-load) or a full page. If PHP’s $phase integer is passed
775+
* directly, it would be misinterpreted as $partial and break the logic.
776+
*
777+
* This closure filters the call, forwarding only the captured HTML buffer.
778+
*/
779+
return $this->replace_content( $content );
780+
}
763781
);
764782
}
765783

inc/tag_replacer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ public function process_image_tags( $content, $images = [] ) {
199199
$image_sizes = self::image_sizes();
200200
$sizes2crop = self::size_to_crop();
201201
if ( OPTML_DEBUG ) {
202-
do_action( 'optml_log', 'process_image_tags: ' . print_r( $images, true ) );
202+
do_action( 'optml_log', 'Images tags to process: ' . print_r( $images, true ) );
203203
}
204204
foreach ( $images[0] as $index => $tag ) {
205205
$width = $height = false;

0 commit comments

Comments
 (0)