forked from johnbillion/query-monitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathREST.php
93 lines (70 loc) · 1.94 KB
/
REST.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
<?php
/**
* REST API request dispatcher.
*
* @package query-monitor
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
class QM_Dispatcher_REST extends QM_Dispatcher {
public $id = 'rest';
public function __construct( QM_Plugin $qm ) {
parent::__construct( $qm );
add_filter( 'rest_post_dispatch', array( $this, 'filter_rest_post_dispatch' ), 1, 3 );
}
/**
* Filters a REST API response in order to add QM's headers.
*
* @param WP_HTTP_Response $result Result to send to the client. Usually a WP_REST_Response.
* @param WP_REST_Server $server Server instance.
* @param WP_REST_Request $request Request used to generate the response.
* @return WP_HTTP_Response Result to send to the client.
*/
public function filter_rest_post_dispatch( WP_HTTP_Response $result, WP_REST_Server $server, WP_REST_Request $request ) {
if ( ! $this->should_dispatch() ) {
return $result;
}
$this->before_output();
/* @var QM_Output_Headers[] */
foreach ( $this->get_outputters( 'headers' ) as $id => $output ) {
$output->output();
}
$this->after_output();
return $result;
}
/**
* @return void
*/
protected function before_output() {
foreach ( glob( $this->qm->plugin_path( 'output/headers/*.php' ) ) as $file ) {
include_once $file;
}
}
/**
* @return bool
*/
public function is_active() {
# If the headers have already been sent then we can't do anything about it
if ( headers_sent() ) {
return false;
}
if ( ! defined( 'REST_REQUEST' ) || ! REST_REQUEST ) {
return false;
}
if ( ! self::user_can_view() ) {
return false;
}
return true;
}
}
/**
* @param array<string, QM_Dispatcher> $dispatchers
* @param QM_Plugin $qm
* @return array<string, QM_Dispatcher>
*/
function register_qm_dispatcher_rest( array $dispatchers, QM_Plugin $qm ) {
$dispatchers['rest'] = new QM_Dispatcher_REST( $qm );
return $dispatchers;
}
add_filter( 'qm/dispatchers', 'register_qm_dispatcher_rest', 10, 2 );