forked from JJJ/chelsea
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchelsea.php
400 lines (337 loc) · 12 KB
/
chelsea.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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
<?php
/**
* Chelsea is a versatile, custom WordPress bootloader using SHORTINIT.
*
* It is a robust drop-in file for your WordPress ABSPATH folder that (when
* properly customized) allows you to conditionally load the bare minimum of PHP
* files required to process a custom request while maintaining an ultra-small
* memory footprint.
*
* Named after JJJ's favorite type of boot:
* https://en.wikipedia.org/wiki/Chelsea_boot
*/
/**
* Define required WordPress globals.
*
* @global WP $wp Current WordPress environment instance.
* @global WP_Query $wp_query WordPress Query object.
* @global WP_Query $wp_the_query WordPress Query object.
*/
global $wp, $wp_query, $wp_the_query;
/**
* Tell WordPress to only initialize its "short" version.
*/
if ( ! defined( 'SHORTINIT' ) ) {
define( 'SHORTINIT', true );
}
/**
* Load WordPress if not already (loads wp-config.php & wp-settings.php).
*/
if ( ! defined( 'ABSPATH' ) ) {
require __DIR__ . '/wp-load.php';
}
/**
* Load a bunch of really useful global PHP variables.
*/
if ( function_exists( 'is_admin' ) ) {
require_once ABSPATH . WPINC . '/vars.php';
}
/** Request Overrides *********************************************************/
/**
* By default, Chelsea assumes that you do not want for WordPress to go through
* all of the normal work of parsing the request, because the major benefit of
* a custom bootloader is that you probably already know what you are doing.
*
* To continue letting WordPress parse the request, delete these filters:
*/
add_filter( 'do_parse_request', '__return_false' );
add_filter( 'pre_handle_404', '__return_true' );
/** Optional Requirements *****************************************************/
/**
* Edit this portion of this file to "require_once" any files that should be
* part of your custom WordPress bootloader.
**
* Warning: You are 100% responsible for the entire dependency tree, and this
* quickly spirals out of control into pure chaos if you do not test
* diligently and/or implement your own autoloading.
*
* Suggestion: Learn autoloading deeply <3
*/
/** Optional Query Arguments **************************************************/
/**
* If you plan to use the default WordPress request parser, you will probably
* want to "register" your custom query variables so that you can precisely
* target the proper requests.
*
* These variables are obviously examples, and you would want to change them
* to anything better that suits your needs.
*/
$extra_query_vars = array(
'chelsea',
'bootloader'
);
// Skip if emptied above (or just delete this block if it's not needed)
if ( ! empty( $extra_query_vars ) ) {
/**
* Filter "query_vars" and add the extra vars to the existing public ones.
*/
add_filter( 'query_vars', function( $public_query_vars = array() ) use ( $extra_query_vars ) {
// Merge the arrays
$retval = array_merge( $public_query_vars, $extra_query_vars );
// Return
return $retval;
} );
}
/** Unhook Defaults ***********************************************************/
/**
* The hooks below are loaded by WordPress via default-filters.php, and are
* fully capable of fatal-erroring your bootloader if you are not careful.
*
* Delete them if you need to, but only if you are really-really sure!
*/
/**
* Unhook KSES init
*/
if ( ! function_exists( 'kses_init' ) ) {
remove_action( 'init', 'kses_init' );
remove_action( 'set_current_user', 'kses_init' );
}
/**
* Maybe unhook REST API
*/
if ( ! function_exists( 'rest_cookie_collect_status' ) ) {
// Init
remove_action( 'init', 'rest_api_init' );
remove_action( 'rest_api_init', 'rest_api_default_filters', 10, 1 );
remove_action( 'rest_api_init', 'register_initial_settings', 10 );
remove_action( 'rest_api_init', 'create_initial_rest_routes', 99 );
remove_action( 'parse_request', 'rest_api_loaded' );
// Auth
remove_action( 'xmlrpc_rsd_apis', 'rest_output_rsd' );
remove_action( 'wp_head', 'rest_output_link_wp_head', 10, 0 );
remove_action( 'template_redirect', 'rest_output_link_header', 11, 0 );
remove_action( 'auth_cookie_malformed', 'rest_cookie_collect_status' );
remove_action( 'auth_cookie_expired', 'rest_cookie_collect_status' );
remove_action( 'auth_cookie_bad_username', 'rest_cookie_collect_status' );
remove_action( 'auth_cookie_bad_hash', 'rest_cookie_collect_status' );
remove_action( 'auth_cookie_valid', 'rest_cookie_collect_status' );
remove_action( 'application_password_failed_authentication', 'rest_application_password_collect_status' );
remove_action( 'application_password_did_authenticate', 'rest_application_password_collect_status', 10, 2 );
remove_filter( 'rest_authentication_errors', 'rest_application_password_check_errors', 90 );
remove_filter( 'rest_authentication_errors', 'rest_cookie_check_errors', 100 );
}
/**
* Maybe unhook Post Formats
*/
if ( ! function_exists( '_post_format_request' ) ) {
remove_filter( 'request', '_post_format_request' );
remove_filter( 'term_link', '_post_format_link', 10, 3 );
remove_filter( 'get_post_format', '_post_format_get_term' );
remove_filter( 'get_terms', '_post_format_get_terms', 10, 3 );
remove_filter( 'wp_get_object_terms', '_post_format_wp_get_object_terms' );
}
/**
* Maybe unhook closing comments for old posts
*/
if ( ! function_exists( '_close_comments_for_old_posts' ) ) {
remove_filter( 'the_posts', '_close_comments_for_old_posts', 10, 2 );
}
/** User Files ****************************************************************/
/**
* Needed for: wp_set_current_user() and related auth
*/
if ( ! function_exists( 'wp_set_current_user' ) ) {
require_once ABSPATH . WPINC . '/pluggable.php';
require_once ABSPATH . WPINC . '/user.php';
require_once ABSPATH . WPINC . '/capabilities.php';
require_once ABSPATH . WPINC . '/class-wp-query.php';
require_once ABSPATH . WPINC . '/class-wp-role.php';
require_once ABSPATH . WPINC . '/class-wp-roles.php';
require_once ABSPATH . WPINC . '/class-wp-user.php';
require_once ABSPATH . WPINC . '/class-wp-session-tokens.php';
require_once ABSPATH . WPINC . '/class-wp-user-meta-session-tokens.php';
}
/** Init Constants ************************************************************/
/**
* 'WP_PLUGIN_URL' and others are used by: wp_cookie_constants()
*
* @see WP->init();
*/
wp_plugin_directory_constants();
/**
* 'ADMIN_COOKIE_PATH' and others are used by: wp_set_auth_cookie()
*
* @see WP->init();
*/
if ( is_multisite() ) {
ms_cookie_constants();
}
/**
* 'SECURE_AUTH_COOKIE' and others are used by: wp_parse_auth_cookie()
*
* @see WP->init();
*/
wp_cookie_constants();
/**
* Sets: 'FORCE_SSL_ADMIN' and 'FORCE_SSL_LOGIN'
*
* @see WP->init();
*/
wp_ssl_constants();
/** Parse Request *************************************************************/
/**
* Filters 'do_parse_request' and attempts to fix common fatal errors from
* calling WP()->send_headers() & friends without including the necessary files.
*
* This big'ol thing only matters when you are trusting WordPress to parse the
* request by deleting line 52 above. It's included here as an example of how
* much of a pain it is to conditionally & manually load files on your own.
*
* @since 1.0.0
* @param bool $doing
* @return bool
*/
function chelsea_do_parse_request_fatal_error_fixer( $doing = true ) {
global $wp_rewrite, $wp_query, $wp_the_query;
// Bail early if already skipping request parsing
if ( empty( $doing ) ) {
return $doing;
}
// Setup The Query global
if ( empty( $wp_the_query ) ) {
require_once ABSPATH . WPINC . '/class-wp-post.php';
$wp_the_query = new WP_Query();
}
// Copy The Query global
if ( empty( $wp_query ) ) {
$wp_query = $wp_the_query;
}
// Load rewrite rules
if ( empty( $wp_rewrite ) ) {
require_once ABSPATH . WPINC . '/rewrite.php';
require_once ABSPATH . WPINC . '/class-wp-rewrite.php';
$wp_rewrite = new WP_Rewrite();
if ( ! function_exists( 'home_url' ) ) {
require_once ABSPATH . WPINC . '/link-template.php';
}
}
// Load query functions
if ( ! function_exists( 'is_404' ) ) {
require_once ABSPATH . WPINC . '/query.php';
}
// Load post functions
if ( ! function_exists( 'get_post_types' ) ) {
require_once ABSPATH . WPINC . '/post.php';
}
// Load taxonomy functions
if ( ! function_exists( 'get_taxonomies' ) ) {
require_once ABSPATH . WPINC . '/taxonomy.php';
}
// Load the feed functions
if ( ! function_exists( 'get_default_feed' ) ) {
require_once ABSPATH . WPINC . '/feed.php';
}
// Load the comment functions
if ( ! function_exists( 'get_lastcommentmodified' ) ) {
require_once ABSPATH . WPINC . '/comment.php';
}
// Options are oddly included in: functions.php, but...
if ( ! function_exists( 'get_option' ) ) {
require_once ABSPATH . WPINC . '/option.php';
}
// Return
return $doing;
}
add_filter( 'do_parse_request', 'chelsea_do_parse_request_fatal_error_fixer' , 99, 3 );
/** Setup WordPress ***********************************************************/
/**
* Below is, essentially, what every WordPress request does when it is trying
* to determine what content to display in a browser, to an API endpoint, or
* whatever else you may want to do with it.
*
* Because this is a custom bootloader, you are free to totally replace most of
* this with anything if you want to, once you understand how all of it works.
*/
// Create a "wp" WordPress global object
$wp = new WP();
// Maybe set the current WordPress user
if ( function_exists( 'wp_set_current_user' ) ) {
$wp->init();
}
/**
* Here is where WordPress would normally parse the request into an array of
* arguments that would be used to query the database.
*/
$wp->parsed = $wp->parse_request( $extra_query_vars );
// Send the basic headers
$wp->send_headers();
// Build the query string
$wp->build_query_string();
/**
* Here is when WordPress would normally query the database for Posts from
* inside of the `wp_posts` database table, using the $wp_the_query global,
* which is a WP_Query object.
*/
if ( ! empty( $wp->parsed ) && is_callable( array( $wp_the_query, 'query' ) ) ) {
$wp_the_query->query( $wp->query_vars );
}
/**
* Here is when WordPress would normally attempt to handle a 404 situation.
*
* Some basic 404 handling is included below if you have chosen to override
* it using the "pre_handle_404" filter above on line 53.
*/
if ( has_filter( 'pre_handle_404', '__return_true' ) ) {
// Default status header code
$wp->status_header = 404;
/**
* When your custom conditions are met, if any, set the status_header value
* to 200.
*/
if ( true === true ) {
$wp->status_header = 200;
}
/**
* You've decided that nothing could be found, so be sure to send over
* all of the necessary 404 headers and things that a browser expects
* to receive when that happens.
*/
if ( 404 === $wp->status_header ) {
/**
* Here is when WordPress would normally set the 404 status of the
* global $wp_query variable.
*/
if ( ! empty( $wp->parsed ) && is_callable( array( $wp_query, 'set_404' ) ) ) {
$wp_query->set_404();
}
// Send the no-cache headers so that the 404 response is not cached
nocache_headers();
}
// Always send the status header with the code from above
status_header( $wp->status_header );
/**
* Fallback to the default WP()->handle_404() handler.
*/
} else {
$wp->handle_404();
}
/**
* Here is when WordPress would normally barf out all of its parsed query_vars
* into global PHP variables.
*/
if ( ! empty( $wp->parsed ) ) {
$wp->register_globals();
}
/**
* Fires once the WordPress environment has been set up.
*
* @param WP $wp Current WordPress environment instance (passed by reference).
*/
do_action_ref_array( 'wp', array( &$wp ) );
/**
* Output a boot.
*
* You can pretty much do whatever you want from here (but why would you _not_
* want to show everyone such an incredible looking boot?!)
*/
echo "\u{1F97E}";