From d7915d372b9f226656bd669195e8d437ad8fc1fd Mon Sep 17 00:00:00 2001 From: FrankZamora Date: Thu, 27 Nov 2025 15:38:49 -0600 Subject: [PATCH] refactor(adsense): remove dead code + PageSpeed CSS defer MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Remove unused roi_should_disable_auto_ads() function - Remove enable_page_level_ads code (Auto Ads disabled in Google panel) - Defer non-critical CSS with media=print pattern (Fase 4.2 PageSpeed) - Fix roi-accessibility dependency: roi-theme-style -> roi-main-style 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- Inc/adsense-placement.php | 40 ---------- Inc/enqueue-scripts.php | 73 +++++++++++++++++-- .../Wordpress/YoutubeFacadeHooksRegistrar.php | 3 +- 3 files changed, 67 insertions(+), 49 deletions(-) diff --git a/Inc/adsense-placement.php b/Inc/adsense-placement.php index 640c2d73..a7f78ca1 100644 --- a/Inc/adsense-placement.php +++ b/Inc/adsense-placement.php @@ -5,7 +5,6 @@ * 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')) { @@ -131,35 +130,8 @@ add_action('wp_footer', function() { 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 - * - * IMPORTANTE: Si disable_auto_ads está activo, se añade código para - * deshabilitar Auto Ads (page-level ads) de Google. */ function roi_enqueue_adsense_script(): void { @@ -183,25 +155,13 @@ function roi_enqueue_adsense_script(): void } $delayEnabled = ($settings['forms']['delay_enabled'] ?? true) === true; - $disableAutoAds = ($settings['visibility']['disable_auto_ads'] ?? true) === true; - // Script principal de AdSense if ($delayEnabled) { echo '' . "\n"; } else { echo '' . "\n"; } - // Deshabilitar Auto Ads (page-level ads) si está configurado - if ($disableAutoAds) { - echo '' . "\n"; - } - } catch (\Throwable $e) { if (defined('WP_DEBUG') && WP_DEBUG) { error_log('ROI AdSense: ' . $e->getMessage()); diff --git a/Inc/enqueue-scripts.php b/Inc/enqueue-scripts.php index 9361a55c..87a1b1cf 100644 --- a/Inc/enqueue-scripts.php +++ b/Inc/enqueue-scripts.php @@ -11,6 +11,56 @@ if (!defined('ABSPATH')) { exit; } +/** + * CSS no críticos que se cargarán de forma diferida (Fase 4.2 PageSpeed) + * + * Estos CSS se cargan con media="print" y onload="this.media='all'" + * para evitar bloquear el renderizado inicial. + * + * @since 1.0.21 + */ +define('ROI_DEFERRED_CSS', [ + 'roi-badges', + 'roi-pagination', + 'roi-generic-tables', + 'roi-video', + 'roi-animations', + 'roi-youtube-facade', + 'roi-tables-apu', +]); + +/** + * Add async loading to deferred stylesheets (Fase 4.2 PageSpeed) + * + * Converts media="print" to media="all" after load to prevent render-blocking. + * + * @since 1.0.21 + * @param string $html The link tag HTML. + * @param string $handle The stylesheet handle. + * @return string Modified link tag. + */ +function roi_add_deferred_css_onload($html, $handle) { + if (!in_array($handle, ROI_DEFERRED_CSS, true)) { + return $html; + } + + // Add onload attribute to switch media to "all" after load + $html = str_replace( + "media='print'", + "media='print' onload=\"this.media='all'\"", + $html + ); + + // Add noscript fallback for users without JavaScript + $href = preg_match('/href=[\'"]([^\'"]+)[\'"]/', $html, $matches) ? $matches[1] : ''; + if ($href) { + $html .= '' . "\n"; + } + + return $html; +} +add_filter('style_loader_tag', 'roi_add_deferred_css_onload', 10, 2); + /** * Enqueue typography system * @@ -113,21 +163,23 @@ function roi_enqueue_fase2_styles() { // @see Public/Hero/Infrastructure/Ui/HeroRenderer.php // Category Badges CSS - Clase genérica (Issue #62) + // DIFERIDO: Fase 4.2 PageSpeed - media='print' + onload wp_enqueue_style( 'roi-badges', get_template_directory_uri() . '/Assets/css/css-global-badges.css', array('roi-bootstrap'), filemtime(get_template_directory() . '/Assets/css/css-global-badges.css'), - 'all' + 'print' // Diferido para no bloquear renderizado ); // Pagination CSS - Estilos personalizados (Issue #64) + // DIFERIDO: Fase 4.2 PageSpeed - below the fold wp_enqueue_style( 'roi-pagination', get_template_directory_uri() . '/Assets/css/css-global-pagination.css', array('roi-bootstrap'), filemtime(get_template_directory() . '/Assets/css/css-global-pagination.css'), - 'all' + 'print' // Diferido para no bloquear renderizado ); // Post Content Typography y Related Posts - DESHABILITADOS @@ -232,12 +284,13 @@ function roi_enqueue_generic_tables() { } // Generic Tables CSS + // DIFERIDO: Fase 4.2 PageSpeed - below the fold wp_enqueue_style( 'roi-generic-tables', get_template_directory_uri() . '/Assets/css/css-global-generic-tables.css', array('roi-bootstrap'), ROI_VERSION, - 'all' + 'print' // Diferido para no bloquear renderizado ); } @@ -256,12 +309,13 @@ function roi_enqueue_video_styles() { } // Video CSS + // DIFERIDO: Fase 4.2 PageSpeed - below the fold wp_enqueue_style( 'roi-video', get_template_directory_uri() . '/Assets/css/css-global-video.css', array('roi-bootstrap'), ROI_VERSION, - 'all' + 'print' // Diferido para no bloquear renderizado ); } @@ -307,10 +361,11 @@ add_action('wp_enqueue_scripts', 'roi_enqueue_main_javascript', 11); */ function roi_enqueue_accessibility() { // Accessibility CSS + // CORREGIDO: Fase 4.2 PageSpeed - dependencia 'roi-theme-style' no existía wp_enqueue_style( 'roi-accessibility', get_template_directory_uri() . '/Assets/css/css-global-accessibility.css', - array('roi-theme-style'), + array('roi-main-style'), // Corregido: 'roi-theme-style' -> 'roi-main-style' ROI_VERSION, 'all' ); @@ -384,12 +439,13 @@ function roi_enqueue_theme_styles() { // ); // Theme Animations + // DIFERIDO: Fase 4.2 PageSpeed - mejoras visuales no críticas wp_enqueue_style( 'roi-animations', get_template_directory_uri() . '/Assets/css/css-global-animations.css', - array('roi-bootstrap'), // Cambiado de 'roi-theme' a 'roi-bootstrap' + array('roi-bootstrap'), '1.0.0', - 'all' + 'print' // Diferido para no bloquear renderizado ); // Theme Responsive Styles @@ -436,12 +492,13 @@ add_action('wp_enqueue_scripts', 'roi_enqueue_theme_styles', 13); */ function roi_enqueue_apu_tables_styles() { // APU Tables CSS + // DIFERIDO: Fase 4.2 PageSpeed - below the fold wp_enqueue_style( 'roi-tables-apu', get_template_directory_uri() . '/Assets/css/css-tablas-apu.css', array('roi-bootstrap'), ROI_VERSION, - 'all' + 'print' // Diferido para no bloquear renderizado ); } diff --git a/Public/YoutubeFacade/Infrastructure/Wordpress/YoutubeFacadeHooksRegistrar.php b/Public/YoutubeFacade/Infrastructure/Wordpress/YoutubeFacadeHooksRegistrar.php index 2c39e56e..b854c745 100644 --- a/Public/YoutubeFacade/Infrastructure/Wordpress/YoutubeFacadeHooksRegistrar.php +++ b/Public/YoutubeFacade/Infrastructure/Wordpress/YoutubeFacadeHooksRegistrar.php @@ -50,12 +50,13 @@ final class YoutubeFacadeHooksRegistrar return; } + // DIFERIDO: Fase 4.2 PageSpeed - below the fold wp_enqueue_style( 'roi-youtube-facade', get_template_directory_uri() . '/Public/YoutubeFacade/Infrastructure/Ui/Assets/Css/youtube-facade.css', [], ROI_VERSION, - 'all' + 'print' // Diferido para no bloquear renderizado ); wp_enqueue_script(