-
Notifications
You must be signed in to change notification settings - Fork 1
/
konclude-wp-optimizer.php
244 lines (214 loc) · 6.69 KB
/
konclude-wp-optimizer.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
244
<?php
/**
* @package Konclude WordPress Optimizer
* @author Archie Makuwa
* @copyright 2024 Konclude (Pty) Ltd
* @license ??
*
* Plugin Name: Konclude WordPress Optimizer
* Description: A plugin to enhance the security of your WordPress site by setting various security and optimizations settings.
* Version: 1.0.0
* Author: Konclude (Archie Makuwa)
* Author URI: https://www.konclu.de
*
*/
if (!defined('ABSPATH')) {
exit; // Exit if accessed directly.
}
// Add security headers based on options
function kwo_add_security_headers() {
$options = get_option('kwo_options');
if (!empty($options['x_frame_options'])) {
header('X-Frame-Options: SAMEORIGIN');
}
if (!empty($options['csp'])) {
header("Content-Security-Policy: default-src 'self'; script-src 'self' 'unsafe-inline' https://trusted.cdn.com; style-src 'self' 'unsafe-inline'; img-src 'self' data: https:; connect-src 'self' https://api.trusted.com");
}
if (!empty($options['hsts'])) {
header('Strict-Transport-Security: max-age=31536000; includeSubDomains; preload');
}
if (!empty($options['x_content_type_options'])) {
header('X-Content-Type-Options: nosniff');
}
if (!empty($options['x_xss_protection'])) {
header('X-XSS-Protection: 1; mode=block');
}
if (!empty($options['referrer_policy'])) {
header('Referrer-Policy: no-referrer-when-downgrade');
}
if (!empty($options['permissions_policy'])) {
header('Permissions-Policy: geolocation=(self), microphone=()');
}
}
add_action('send_headers', 'kwo_add_security_headers');
// Disable XML-RPC to prevent brute force attacks
if (!empty(get_option('kwo_options')['disable_xmlrpc'])) {
add_filter('xmlrpc_enabled', '__return_false');
}
// Disable file editing from the admin panel
if (!empty(get_option('kwo_options')['disable_file_editing'])) {
define('DISALLOW_FILE_EDIT', true);
}
// Add options page
function kwo_add_admin_menu() {
add_options_page(
'Konclude WordPress Optimizer',
'Security Headers',
'manage_options',
'kwo',
'kwo_options_page'
);
}
add_action('admin_menu', 'kwo_add_admin_menu');
// Register settings
function kwo_settings_init() {
register_setting('kwo', 'kwo_options');
add_settings_section(
'kwo_section_headers',
__('Security Headers', 'kwo'),
'kwo_section_headers_cb',
'kwo'
);
add_settings_field(
'x_frame_options',
__('X-Frame-Options', 'kwo'),
'kwo_x_frame_options_render',
'kwo',
'kwo_section_headers'
);
add_settings_field(
'csp',
__('Content-Security-Policy', 'kwo'),
'kwo_csp_render',
'kwo',
'kwo_section_headers'
);
add_settings_field(
'hsts',
__('HTTP Strict Transport Security (HSTS)', 'kwo'),
'kwo_hsts_render',
'kwo',
'kwo_section_headers'
);
add_settings_field(
'x_content_type_options',
__('X-Content-Type-Options', 'kwo'),
'kwo_x_content_type_options_render',
'kwo',
'kwo_section_headers'
);
add_settings_field(
'x_xss_protection',
__('X-XSS-Protection', 'kwo'),
'kwo_x_xss_protection_render',
'kwo',
'kwo_section_headers'
);
add_settings_field(
'referrer_policy',
__('Referrer Policy', 'kwo'),
'kwo_referrer_policy_render',
'kwo',
'kwo_section_headers'
);
add_settings_field(
'permissions_policy',
__('Permissions Policy', 'kwo'),
'kwo_permissions_policy_render',
'kwo',
'kwo_section_headers'
);
add_settings_section(
'kwo_section_other',
__('Other Security Options', 'kwo'),
'kwo_section_other_cb',
'kwo'
);
add_settings_field(
'disable_xmlrpc',
__('Disable XML-RPC', 'kwo'),
'kwo_disable_xmlrpc_render',
'kwo',
'kwo_section_other'
);
add_settings_field(
'disable_file_editing',
__('Disable File Editing', 'kwo'),
'kwo_disable_file_editing_render',
'kwo',
'kwo_section_other'
);
}
add_action('admin_init', 'kwo_settings_init');
function kwo_section_headers_cb() {
echo __('Configure the HTTP security headers you want to enable.', 'kwo');
}
function kwo_section_other_cb() {
echo __('Additional security options.', 'kwo');
}
function kwo_x_frame_options_render() {
$options = get_option('kwo_options');
?>
<input type='checkbox' name='kwo_options[x_frame_options]' <?php checked($options['x_frame_options'], 1); ?> value='1'>
<?php
}
function kwo_csp_render() {
$options = get_option('kwo_options');
?>
<input type='checkbox' name='kwo_options[csp]' <?php checked($options['csp'], 1); ?> value='1'>
<?php
}
function kwo_hsts_render() {
$options = get_option('kwo_options');
?>
<input type='checkbox' name='kwo_options[hsts]' <?php checked($options['hsts'], 1); ?> value='1'>
<?php
}
function kwo_x_content_type_options_render() {
$options = get_option('kwo_options');
?>
<input type='checkbox' name='kwo_options[x_content_type_options]' <?php checked($options['x_content_type_options'], 1); ?> value='1'>
<?php
}
function kwo_x_xss_protection_render() {
$options = get_option('kwo_options');
?>
<input type='checkbox' name='kwo_options[x_xss_protection]' <?php checked($options['x_xss_protection'], 1); ?> value='1'>
<?php
}
function kwo_referrer_policy_render() {
$options = get_option('kwo_options');
?>
<input type='checkbox' name='kwo_options[referrer_policy]' <?php checked($options['referrer_policy'], 1); ?> value='1'>
<?php
}
function kwo_permissions_policy_render() {
$options = get_option('kwo_options');
?>
<input type='checkbox' name='kwo_options[permissions_policy]' <?php checked($options['permissions_policy'], 1); ?> value='1'>
<?php
}
function kwo_disable_xmlrpc_render() {
$options = get_option('kwo_options');
?>
<input type='checkbox' name='kwo_options[disable_xmlrpc]' <?php checked($options['disable_xmlrpc'], 1); ?> value='1'>
<?php
}
function kwo_disable_file_editing_render() {
$options = get_option('kwo_options');
?>
<input type='checkbox' name='kwo_options[disable_file_editing]' <?php checked($options['disable_file_editing'], 1); ?> value='1'>
<?php
}
function kwo_options_page() {
?>
<form action='options.php' method='post'>
<h2>Konclude WordPress Optimizer</h2>
<?php
settings_fields('kwo');
do_settings_sections('kwo');
submit_button();
?>
</form>
<?php
}