Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 47
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
Colors_API
0.00% covered (danger)
0.00%
0 / 47
0.00% covered (danger)
0.00%
0 / 4
306
0.00% covered (danger)
0.00%
0 / 1
 call
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 1
72
 is_valid_route
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 validate_args
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
20
 wpcom_json_api_request_as_blog
0.00% covered (danger)
0.00%
0 / 25
0.00% covered (danger)
0.00%
0 / 1
20
1<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName,Squiz.Commenting.FileComment.Missing
2
3/**
4 * Colors API class.
5 */
6class Colors_API {
7    /**
8     * List of valid routes.
9     *
10     * @var array
11     */
12    public static $valid_routes = array( 'palettes', 'patterns' );
13    /**
14     * List of valid args.
15     *
16     * @var array
17     */
18    public static $valid_args = array( 'colors', 'color', 'limit', 'offset' );
19    /**
20     * API base route.
21     *
22     * @var string
23     */
24    public static $base = 'colors/';
25
26    /**
27     * Make a call to the Colors API.
28     *
29     * @param string|false $route API route.
30     * @param array        $args API args.
31     * @param int|false    $id Item ID if available, false otherwise.
32     * @return array|\WP_Error
33     */
34    public static function call( $route = false, $args = array(), $id = false ) {
35        if ( ! self::is_valid_route( $route ) ) {
36            return new WP_Error( 'Invalid route.' );
37        }
38        $args = self::validate_args( $args );
39        if ( ! is_array( $args ) ) {
40            return new WP_Error( 'Arguments should be an array.' );
41        }
42        if ( empty( $args ) ) {
43            $args = null;
44        }
45        $url = self::$base . $route;
46        if ( $id ) {
47            $url = $url . '/' . absint( $id );
48        }
49        $response = self::wpcom_json_api_request_as_blog( $url, 2, array( 'method' => 'GET' ), $args, 'wpcom' );
50        if ( is_wp_error( $response ) || 200 !== $response['response']['code'] || ! isset( $response['body'] ) ) {
51            return array();
52        }
53        return json_decode( $response['body'], true );
54    }
55
56    /**
57     * Check if route is valid.
58     *
59     * @param string|false $route Color API route.
60     *
61     * @return bool
62     */
63    public static function is_valid_route( $route ) {
64        return in_array( $route, self::$valid_routes, true );
65    }
66
67    /**
68     * Validate args.
69     *
70     * @param array $args Color API args.
71     *
72     * @return bool|array
73     */
74    public static function validate_args( $args ) {
75        if ( ! is_array( $args ) ) {
76            return false;
77        }
78        $valid_args = array();
79        foreach ( $args as $arg => $value ) {
80            if ( in_array( $arg, self::$valid_args, true ) ) {
81                $valid_args[ $arg ] = $value;
82            }
83        }
84        return $valid_args;
85    }
86
87    /**
88     * Query the WordPress.com REST API using the blog token
89     *
90     * Based on `wpcom_json_api_request_as_blog` in fbhepr%2Skers%2Swrgcnpx%2Spynff.wrgcnpx%2Qpyvrag.cuc-og
91     * Modified to work with v2 wpcom endpoints
92     *
93     * @param string            $path Request path.
94     * @param int|string        $version API version.
95     * @param array             $args Request args.
96     * @param array|string|null $body Request body.
97     * @param string            $base_api_path Determines the base API path for jetpack requests; defaults to 'rest'.
98     *
99     * @return array|WP_Error $response Data.
100     */
101    public static function wpcom_json_api_request_as_blog( $path, $version = 1, $args = array(), $body = null, $base_api_path = 'rest' ) {
102        $filtered_args = array_intersect_key(
103            $args,
104            array(
105                'method'      => 'string',
106                'timeout'     => 'int',
107                'redirection' => 'int',
108                'stream'      => 'boolean',
109                'filename'    => 'string',
110                'sslverify'   => 'boolean',
111            )
112        );
113
114        /**
115         * Determines whether Jetpack can send outbound https requests to the WPCOM api.
116         *
117         * @since 3.6.0
118         *
119         * @param bool $proto Defaults to true.
120         */
121        $proto = apply_filters( 'jetpack_can_make_outbound_https', true ) ? 'https' : 'http';
122
123        // unprecedingslashit
124        $_path = preg_replace( '/^\//', '', $path );
125
126        // Use GET by default whereas `remote_request` uses POST
127        if ( isset( $filtered_args['method'] ) && strtoupper( $filtered_args['method'] ) === 'POST' ) {
128            $request_method = 'POST';
129        } else {
130            $request_method = 'GET';
131        }
132
133        $validated_args = array_merge(
134            $filtered_args,
135            array(
136                'url'     => sprintf( '%s://%s/%s/v%s/%s', $proto, JETPACK__WPCOM_JSON_API_HOST, $base_api_path, $version, $_path ),
137                'blog_id' => (int) Jetpack_Options::get_option( 'id' ),
138                'method'  => $request_method,
139            )
140        );
141
142        return Automattic\Jetpack\Connection\Client::remote_request( $validated_args, $body );
143    }
144}