-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDeclutterWP.Declutter.php
More file actions
148 lines (116 loc) · 4.89 KB
/
DeclutterWP.Declutter.php
File metadata and controls
148 lines (116 loc) · 4.89 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
<?php
/*
* Declutter
*/
class DeclutterWP_Declutter {
/*
* Constructor
*/
public function __construct($options) {
if (isset($options['remove-header-junk'])) { $this->removeHeaderJunk(); }
if (isset($options['disable-json-rest-api'])) { $this->disableJsonRestApi(); }
if (isset($options['disable-oembed'])) { $this->disableOEmbed(); }
if (isset($options['disable-xml-rpc'])) { $this->disableXmlRpc(); }
if (isset($options['remove-emoji-support'])) { $this->removeEmojiSupport(); }
}
/*
* Methods
*/
private function removeHeaderJunk() {
// Remove canonical URL and shortlink
remove_action('wp_head', 'rel_canonical');
remove_action('wp_head', 'wp_shortlink_wp_head');
// Remove links to index, parent, prev, next and random post
remove_action('wp_head', 'index_rel_link');
remove_action('wp_head', 'parent_post_rel_link', 10, 0);
remove_action('wp_head', 'adjacent_posts_rel_link', 10, 0);
remove_action('wp_head', 'adjacent_posts_rel_link_wp_head', 10);
remove_action('wp_head', 'start_post_rel_link', 10, 0);
// Remove feed links
remove_action('wp_head', 'feed_links', 2);
remove_action('wp_head', 'feed_links_extra', 3);
// Remove generator from html and feed
remove_action('wp_head', 'wp_generator');
add_filter('the_generator', '__return_false');
}
private function disableJsonRestApi() {
// Filters for WP-API version 1.x
add_filter('json_enabled', '__return_false');
add_filter('json_jsonp_enabled', '__return_false');
// Filters for WP-API version 2.x
add_filter('rest_enabled', '__return_false');
add_filter('rest_jsonp_enabled', '__return_false');
// Remove REST API info from head and headers
remove_action('wp_head', 'rest_output_link_wp_head', 10);
remove_action('xmlrpc_rsd_apis', 'rest_output_rsd');
remove_action('template_redirect', 'rest_output_link_header', 11);
}
private function disableOEmbed() {
// @var WP $wp
global $wp;
// Remove embed query var
$wp->public_query_vars = array_diff( $wp->public_query_vars, array('embed'));
// Remove REST API endpoint
remove_action('rest_api_init', 'wp_oembed_register_route');
// Turn off oEmbed auto discovery
add_filter('embed_oembed_discover', '__return_false');
// Don't filter oEmbed results.
remove_filter('oembed_dataparse', 'wp_filter_oembed_result', 10);
// Remove all embeds rewrite rules.
add_filter('rewrite_rules_array', array('DeclutterWP_Declutter', 'disableEmbedsRewrites'));
// Remove filter of oEmbed result before HTTP requests are made
remove_filter('pre_oembed_result', 'wp_filter_pre_oembed_result', 10);
// Remove discovery link from header
remove_action('wp_head', 'wp_oembed_add_discovery_links', 10);
// Remove oembed scripts
add_action('wp_enqueue_scripts', function () { wp_deregister_script('wp-embed'); });
remove_action('wp_head', 'wp_oembed_add_host_js');
// Disable embeds in visual editor
add_filter('tiny_mce_plugins', function ($plugins) {
return array_diff( $plugins, array('wpview') );
});
// Disable embeds in content output
remove_filter('the_content', array( $GLOBALS['wp_embed'], 'autoembed'), 8);
}
private function disableXmlRpc() {
// Disable XML-RPC
add_filter('xmlrpc_enabled', '__return_false');
remove_action('wp_head', 'rsd_link');
remove_action('wp_head', 'wlwmanifest_link');
// Remove XMLRPC header entry
add_filter('wp_header', function ($headers) {
unset($headers['X-Pingback']);
return $headers;
});
}
private function removeEmojiSupport() {
// Remove emojis from frontend
add_filter('emoji_svg_url', '__return_false');
remove_action('wp_print_styles', 'print_emoji_styles');
remove_action('wp_head', 'print_emoji_detection_script', 7);
// Remove emojis from admin
add_filter('tiny_mce_plugins', array('DeclutterWP_Declutter', 'disableEmojiInEditor'));
remove_action('admin_print_styles', 'print_emoji_styles');
remove_action('admin_print_scripts', 'print_emoji_detection_script');
// More
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');
}
// Helper methods below
public static function disableEmojiInEditor($plugins) {
if (is_array($plugins)) {
return array_diff($plugins, array('wpemoji'));
} else {
return array();
}
}
public static function disableEmbedsRewrites($rules) {
foreach ($rules as $rule => $rewrite) {
if (false !== strpos($rewrite, 'embed=true')) {
unset( $rules[ $rule ]);
}
}
return $rules;
}
}