PluginProbe
aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder / 2.13.0
aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder v2.13.0
2.13.0 2.13.1 2.12.0 2.11.1 2.11.0 2.10.0 2.9.0 2.7.4 2.7.5 2.7.6 2.7.7 2.8.0 2.8.1 2.9.1 trunk 1.0 1.0-beta1 1.0-beta2 1.0-beta3 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.2 All 80 releases
ablocks / includes / performance / optimizations.php

optimizations.php in aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder 2.13.0, at includes/performance/optimizations.php

132 lines 4.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 // Heartbeat is the one optimization aimed at wp-admin — the API polls
26 // hardest in the editor — so it is wired up before the frontend guard.
27 if ( $this->enabled( 'perf_control_heartbeat' ) ) {
28 add_filter( 'heartbeat_settings', [ $this, 'heartbeat_frequency' ] );
29 }
30
31 // Everything below only shaves weight off what a visitor downloads. No
32 // page-speed tool measures wp-admin, so there is nothing to win there
33 // and plenty to break — stripping core assets from the admin costs the
34 // editor its emoji, icons and scripts for zero benefit. Frontend only.
35 if ( is_admin() ) {
36 return;
37 }
38
39 if ( $this->enabled( 'perf_disable_emojis' ) ) {
40 $this->disable_emojis();
41 }
42 if ( $this->enabled( 'perf_disable_embeds' ) ) {
43 $this->disable_embeds();
44 }
45 if ( $this->enabled( 'perf_disable_dashicons' ) ) {
46 add_action( 'wp_enqueue_scripts', [ $this, 'disable_dashicons' ], 100 );
47 }
48 if ( $this->enabled( 'perf_disable_jquery_migrate' ) ) {
49 // Run late on wp_enqueue_scripts (after everything registers, before
50 // scripts print) — hooking wp_default_scripts from init is unreliable
51 // because it can fire before this callback is registered.
52 add_action( 'wp_enqueue_scripts', [ $this, 'remove_jquery_migrate' ], 100 );
53 }
54 }
55
56 private function enabled( $key ) {
57 $value = (bool) Helper::get_settings( $key, false );
58 return (bool) apply_filters( "ablocks/perf/{$key}", $value );
59 }
60
61 /**
62 * Remove the WordPress emoji detection script + styles.
63 *
64 * Frontend only — see the guard in apply(). wp-admin keeps core's emoji
65 * support: the detection script is what draws the emoji the viewer's
66 * platform has no glyph for, so unhooking it there just leaves blanks in
67 * the editor, menus and list tables.
68 */
69 public function disable_emojis() {
70 remove_action( 'wp_head', 'print_emoji_detection_script', 7 );
71 // Unhooking the back-compat action is also how wp_enqueue_emoji_styles()
72 // (WP 6.4+) decides to skip the inline stylesheet.
73 remove_action( 'wp_print_styles', 'print_emoji_styles' );
74 remove_filter( 'the_content_feed', 'wp_staticize_emoji' );
75 remove_filter( 'comment_text_rss', 'wp_staticize_emoji' );
76 }
77
78 /**
79 * Remove the wp-embed script and oEmbed discovery/host output on the frontend.
80 */
81 public function disable_embeds() {
82 remove_action( 'wp_head', 'wp_oembed_add_discovery_links' );
83 remove_action( 'wp_head', 'wp_oembed_add_host_js' );
84 add_action(
85 'wp_footer',
86 function () {
87 wp_dequeue_script( 'wp-embed' );
88 }
89 );
90 }
91
92 /**
93 * Drop the front-end Dashicons stylesheet for logged-out visitors (kept for
94 * logged-in users so the admin bar still renders).
95 */
96 public function disable_dashicons() {
97 if ( ! is_user_logged_in() ) {
98 wp_dequeue_style( 'dashicons' );
99 }
100 }
101
102 /**
103 * Remove the legacy jquery-migrate dependency on the frontend.
104 */
105 public function remove_jquery_migrate() {
106 if ( is_admin() ) {
107 return;
108 }
109 $scripts = wp_scripts();
110 foreach ( [ 'jquery', 'jquery-core' ] as $handle ) {
111 if ( isset( $scripts->registered[ $handle ] ) && ! empty( $scripts->registered[ $handle ]->deps ) ) {
112 $scripts->registered[ $handle ]->deps = array_diff(
113 $scripts->registered[ $handle ]->deps,
114 [ 'jquery-migrate' ]
115 );
116 }
117 }
118 // Deregister so nothing can pull it in directly either.
119 $scripts->remove( 'jquery-migrate' );
120 $scripts->dequeue( 'jquery-migrate' );
121 }
122
123 /**
124 * Throttle the Heartbeat API to a configurable interval (WP clamps 15–300s).
125 */
126 public function heartbeat_frequency( $settings ) {
127 $freq = (int) Helper::get_settings( 'perf_heartbeat_frequency', 60 );
128 $settings['interval'] = max( 15, min( 300, $freq ) );
129 return $settings;
130 }
131 }
132