settings = $settings; } /** * Register bloat removal hooks based on active settings. */ public function init() { // 1. Disable Emojis if ( $this->settings->get( 'disable_emojis', true ) ) { $this->disable_emojis(); } // 2. Disable Embeds if ( $this->settings->get( 'disable_embeds', true ) ) { $this->disable_embeds(); } // 3. Remove Query Strings from static assets if ( $this->settings->get( 'remove_query_strings', true ) ) { add_filter( 'script_loader_src', array( $this, 'strip_query_strings' ), 15 ); add_filter( 'style_loader_src', array( $this, 'strip_query_strings' ), 15 ); } // 4. Heartbeat Control $hb_mode = $this->settings->get( 'heartbeat_control', 'modify_frequency' ); if ( 'disable_frontend' === $hb_mode ) { add_action( 'init', array( $this, 'disable_frontend_heartbeat' ), 1 ); } elseif ( 'modify_frequency' === $hb_mode ) { add_filter( 'heartbeat_settings', array( $this, 'modify_heartbeat_frequency' ) ); } } /** * Completely disable WordPress Emojis. */ private function disable_emojis() { add_action( 'init', function() { 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 TinyMCE emojis add_filter( 'tiny_mce_plugins', function( $plugins ) { if ( is_array( $plugins ) ) { return array_diff( $plugins, array( 'wpemoji' ) ); } return array(); } ); // Remove DNS prefetch for s.w.org emojis add_filter( 'wp_resource_hints', function( $urls, $relation_type ) { if ( 'dns-prefetch' === $relation_type ) { $emoji_svg_url = apply_filters( 'emoji_svg_url', 'https://s.w.org/images/core/emoji/' ); foreach ( $urls as $key => $url ) { if ( false !== strpos( $url, $emoji_svg_url ) ) { unset( $urls[ $key ] ); } } } return $urls; }, 10, 2 ); } ); } /** * Disable WordPress oEmbed scripts. */ private function disable_embeds() { add_action( 'wp_footer', function() { wp_deregister_script( 'wp-embed' ); } ); add_action( 'init', function() { remove_action( 'rest_api_init', 'wp_oembed_register_route' ); remove_filter( 'oembed_dataparse', 'wp_filter_oembed_result', 10 ); remove_action( 'wp_head', 'wp_oembed_add_discovery_links' ); remove_action( 'wp_head', 'wp_oembed_add_host_js' ); }, 9999 ); } /** * Strip version query strings from static CSS and JS. * * @param string $src Asset URL. * @return string */ public function strip_query_strings( $src ) { if ( empty( $src ) || false === strpos( $src, 'ver=' ) ) { return $src; } $parts = explode( '?ver=', $src ); return $parts[0]; } /** * Disable Heartbeat on frontend only. */ public function disable_frontend_heartbeat() { if ( ! is_admin() ) { wp_deregister_script( 'heartbeat' ); } } /** * Modify Heartbeat frequency interval to reduce server requests. * * @param array $settings * @return array */ public function modify_heartbeat_frequency( $settings ) { $freq = (int) $this->settings->get( 'heartbeat_frequency', 60 ); $settings['interval'] = max( 15, min( 120, $freq ) ); return $settings; } }