Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 40
0.00% covered (danger)
0.00%
0 / 5
CRAP
n/a
0 / 0
jetpack_inspect_default_args
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
jetpack_inspect_connection_request
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
12
silent_json_decode
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
12
jetpack_inspect_wp_request
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
jetpack_inspect_request
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3use Automattic\Jetpack\Connection\Client;
4
5function jetpack_inspect_default_args( $args = array() ) {
6    $defaults = array(
7        'method'  => 'GET',
8        'body'    => null,
9        'headers' => array(),
10    );
11
12    return wp_parse_args( $args, $defaults );
13}
14
15function jetpack_inspect_connection_request( $url, $args = array() ) {
16
17    $args = jetpack_inspect_default_args( $args );
18
19    // Building signed request interface differs from wp_remote_request.
20    // Body is passed as an argument.
21    $body = $args['body'];
22    unset( $args['body'] );
23
24    // Request signing process expects the URL to be provided in arguments.
25    $args['url'] = $url;
26
27    // Workaround the Jetpack Connection empty body feature/bug:
28    // @TODO: Maybe show this as a warning/error in the UI?
29    // This might lead to situations "Works in Jetpack Inspector but not IRL"
30    if ( empty( $body ) ) {
31        $body = null;
32    }
33
34    $signature = Client::build_signed_request( $args, $body );
35
36    if ( is_wp_error( $signature ) ) {
37        return $signature;
38    }
39
40    return array(
41        'signature' => $signature,
42        // @phan-suppress-next-line PhanAccessMethodInternal -- Phan is correct, but the usage is intentional.
43        'result'    => Client::_wp_remote_request( $signature['url'], $signature['request'] ),
44    );
45}
46
47function silent_json_decode( $string ) {
48    try {
49        $json = json_decode( $string, false, 512, JSON_THROW_ON_ERROR );
50        if ( is_object( $json ) ) {
51            return $json;
52        }
53    } catch ( Exception $e ) {
54        return $string;
55    }
56}
57
58function jetpack_inspect_wp_request() {
59}
60
61function jetpack_inspect_request( $url, $args ) {
62
63    $args = jetpack_inspect_default_args( $args );
64    // I've been using this for a while now, can't remember why anymore. Nice.
65    // Commented out for now.
66    // if ( ! isset( $headers['Content-Type'] ) ) {
67    // $headers['Content-Type'] = 'application/json; charset=utf-8;';
68    // }
69
70    $request = jetpack_inspect_connection_request( $url, $args );
71
72    if ( is_wp_error( $request ) ) {
73        return $request;
74    }
75
76    $signature = $request['signature'];
77    $result    = $request['result'];
78
79    $body = wp_remote_retrieve_body( $result );
80
81    return array(
82        'body'      => silent_json_decode( $body ),
83        'headers'   => wp_remote_retrieve_headers( $result ),
84        'cookies'   => wp_remote_retrieve_cookies( $result ),
85        'signature' => $signature,
86        'args'      => $args,
87        'response'  => $result,
88    );
89}