Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 109 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
| FlowController | |
0.00% |
0 / 109 |
|
0.00% |
0 / 6 |
240 | |
0.00% |
0 / 1 |
| register_routes | |
0.00% |
0 / 39 |
|
0.00% |
0 / 1 |
2 | |||
| get_switch_args | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
2 | |||
| get | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| save_onboarding_flow_data | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
6 | |||
| complete | |
0.00% |
0 / 36 |
|
0.00% |
0 / 1 |
56 | |||
| switch | |
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\Data\Services\FlowService; |
| 7 | use NewfoldLabs\WP\Module\Onboarding\Services\PluginService; |
| 8 | use NewfoldLabs\WP\Module\Onboarding\Data\Services\SiteGenService; |
| 9 | use NewfoldLabs\WP\Module\Onboarding\Data\Services\GlobalStylesService; |
| 10 | use NewfoldLabs\WP\Module\Onboarding\Services\StatusService; |
| 11 | use NewfoldLabs\WP\Module\Onboarding\Data\Services\TemplatePartsService; |
| 12 | |
| 13 | /** |
| 14 | * Class FlowController |
| 15 | */ |
| 16 | class FlowController { |
| 17 | |
| 18 | /** |
| 19 | * This is the REST API namespace that will be used for our custom API |
| 20 | * |
| 21 | * @var string |
| 22 | */ |
| 23 | protected $namespace = 'newfold-onboarding/v1'; |
| 24 | |
| 25 | /** |
| 26 | * This is the REST endpoint |
| 27 | * |
| 28 | * @var string |
| 29 | */ |
| 30 | protected $rest_base = '/flow'; |
| 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' ), |
| 45 | 'permission_callback' => array( Permissions::class, 'rest_is_authorized_admin' ), |
| 46 | ), |
| 47 | array( |
| 48 | 'methods' => \WP_REST_Server::EDITABLE, |
| 49 | 'callback' => array( $this, 'save_onboarding_flow_data' ), |
| 50 | 'permission_callback' => array( Permissions::class, 'rest_is_authorized_admin' ), |
| 51 | ), |
| 52 | ) |
| 53 | ); |
| 54 | \register_rest_route( |
| 55 | $this->namespace, |
| 56 | $this->rest_base . '/complete', |
| 57 | array( |
| 58 | array( |
| 59 | 'methods' => \WP_REST_Server::CREATABLE, |
| 60 | 'callback' => array( $this, 'complete' ), |
| 61 | 'permission_callback' => array( Permissions::class, 'rest_is_authorized_admin' ), |
| 62 | ), |
| 63 | ) |
| 64 | ); |
| 65 | |
| 66 | \register_rest_route( |
| 67 | $this->namespace, |
| 68 | $this->rest_base . '/switch', |
| 69 | array( |
| 70 | array( |
| 71 | 'methods' => \WP_REST_Server::CREATABLE, |
| 72 | 'callback' => array( $this, 'switch' ), |
| 73 | 'permission_callback' => array( Permissions::class, 'rest_is_authorized_admin' ), |
| 74 | 'args' => $this->get_switch_args(), |
| 75 | ), |
| 76 | ) |
| 77 | ); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Get the valid request params for the switch endpoint. |
| 82 | * |
| 83 | * @return array |
| 84 | */ |
| 85 | public function get_switch_args() { |
| 86 | return array( |
| 87 | 'flow' => array( |
| 88 | 'required' => true, |
| 89 | 'type' => 'string', |
| 90 | 'sanitize_callback' => 'sanitize_text_field', |
| 91 | ), |
| 92 | ); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Get Onboarding flow data. |
| 97 | * |
| 98 | * @return \WP_REST_Response |
| 99 | */ |
| 100 | public function get() { |
| 101 | return new \WP_REST_Response( |
| 102 | FlowService::get_data(), |
| 103 | 200 |
| 104 | ); |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Update the Onboarding flow data. |
| 109 | * |
| 110 | * @param \WP_REST_Request $request The incoming request. |
| 111 | * |
| 112 | * @return \WP_Error|\WP_REST_Response |
| 113 | */ |
| 114 | public function save_onboarding_flow_data( \WP_REST_Request $request ) { |
| 115 | $params = json_decode( $request->get_body(), true ); |
| 116 | |
| 117 | // Mark Onboarding as started only on the first REST API request from the React App. |
| 118 | update_option( Options::get_option_name( 'redirect' ), '0' ); |
| 119 | StatusService::handle_started(); |
| 120 | |
| 121 | $flow_data = FlowService::update_data( $params ); |
| 122 | if ( is_wp_error( $flow_data ) ) { |
| 123 | return $flow_data; |
| 124 | } |
| 125 | |
| 126 | return new \WP_REST_Response( |
| 127 | $flow_data, |
| 128 | 200 |
| 129 | ); |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Handles the completion of different onboarding flows. |
| 134 | * |
| 135 | * This function processes the completion of various onboarding flows, |
| 136 | * such as site generation and site page publishing, and updates global |
| 137 | * styles and template parts if necessary. |
| 138 | * |
| 139 | * @param \WP_REST_Request $request The request object containing the flow parameter. |
| 140 | * |
| 141 | * @return \WP_REST_Response|\WP_Error A WP_REST_Response object on success, or a WP_Error object on failure. |
| 142 | */ |
| 143 | public function complete( $request ) { |
| 144 | // Retrieve the 'flow' parameter from the request |
| 145 | $flow = $request->get_param( 'flow' ); |
| 146 | |
| 147 | if ( 'sitegen' === $flow ) { |
| 148 | // Read flow data from the WordPress options table |
| 149 | $flow_data_option = FlowService::read_data_from_wp_option( false ); |
| 150 | // Check if flow data does not exist or 'data' key is not set |
| 151 | if ( false === $flow_data_option || ! isset( $flow_data_option['data'] ) ) { |
| 152 | return new \WP_Error( |
| 153 | 'nfd_onboarding_error', |
| 154 | __( 'Flow data does not exist to generate a child theme.', 'wp-module-onboarding' ), |
| 155 | array( 'status' => 500 ) |
| 156 | ); |
| 157 | } |
| 158 | // Retrieve homepage data and the active homepage from the flow data |
| 159 | $homepage_data = $flow_data_option['sitegen']['homepages']['data']; |
| 160 | $active_homepage = $flow_data_option['sitegen']['homepages']['active']; |
| 161 | // Complete the Sitegen flow using the retrieved data |
| 162 | SiteGenService::complete( $active_homepage, $homepage_data ); |
| 163 | return new \WP_REST_Response( |
| 164 | array(), |
| 165 | 201 |
| 166 | ); |
| 167 | } |
| 168 | // If the flow is not 'sitegen', proceed with publishing site pages |
| 169 | $site_pages_publish_request = new \WP_REST_Request( |
| 170 | 'POST', |
| 171 | '/newfold-onboarding/v1/site-pages/publish' |
| 172 | ); |
| 173 | // Execute the request to publish site pages |
| 174 | $site_pages_publish_response = rest_do_request( $site_pages_publish_request ); |
| 175 | if ( $site_pages_publish_response->is_error() ) { |
| 176 | return $site_pages_publish_response->as_error(); |
| 177 | } |
| 178 | |
| 179 | // Get the post ID of the active custom global styles and update it |
| 180 | $active_custom_global_styles_post_id = GlobalStylesService::get_active_custom_global_styles_post_id(); |
| 181 | $status = GlobalStylesService::update_diy_global_style_variation( $active_custom_global_styles_post_id, ); |
| 182 | if ( is_wp_error( $status ) ) { |
| 183 | return $status; |
| 184 | } |
| 185 | |
| 186 | // Update the selected template parts |
| 187 | $status = TemplatePartsService::update_diy_selected_template_parts(); |
| 188 | if ( is_wp_error( $status ) ) { |
| 189 | return $status; |
| 190 | } |
| 191 | |
| 192 | // Return a successful response after completing all processes |
| 193 | return new \WP_REST_Response( |
| 194 | array( |
| 195 | 'message' => __( 'Success.', 'wp-module-onboarding' ), |
| 196 | ), |
| 197 | 201 |
| 198 | ); |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * Switch the Onboarding flow. |
| 203 | * |
| 204 | * @param \WP_REST_Request $request The incoming switch request. |
| 205 | * @return \WP_Error|\WP_REST_Response |
| 206 | */ |
| 207 | public function switch( \WP_REST_Request $request ) { |
| 208 | $flow = $request->get_param( 'flow' ); |
| 209 | $status = FlowService::switch_flow( $flow ); |
| 210 | if ( \is_wp_error( $status ) ) { |
| 211 | return $status; |
| 212 | } |
| 213 | |
| 214 | if ( ! PluginService::initialize() ) { |
| 215 | return new \WP_REST_Response( |
| 216 | array(), |
| 217 | 500 |
| 218 | ); |
| 219 | } |
| 220 | |
| 221 | return new \WP_REST_Response( |
| 222 | array(), |
| 223 | 200 |
| 224 | ); |
| 225 | } |
| 226 | } |