-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwp-anti-detritus.php
146 lines (130 loc) · 6.01 KB
/
wp-anti-detritus.php
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
<?php
/*
* Plugin Name: WP Anti-Detritus
* Plugin URI: https://github.com/FPCSJames/wp-anti-detritus
* GitHub Plugin URI: https://github.com/FPCSJames/wp-anti-detritus
* Description: Ditch the crap in the HTML output and admin area of WordPress.
* Version: 1.1.3
* Author: James M. Joyce, Flashpoint Computer Services, LLC
* Author URI: https://www.flashpointcs.net
* License: MIT
* License URI: https://fpcs.mit-license.org
*/
if(!defined('ABSPATH')) { exit; }
final class WP_Anti_Detritus {
public function __construct() {
add_action('admin_bar_menu', [$this, 'remove_admin_bar_items'], 999);
add_action('wp_dashboard_setup', [$this, 'clean_wp_admin']);
add_action('wp_loaded', [$this, 'clean_wp_head']);
add_filter('body_class', [$this, 'add_slug_to_body_class']);
add_action('widgets_init', [$this, 'remove_default_widgets']);
add_filter('wp_headers', [$this, 'remove_pingback_header']);
add_action('login_headerurl', function() { return home_url(); });
add_filter('admin_footer_text', '__return_null');
add_filter('emoji_svg_url', '__return_false');
add_filter('enable_post_by_email_configuration', '__return_false', 999);
add_filter('feed_links_show_comments_feed', '__return_false');
add_filter('get_image_tag_class', function($c, $i, $align, $s) { return 'align'.esc_attr($align); }, 10, 4);
add_filter('jpeg_quality', function($v) { return 90; });
add_filter('the_generator', '__return_empty_string');
remove_action('welcome_panel', 'wp_welcome_panel');
if(class_exists('RevSliderFront')) {
add_filter('revslider_meta_generator', '__return_null');
}
if(defined('WPAD_DISABLE_REST') && WPAD_DISABLE_REST) {
add_filter('rest_authentication_errors', [$this, 'restrict_rest_api_to_authenticated_users']);
}
}
public function add_slug_to_body_class($classes) {
global $post;
if(isset($post) && is_singular()) {
$classes[] = $post->post_name;
}
return $classes;
}
public function clean_wp_admin() {
remove_meta_box('dashboard_primary', 'dashboard', 'side');
remove_meta_box('dashboard_right_now', 'dashboard', 'normal');
remove_meta_box('dashboard_secondary', 'dashboard', 'normal');
remove_meta_box('dashboard_quick_press', 'dashboard', 'side'); // Quick Draft
remove_meta_box('wpseo-dashboard-overview', 'dashboard', 'normal'); // Yoast
remove_submenu_page('themes.php', 'custom-header'); // Appearance > Header
remove_submenu_page('themes.php', 'custom-background'); // Appearance > Background
if(defined('ELEMENTOR_VERSION')) {
remove_meta_box('e-dashboard-overview', 'dashboard', 'normal');
}
if(class_exists('WooCommerce')) {
remove_meta_box('woocommerce_dashboard_status', 'dashboard', 'normal'); // WooCommerce
}
if(function_exists('tribe_get_events')) {
remove_meta_box('tribe_dashboard_widget', 'dashboard', 'side'); // The Events Calendar
}
if(class_exists('OCEANWP_Theme_Class')) {
remove_meta_box('owp_dashboard_news', 'dashboard', 'normal'); // OceanWP News
}
}
public function clean_wp_head() {
remove_action('wp_head', 'adjacent_posts_rel_link_wp_head');
remove_action('wp_head', 'index_rel_link');
remove_action('wp_head', 'parent_post_rel_link');
remove_action('wp_head', 'print_emoji_detection_script', 7);
remove_action('wp_print_styles', 'print_emoji_styles');
remove_action('wp_head', 'rel_canonical');
remove_action('wp_head', 'rest_output_link_wp_head');
remove_action('wp_head', 'rsd_link');
remove_action('wp_head', 'wlwmanifest_link' );
remove_action('wp_head', 'wp_generator');
remove_action('wp_head', 'wp_oembed_add_discovery_links');
remove_action('wp_head', 'wp_shortlink_wp_head');
// Plugin-specific cleanup
if(function_exists('visual_composer')) { // WPBakery Page Builder: remove generator tag
remove_action('wp_head', [visual_composer(), 'addMetaData']);
}
if(defined('W3TC') && W3TC) { // W3 Total Cache: remove comment in footer
add_filter('w3tc_can_print_comment', '__return_false');
}
if(class_exists('WPSEO_Frontend') && method_exists('WPSEO_Frontend', 'debug_mark')) { // Yoast SEO: remove comments in head
remove_action('wpseo_head', [WPSEO_Frontend::get_instance(), 'debug_mark'], 2);
}
if(class_exists('WooCommerce')) { // WooCommerce: remove generator tag
remove_action('wp_head', 'woo_version');
}
if(class_exists('Easy_Digital_Downloads')) { // EDD: remove generator tag
remove_action('wp_head', 'edd_version_in_header');
}
// Remove Recent Comments markup
global $wp_widget_factory;
if(has_filter('wp_head', 'wp_widget_recent_comments_style')) {
remove_filter('wp_head', 'wp_widget_recent_comments_style');
}
if(isset($wp_widget_factory->widgets['WP_Widget_Recent_Comments'])) {
remove_action('wp_head', [$wp_widget_factory->widgets['WP_Widget_Recent_Comments'], 'recent_comments_style']);
}
wp_deregister_script('wp-embed');
}
public function remove_admin_bar_items($wp_admin_bar) {
$wp_admin_bar->remove_node('wp-logo');
$wp_admin_bar->remove_node('new-content');
}
public function remove_default_widgets() {
unregister_widget('WP_Widget_Pages');
unregister_widget('WP_Widget_Meta');
unregister_widget('WP_Widget_Tag_Cloud');
unregister_widget('WP_Widget_RSS');
unregister_widget('WP_Widget_Calendar');
}
public function remove_pingback_header($headers) {
unset($headers['X-Pingback']);
return $headers;
}
public function restrict_rest_api_to_authenticated_users($result) {
if(!empty($result)) {
return $result;
}
if(!is_user_logged_in()) {
return new WP_Error('rest_not_logged_in', 'API access is restricted to logged-in users.', ['status' => 401]);
}
return $result;
}
}
new WP_Anti_Detritus();