Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
30 / 30 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
Verify | |
100.00% |
30 / 30 |
|
100.00% |
3 / 3 |
4 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
register_routes | |
100.00% |
17 / 17 |
|
100.00% |
1 / 1 |
1 | |||
get_items | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
2 |
1 | <?php |
2 | |
3 | namespace NewfoldLabs\WP\Module\Data\API; |
4 | |
5 | use NewfoldLabs\WP\Module\Data\HiiveConnection; |
6 | use WP_Error; |
7 | use WP_REST_Controller; |
8 | use WP_REST_Request; |
9 | use WP_REST_Server; |
10 | use WP_REST_Response; |
11 | |
12 | /** |
13 | * REST API controller for verifying a hiive connection attempt |
14 | */ |
15 | class Verify extends WP_REST_Controller { |
16 | |
17 | /** |
18 | * Instance of HiiveConnection class |
19 | * |
20 | * @var HiiveConnection |
21 | */ |
22 | public $hiive; |
23 | |
24 | /** |
25 | * Constructor. |
26 | * |
27 | * @param HiiveConnection $hiive Instance of the hiive connection manager |
28 | * @since 4.7.0 |
29 | */ |
30 | public function __construct( HiiveConnection $hiive ) { |
31 | $this->hiive = $hiive; |
32 | $this->namespace = 'newfold-data/v1'; |
33 | $this->rest_base = 'verify'; |
34 | } |
35 | |
36 | /** |
37 | * Registers the routes for the objects of the controller. |
38 | * |
39 | * @since 4.7.0 |
40 | * |
41 | * @see register_rest_route() |
42 | * @see HiiveConnection::rest_api_init() |
43 | */ |
44 | public function register_routes() { |
45 | |
46 | register_rest_route( |
47 | $this->namespace, |
48 | '/' . $this->rest_base . '/(?P<token>[a-f0-9]{32})', |
49 | array( |
50 | 'args' => array( |
51 | 'token' => array( |
52 | 'description' => __( 'Connection verification token.' ), |
53 | 'type' => 'string', |
54 | ), |
55 | ), |
56 | array( |
57 | 'methods' => WP_REST_Server::READABLE, |
58 | 'callback' => array( $this, 'get_items' ), |
59 | 'permission_callback' => '__return_true', |
60 | ), |
61 | ) |
62 | ); |
63 | } |
64 | |
65 | /** |
66 | * Returns a verification of the supplied connection token |
67 | * |
68 | * @since 1.0 |
69 | * |
70 | * @param WP_REST_Request $request Full details about the request. |
71 | * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
72 | */ |
73 | public function get_items( $request ) { |
74 | $valid = $this->hiive->verify_token( $request['token'] ); |
75 | $status = ( $valid ) ? 200 : 401; |
76 | |
77 | $response = new WP_REST_Response( |
78 | array( |
79 | 'token' => $request['token'], |
80 | 'valid' => $valid, |
81 | ), |
82 | $status |
83 | ); |
84 | |
85 | return $response; |
86 | } |
87 | } |