| 1 |
<?php |
| 2 |
namespace ABlocks\Performance; |
| 3 |
|
| 4 |
if ( ! defined( 'ABSPATH' ) ) { |
| 5 |
exit; |
| 6 |
} |
| 7 |
|
| 8 |
use ABlocks\Helper; |
| 9 |
|
| 10 |
/** |
| 11 |
* Performance Suite — platform-level optimizations ("WordPress bloat" removers). |
| 12 |
* |
| 13 |
* Every optimization is opt-in via the Performance settings tab (default off, so |
| 14 |
* existing sites are unchanged) and exposes an `ablocks/perf/{key}` filter for |
| 15 |
* programmatic control. See docs/PERFORMANCE-FEATURES-SPEC.md. |
| 16 |
*/ |
| 17 |
class Optimizations { |
| 18 |
|
| 19 |
public static function init() { |
| 20 |
$self = new self(); |
| 21 |
add_action( 'init', [ $self, 'apply' ] ); |
| 22 |
} |
| 23 |
|
| 24 |
public function apply() { |
| 25 |
if ( $this->enabled( 'perf_disable_emojis' ) ) { |
| 26 |
$this->disable_emojis(); |
| 27 |
} |
| 28 |
if ( $this->enabled( 'perf_disable_embeds' ) ) { |
| 29 |
$this->disable_embeds(); |
| 30 |
} |
| 31 |
if ( $this->enabled( 'perf_disable_dashicons' ) ) { |
| 32 |
add_action( 'wp_enqueue_scripts', [ $this, 'disable_dashicons' ], 100 ); |
| 33 |
} |
| 34 |
if ( $this->enabled( 'perf_disable_jquery_migrate' ) ) { |
| 35 |
// Run late on wp_enqueue_scripts (after everything registers, before |
| 36 |
// scripts print) — hooking wp_default_scripts from init is unreliable |
| 37 |
// because it can fire before this callback is registered. |
| 38 |
add_action( 'wp_enqueue_scripts', [ $this, 'remove_jquery_migrate' ], 100 ); |
| 39 |
} |
| 40 |
if ( $this->enabled( 'perf_control_heartbeat' ) ) { |
| 41 |
add_filter( 'heartbeat_settings', [ $this, 'heartbeat_frequency' ] ); |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
private function enabled( $key ) { |
| 46 |
$value = (bool) Helper::get_settings( $key, false ); |
| 47 |
return (bool) apply_filters( "ablocks/perf/{$key}", $value ); |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Remove the WordPress emoji detection script + styles. |
| 52 |
*/ |
| 53 |
public function disable_emojis() { |
| 54 |
remove_action( 'wp_head', 'print_emoji_detection_script', 7 ); |
| 55 |
remove_action( 'admin_print_scripts', 'print_emoji_detection_script' ); |
| 56 |
remove_action( 'wp_print_styles', 'print_emoji_styles' ); |
| 57 |
remove_action( 'admin_print_styles', 'print_emoji_styles' ); |
| 58 |
remove_filter( 'the_content_feed', 'wp_staticize_emoji' ); |
| 59 |
remove_filter( 'comment_text_rss', 'wp_staticize_emoji' ); |
| 60 |
remove_filter( 'wp_mail', 'wp_staticize_emoji_for_email' ); |
| 61 |
add_filter( 'emoji_svg_url', '__return_false' ); |
| 62 |
add_filter( |
| 63 |
'tiny_mce_plugins', |
| 64 |
function ( $plugins ) { |
| 65 |
return is_array( $plugins ) ? array_diff( $plugins, [ 'wpemoji' ] ) : []; |
| 66 |
} |
| 67 |
); |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Remove the wp-embed script and oEmbed discovery/host output on the frontend. |
| 72 |
*/ |
| 73 |
public function disable_embeds() { |
| 74 |
remove_action( 'wp_head', 'wp_oembed_add_discovery_links' ); |
| 75 |
remove_action( 'wp_head', 'wp_oembed_add_host_js' ); |
| 76 |
add_action( |
| 77 |
'wp_footer', |
| 78 |
function () { |
| 79 |
wp_dequeue_script( 'wp-embed' ); |
| 80 |
} |
| 81 |
); |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Drop the front-end Dashicons stylesheet for logged-out visitors (kept for |
| 86 |
* logged-in users so the admin bar still renders). |
| 87 |
*/ |
| 88 |
public function disable_dashicons() { |
| 89 |
if ( ! is_user_logged_in() ) { |
| 90 |
wp_dequeue_style( 'dashicons' ); |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Remove the legacy jquery-migrate dependency on the frontend. |
| 96 |
*/ |
| 97 |
public function remove_jquery_migrate() { |
| 98 |
if ( is_admin() ) { |
| 99 |
return; |
| 100 |
} |
| 101 |
$scripts = wp_scripts(); |
| 102 |
foreach ( [ 'jquery', 'jquery-core' ] as $handle ) { |
| 103 |
if ( isset( $scripts->registered[ $handle ] ) && ! empty( $scripts->registered[ $handle ]->deps ) ) { |
| 104 |
$scripts->registered[ $handle ]->deps = array_diff( |
| 105 |
$scripts->registered[ $handle ]->deps, |
| 106 |
[ 'jquery-migrate' ] |
| 107 |
); |
| 108 |
} |
| 109 |
} |
| 110 |
// Deregister so nothing can pull it in directly either. |
| 111 |
$scripts->remove( 'jquery-migrate' ); |
| 112 |
$scripts->dequeue( 'jquery-migrate' ); |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Throttle the Heartbeat API to a configurable interval (WP clamps 15–300s). |
| 117 |
*/ |
| 118 |
public function heartbeat_frequency( $settings ) { |
| 119 |
$freq = (int) Helper::get_settings( 'perf_heartbeat_frequency', 60 ); |
| 120 |
$settings['interval'] = max( 15, min( 300, $freq ) ); |
| 121 |
return $settings; |
| 122 |
} |
| 123 |
} |
| 124 |
|