Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
42.31% covered (danger)
42.31%
11 / 26
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
CTBApi
42.31% covered (danger)
42.31%
11 / 26
0.00% covered (danger)
0.00%
0 / 1
2.77
0.00% covered (danger)
0.00%
0 / 1
 registerRoutes
42.31% covered (danger)
42.31%
11 / 26
0.00% covered (danger)
0.00%
0 / 1
2.77
1<?php
2
3namespace NewfoldLabs\WP\Module\GlobalCTB;
4
5use NewfoldLabs\WP\Module\Data\HiiveConnection;
6use NewfoldLabs\WP\Module\Data\SiteCapabilities;
7use WP_Error;
8use function NewfoldLabs\WP\ModuleLoader\container;
9
10/**
11 * Class CTBApi
12 *
13 * @package NewfoldLabs\CTB
14 */
15class CTBApi {
16
17    /**
18     * Register notification routes.
19     */
20    public static function registerRoutes() {
21
22        // Add route for fetching a CTB
23        register_rest_route(
24            'newfold-ctb/v2',
25            '/ctb/(?P<id>[a-zA-Z0-9-]+)',
26            array(
27                'methods'             => \WP_REST_Server::READABLE,
28                'permission_callback' => function () {
29                    return current_user_can( 'manage_options' );
30                },
31                'callback'            => function ( \WP_REST_Request $request ) {
32                    $response = wp_remote_get(
33                        NFD_HIIVE_URL . '/sites/v2/ctb/' . $request->get_param( 'id' ) . '',
34                        array(
35                            'headers' => array(
36                                'Content-Type'  => 'application/json',
37                                'Accept'        => 'application/json',
38                                'Authorization' => 'Bearer ' . HiiveConnection::get_auth_token(),
39                            ),
40                            'timeout' => 20,
41                        )
42                    );
43
44                    if ( $response instanceof WP_Error ) {
45                        return $response;
46                    }
47
48                    return new \WP_REST_Response( json_decode( wp_remote_retrieve_body( $response ) ), wp_remote_retrieve_response_code( $response ) );
49                },
50            )
51        );
52    }
53}