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
|
<?php declare(strict_types = 1); /** * REST API enveloped request dispatcher. * * @package query-monitor */
class QM_Dispatcher_REST_Envelope extends QM_Dispatcher {
public $id = 'rest_envelope';
public function __construct( QM_Plugin $qm ) { parent::__construct( $qm );
add_filter( 'rest_envelope_response', array( $this, 'filter_rest_envelope_response' ), 999, 2 ); }
/** * Filters the enveloped form of a REST API response to add QM's data. * * @param array<string, mixed> $envelope Envelope data. * @param WP_REST_Response $response Original response data. * @return array<string, mixed> Envelope data. */ public function filter_rest_envelope_response( array $envelope, WP_REST_Response $response ) { if ( ! $this->should_dispatch() ) { return $envelope; }
$data = array();
$this->before_output();
/** @var array<string, QM_Output_Raw> $outputters */ $outputters = $this->get_outputters( 'raw' );
foreach ( $outputters as $id => $output ) { $data[ $id ] = $output->get_output(); }
$this->after_output();
$envelope['qm'] = $data;
return $envelope; }
/** * @return void */ protected function before_output() { foreach ( (array) glob( $this->qm->plugin_path( 'output/raw/*.php' ) ) as $file ) { include_once $file; } }
/** * @return bool */ public function is_active() { 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_envelope( array $dispatchers, QM_Plugin $qm ) { $dispatchers['rest_envelope'] = new QM_Dispatcher_REST_Envelope( $qm ); return $dispatchers; }
add_filter( 'qm/dispatchers', 'register_qm_dispatcher_rest_envelope', 10, 2 );
|