From 959d76fd92474dbd62d089fe5ccdb351a1334eac Mon Sep 17 00:00:00 2001 From: FrankZamora Date: Wed, 10 Dec 2025 11:52:44 -0600 Subject: [PATCH] fix(php): rewrite forbidden zones detection with strpos --- .../Services/ContentAdInjector.php | 86 +++++++++++-------- 1 file changed, 50 insertions(+), 36 deletions(-) diff --git a/Public/AdsensePlacement/Infrastructure/Services/ContentAdInjector.php b/Public/AdsensePlacement/Infrastructure/Services/ContentAdInjector.php index c9e40677..7b02df25 100644 --- a/Public/AdsensePlacement/Infrastructure/Services/ContentAdInjector.php +++ b/Public/AdsensePlacement/Infrastructure/Services/ContentAdInjector.php @@ -52,11 +52,20 @@ final class ContentAdInjector // Determinar modo de operacion $mode = $this->settings['incontent_advanced']['incontent_mode'] ?? 'paragraphs_only'; + // DEBUG TEMPORAL + $forbiddenZones = $this->mapForbiddenZones($content); + $debugInfo = sprintf( + '', + $mode, + count($forbiddenZones), + strlen($content) + ); + if ($mode === 'paragraphs_only') { - return $this->injectParagraphsOnly($content); + return $debugInfo . $this->injectParagraphsOnly($content); } - return $this->injectAdvanced($content); + return $debugInfo . $this->injectAdvanced($content); } /** @@ -231,50 +240,55 @@ final class ContentAdInjector private function mapForbiddenZones(string $content): array { $zones = []; + $contentLength = strlen($content); - // Tablas: ...
- if (preg_match_all('/]*>.*?<\/table>/is', $content, $matches, PREG_OFFSET_CAPTURE)) { - foreach ($matches[0] as $match) { - $zones[] = [ - 'start' => $match[1], - 'end' => $match[1] + strlen($match[0]), - ]; - } - } + // Tablas: buscar cada y su
correspondiente + $this->findMatchingTags($content, 'table', $zones); - // Iframes (YouTube, Vimeo, etc): - if (preg_match_all('/]*>.*?<\/iframe>/is', $content, $matches, PREG_OFFSET_CAPTURE)) { - foreach ($matches[0] as $match) { - $zones[] = [ - 'start' => $match[1], - 'end' => $match[1] + strlen($match[0]), - ]; - } - } + // Iframes (YouTube, Vimeo, etc) + $this->findMatchingTags($content, 'iframe', $zones); - // Divs con clase de embed/video (wp-block-embed, youtube, video-container, etc) - if (preg_match_all('/]*class="[^"]*(?:embed|video|youtube|vimeo|player)[^"]*"[^>]*>.*?<\/div>/is', $content, $matches, PREG_OFFSET_CAPTURE)) { + // Figure con clase wp-block-embed (embeds de WordPress) + if (preg_match_all('/]*class="[^"]*wp-block-embed[^"]*"[^>]*>/i', $content, $matches, PREG_OFFSET_CAPTURE)) { foreach ($matches[0] as $match) { - $zones[] = [ - 'start' => $match[1], - 'end' => $match[1] + strlen($match[0]), - ]; - } - } - - // Figure con iframe (embeds de WordPress) - if (preg_match_all('/]*class="[^"]*wp-block-embed[^"]*"[^>]*>.*?<\/figure>/is', $content, $matches, PREG_OFFSET_CAPTURE)) { - foreach ($matches[0] as $match) { - $zones[] = [ - 'start' => $match[1], - 'end' => $match[1] + strlen($match[0]), - ]; + $startPos = $match[1]; + $closePos = strpos($content, '', $startPos); + if ($closePos !== false) { + $zones[] = [ + 'start' => $startPos, + 'end' => $closePos + strlen(''), + ]; + } } } return $zones; } + /** + * Encuentra tags de apertura y cierre correspondientes + */ + private function findMatchingTags(string $content, string $tagName, array &$zones): void + { + $openTag = '<' . $tagName; + $closeTag = ''; + $offset = 0; + + while (($startPos = stripos($content, $openTag, $offset)) !== false) { + // Buscar el cierre correspondiente + $closePos = stripos($content, $closeTag, $startPos); + if ($closePos !== false) { + $zones[] = [ + 'start' => $startPos, + 'end' => $closePos + strlen($closeTag), + ]; + $offset = $closePos + strlen($closeTag); + } else { + break; + } + } + } + /** * Verifica si una posicion esta dentro de una zona prohibida */