Estado actual: - Bootstrap Icons completo: 211 KB (2050 iconos) - Solo usamos 105 iconos (5.1%) Próximo paso: crear subset de iconos para ahorrar ~199 KB 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
74 lines
2.3 KiB
PHP
74 lines
2.3 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace ROITheme\Public\AdsensePlacement\Infrastructure\Services;
|
|
|
|
use ROITheme\Public\AdsensePlacement\Infrastructure\Ui\AdsensePlacementRenderer;
|
|
|
|
/**
|
|
* Inyecta anuncios dentro del contenido del post
|
|
* via filtro the_content
|
|
*/
|
|
final class ContentAdInjector
|
|
{
|
|
public function __construct(
|
|
private array $settings,
|
|
private AdsensePlacementRenderer $renderer
|
|
) {}
|
|
|
|
/**
|
|
* Filtra the_content para insertar anuncios
|
|
*/
|
|
public function inject(string $content): string
|
|
{
|
|
if (!($this->settings['behavior']['post_content_enabled'] ?? false)) {
|
|
return $content;
|
|
}
|
|
|
|
// Verificar longitud minima
|
|
$minLength = (int)($this->settings['forms']['min_content_length'] ?? 500);
|
|
if (strlen(strip_tags($content)) < $minLength) {
|
|
return $content;
|
|
}
|
|
|
|
$afterParagraphs = (int)($this->settings['behavior']['post_content_after_paragraphs'] ?? 3);
|
|
$maxAds = (int)($this->settings['behavior']['post_content_max_ads'] ?? 2);
|
|
|
|
// Dividir contenido en parrafos
|
|
$paragraphs = explode('</p>', $content);
|
|
$totalParagraphs = count($paragraphs);
|
|
|
|
if ($totalParagraphs < $afterParagraphs + 1) {
|
|
return $content;
|
|
}
|
|
|
|
$adsInserted = 0;
|
|
$newContent = '';
|
|
|
|
foreach ($paragraphs as $index => $paragraph) {
|
|
$newContent .= $paragraph;
|
|
|
|
if ($index < $totalParagraphs - 1) {
|
|
$newContent .= '</p>';
|
|
}
|
|
|
|
$paragraphNumber = $index + 1;
|
|
|
|
// Primer anuncio despues del parrafo indicado
|
|
if ($paragraphNumber === $afterParagraphs && $adsInserted < $maxAds) {
|
|
$newContent .= $this->renderer->renderSlot($this->settings, 'post-content-' . ($adsInserted + 1));
|
|
$adsInserted++;
|
|
}
|
|
|
|
// Segundo anuncio a mitad del contenido restante
|
|
$midPoint = $afterParagraphs + (int)(($totalParagraphs - $afterParagraphs) / 2);
|
|
if ($paragraphNumber === $midPoint && $adsInserted < $maxAds && $maxAds > 1) {
|
|
$newContent .= $this->renderer->renderSlot($this->settings, 'post-content-' . ($adsInserted + 1));
|
|
$adsInserted++;
|
|
}
|
|
}
|
|
|
|
return $newContent;
|
|
}
|
|
}
|