Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 20 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
| Incoming_REST_API | |
0.00% |
0 / 20 |
|
0.00% |
0 / 4 |
72 | |
0.00% |
0 / 1 |
| attach_hooks | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| detach_hooks | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| log | |
0.00% |
0 / 17 |
|
0.00% |
0 / 1 |
30 | |||
| get | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Automattic\Jetpack_Inspect\Monitor; |
| 4 | |
| 5 | class Incoming_REST_API implements Observable { |
| 6 | private $logs = array(); |
| 7 | |
| 8 | public function attach_hooks() { |
| 9 | add_action( 'rest_request_after_callbacks', array( $this, 'log' ), 10, 3 ); |
| 10 | } |
| 11 | |
| 12 | public function detach_hooks() { |
| 13 | remove_action( 'rest_request_after_callbacks', array( $this, 'log' ) ); |
| 14 | } |
| 15 | |
| 16 | public function log( $response, $handler, $request ) { |
| 17 | // We might accidentally log too much. |
| 18 | // If route starts with `/jetpack-inspect` ignore it: |
| 19 | if ( strpos( $request->get_route(), '/jetpack-inspect' ) === 0 ) { |
| 20 | return $response; |
| 21 | } |
| 22 | |
| 23 | $url = rest_url( $request->get_route() ); |
| 24 | |
| 25 | $headers = $request->get_headers(); |
| 26 | if ( isset( $headers['cookie'] ) && ! ( defined( 'JETPACK_INSPECT_DEBUG' ) && JETPACK_INSPECT_DEBUG ) ) { |
| 27 | $headers['cookie'] = '<hidden>'; |
| 28 | } |
| 29 | |
| 30 | $this->logs[] = array( |
| 31 | 'url' => $url, |
| 32 | 'request' => array( |
| 33 | 'method' => $request->get_method(), |
| 34 | 'body' => $request->get_body(), |
| 35 | 'query' => $request->get_query_params(), |
| 36 | 'headers' => $headers, |
| 37 | |
| 38 | ), |
| 39 | 'response' => $response, |
| 40 | ); |
| 41 | |
| 42 | return $response; |
| 43 | } |
| 44 | |
| 45 | public function get() { |
| 46 | return $this->logs; |
| 47 | } |
| 48 | } |