Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 35 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
GlobalStylesController | |
0.00% |
0 / 35 |
|
0.00% |
0 / 3 |
42 | |
0.00% |
0 / 1 |
register_routes | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
2 | |||
get_set_color_palette_args | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 | |||
set_color_palette | |
0.00% |
0 / 17 |
|
0.00% |
0 / 1 |
20 |
1 | <?php |
2 | |
3 | namespace NewfoldLabs\WP\Module\Onboarding\RestApi; |
4 | |
5 | use NewfoldLabs\WP\Module\Onboarding\Permissions; |
6 | use NewfoldLabs\WP\Module\Onboarding\Services\GlobalStylesService; |
7 | |
8 | class GlobalStylesController { |
9 | |
10 | /** |
11 | * This is the REST API namespace that will be used for our custom API |
12 | * |
13 | * @var string |
14 | */ |
15 | protected $namespace = 'newfold-onboarding/v1'; |
16 | |
17 | /** |
18 | * This is the REST endpoint |
19 | * |
20 | * @var string |
21 | */ |
22 | protected $rest_base = '/global-styles'; |
23 | |
24 | public function register_routes() { |
25 | \register_rest_route( |
26 | $this->namespace, |
27 | $this->rest_base . '/set-color-palette', |
28 | array( |
29 | array( |
30 | 'methods' => \WP_REST_Server::EDITABLE, |
31 | 'callback' => array( $this, 'set_color_palette' ), |
32 | 'args' => $this->get_set_color_palette_args(), |
33 | 'permission_callback' => array( Permissions::class, 'rest_is_authorized_admin' ), |
34 | ), |
35 | ) |
36 | ); |
37 | } |
38 | |
39 | public function get_set_color_palette_args() { |
40 | return array( |
41 | 'color_palette' => array( |
42 | 'type' => 'object', |
43 | 'required' => true, |
44 | ), |
45 | ); |
46 | } |
47 | |
48 | /** |
49 | * Set the color palette. |
50 | * |
51 | * @param \WP_REST_Request $request The request object. |
52 | * @return \WP_REST_Response |
53 | */ |
54 | public function set_color_palette( \WP_REST_Request $request ): \WP_REST_Response { |
55 | $data = json_decode( $request->get_body(), true ); |
56 | $color_palette = $data['color_palette']; |
57 | if ( ! is_array( $color_palette ) || empty( $color_palette ) ) { |
58 | return new \WP_REST_Response( |
59 | 'Color palette is invalid.', |
60 | 400 |
61 | ); |
62 | } |
63 | |
64 | $response = ( new GlobalStylesService() )->set_color_palette( $color_palette ); |
65 | if ( is_wp_error( $response ) ) { |
66 | return new \WP_REST_Response( |
67 | 'Error setting color palette.', |
68 | 500 |
69 | ); |
70 | } |
71 | |
72 | return new \WP_REST_Response( |
73 | array( 'colors' => $response ), |
74 | 200 |
75 | ); |
76 | } |
77 | } |