-
Notifications
You must be signed in to change notification settings - Fork 0
/
wpudisabler.php
243 lines (204 loc) · 8.8 KB
/
wpudisabler.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
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
<?php
defined('ABSPATH') || die;
/*
Plugin Name: WPU Disabler
Description: Disable WordPress features
Plugin URI: https://github.com/wordPressUtilities/wpudisabler
Update URI: https://github.com/wordPressUtilities/wpudisabler
Version: 0.7.0
Author: Darklg
Author URI: https://darklg.me/
Text Domain: wpudisabler
Domain Path: /lang
Requires at least: 6.2
Requires PHP: 8.0
Network: Optional
License: MIT License
License URI: https://opensource.org/licenses/MIT
*/
class WPUDisabler {
private $plugin_version = '0.7.0';
private $plugin_description;
private $settings_update;
private $disable_wp_api_user_level;
public function __construct() {
add_action('plugins_loaded', array(&$this, 'plugins_loaded'));
add_action('init', array(&$this, 'load_translation'));
}
# TRANSLATION
public function load_translation() {
$lang_dir = dirname(plugin_basename(__FILE__)) . '/lang/';
if (strpos(__DIR__, 'mu-plugins') !== false) {
load_muplugin_textdomain('wpudisabler', $lang_dir);
} else {
load_plugin_textdomain('wpudisabler', false, $lang_dir);
}
$this->plugin_description = __('Disable WordPress features', 'wpudisabler');
}
public function plugins_loaded() {
/* Base UPDATE */
require_once __DIR__ . '/inc/WPUBaseUpdate/WPUBaseUpdate.php';
$this->settings_update = new \wpudisabler\WPUBaseUpdate(
'WordPressUtilities',
'wpudisabler',
$this->plugin_version);
/* Filters */
if (apply_filters('wpudisabler__disable_author_page', false)) {
$this->disable_author_page();
}
if (apply_filters('wpudisabler__disable_feeds', false)) {
$this->disable_feeds();
}
if (apply_filters('wpudisabler__disable_plugin_deactivation', false)) {
$this->disable_plugin_deactivation();
}
$this->disable_wp_api_user_level = apply_filters('wpudisabler__disable_wp_api_user_level', 'remove_users');
if (apply_filters('wpudisabler__disable_wp_api', false)) {
$this->disable_wp_api();
}
if (apply_filters('wpudisabler__disable_wp_api__logged_in', false)) {
$this->disable_wp_api__logged_in();
}
if (apply_filters('wpudisabler__disable_wp_oembed', false)) {
$this->disable_wp_oembed();
}
}
/* ----------------------------------------------------------
Disable author page
---------------------------------------------------------- */
public function disable_author_page() {
add_filter('template_redirect', array(&$this, 'author_page'), 50);
add_filter('author_link', array(&$this, 'get_the_author_url'), 50, 1);
add_filter('get_the_author_url', array(&$this, 'get_the_author_url'), 50, 1);
add_filter('wp_sitemaps_enabled', array(&$this, 'disable_users_sitemap__wp_sitemaps_enabled'), 10, 1);
add_filter('wp_sitemaps_add_provider', array(&$this, 'disable_users_sitemap__wp_sitemaps_add_provider'), 10, 2);
}
public function author_page($template) {
if (is_author()) {
wp_redirect(site_url());
die;
}
return $template;
}
public function get_the_author_url($url) {
return home_url();
}
public function disable_users_sitemap__wp_sitemaps_enabled($is_enabled) {
global $wp_query;
if (is_object($wp_query) && isset($wp_query->query_vars['sitemap']) && $wp_query->query_vars['sitemap'] == 'users') {
return false;
}
return $is_enabled;
}
public function disable_users_sitemap__wp_sitemaps_add_provider($provider, $name) {
if ($name == 'users') {
return false;
}
return $provider;
}
/* ----------------------------------------------------------
Disable feeds
---------------------------------------------------------- */
public function disable_feeds() {
add_action('do_feed', array(&$this, 'disable_feed'), 1);
add_action('do_feed_rdf', array(&$this, 'disable_feed'), 1);
add_action('do_feed_rss', array(&$this, 'disable_feed'), 1);
add_action('do_feed_rss2', array(&$this, 'disable_feed'), 1);
add_action('do_feed_atom', array(&$this, 'disable_feed'), 1);
add_action('do_feed_rss2_comments', array(&$this, 'disable_feed'), 1);
add_action('do_feed_atom_comments', array(&$this, 'disable_feed'), 1);
add_action('feed_links_show_posts_feed', '__return_false', 1);
add_action('feed_links_show_comments_feed', '__return_false', 1);
remove_action('wp_head', 'feed_links_extra', 3);
}
public function disable_feed() {
wp_die(sprintf(__('No feed available, please visit our <a href="%s">homepage</a>!', 'wpudisabler'), get_bloginfo('url')));
}
/* ----------------------------------------------------------
Disable plugin deactivation
---------------------------------------------------------- */
public function disable_plugin_deactivation() {
add_filter('plugin_action_links', array(&$this, 'disable_plugin_deactivation__action'));
}
public function disable_plugin_deactivation__action($links) {
if (isset($links['deactivate'])) {
unset($links['deactivate']);
}
if (isset($links['activate'])) {
unset($links['activate']);
}
return $links;
}
/* ----------------------------------------------------------
Disable WP API
---------------------------------------------------------- */
public function disable_wp_api() {
// XML RPC
add_filter('xmlrpc_enabled', '__return_false');
remove_action('wp_head', 'rsd_link');
remove_action('xmlrpc_rsd_apis', 'rest_output_rsd');
// WP-API version 1.x
add_filter('json_enabled', '__return_false');
add_filter('json_jsonp_enabled', '__return_false');
// WP-API version 2.x
add_filter('rest_enabled', '__return_false');
add_filter('rest_jsonp_enabled', '__return_false');
remove_action('auth_cookie_bad_hash', 'rest_cookie_collect_status');
remove_action('auth_cookie_bad_username', 'rest_cookie_collect_status');
remove_action('auth_cookie_expired', 'rest_cookie_collect_status');
remove_action('auth_cookie_malformed', 'rest_cookie_collect_status');
remove_action('auth_cookie_valid', 'rest_cookie_collect_status');
remove_action('init', 'rest_api_init');
remove_action('parse_request', 'rest_api_loaded');
remove_action('template_redirect', 'rest_output_link_header', 11);
remove_action('wp_head', 'rest_output_link_wp_head', 10);
add_filter('rest_authentication_errors', array(&$this, 'rest_authentication_errors'));
}
public function rest_authentication_errors($access) {
if (!is_user_logged_in()) {
return new WP_Error('rest_cannot_access', __('Nope', 'wpudisabler'), array('status' => rest_authorization_required_code()));
}
return $access;
}
/* Only for logged in users
-------------------------- */
public function disable_wp_api__logged_in() {
if (is_user_logged_in() && current_user_can($this->disable_wp_api_user_level)) {
return;
}
add_action('wp', array(&$this, 'disable_wp_api__logged_in__wp_head'), 999);
add_filter('rest_authentication_errors', array(&$this, 'disable_wp_api__logged_in__rest_authentication_errors'));
}
public function disable_wp_api__logged_in__rest_authentication_errors($result) {
if (!empty($result)) {
return $result;
}
if (!is_user_logged_in()) {
return new WP_Error('rest_not_logged_in', 'You are not currently logged in.', array(
'status' => 401
));
}
if (!current_user_can($this->disable_wp_api_user_level)) {
return new WP_Error('rest_not_admin', 'You are not an administrator.', array(
'status' => 401
));
}
return $result;
}
public function disable_wp_api__logged_in__wp_head() {
remove_action('template_redirect', 'rest_output_link_header', 11);
remove_action('xmlrpc_rsd_apis', 'rest_output_rsd');
remove_action('wp_head', 'rest_output_link_wp_head', 10);
}
/* ----------------------------------------------------------
Disable WP Oembed
---------------------------------------------------------- */
public function disable_wp_oembed() {
/* Header items */
remove_action('wp_head', 'wp_oembed_add_host_js');
remove_action('wp_head', 'wp_oembed_add_discovery_links');
/* REST API route */
remove_action('rest_api_init', 'wp_oembed_register_route');
}
}
$WPUDisabler = new WPUDisabler();