fix(php): rewrite forbidden zones detection with strpos
This commit is contained in:
@@ -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(
|
||||
'<!-- ROI_DEBUG: mode=%s, forbidden_zones=%d, content_len=%d -->',
|
||||
$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: <table>...</table>
|
||||
if (preg_match_all('/<table[^>]*>.*?<\/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 <table> y su </table> correspondiente
|
||||
$this->findMatchingTags($content, 'table', $zones);
|
||||
|
||||
// Iframes (YouTube, Vimeo, etc): <iframe>...</iframe>
|
||||
if (preg_match_all('/<iframe[^>]*>.*?<\/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('/<div[^>]*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('/<figure[^>]*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('/<figure[^>]*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, '</figure>', $startPos);
|
||||
if ($closePos !== false) {
|
||||
$zones[] = [
|
||||
'start' => $startPos,
|
||||
'end' => $closePos + strlen('</figure>'),
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $zones;
|
||||
}
|
||||
|
||||
/**
|
||||
* Encuentra tags de apertura y cierre correspondientes
|
||||
*/
|
||||
private function findMatchingTags(string $content, string $tagName, array &$zones): void
|
||||
{
|
||||
$openTag = '<' . $tagName;
|
||||
$closeTag = '</' . $tagName . '>';
|
||||
$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
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user