-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfrontend.php
More file actions
267 lines (225 loc) · 7.45 KB
/
Copy pathfrontend.php
File metadata and controls
267 lines (225 loc) · 7.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
<?php
/**
* Frontend optimization callbacks.
*
* @package EngineScript_Site_Optimizer
*/
if ( ! defined( 'ABSPATH' ) ) {
return;
}
/**
* Determine whether frontend-only optimizations should run.
*
* @since 2.0.0
* @return bool True when the current request is a regular frontend request.
*/
function es_optimizer_is_frontend_request(): bool {
return ! is_admin() && ! wp_doing_ajax();
}
/**
* Disable WordPress emoji functionality.
*
* @since 1.0.0
*/
function es_optimizer_disable_emojis(): void {
if ( ! es_optimizer_is_option_enabled( 'disable_emojis' ) ) {
return;
}
remove_action( 'wp_head', 'print_emoji_detection_script', 7 );
remove_action( 'wp_print_styles', 'print_emoji_styles' );
remove_action( 'admin_print_scripts', 'print_emoji_detection_script' );
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' );
add_filter( 'tiny_mce_plugins', 'es_optimizer_disable_emojis_tinymce' );
add_filter( 'wp_resource_hints', 'es_optimizer_disable_emojis_remove_dns_prefetch', 10, 2 );
}
/**
* Filter function used to remove the TinyMCE emoji plugin.
*
* @since 1.0.0
* @param array<int, string> $plugins Array of TinyMCE plugins.
* @return array<int, string> Plugins without wpemoji.
*/
function es_optimizer_disable_emojis_tinymce( array $plugins ): array {
return array_values( array_diff( $plugins, array( 'wpemoji' ) ) );
}
/**
* Remove emoji CDN hostname from DNS prefetching hints.
*
* @since 1.0.0
* @param array<int, array<string, mixed>|string> $urls URLs to print for resource hints.
* @param string $relation_type The relation type the URLs are printed for.
* @return array<int, array<string, mixed>|string> Filtered URLs.
*/
function es_optimizer_disable_emojis_remove_dns_prefetch( array $urls, string $relation_type ): array {
if ( 'dns-prefetch' !== $relation_type ) {
return $urls;
}
$emoji_svg_url = apply_filters( 'emoji_svg_url', 'https://s.w.org/images/core/emoji/2/svg/' );
$emoji_host = wp_parse_url( $emoji_svg_url, PHP_URL_HOST );
if ( ! is_string( $emoji_host ) || '' === $emoji_host ) {
return $urls;
}
return array_values(
array_filter(
$urls,
static function ( array|string $url ) use ( $emoji_host ): bool {
$href = is_array( $url ) ? (string) ( $url['href'] ?? '' ) : $url;
$host = wp_parse_url( $href, PHP_URL_HOST );
return $emoji_host !== $host;
}
)
);
}
/**
* Remove jQuery Migrate from the frontend jQuery dependency list.
*
* @since 1.0.0
* @param WP_Scripts $scripts WP_Scripts object.
*/
function es_optimizer_remove_jquery_migrate( WP_Scripts $scripts ): void {
if ( ! es_optimizer_is_option_enabled( 'remove_jquery_migrate' ) || is_admin() ) {
return;
}
if ( empty( $scripts->registered['jquery'] ) ) {
return;
}
$script = $scripts->registered['jquery'];
$script->deps = array_values( array_diff( $script->deps, array( 'jquery-migrate' ) ) );
}
/**
* Disable classic theme styles added in WordPress 6.1+.
*
* @since 1.3.0
*/
function es_optimizer_disable_classic_theme_styles(): void {
if ( ! es_optimizer_is_option_enabled( 'disable_classic_theme_styles' ) ) {
return;
}
wp_dequeue_style( 'classic-theme-styles' );
wp_deregister_style( 'classic-theme-styles' );
}
/**
* Remove selected WordPress header items.
*
* @since 1.0.0
*/
function es_optimizer_remove_header_items(): void {
if ( es_optimizer_is_option_enabled( 'remove_wp_version' ) ) {
remove_action( 'wp_head', 'wp_generator' );
}
if ( es_optimizer_is_option_enabled( 'remove_rsd_link' ) ) {
remove_action( 'wp_head', 'rsd_link' );
}
if ( es_optimizer_is_option_enabled( 'remove_wlw_manifest' ) ) {
remove_action( 'wp_head', 'wlwmanifest_link' );
}
if ( es_optimizer_is_option_enabled( 'remove_shortlink' ) ) {
remove_action( 'wp_head', 'wp_shortlink_wp_head', 10 );
}
}
/**
* Remove Recent Comments Widget CSS styles.
*
* @since 1.0.0
*/
function es_optimizer_remove_recent_comments_style(): void {
if ( ! es_optimizer_is_option_enabled( 'remove_recent_comments_style' ) ) {
return;
}
add_filter( 'show_recent_comments_widget_style', '__return_false', PHP_INT_MAX );
}
/**
* Add preconnect hints through the native WordPress resource hints API.
*
* @since 1.4.1
* @param array<int, array<string, mixed>|string> $urls URLs to print for resource hints.
* @param string $relation_type The relation type the URLs are printed for.
* @return array<int, array<string, mixed>|string> Resource hints.
*/
function es_optimizer_add_preconnect_resource_hints( array $urls, string $relation_type ): array {
if ( 'preconnect' !== $relation_type || ! es_optimizer_is_frontend_request() ) {
return $urls;
}
if ( ! es_optimizer_is_option_enabled( 'enable_preconnect' ) ) {
return $urls;
}
$font_domains = array( 'fonts.googleapis.com', 'fonts.gstatic.com' );
foreach ( es_optimizer_get_validated_domains( 'preconnect_domains' ) as $domain ) {
if ( es_optimizer_resource_hint_exists( $urls, $domain ) ) {
continue;
}
$host = wp_parse_url( $domain, PHP_URL_HOST );
$hint = array( 'href' => $domain );
if ( is_string( $host ) && in_array( $host, $font_domains, true ) ) {
$hint['crossorigin'] = 'anonymous';
}
$urls[] = $hint;
}
return $urls;
}
/**
* Add DNS prefetch hints through the native WordPress resource hints API.
*
* @since 1.8.0
* @param array<int, array<string, mixed>|string> $urls URLs to print for resource hints.
* @param string $relation_type The relation type the URLs are printed for.
* @return array<int, array<string, mixed>|string> Resource hints.
*/
function es_optimizer_add_dns_prefetch_resource_hints( array $urls, string $relation_type ): array {
if ( 'dns-prefetch' !== $relation_type || ! es_optimizer_is_frontend_request() ) {
return $urls;
}
if ( ! es_optimizer_is_option_enabled( 'enable_dns_prefetch' ) ) {
return $urls;
}
foreach ( es_optimizer_get_validated_domains( 'dns_prefetch_domains' ) as $domain ) {
if ( ! es_optimizer_resource_hint_exists( $urls, $domain ) ) {
$urls[] = $domain;
}
}
return $urls;
}
/**
* Determine whether a resource hint already exists.
*
* @since 2.0.0
* @param array<int, array<string, mixed>|string> $urls Resource hints.
* @param string $href URL to check.
* @return bool True when the hint exists.
*/
function es_optimizer_resource_hint_exists( array $urls, string $href ): bool {
foreach ( $urls as $url ) {
$existing_href = is_array( $url ) ? (string) ( $url['href'] ?? '' ) : $url;
if ( $href === $existing_href ) {
return true;
}
}
return false;
}
/**
* Disable Jetpack advertisements.
*
* @since 1.0.0
*/
function es_optimizer_disable_jetpack_ads(): void {
if ( ! es_optimizer_is_option_enabled( 'disable_jetpack_ads' ) ) {
return;
}
add_filter( 'jetpack_just_in_time_msgs', '__return_false', PHP_INT_MAX );
add_filter( 'jetpack_show_promotions', '__return_false', PHP_INT_MAX );
add_filter( 'jetpack_blaze_enabled', '__return_false', PHP_INT_MAX );
}
/**
* Disable WordPress post via email functionality.
*
* @since 1.0.0
*/
function es_optimizer_disable_post_via_email(): void {
if ( ! es_optimizer_is_option_enabled( 'disable_post_via_email' ) ) {
return;
}
add_filter( 'enable_post_by_email_configuration', '__return_false', PHP_INT_MAX );
}