Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 21
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
Boost_API
0.00% covered (danger)
0.00%
0 / 21
0.00% covered (danger)
0.00%
0 / 5
90
0.00% covered (danger)
0.00%
0 / 1
 get_client
n/a
0 / 0
n/a
0 / 0
1
 get_api_client
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 get
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 post
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 merge_args
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
6
 default_headers
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2/**
3 * A helper class to help with Boost API interactions.
4 *
5 * @package automattic/jetpack-boost-core
6 */
7
8namespace Automattic\Jetpack\Boost_Core\Lib;
9
10use Automattic\Jetpack\Boost_Core\Contracts\Boost_API_Client;
11
12/**
13 * A class that handles the Boost API client.
14 *
15 * The communication to the backend is done using this class on top of the Boost_API_Client interface.
16 */
17class Boost_API {
18
19    /**
20     * The API client instance.
21     *
22     * @var Boost_API_Client
23     */
24    private static $api_client;
25
26    /**
27     * Get the API client instance.
28     *
29     * @return Boost_API_Client
30     * @deprecated 3.1.1 Use get(), and post() directly instead.
31     */
32    public static function get_client() {
33        return self::get_api_client();
34    }
35
36    /**
37     * Instantiate the API client.
38     *
39     * @return Boost_API_Client
40     */
41    private static function get_api_client() {
42        if ( ! self::$api_client ) {
43            $class            = apply_filters( 'jetpack_boost_api_client_class', WPCOM_Boost_API_Client::class );
44            self::$api_client = new $class();
45        }
46        return self::$api_client;
47    }
48
49    /**
50     * Make a get request to boost API and return response.
51     *
52     * @param string  $path - Request path.
53     * @param mixed[] $query - Query parameters.
54     * @param mixed[] $args - Request arguments.
55     * @return array|\WP_Error
56     */
57    public static function get( $path, $query = array(), $args = null ) {
58        // @phan-suppress-next-line PhanParamTooMany -- By default this is WPCOM_Boost_API_Client, which accepts an extra param.
59        return self::get_api_client()->get( $path, $query, self::merge_args( $args ) );
60    }
61
62    /**
63     * Submit a request to boost API and return response.
64     *
65     * @param string  $path - Request path.
66     * @param mixed[] $payload - Request arguments.
67     * @param mixed[] $args - Request arguments.
68     * @return mixed
69     */
70    public static function post( $path, $payload = array(), $args = null ) {
71        // @phan-suppress-next-line PhanParamTooMany -- By default this is WPCOM_Boost_API_Client, which accepts an extra param.
72        return self::get_api_client()->post( $path, $payload, self::merge_args( $args ) );
73    }
74
75    /**
76     * Merge the arguments with the defaults.
77     *
78     * @param mixed[] $args - Provided arguments.
79     * @return mixed[]
80     */
81    private static function merge_args( $args ) {
82        if ( ! is_array( $args ) ) {
83            $args = wp_parse_args(
84                $args,
85                array(
86                    'headers' => self::default_headers(),
87                )
88            );
89        } else {
90            $args['headers'] = wp_parse_args( $args['headers'] ?? array(), self::default_headers() );
91        }
92
93        return $args;
94    }
95
96    /**
97     * Get the default headers to include with each request.
98     *
99     * @return string[]
100     */
101    public static function default_headers() {
102        $headers = array(
103            'Content-Type' => 'application/json; charset=utf-8',
104        );
105
106        if ( defined( 'JETPACK_BOOST_VERSION' ) ) {
107            $headers['X-Jetpack-Boost-Version'] = JETPACK_BOOST_VERSION;
108        }
109
110        return $headers;
111    }
112}