Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 40 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
SiteImagesController | |
0.00% |
0 / 40 |
|
0.00% |
0 / 3 |
30 | |
0.00% |
0 / 1 |
register_routes | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
2 | |||
get_images | |
0.00% |
0 / 21 |
|
0.00% |
0 / 1 |
12 | |||
get_request_params | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | namespace NewfoldLabs\WP\Module\Onboarding\RestApi; |
3 | |
4 | use NewfoldLabs\WP\Module\Onboarding\Permissions; |
5 | |
6 | /** |
7 | * Class SiteImagesController |
8 | */ |
9 | class SiteImagesController extends BaseHiiveController { |
10 | |
11 | /** |
12 | * The namespace of this controller's route. |
13 | * |
14 | * @var string |
15 | */ |
16 | protected $namespace = 'newfold-onboarding/v1'; |
17 | |
18 | /** |
19 | * This is the REST endpoint |
20 | * |
21 | * @var string |
22 | */ |
23 | protected $rest_base = '/site-images'; |
24 | |
25 | /** |
26 | * Results per page |
27 | * |
28 | * @var int |
29 | */ |
30 | protected $results_per_page = 25; |
31 | |
32 | /** |
33 | * Registers rest routes for this controller class. |
34 | * |
35 | * @return void |
36 | */ |
37 | public function register_routes() { |
38 | \register_rest_route( |
39 | $this->namespace, |
40 | $this->rest_base, |
41 | array( |
42 | array( |
43 | 'methods' => \WP_REST_Server::READABLE, |
44 | 'callback' => array( $this, 'get_images' ), |
45 | 'permission_callback' => array( Permissions::class, 'rest_is_authorized_admin' ), |
46 | 'args' => $this->get_request_params(), |
47 | ), |
48 | ) |
49 | ); |
50 | } |
51 | |
52 | /** |
53 | * Query Hiive Unsplash worker for images. |
54 | * |
55 | * @param \WP_REST_Request $request Request model. |
56 | * |
57 | * @return \WP_REST_Response|\WP_Error |
58 | */ |
59 | public function get_images( \WP_REST_Request $request ) { |
60 | // Define the required request and response args. |
61 | $request_args = array( |
62 | 'query' => $request->get_param( 'siteType' ), |
63 | 'per_page' => $this->results_per_page, |
64 | ); |
65 | $response_args = array( 'id', 'width', 'height', 'links', 'description', 'alt_description', 'urls' ); |
66 | |
67 | // Request the Hiive Unsplash worker for images. |
68 | $payload = $this->get( '/workers/unsplash/search/photos', $request_args ); |
69 | if ( \is_wp_error( $payload ) ) { |
70 | return $payload; |
71 | } |
72 | |
73 | // Filter out unnecessary keys from the results. |
74 | $results = json_decode( $payload, true )['results']; |
75 | foreach ( $results as $index => $result ) { |
76 | $results[ $index ] = array_filter( |
77 | $result, |
78 | function ( $key ) use ( $response_args ) { |
79 | return in_array( $key, $response_args ); |
80 | }, |
81 | ARRAY_FILTER_USE_KEY |
82 | ); |
83 | } |
84 | |
85 | return new \WP_REST_Response( |
86 | $results, |
87 | 200 |
88 | ); |
89 | } |
90 | |
91 | /** |
92 | * Get query params for the route. |
93 | * |
94 | * @return array |
95 | */ |
96 | public function get_request_params() { |
97 | return array( |
98 | 'siteType' => array( |
99 | 'type' => 'string', |
100 | 'required' => true, |
101 | 'sanitize_callback' => 'sanitize_text_field', |
102 | ), |
103 | ); |
104 | } |
105 | } |