Agregar estructura completa del tema APUS con Bootstrap 5 y optimizaciones de rendimiento
Se implementa tema WordPress personalizado para Análisis de Precios Unitarios con funcionalidades avanzadas: - Sistema de templates (front-page, single, archive, page, 404, search) - Integración de Bootstrap 5.3.8 con estructura modular de assets - Panel de opciones del tema con Customizer API - Optimizaciones de rendimiento (Critical CSS, Image Optimization, Performance) - Funcionalidades SEO y compatibilidad con Rank Math - Sistema de posts relacionados y tabla de contenidos - Badge de categorías y manejo de AdSense diferido - Tipografías Google Fonts configurables - Documentación completa del tema y guías de uso 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
163
wp-content/themes/apus-theme/inc/adsense-delay.php
Normal file
163
wp-content/themes/apus-theme/inc/adsense-delay.php
Normal file
@@ -0,0 +1,163 @@
|
||||
<?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');
|
||||
|
||||
/**
|
||||
* Sanitize checkbox input
|
||||
*
|
||||
* @param bool $checked Whether the checkbox is checked
|
||||
* @return bool Sanitized value
|
||||
*/
|
||||
function apus_sanitize_checkbox($checked) {
|
||||
return (isset($checked) && $checked === true || $checked === '1') ? true : false;
|
||||
}
|
||||
Reference in New Issue
Block a user