PROBLEMA RESUELTO: - Eliminados apus_remove_dns_prefetch() y apus_add_dns_prefetch() que causaban loops infinitos en wp_resource_hints - Eliminadas funciones de Heartbeat, defer JS, query string removal que podían causar conflictos OPTIMIZACIONES CONSERVADAS (seguras): ✓ Disable emojis (sin DNS prefetch filter) ✓ Disable oEmbed ✓ Disable feeds ✓ Disable RSD/WLW ✓ Disable Dashicons para no-logged users ✓ Disable Block Library CSS ✓ Remove WordPress version ✓ Disable XML-RPC ✓ Remove jQuery Migrate ✓ Optimize queries ✓ Disable admin bar para no-admins RESULTADO: - Site responde HTTP 200 sin timeouts - Sin memory exhaustion (antes: 14GB+) - Performance optimizations básicas activas Funciones problemáticas documentadas en comentarios para evaluación futura. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
300 lines
8.0 KiB
PHP
300 lines
8.0 KiB
PHP
<?php
|
|
/**
|
|
* Performance Optimization Functions
|
|
*
|
|
* Functions to remove WordPress bloat and improve performance.
|
|
*
|
|
* NOTA: Versión reducida con solo optimizaciones seguras después de
|
|
* resolver problemas de memory exhaustion en Issue #22.
|
|
*
|
|
* @package Apus_Theme
|
|
* @since 1.0.0
|
|
*/
|
|
|
|
// Exit if accessed directly.
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
|
exit;
|
|
}
|
|
|
|
/**
|
|
* Disable WordPress Emojis
|
|
*
|
|
* Removes emoji detection scripts and styles from WordPress.
|
|
* These scripts are loaded on every page but rarely used.
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
function apus_disable_emojis() {
|
|
remove_action( 'wp_head', 'print_emoji_detection_script', 7 );
|
|
remove_action( 'admin_print_scripts', 'print_emoji_detection_script' );
|
|
remove_action( 'wp_print_styles', 'print_emoji_styles' );
|
|
remove_action( 'admin_print_styles', 'print_emoji_styles' );
|
|
remove_filter( 'the_content_feed', 'wp_staticize_emoji' );
|
|
remove_filter( 'comment_text_rss', 'wp_staticize_emoji' );
|
|
remove_filter( 'wp_mail', 'wp_staticize_emoji_for_email' );
|
|
|
|
// Remove from TinyMCE.
|
|
add_filter( 'tiny_mce_plugins', 'apus_disable_emojis_tinymce' );
|
|
}
|
|
add_action( 'init', 'apus_disable_emojis' );
|
|
|
|
/**
|
|
* Filter function used to remove emoji plugin from TinyMCE
|
|
*
|
|
* @since 1.0.0
|
|
* @param array $plugins An array of default TinyMCE plugins.
|
|
* @return array Modified array of TinyMCE plugins without emoji plugin.
|
|
*/
|
|
function apus_disable_emojis_tinymce( $plugins ) {
|
|
if ( is_array( $plugins ) ) {
|
|
return array_diff( $plugins, array( 'wpemoji' ) );
|
|
}
|
|
|
|
return array();
|
|
}
|
|
|
|
/**
|
|
* Disable WordPress oEmbed
|
|
*
|
|
* Removes oEmbed discovery links and scripts.
|
|
* Only disable if you don't need to embed content from other sites.
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
function apus_disable_oembed() {
|
|
// Remove oEmbed discovery links.
|
|
remove_action( 'wp_head', 'wp_oembed_add_discovery_links' );
|
|
|
|
// Remove oEmbed-specific JavaScript from the front-end and back-end.
|
|
remove_action( 'wp_head', 'wp_oembed_add_host_js' );
|
|
|
|
// Remove all embeds rewrite rules.
|
|
add_filter( 'rewrite_rules_array', 'apus_disable_oembed_rewrites' );
|
|
|
|
// Remove filter of the oEmbed result before any HTTP requests are made.
|
|
remove_filter( 'pre_oembed_result', 'wp_filter_pre_oembed_result', 10 );
|
|
}
|
|
add_action( 'init', 'apus_disable_oembed', 9999 );
|
|
|
|
/**
|
|
* Remove all rewrite rules related to embeds
|
|
*
|
|
* @since 1.0.0
|
|
* @param array $rules WordPress rewrite rules.
|
|
* @return array Modified rewrite rules.
|
|
*/
|
|
function apus_disable_oembed_rewrites( $rules ) {
|
|
foreach ( $rules as $rule => $rewrite ) {
|
|
if ( false !== strpos( $rewrite, 'embed=true' ) ) {
|
|
unset( $rules[ $rule ] );
|
|
}
|
|
}
|
|
return $rules;
|
|
}
|
|
|
|
/**
|
|
* Disable wp-embed.js script
|
|
*
|
|
* Dequeues the wp-embed.js script that WordPress loads by default.
|
|
* This script is used for embedding WordPress posts on other sites.
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
function apus_dequeue_embed_script() {
|
|
wp_deregister_script( 'wp-embed' );
|
|
}
|
|
add_action( 'wp_footer', 'apus_dequeue_embed_script' );
|
|
|
|
/**
|
|
* Disable WordPress Feeds
|
|
*
|
|
* Removes RSS, RDF, and Atom feeds.
|
|
* Only disable if you don't need feed functionality.
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
function apus_disable_feeds() {
|
|
wp_die(
|
|
esc_html__( 'No feed available, please visit our homepage!', 'apus' ),
|
|
esc_html__( 'Feed Not Available', 'apus' ),
|
|
array(
|
|
'response' => 404,
|
|
'back_link' => true,
|
|
)
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Remove feed links and redirect feed URLs
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
function apus_disable_feed_links() {
|
|
// Remove feed links from header.
|
|
remove_action( 'wp_head', 'feed_links', 2 );
|
|
remove_action( 'wp_head', 'feed_links_extra', 3 );
|
|
|
|
// Redirect feed URLs to homepage.
|
|
add_action( 'do_feed', 'apus_disable_feeds', 1 );
|
|
add_action( 'do_feed_rdf', 'apus_disable_feeds', 1 );
|
|
add_action( 'do_feed_rss', 'apus_disable_feeds', 1 );
|
|
add_action( 'do_feed_rss2', 'apus_disable_feeds', 1 );
|
|
add_action( 'do_feed_atom', 'apus_disable_feeds', 1 );
|
|
add_action( 'do_feed_rss2_comments', 'apus_disable_feeds', 1 );
|
|
add_action( 'do_feed_atom_comments', 'apus_disable_feeds', 1 );
|
|
}
|
|
add_action( 'init', 'apus_disable_feed_links' );
|
|
|
|
/**
|
|
* Disable RSD and Windows Live Writer Manifest
|
|
*
|
|
* Really Simple Discovery (RSD) and Windows Live Writer (WLW) manifest
|
|
* are rarely used and can be safely removed.
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
function apus_disable_rsd_wlw() {
|
|
// Remove RSD link.
|
|
remove_action( 'wp_head', 'rsd_link' );
|
|
|
|
// Remove Windows Live Writer manifest link.
|
|
remove_action( 'wp_head', 'wlwmanifest_link' );
|
|
}
|
|
add_action( 'init', 'apus_disable_rsd_wlw' );
|
|
|
|
/**
|
|
* Disable Dashicons for non-logged users
|
|
*
|
|
* Dashicons are only needed in the admin area and for logged-in users.
|
|
* This removes them from the front-end for visitors.
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
function apus_disable_dashicons() {
|
|
if ( ! is_user_logged_in() ) {
|
|
wp_deregister_style( 'dashicons' );
|
|
}
|
|
}
|
|
add_action( 'wp_enqueue_scripts', 'apus_disable_dashicons' );
|
|
|
|
/**
|
|
* Disable Block Library CSS
|
|
*
|
|
* Removes the default WordPress block library styles.
|
|
* Only disable if you're not using the block editor or if you're
|
|
* providing your own block styles.
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
function apus_disable_block_library_css() {
|
|
// Remove default block library styles.
|
|
wp_dequeue_style( 'wp-block-library' );
|
|
wp_dequeue_style( 'wp-block-library-theme' );
|
|
|
|
// Remove inline global styles.
|
|
wp_dequeue_style( 'global-styles' );
|
|
|
|
// Remove classic theme styles (if not using classic editor).
|
|
wp_dequeue_style( 'classic-theme-styles' );
|
|
}
|
|
add_action( 'wp_enqueue_scripts', 'apus_disable_block_library_css', 100 );
|
|
|
|
/**
|
|
* Remove WordPress version from head and feeds
|
|
*
|
|
* Removes the WordPress version number for security reasons.
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
function apus_remove_wp_version() {
|
|
return '';
|
|
}
|
|
add_filter( 'the_generator', 'apus_remove_wp_version' );
|
|
|
|
/**
|
|
* Disable XML-RPC
|
|
*
|
|
* XML-RPC is often targeted by brute force attacks.
|
|
* Disable if you don't need remote publishing functionality.
|
|
*
|
|
* @since 1.0.0
|
|
* @return bool
|
|
*/
|
|
function apus_disable_xmlrpc() {
|
|
return false;
|
|
}
|
|
add_filter( 'xmlrpc_enabled', 'apus_disable_xmlrpc' );
|
|
|
|
/**
|
|
* Remove jQuery Migrate
|
|
*
|
|
* jQuery Migrate is used for backwards compatibility but adds extra overhead.
|
|
* Only remove if you've verified all scripts work without it.
|
|
*
|
|
* @since 1.0.0
|
|
* @param WP_Scripts $scripts WP_Scripts object.
|
|
*/
|
|
function apus_remove_jquery_migrate( $scripts ) {
|
|
if ( ! is_admin() && isset( $scripts->registered['jquery'] ) ) {
|
|
$script = $scripts->registered['jquery'];
|
|
|
|
if ( $script->deps ) {
|
|
// Remove jquery-migrate from dependencies.
|
|
$script->deps = array_diff( $script->deps, array( 'jquery-migrate' ) );
|
|
}
|
|
}
|
|
}
|
|
add_action( 'wp_default_scripts', 'apus_remove_jquery_migrate' );
|
|
|
|
/**
|
|
* Optimize WordPress Database Queries
|
|
*
|
|
* Removes unnecessary meta queries for better performance.
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
function apus_optimize_queries() {
|
|
// Remove unnecessary post meta from queries
|
|
remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10 );
|
|
remove_action( 'wp_head', 'wp_shortlink_wp_head', 10 );
|
|
}
|
|
add_action( 'init', 'apus_optimize_queries' );
|
|
|
|
/**
|
|
* Disable WordPress Admin Bar for Non-Admins
|
|
*
|
|
* Reduces HTTP requests for non-admin users.
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
function apus_disable_admin_bar() {
|
|
if ( ! current_user_can( 'administrator' ) && ! is_admin() ) {
|
|
show_admin_bar( false );
|
|
}
|
|
}
|
|
add_action( 'after_setup_theme', 'apus_disable_admin_bar' );
|
|
|
|
/*
|
|
* FUNCIONES DESHABILITADAS TEMPORALMENTE
|
|
*
|
|
* Las siguientes funciones han sido comentadas porque causaban
|
|
* problemas de memory exhaustion (14GB+) en Issue #22:
|
|
*
|
|
* - apus_remove_dns_prefetch() y apus_add_dns_prefetch()
|
|
* Causaban loops infinitos al interactuar con wp_resource_hints
|
|
*
|
|
* - apus_modify_heartbeat_settings()
|
|
* Modificación del Heartbeat API - revisar interacciones
|
|
*
|
|
* - apus_defer_parsing_of_js()
|
|
* Defer de scripts JS - puede causar problemas de dependencias
|
|
*
|
|
* - apus_remove_query_strings()
|
|
* Remoción de query strings - verificar compatibilidad con caché
|
|
*
|
|
* - apus_preload_critical_resources()
|
|
* Preload de recursos - mantener simple por ahora
|
|
*
|
|
* Se pueden reactivar individualmente después de pruebas exhaustivas.
|
|
*/
|