enabled( 'perf_control_heartbeat' ) ) { add_filter( 'heartbeat_settings', [ $this, 'heartbeat_frequency' ] ); } // Everything below only shaves weight off what a visitor downloads. No // page-speed tool measures wp-admin, so there is nothing to win there // and plenty to break — stripping core assets from the admin costs the // editor its emoji, icons and scripts for zero benefit. Frontend only. if ( is_admin() ) { return; } if ( $this->enabled( 'perf_disable_emojis' ) ) { $this->disable_emojis(); } if ( $this->enabled( 'perf_disable_embeds' ) ) { $this->disable_embeds(); } if ( $this->enabled( 'perf_disable_dashicons' ) ) { add_action( 'wp_enqueue_scripts', [ $this, 'disable_dashicons' ], 100 ); } if ( $this->enabled( 'perf_disable_jquery_migrate' ) ) { // Run late on wp_enqueue_scripts (after everything registers, before // scripts print) — hooking wp_default_scripts from init is unreliable // because it can fire before this callback is registered. add_action( 'wp_enqueue_scripts', [ $this, 'remove_jquery_migrate' ], 100 ); } } private function enabled( $key ) { $value = (bool) Helper::get_settings( $key, false ); return (bool) apply_filters( "ablocks/perf/{$key}", $value ); } /** * Remove the WordPress emoji detection script + styles. * * Frontend only — see the guard in apply(). wp-admin keeps core's emoji * support: the detection script is what draws the emoji the viewer's * platform has no glyph for, so unhooking it there just leaves blanks in * the editor, menus and list tables. */ public function disable_emojis() { remove_action( 'wp_head', 'print_emoji_detection_script', 7 ); // Unhooking the back-compat action is also how wp_enqueue_emoji_styles() // (WP 6.4+) decides to skip the inline stylesheet. remove_action( 'wp_print_styles', 'print_emoji_styles' ); remove_filter( 'the_content_feed', 'wp_staticize_emoji' ); remove_filter( 'comment_text_rss', 'wp_staticize_emoji' ); } /** * Remove the wp-embed script and oEmbed discovery/host output on the frontend. */ public function disable_embeds() { remove_action( 'wp_head', 'wp_oembed_add_discovery_links' ); remove_action( 'wp_head', 'wp_oembed_add_host_js' ); add_action( 'wp_footer', function () { wp_dequeue_script( 'wp-embed' ); } ); } /** * Drop the front-end Dashicons stylesheet for logged-out visitors (kept for * logged-in users so the admin bar still renders). */ public function disable_dashicons() { if ( ! is_user_logged_in() ) { wp_dequeue_style( 'dashicons' ); } } /** * Remove the legacy jquery-migrate dependency on the frontend. */ public function remove_jquery_migrate() { if ( is_admin() ) { return; } $scripts = wp_scripts(); foreach ( [ 'jquery', 'jquery-core' ] as $handle ) { if ( isset( $scripts->registered[ $handle ] ) && ! empty( $scripts->registered[ $handle ]->deps ) ) { $scripts->registered[ $handle ]->deps = array_diff( $scripts->registered[ $handle ]->deps, [ 'jquery-migrate' ] ); } } // Deregister so nothing can pull it in directly either. $scripts->remove( 'jquery-migrate' ); $scripts->dequeue( 'jquery-migrate' ); } /** * Throttle the Heartbeat API to a configurable interval (WP clamps 15–300s). */ public function heartbeat_frequency( $settings ) { $freq = (int) Helper::get_settings( 'perf_heartbeat_frequency', 60 ); $settings['interval'] = max( 15, min( 300, $freq ) ); return $settings; } }