generated from alleyinteractive/create-wordpress-plugin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
settings.php
323 lines (291 loc) · 10.2 KB
/
settings.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
<?php
/**
* Plugin Settings
*
* @package rest-api-guard
*/
namespace Alley\WP\REST_API_Guard;
use Firebase\JWT\JWT;
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
add_action( 'admin_menu', __NAMESPACE__ . '\on_admin_menu' );
add_action( 'admin_init', __NAMESPACE__ . '\on_admin_init' );
/**
* Slug for the settings.
*
* @var string
*/
const SETTINGS_KEY = 'rest_api_guard';
/**
* Register the Admin Settings page.
*/
function on_admin_menu() {
/**
* Filter to disable the admin settings page.
*
* @param bool $disable Whether to disable the admin settings page.
*/
if ( true === apply_filters( 'rest_api_guard_disable_admin_settings', false ) ) {
return;
}
add_options_page(
__( 'REST API Guard', 'rest-api-guard' ),
__( 'REST API Guard', 'rest-api-guard' ),
'manage_options',
SETTINGS_KEY,
__NAMESPACE__ . '\render_admin_page',
);
}
/**
* Render the admin settings.
*/
function render_admin_page() {
?>
<div class="wrap">
<h2>
<?php esc_html_e( 'REST API Guard', 'rest-api-guard' ); ?>
</h2>
<?php settings_errors(); ?>
<form method="post" action="options.php">
<?php
settings_fields( SETTINGS_KEY );
do_settings_sections( SETTINGS_KEY );
submit_button();
?>
</form>
</div>
<?php
}
/**
* Register the admin settings.
*/
function on_admin_init() {
register_setting(
SETTINGS_KEY,
SETTINGS_KEY,
[
'sanitize_callback' => __NAMESPACE__ . '\sanitize_settings',
'show_in_rest' => false,
'type' => 'array',
],
);
add_settings_section(
SETTINGS_KEY,
__( 'Settings', 'rest-api-guard' ),
'__return_empty_string',
SETTINGS_KEY,
);
add_settings_field(
'prevent_anonymous_access',
__( 'Prevent Anonymous Access', 'rest-api-guard' ),
__NAMESPACE__ . '\render_field',
SETTINGS_KEY,
SETTINGS_KEY,
[
'description' => __( 'Prevent any anonyous access to the REST API.', 'rest-api-guard' ),
'filter' => 'rest_api_guard_prevent_anonymous_access',
'id' => 'prevent_anonymous_access',
'type' => 'checkbox',
],
);
add_settings_field(
'allow_index_access',
__( 'Allow Index Access', 'rest-api-guard' ),
__NAMESPACE__ . '\render_field',
SETTINGS_KEY,
SETTINGS_KEY,
[
'description' => __( 'Allow access to the REST API Index (/wp-json/).', 'rest-api-guard' ),
'filter' => 'rest_api_guard_allow_index_access',
'id' => 'allow_index_access',
'type' => 'checkbox',
],
);
add_settings_field(
'allow_namespace_access',
__( 'Allow Namespace Access', 'rest-api-guard' ),
__NAMESPACE__ . '\render_field',
SETTINGS_KEY,
SETTINGS_KEY,
[
'description' => __( 'Allow access to the REST API Namespaces (/wp-json/wp/v2/).', 'rest-api-guard' ),
'filter' => 'rest_api_guard_allow_namespace_access',
'id' => 'allow_namespace_access',
'type' => 'checkbox',
],
);
add_settings_field(
'allow_user_access',
__( 'Allow User Access', 'rest-api-guard' ),
__NAMESPACE__ . '\render_field',
SETTINGS_KEY,
SETTINGS_KEY,
[
'description' => __( 'Allow access to the users endpoint (/wp-json/wp/v2/users/).', 'rest-api-guard' ),
'filter' => 'rest_api_guard_allow_user_access',
'id' => 'allow_user_access',
'type' => 'checkbox',
],
);
add_settings_field(
'check_options_requests',
__( 'Apply checks to OPTIONS requests', 'rest-api-guard' ),
__NAMESPACE__ . '\render_field',
SETTINGS_KEY,
SETTINGS_KEY,
[
'description' => __( 'Apply the same checks to OPTIONS requests as other requests.', 'rest-api-guard' ),
'additional' => __( 'By default, the plugin will not apply any checks to OPTIONS requests. This setting will force the plugin to apply the same checks to OPTIONS requests as other requests. For CORS requests, this may need to be disabled to allow authentication with a JWT.', 'rest-api-guard' ),
'filter' => 'rest_api_guard_check_options_requests',
'id' => 'check_options_requests',
'type' => 'checkbox',
],
);
add_settings_field(
'anonymous_requests_allowlist',
__( 'Anonymous Request Allowlist', 'rest-api-guard' ),
__NAMESPACE__ . '\render_field',
SETTINGS_KEY,
SETTINGS_KEY,
[
'description' => __( 'Line-seperated allowlist for anonymous requests that should be allowed. All other requests not matching the list will be denied. This setting takes priority over the denylist below. Supports * as a wildcard.', 'rest-api-guard' ),
'filter' => 'rest_api_guard_anonymous_requests_allowlist',
'id' => 'anonymous_requests_allowlist',
'type' => 'textarea',
],
);
add_settings_field(
'anonymous_requests_denylist',
__( 'Anonymous Request Denylist', 'rest-api-guard' ),
__NAMESPACE__ . '\render_field',
SETTINGS_KEY,
SETTINGS_KEY,
[
'description' => __( 'Line-seperated denylist for anonymous requests that should be denied. All other requests not matching the list will be allowed. Supports * as a wildcard.', 'rest-api-guard' ),
'filter' => 'rest_api_guard_anonymous_requests_denylist',
'id' => 'anonymous_requests_denylist',
'type' => 'textarea',
],
);
if ( class_exists( JWT::class ) ) {
add_settings_field(
'authentication_jwt',
__( 'Require Authentication with JSON Web Token', 'rest-api-guard' ),
__NAMESPACE__ . '\render_field',
SETTINGS_KEY,
SETTINGS_KEY,
[
'description' => __( 'Require authentication with a JSON Web Token (JWT) for all anonymous requests.', 'rest-api-guard' ),
'additional' => sprintf(
/* translators: 1: The JWT audience. 2: The JWT issuer. */
__( 'When enabled, the plugin will require anonymous users to pass an "Authorization: Bearer <token>" with the token being a valid JSON Web Token (JWT). The plugin will be expecting a JWT with an audience of "%1$s", issuer of "%2$s", and secret that matches the value of the "rest_api_guard_jwt_secret" option. When using the token, the user will have unrestricted read-only access to the REST API.', 'rest-api-guard' ),
get_jwt_audience(),
get_jwt_issuer(),
),
'filter' => 'rest_api_guard_authentication_jwt',
'id' => 'authentication_jwt',
'type' => 'checkbox',
],
);
add_settings_field(
'user_authentication_jwt',
__( 'Allow User Authentication with JSON Web Token', 'rest-api-guard' ),
__NAMESPACE__ . '\render_field',
SETTINGS_KEY,
SETTINGS_KEY,
[
'description' => __( 'Allow user authentication with a JSON Web Token (JWT) for all requests.', 'rest-api-guard' ),
'additional' => sprintf(
/* translators: 1: The JWT audience. 2: The JWT issuer. */
__( 'When enabled, the plugin will allow JWTs to be generated against authenticated users. They can be passed as a "Authorization: Bearer <token>" with the token being a valid JSON Web Token (JWT). The plugin will be expecting a JWT with an audience of "%1$s", issuer of "%2$s", and secret that matches the value of the "rest_api_guard_jwt_secret" option. When using the token, the user will have unrestricted access to the REST API mirroring whatever permissions the user associated with the token would have.', 'rest-api-guard' ),
get_jwt_audience(),
get_jwt_issuer(),
),
'filter' => 'rest_api_guard_user_authentication_jwt',
'id' => 'user_authentication_jwt',
'type' => 'checkbox',
],
);
}
}
/**
* Sanitize the settings before saving.
*
* @param array $input The settings to sanitize.
* @return array
*/
function sanitize_settings( $input ) {
if ( empty( $input ) || ! is_array( $input ) ) {
$input = [];
}
return [
'prevent_anonymous_access' => ! empty( $input['prevent_anonymous_access'] ),
'allow_index_access' => ! empty( $input['allow_index_access'] ),
'allow_namespace_access' => ! empty( $input['allow_namespace_access'] ),
'allow_user_access' => ! empty( $input['allow_user_access'] ),
'check_options_requests' => ! empty( $input['check_options_requests'] ),
'anonymous_requests_allowlist' => ! empty( $input['anonymous_requests_allowlist'] ) ? sanitize_textarea_field( $input['anonymous_requests_allowlist'] ) : '',
'anonymous_requests_denylist' => ! empty( $input['anonymous_requests_denylist'] ) ? sanitize_textarea_field( $input['anonymous_requests_denylist'] ) : '',
'authentication_jwt' => ! empty( $input['authentication_jwt'] ),
'user_authentication_jwt' => ! empty( $input['user_authentication_jwt'] ),
];
}
/**
* Render a settings field.
*
* @param array $input Input settings.
*/
function render_field( array $input ) {
$disabled = ! empty( $input['filter'] ) && has_filter( $input['filter'] );
$value = get_option( SETTINGS_KEY )[ $input['id'] ] ?? '';
switch ( $input['type'] ) {
case 'checkbox':
if ( $disabled ) {
/* Documented in plugin.php. */
$value = apply_filters( $input['filter'], false, $value ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.DynamicHooknameFound
}
printf(
'<label for="%1$s"><input type="checkbox" name="%2$s[%1$s]" id="%1$s" value="1" %3$s %4$s /> %5$s</label>',
esc_attr( $input['id'] ),
esc_attr( SETTINGS_KEY ),
checked( $value, 1, false ),
disabled( $disabled, true, false ),
esc_html( $input['description'] )
);
break;
case 'textarea':
if ( $disabled ) {
/* Documented in plugin.php. */
$value = apply_filters( $input['filter'], $value ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.DynamicHooknameFound
}
printf(
'<p><label for="%1$s">%2$s</label></p><p><textarea name="%3$s[%1$s]" id="%1$s" rows="10" cols="50" %4$s>%5$s</textarea></p>',
esc_attr( $input['id'] ),
esc_html( $input['description'] ),
esc_attr( SETTINGS_KEY ),
disabled( $disabled, true, false ),
esc_html( $value )
);
break;
default:
esc_html_e( 'Unknown field type.', 'rest-api-guard' );
break;
}
if ( ! empty( $input['additional'] ) ) {
printf(
'<p><em>%s</em></p>',
esc_html( $input['additional'] )
);
}
if ( $disabled ) {
printf(
'<p><em>%s</em></p>',
sprintf(
/* translators: %s: The name of the filter. */
esc_html__( 'This setting is controlled by a filter: %s.', 'rest-api-guard' ),
'<code>' . esc_html( $input['filter'] ?? '' ) . '</code>'
)
);
}
}