fix(php): rewrite forbidden zones detection with strpos
This commit is contained in:
@@ -52,11 +52,20 @@ final class ContentAdInjector
|
|||||||
// Determinar modo de operacion
|
// Determinar modo de operacion
|
||||||
$mode = $this->settings['incontent_advanced']['incontent_mode'] ?? 'paragraphs_only';
|
$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') {
|
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
|
private function mapForbiddenZones(string $content): array
|
||||||
{
|
{
|
||||||
$zones = [];
|
$zones = [];
|
||||||
|
$contentLength = strlen($content);
|
||||||
|
|
||||||
// Tablas: <table>...</table>
|
// Tablas: buscar cada <table> y su </table> correspondiente
|
||||||
if (preg_match_all('/<table[^>]*>.*?<\/table>/is', $content, $matches, PREG_OFFSET_CAPTURE)) {
|
$this->findMatchingTags($content, 'table', $zones);
|
||||||
foreach ($matches[0] as $match) {
|
|
||||||
$zones[] = [
|
|
||||||
'start' => $match[1],
|
|
||||||
'end' => $match[1] + strlen($match[0]),
|
|
||||||
];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Iframes (YouTube, Vimeo, etc): <iframe>...</iframe>
|
// Iframes (YouTube, Vimeo, etc)
|
||||||
if (preg_match_all('/<iframe[^>]*>.*?<\/iframe>/is', $content, $matches, PREG_OFFSET_CAPTURE)) {
|
$this->findMatchingTags($content, 'iframe', $zones);
|
||||||
foreach ($matches[0] as $match) {
|
|
||||||
$zones[] = [
|
|
||||||
'start' => $match[1],
|
|
||||||
'end' => $match[1] + strlen($match[0]),
|
|
||||||
];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Divs con clase de embed/video (wp-block-embed, youtube, video-container, etc)
|
// Figure con clase wp-block-embed (embeds de WordPress)
|
||||||
if (preg_match_all('/<div[^>]*class="[^"]*(?:embed|video|youtube|vimeo|player)[^"]*"[^>]*>.*?<\/div>/is', $content, $matches, PREG_OFFSET_CAPTURE)) {
|
if (preg_match_all('/<figure[^>]*class="[^"]*wp-block-embed[^"]*"[^>]*>/i', $content, $matches, PREG_OFFSET_CAPTURE)) {
|
||||||
foreach ($matches[0] as $match) {
|
foreach ($matches[0] as $match) {
|
||||||
$zones[] = [
|
$startPos = $match[1];
|
||||||
'start' => $match[1],
|
$closePos = strpos($content, '</figure>', $startPos);
|
||||||
'end' => $match[1] + strlen($match[0]),
|
if ($closePos !== false) {
|
||||||
];
|
$zones[] = [
|
||||||
}
|
'start' => $startPos,
|
||||||
}
|
'end' => $closePos + strlen('</figure>'),
|
||||||
|
];
|
||||||
// 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]),
|
|
||||||
];
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return $zones;
|
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
|
* Verifica si una posicion esta dentro de una zona prohibida
|
||||||
*/
|
*/
|
||||||
|
|||||||
Reference in New Issue
Block a user