Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 105 |
|
0.00% |
0 / 7 |
CRAP | |
0.00% |
0 / 1 |
| ReduxStateController | |
0.00% |
0 / 105 |
|
0.00% |
0 / 7 |
272 | |
0.00% |
0 / 1 |
| register_routes | |
0.00% |
0 / 48 |
|
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 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| update_sitegen_slice_state | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
12 | |||
| get_blueprints_slice_state | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| update_blueprints_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 | \register_rest_route( |
| 63 | $this->namespace, |
| 64 | $this->rest_base . '/blueprints-slice', |
| 65 | array( |
| 66 | array( |
| 67 | 'methods' => \WP_REST_Server::READABLE, |
| 68 | 'callback' => array( $this, 'get_blueprints_slice_state' ), |
| 69 | 'permission_callback' => array( Permissions::class, 'rest_is_authorized_admin' ), |
| 70 | ), |
| 71 | array( |
| 72 | 'methods' => \WP_REST_Server::EDITABLE, |
| 73 | 'callback' => array( $this, 'update_blueprints_slice_state' ), |
| 74 | 'permission_callback' => array( Permissions::class, 'rest_is_authorized_admin' ), |
| 75 | ), |
| 76 | ) |
| 77 | ); |
| 78 | } |
| 79 | |
| 80 | |
| 81 | /** |
| 82 | * Get the input slice state. |
| 83 | * |
| 84 | * @return \WP_REST_Response |
| 85 | */ |
| 86 | public function get_input_slice_state() { |
| 87 | $data = \get_option( Options::get_option_name( 'state_input' ), false ); |
| 88 | if ( ! $data ) { |
| 89 | $data = array(); |
| 90 | } |
| 91 | |
| 92 | return new \WP_REST_Response( |
| 93 | $data, |
| 94 | 200 |
| 95 | ); |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Update the input slice state. |
| 100 | * |
| 101 | * @param \WP_REST_Request $request The request object. |
| 102 | * @return \WP_REST_Response |
| 103 | */ |
| 104 | public function update_input_slice_state( \WP_REST_Request $request ): \WP_REST_Response { |
| 105 | $data = json_decode( $request->get_body(), true ); |
| 106 | if ( ! $data ) { |
| 107 | return new \WP_REST_Response( |
| 108 | 'Invalid data', |
| 109 | 400 |
| 110 | ); |
| 111 | } |
| 112 | |
| 113 | $result = ReduxStateService::update( 'input', $data ); |
| 114 | if ( ! $result ) { |
| 115 | return new \WP_REST_Response( |
| 116 | 'Failed to update input slice state', |
| 117 | 500 |
| 118 | ); |
| 119 | } |
| 120 | |
| 121 | return new \WP_REST_Response( |
| 122 | $data, |
| 123 | 200 |
| 124 | ); |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Get the sitegen slice state. |
| 129 | * |
| 130 | * @return \WP_REST_Response |
| 131 | */ |
| 132 | public function get_sitegen_slice_state() { |
| 133 | $data = \get_option( Options::get_option_name( 'state_sitegen' ), false ); |
| 134 | if ( ! $data ) { |
| 135 | $data = array(); |
| 136 | } |
| 137 | |
| 138 | return new \WP_REST_Response( $data, 200 ); |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Update the sitegen slice state. |
| 143 | * |
| 144 | * @param \WP_REST_Request $request The request object. |
| 145 | * @return \WP_REST_Response |
| 146 | */ |
| 147 | public function update_sitegen_slice_state( \WP_REST_Request $request ): \WP_REST_Response { |
| 148 | $data = json_decode( $request->get_body(), true ); |
| 149 | if ( ! $data ) { |
| 150 | return new \WP_REST_Response( |
| 151 | 'Invalid data', |
| 152 | 400 |
| 153 | ); |
| 154 | } |
| 155 | |
| 156 | $result = ReduxStateService::update( 'sitegen', $data ); |
| 157 | if ( ! $result ) { |
| 158 | return new \WP_REST_Response( |
| 159 | 'Failed to update sitegen slice state', |
| 160 | 500 |
| 161 | ); |
| 162 | } |
| 163 | |
| 164 | return new \WP_REST_Response( $data, 200 ); |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Get the blueprints slice state. |
| 169 | * |
| 170 | * @return \WP_REST_Response |
| 171 | */ |
| 172 | public function get_blueprints_slice_state() { |
| 173 | $data = \get_option( Options::get_option_name( 'state_blueprints' ), false ); |
| 174 | if ( ! $data ) { |
| 175 | $data = array(); |
| 176 | } |
| 177 | |
| 178 | return new \WP_REST_Response( $data, 200 ); |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * Update the blueprints slice state. |
| 183 | * |
| 184 | * @param \WP_REST_Request $request The request object. |
| 185 | * @return \WP_REST_Response |
| 186 | */ |
| 187 | public function update_blueprints_slice_state( \WP_REST_Request $request ): \WP_REST_Response { |
| 188 | $data = json_decode( $request->get_body(), true ); |
| 189 | if ( ! $data ) { |
| 190 | return new \WP_REST_Response( |
| 191 | 'Invalid data', |
| 192 | 400 |
| 193 | ); |
| 194 | } |
| 195 | |
| 196 | $result = ReduxStateService::update( 'blueprints', $data ); |
| 197 | if ( ! $result ) { |
| 198 | return new \WP_REST_Response( |
| 199 | 'Failed to update blueprints slice state', |
| 200 | 500 |
| 201 | ); |
| 202 | } |
| 203 | |
| 204 | return new \WP_REST_Response( $data, 200 ); |
| 205 | } |
| 206 | } |