Files
roi-theme/wp-content/themes/apus-theme/inc/adsense-delay.php
FrankZamora a0633f4000 Fix: Consolidar funciones sanitizadoras para resolver error fatal de redeclaración
Resuelve issue #21 y sub-issue #22

PROBLEMA:
- La función apus_sanitize_checkbox() estaba definida en 4 archivos diferentes
- Causaba error fatal: "Cannot redeclare apus_sanitize_checkbox()"
- Impedía activación del tema en staging

SOLUCIÓN:
1. Crear inc/sanitize-functions.php con funciones centralizadas
2. Incluir sanitize-functions.php al inicio de functions.php
3. Eliminar definiciones duplicadas en:
   - inc/customizer-fonts.php (líneas 83-93)
   - inc/adsense-delay.php (líneas 161-163)
   - inc/admin/options-api.php (líneas 240-242)
   - inc/critical-css.php (líneas 361-363)

ARCHIVOS MODIFICADOS:
- inc/sanitize-functions.php (nuevo)
- functions.php (incluir sanitize-functions.php)
- inc/customizer-fonts.php (eliminar duplicados)
- inc/adsense-delay.php (eliminar duplicados)
- inc/admin/options-api.php (eliminar duplicados)
- inc/critical-css.php (eliminar duplicados)

FUNCIONES CONSOLIDADAS:
- apus_sanitize_checkbox($input): Sanitiza valores boolean
- apus_sanitize_select($input, $setting): Sanitiza valores select

VERIFICACIÓN:
 Sintaxis PHP correcta en todos los archivos
 No hay redeclaraciones de funciones
 Funciones protegidas con function_exists()

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-04 10:41:18 -06:00

155 lines
5.1 KiB
PHP

<?php
/**
* AdSense Delay Loading Functionality
*
* Delays the loading of AdSense scripts until user interaction or timeout
* to improve initial page load performance.
*
* @package Apus_Theme
* @since 1.0.0
*/
// Exit if accessed directly
if (!defined('ABSPATH')) {
exit;
}
/**
* Delay AdSense scripts by intercepting output buffer
*
* This function starts output buffering and replaces AdSense scripts
* with delayed versions when the page is rendered.
*/
function apus_delay_adsense_scripts() {
// Only run on frontend
if (is_admin()) {
return;
}
// Check if AdSense delay is enabled in theme options
$delay_enabled = get_theme_mod('apus_adsense_delay_enabled', true);
if (!$delay_enabled) {
return;
}
// Start output buffering
ob_start('apus_replace_adsense_scripts');
}
add_action('template_redirect', 'apus_delay_adsense_scripts', 1);
/**
* Replace AdSense scripts with delayed versions
*
* This function processes the HTML output and replaces standard AdSense
* script tags with delayed loading versions.
*
* @param string $html The HTML content to process
* @return string Modified HTML with delayed AdSense scripts
*/
function apus_replace_adsense_scripts($html) {
// Only process if there's actual AdSense content
if (strpos($html, 'pagead2.googlesyndication.com') === false &&
strpos($html, 'adsbygoogle.js') === false) {
return $html;
}
// Pattern to match AdSense script tags
$patterns = array(
// Match async script tags for AdSense
'/<script\s+async\s+src=["\']https:\/\/pagead2\.googlesyndication\.com\/pagead\/js\/adsbygoogle\.js[^"\']*["\']\s*(?:crossorigin=["\']anonymous["\'])?\s*><\/script>/i',
// Match script tags without async
'/<script\s+src=["\']https:\/\/pagead2\.googlesyndication\.com\/pagead\/js\/adsbygoogle\.js[^"\']*["\']\s*(?:crossorigin=["\']anonymous["\'])?\s*><\/script>/i',
// Match inline adsbygoogle.push scripts
'/<script>\s*\(adsbygoogle\s*=\s*window\.adsbygoogle\s*\|\|\s*\[\]\)\.push\(\{[^}]*\}\);\s*<\/script>/is',
);
// Replace async AdSense scripts with delayed versions
$replacements = array(
// Replace async script tag with data attribute for delayed loading
'<script type="text/plain" data-adsense-script src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js" crossorigin="anonymous"></script>',
// Replace non-async script tag
'<script type="text/plain" data-adsense-script src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js" crossorigin="anonymous"></script>',
// Replace inline push scripts with delayed versions
'<script type="text/plain" data-adsense-push>$0</script>',
);
// First pass: replace script tags
$html = preg_replace($patterns[0], $replacements[0], $html);
$html = preg_replace($patterns[1], $replacements[1], $html);
// Second pass: replace inline push calls
$html = preg_replace_callback(
'/<script>\s*\(adsbygoogle\s*=\s*window\.adsbygoogle\s*\|\|\s*\[\]\)\.push\(\{[^}]*\}\);\s*<\/script>/is',
function($matches) {
return '<script type="text/plain" data-adsense-push>' . $matches[0] . '</script>';
},
$html
);
// Add a comment to indicate processing occurred
if (defined('WP_DEBUG') && WP_DEBUG) {
$html = str_replace('</body>', '<!-- AdSense scripts delayed by Apus Theme --></body>', $html);
}
return $html;
}
/**
* Add inline script to initialize delayed AdSense
*
* This adds a small inline script that marks AdSense as ready to load
* after the adsense-loader.js has been enqueued.
*/
function apus_add_adsense_init_script() {
$delay_enabled = get_theme_mod('apus_adsense_delay_enabled', true);
if (!$delay_enabled || is_admin()) {
return;
}
?>
<script>
// Initialize AdSense delay flag
window.apusAdsenseDelayed = true;
</script>
<?php
}
add_action('wp_head', 'apus_add_adsense_init_script', 1);
/**
* Register customizer settings for AdSense delay
*
* @param WP_Customize_Manager $wp_customize Theme Customizer object
*/
function apus_adsense_delay_customizer($wp_customize) {
// Add Performance section if it doesn't exist
if (!$wp_customize->get_section('apus_performance')) {
$wp_customize->add_section('apus_performance', array(
'title' => __('Performance Settings', 'apus-theme'),
'priority' => 130,
));
}
// Add setting for AdSense delay
$wp_customize->add_setting('apus_adsense_delay_enabled', array(
'default' => true,
'sanitize_callback' => 'apus_sanitize_checkbox',
'transport' => 'refresh',
));
// Add control for AdSense delay
$wp_customize->add_control('apus_adsense_delay_enabled', array(
'label' => __('Delay AdSense Loading', 'apus-theme'),
'description' => __('Delay AdSense scripts until user interaction to improve page load speed.', 'apus-theme'),
'section' => 'apus_performance',
'type' => 'checkbox',
));
}
add_action('customize_register', 'apus_adsense_delay_customizer');