-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathonly-rest-api.php
More file actions
335 lines (279 loc) · 7.23 KB
/
only-rest-api.php
File metadata and controls
335 lines (279 loc) · 7.23 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
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
324
325
326
327
328
329
330
331
332
333
334
335
<?php
/**
* Only REST API
*
* @package Only_REST_API
* @author Braad Martin <wordpress@braadmartin.com>
* @license GPL-2.0+
*
* @wordpress-plugin
* Plugin Name: Only REST API
* Plugin URI: https://wordpress.org/plugins/only-rest-api/
* Description: Redirects all front end, non-REST API requests to a single page.
* Version: 1.0.0
* Author: Braad Martin
* Author URI: http://braadmartin.com
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
* Text Domain: only-rest-api
* Domain Path: /languages
*/
// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
die();
}
define( 'ONLY_REST_API_VERSION', '1.0.0' );
/**
* Our main plugin class.
*
* @since 1.0.0
*/
class Only_REST_API {
/**
* Plugin version.
*
* @var string
*/
protected $version;
/**
* Plugin slug.
*
* @var string
*/
protected $slug;
/**
* Plugin display name.
*
* @var string
*/
protected $display_name;
/**
* Plugin option name.
*
* @var string
*/
protected $option_name;
/**
* Plugin options.
*
* @var array
*/
protected $options;
/**
* Define our class properties and set up our hooks.
*
* @since 1.0.0
*/
public function __construct() {
$this->version = ONLY_REST_API_VERSION;
$this->slug = 'only-rest-api';
$this->display_name = __( 'Only REST API', 'only-rest-api' );
$this->option_name = $this->slug . '-options';
$this->initialize();
}
/**
* Set up our hooks.
*
* @since 1.0.0
*/
public function initialize() {
// Primary redirect action.
add_action( 'template_redirect', array( $this, 'template_redirect' ) );
// Add our settings page to the admin menu.
add_action( 'admin_menu', array( $this, 'admin_menu' ) );
// Register our settings.
add_action( 'admin_init', array( $this, 'register_settings' ) );
// Add an action link to the settings page.
add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), array( $this, 'action_links' ) );
}
/**
* If we're displaying a front end page, redirect to the site URL and use our output.
*
* @since 1.0.0
*/
public function template_redirect() {
if ( ! is_front_page() ) {
wp_redirect( home_url() );
exit;
} else {
$this->output_page();
exit;
}
}
/**
* Add our settings page to the admin menu.
*
* @since 1.0.0
*/
public function admin_menu() {
add_options_page(
$this->display_name . ' Settings',
$this->display_name,
'manage_options',
$this->slug,
array( $this, 'options_page' )
);
}
/**
* Output our options page.
*
* @since 1.0.0
*/
public function options_page() {
?>
<div class="wrap only-rest-api-settings-page">
<h1><?php echo $this->display_name; ?></h1>
<form method="post" action="options.php">
<?php
settings_fields( $this->option_name );
do_settings_sections( $this->slug );
submit_button();
?>
</form>
</div>
<?php
}
/**
* Register our settings.
*
* @since 1.0.0
*/
public function register_settings() {
register_setting(
$this->option_name, // Option group.
$this->option_name, // Option name.
array( $this, 'validate_settings' ) // Validate.
);
add_settings_section(
$this->slug . '-general', // Section ID.
__( 'Options', 'only-rest-api' ), // Title.
array( $this, 'settings_section' ), // Callback.
$this->slug // Settings Page.
);
// Message to show on the front end.
add_settings_field(
'message', // Option ID.
__( 'Front end message:', 'only-rest-api' ), // Title.
array( $this, 'message_field' ), // Callback.
$this->slug, // Settings Page.
$this->slug . '-general' // Section ID.
);
}
/**
* Output the settings section.
*
* @since 1.0.0
*/
public function settings_section() {
return;
}
/**
* Output our message field.
*
* @since 1.0.0
*/
public function message_field() {
$options = get_option( $this->option_name );
// Use default message if no custom message has been saved.
if ( ! isset( $options['message'] ) ) {
$options['message'] = __( 'Sorry, this website only answers requests to the REST API. Please try again at a proper endpoint.', 'only-rest-api' );
}
$message = $options['message'];
printf(
'<textarea id="%s-message-field" name="%s" class="widefat" rows="8" cols="60">%s</textarea>',
$this->slug,
$this->option_name . '[message]',
esc_textarea( $message )
);
printf(
'<p>' . __( 'This textarea supports the same HTML as post content. Custom HTML can also be used with the %s and %s filters.', 'only-rest-api' ) . '</p>',
'<code>only_rest_api_page_content</code>',
'<code>only_rest_api_page_html</code>'
);
}
/**
* Validate our settings before saving.
*
* @since 1.0.0
*
* @param array $input The raw settings.
* @return array The clean settings.
*/
public function validate_settings( $input ) {
$new_input = array();
$new_input['message'] = ( isset( $input['message'] ) ) ? wp_kses_post( $input['message'] ) : '';
return $new_input;
}
/**
* Output our front end page.
*
* @since 1.0.0
*/
public function output_page() {
$options = get_option( $this->option_name );
// Use default message if no custom message has been saved.
if ( ! isset( $options['message'] ) ) {
$options['message'] = __( 'Sorry, this website only answers requests to the REST API. Please try again at a proper endpoint.', 'only-rest-api' );
}
// Set up some default css.
$css = '
.page-content {
margin: 200px auto 0;
width: 420px;
max-width: 94%;
font-size: 20px;
text-align: center;
}';
// Allow others to use custom CSS.
$css = apply_filters( 'only_rest_api_page_css', $css, $options );
$content = sprintf(
'<style type="text/css">%s</style><div class="page-content">%s</div>',
$css,
$options['message']
);
// Allow others to use custom output.
$content = apply_filters( 'only_rest_api_page_content', $content, $options );
// Set up our default page HTML.
ob_start();
?>
<!doctype html>
<html lang="">
<head>
<meta charset="utf-8">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<title></title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<?php echo $content; ?>
</body>
</html>
<?php
$page_html = ob_get_clean();
// Allow others to use custom html for the entire page.
$page_html = apply_filters( 'only_rest_api_page_html', $page_html, $options, $content, $css );
echo $page_html;
}
/**
* Add an action link to the settings page.
*
* @since 1.0.0
*
* @param array $link The plugin action links.
* @return array The action links with ours added.
*/
public function action_links( $links ) {
$link = sprintf(
'<a href="%s">%s</a>',
get_admin_url( null, 'options-general.php?page=only-rest-api' ),
__( 'Settings', 'only-rest-api' )
);
$links[] = $link;
return $links;
}
}
/**
* Start the party.
*/
new Only_REST_API();