Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 71 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
ReduxStateController | |
0.00% |
0 / 71 |
|
0.00% |
0 / 5 |
132 | |
0.00% |
0 / 1 |
register_routes | |
0.00% |
0 / 32 |
|
0.00% |
0 / 1 |
2 | |||
get_input_slice_state | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
6 | |||
update_input_slice_state | |
0.00% |
0 / 16 |
|
0.00% |
0 / 1 |
12 | |||
get_sitegen_slice_state | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
update_sitegen_slice_state | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
12 |
1 | <?php |
2 | namespace NewfoldLabs\WP\Module\Onboarding\RestApi; |
3 | |
4 | use NewfoldLabs\WP\Module\Onboarding\Data\Options; |
5 | use NewfoldLabs\WP\Module\Onboarding\Permissions; |
6 | use NewfoldLabs\WP\Module\Onboarding\Services\ReduxStateService; |
7 | |
8 | class ReduxStateController { |
9 | |
10 | /** |
11 | * The namespace of this controller's route. |
12 | * |
13 | * @var string |
14 | */ |
15 | protected $namespace = 'newfold-onboarding/v1'; |
16 | |
17 | /** |
18 | * The endpoint base |
19 | * |
20 | * @var string |
21 | */ |
22 | protected $rest_base = '/redux-state'; |
23 | |
24 | /** |
25 | * Registers rest routes for ReduxStateController class. |
26 | * |
27 | * @return void |
28 | */ |
29 | public function register_routes() { |
30 | \register_rest_route( |
31 | $this->namespace, |
32 | $this->rest_base . '/input-slice', |
33 | array( |
34 | array( |
35 | 'methods' => \WP_REST_Server::READABLE, |
36 | 'callback' => array( $this, 'get_input_slice_state' ), |
37 | 'permission_callback' => array( Permissions::class, 'rest_is_authorized_admin' ), |
38 | ), |
39 | array( |
40 | 'methods' => \WP_REST_Server::EDITABLE, |
41 | 'callback' => array( $this, 'update_input_slice_state' ), |
42 | 'permission_callback' => array( Permissions::class, 'rest_is_authorized_admin' ), |
43 | ), |
44 | ) |
45 | ); |
46 | \register_rest_route( |
47 | $this->namespace, |
48 | $this->rest_base . '/sitegen-slice', |
49 | array( |
50 | array( |
51 | 'methods' => \WP_REST_Server::READABLE, |
52 | 'callback' => array( $this, 'get_sitegen_slice_state' ), |
53 | 'permission_callback' => array( Permissions::class, 'rest_is_authorized_admin' ), |
54 | ), |
55 | array( |
56 | 'methods' => \WP_REST_Server::EDITABLE, |
57 | 'callback' => array( $this, 'update_sitegen_slice_state' ), |
58 | 'permission_callback' => array( Permissions::class, 'rest_is_authorized_admin' ), |
59 | ), |
60 | ) |
61 | ); |
62 | } |
63 | |
64 | |
65 | /** |
66 | * Get the input slice state. |
67 | * |
68 | * @return \WP_REST_Response |
69 | */ |
70 | public function get_input_slice_state() { |
71 | $data = \get_option( Options::get_option_name( 'state_input' ), false ); |
72 | if ( ! $data ) { |
73 | $data = array(); |
74 | } |
75 | |
76 | return new \WP_REST_Response( |
77 | $data, |
78 | 200 |
79 | ); |
80 | } |
81 | |
82 | /** |
83 | * Update the input slice state. |
84 | * |
85 | * @param \WP_REST_Request $request The request object. |
86 | * @return \WP_REST_Response |
87 | */ |
88 | public function update_input_slice_state( \WP_REST_Request $request ): \WP_REST_Response { |
89 | $data = json_decode( $request->get_body(), true ); |
90 | if ( ! $data ) { |
91 | return new \WP_REST_Response( |
92 | 'Invalid data', |
93 | 400 |
94 | ); |
95 | } |
96 | |
97 | $result = ReduxStateService::update( 'input', $data ); |
98 | if ( ! $result ) { |
99 | return new \WP_REST_Response( |
100 | 'Failed to update input slice state', |
101 | 500 |
102 | ); |
103 | } |
104 | |
105 | return new \WP_REST_Response( |
106 | $data, |
107 | 200 |
108 | ); |
109 | } |
110 | |
111 | /** |
112 | * Get the sitegen slice state. |
113 | * |
114 | * @return \WP_REST_Response |
115 | */ |
116 | public function get_sitegen_slice_state() { |
117 | $data = \get_option( Options::get_option_name( 'state_sitegen' ), false ); |
118 | if ( ! $data ) { |
119 | $data = array(); |
120 | } |
121 | } |
122 | |
123 | /** |
124 | * Update the sitegen slice state. |
125 | * |
126 | * @param \WP_REST_Request $request The request object. |
127 | * @return \WP_REST_Response |
128 | */ |
129 | public function update_sitegen_slice_state( \WP_REST_Request $request ): \WP_REST_Response { |
130 | $data = json_decode( $request->get_body(), true ); |
131 | if ( ! $data ) { |
132 | return new \WP_REST_Response( |
133 | 'Invalid data', |
134 | 400 |
135 | ); |
136 | } |
137 | |
138 | $result = ReduxStateService::update( 'sitegen', $data ); |
139 | if ( ! $result ) { |
140 | return new \WP_REST_Response( |
141 | 'Failed to update sitegen slice state', |
142 | 500 |
143 | ); |
144 | } |
145 | |
146 | return new \WP_REST_Response( $data, 200 ); |
147 | } |
148 | } |