Files
roi-theme/Inc/adsense-placement.php
FrankZamora cd09666f1d Backup antes de optimizar Bootstrap Icons (subset)
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>
2025-11-27 14:31:04 -06:00

197 lines
5.3 KiB
PHP

<?php
/**
* AdSense Placement - Helper Functions
*
* Funciones para usar en templates:
* - roi_render_ad_slot('post-top')
* - roi_render_rail_ads() - Para los margenes laterales del viewport
* - roi_should_disable_auto_ads()
*/
if (!defined('ABSPATH')) {
exit;
}
/**
* Renderiza un slot de anuncio en una ubicacion
*
* NOTA DIP: El renderer se obtiene del DIContainer, NO se instancia directamente.
* Esto cumple con el Principio de Inversion de Dependencias.
*/
function roi_render_ad_slot(string $location): string
{
global $container;
if ($container === null) {
return '';
}
try {
$repository = $container->getComponentSettingsRepository();
$settings = $repository->getComponentSettings('adsense-placement');
if (empty($settings)) {
return '';
}
// Verificar exclusiones
if (roi_is_ad_excluded($settings)) {
return '';
}
// Obtener renderer desde DIContainer (DIP compliant)
$renderer = $container->getAdsensePlacementRenderer();
return $renderer->renderSlot($settings, $location);
} catch (\Throwable $e) {
if (defined('WP_DEBUG') && WP_DEBUG) {
error_log('ROI AdSense: ' . $e->getMessage());
}
return '';
}
}
/**
* Verifica si el contenido actual esta excluido
*/
function roi_is_ad_excluded(array $settings): bool
{
$forms = $settings['forms'] ?? [];
// Excluir categorias
$excludeCats = array_filter(array_map('trim', explode(',', $forms['exclude_categories'] ?? '')));
if (!empty($excludeCats) && is_single()) {
$postCats = wp_get_post_categories(get_the_ID());
if (array_intersect($excludeCats, $postCats)) {
return true;
}
}
// Excluir tipos de post
$excludeTypes = array_filter(array_map('trim', explode(',', $forms['exclude_post_types'] ?? '')));
if (!empty($excludeTypes) && in_array(get_post_type(), $excludeTypes, true)) {
return true;
}
// Excluir posts especificos
$excludeIds = array_filter(array_map('trim', explode(',', $forms['exclude_post_ids'] ?? '')));
if (!empty($excludeIds) && in_array((string)get_the_ID(), $excludeIds, true)) {
return true;
}
return false;
}
/**
* Renderiza los Rail Ads (margenes laterales del viewport)
* Se llama desde wp_footer para inyectar al final del body
*
* NOTA DIP: El renderer se obtiene del DIContainer, NO se instancia directamente.
*/
function roi_render_rail_ads(): string
{
global $container;
if ($container === null) {
return '';
}
try {
$repository = $container->getComponentSettingsRepository();
$settings = $repository->getComponentSettings('adsense-placement');
if (empty($settings)) {
return '';
}
// Verificar exclusiones
if (roi_is_ad_excluded($settings)) {
return '';
}
// Obtener renderer desde DIContainer (DIP compliant)
$renderer = $container->getAdsensePlacementRenderer();
return $renderer->renderRailAds($settings);
} catch (\Throwable $e) {
if (defined('WP_DEBUG') && WP_DEBUG) {
error_log('ROI AdSense Rail Ads: ' . $e->getMessage());
}
return '';
}
}
/**
* Hook para inyectar Rail Ads en el footer
*/
add_action('wp_footer', function() {
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo roi_render_rail_ads();
}, 50);
/**
* Verifica si se debe deshabilitar Auto Ads
*/
function roi_should_disable_auto_ads(): bool
{
global $container;
if ($container === null) {
return false;
}
try {
$repository = $container->getComponentSettingsRepository();
$settings = $repository->getComponentSettings('adsense-placement');
$isEnabled = ($settings['visibility']['is_enabled'] ?? false) === true;
$disableAutoAds = ($settings['visibility']['disable_auto_ads'] ?? true) === true;
return $isEnabled && $disableAutoAds;
} catch (\Throwable $e) {
return false;
}
}
/**
* Carga el script principal de AdSense
*/
function roi_enqueue_adsense_script(): void
{
global $container;
if ($container === null || is_admin()) {
return;
}
try {
$repository = $container->getComponentSettingsRepository();
$settings = $repository->getComponentSettings('adsense-placement');
if (!($settings['visibility']['is_enabled'] ?? false)) {
return;
}
$publisherId = $settings['content']['publisher_id'] ?? '';
if (empty($publisherId)) {
return;
}
$delayEnabled = ($settings['forms']['delay_enabled'] ?? true) === true;
if ($delayEnabled) {
echo '<script type="text/plain" data-adsense-script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=' . esc_attr($publisherId) . '" crossorigin="anonymous"></script>' . "\n";
} else {
echo '<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=' . esc_attr($publisherId) . '" crossorigin="anonymous"></script>' . "\n";
}
} catch (\Throwable $e) {
if (defined('WP_DEBUG') && WP_DEBUG) {
error_log('ROI AdSense: ' . $e->getMessage());
}
}
}
add_action('wp_head', 'roi_enqueue_adsense_script', 5);