forked from DeliciousMedia/quickrest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquickrest.php
44 lines (37 loc) · 1.47 KB
/
quickrest.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
<?php
/**
* Plugin Name: QuickREST
* Plugin URI: https://www.deliciousmedia.co.uk/
* Description: Speed up REST API requests by selective loading of plugins.
* Version: 2.1.0
* Author: Delicious Media Limited
* Author URI: https://www.deliciousmedia.co.uk/
* Text Domain: dm-quickrest
* License: GPLv3 or later
*
* @package dm-quickrest
*/
/**
* Filters the active plugins for this request.
*
* @param array $plugins Activated WordPress plugins.
*
* @return array
*/
function quickrest_filter_plugins( $plugins ) {
if ( ! isset( $_SERVER['REQUEST_URI'] ) || false === strpos( stripcslashes( $_SERVER['REQUEST_URI'] ), rest_get_url_prefix() ) ) {
return $plugins;
}
$plugin_whitelist = apply_filters( 'quickrest_plugin_map', [ '_default' => [] ] );
// Split out the request URI, we're interested in element [2] which will be the namespace.
$url_parts = explode( '/', trailingslashit( $_SERVER['REQUEST_URI'] ) );
// Return early if no namespace has been specified.
if ( ! isset( $url_parts[2] ) ) {
return $plugins;
}
// If we have something for this namespace, use that, otherwise fallback to a default.
$plugins_allowed = isset( $plugin_whitelist[ $url_parts[2] ] ) ? $plugin_whitelist[ $url_parts[2] ] : $plugin_whitelist['_default'];
// Return only plugins which are active and in our whitelist.
return array_intersect( $plugins, (array) $plugins_allowed );
}
add_filter( 'option_active_plugins', 'quickrest_filter_plugins', PHP_INT_MAX - 1, 1 );