From 16c8f5db81065feaa656653d8092c8cbe3cb92c3 Mon Sep 17 00:00:00 2001 From: arunshenoy99 Date: Tue, 9 May 2023 23:27:00 +0530 Subject: [PATCH] update primary and secondary types in the new option as well --- includes/RestApi/FlowController.php | 58 +++++++++++++++---- .../RestApi/SiteClassificationController.php | 7 +-- 2 files changed, 49 insertions(+), 16 deletions(-) diff --git a/includes/RestApi/FlowController.php b/includes/RestApi/FlowController.php index 363ebd113..2b9137f53 100644 --- a/includes/RestApi/FlowController.php +++ b/includes/RestApi/FlowController.php @@ -5,6 +5,8 @@ use NewfoldLabs\WP\Module\Onboarding\Data\Flows; use NewfoldLabs\WP\Module\Onboarding\Permissions; use NewfoldLabs\WP\Module\Onboarding\Data\Options; +use NewfoldLabs\WP\Module\Data\SiteClassification\PrimaryType; +use NewfoldLabs\WP\Module\Data\SiteClassification\SecondaryType; /** @@ -106,11 +108,11 @@ public function save_onboarding_flow_data( \WP_REST_Request $request ) { $flow_data = $this->read_details_from_wp_options(); if ( ! $flow_data ) { - $flow_data = Flows::get_data(); - $flow_data['createdAt'] = time(); - // update default data if flow type is ecommerce - $flow_data = $this->update_default_data_for_ecommerce( $flow_data ); - $this->save_details_to_wp_options( $flow_data ); + $flow_data = Flows::get_data(); + $flow_data['createdAt'] = time(); + // update default data if flow type is ecommerce + $flow_data = $this->update_default_data_for_ecommerce( $flow_data ); + $this->save_details_to_wp_options( $flow_data ); } foreach ( $params as $key => $param ) { @@ -124,26 +126,58 @@ public function save_onboarding_flow_data( \WP_REST_Request $request ) { } } + /* + [TODO] Handle this and some of the site name, logo, description logic in a cleaner way. + At least the primary and secondary update does not run on every flow data request. + */ + if ( ! empty( $params['data']['siteType']['primary']['value'] ) && + ( $flow_data['data']['siteType']['primary']['value'] !== $params['data']['siteType']['primary']['value'] ) ) { + if ( class_exists( 'NewfoldLabs\WP\Module\Data\SiteClassification\PrimaryType' ) ) { + $primary_type = new PrimaryType( $params['data']['siteType']['primary']['refers'], $params['data']['siteType']['primary']['value'] ); + if ( ! $primary_type->save() ) { + return new \WP_Error( + 'wrong_param_provided', + __( 'Wrong Parameter Provided : primary => value', 'wp-module-onboarding' ), + array( 'status' => 404 ) + ); + } + } + } + + if ( ! empty( $params['data']['siteType']['secondary']['value'] ) && + ( $flow_data['data']['siteType']['secondary']['value'] !== $params['data']['siteType']['secondary']['value'] ) ) { + if ( class_exists( 'NewfoldLabs\WP\Module\Data\SiteClassification\SecondaryType' ) ) { + $secondary_type = new SecondaryType( $params['data']['siteType']['secondary']['refers'], $params['data']['siteType']['secondary']['value'] ); + if ( ! $secondary_type->save() ) { + return new \WP_Error( + 'wrong_param_provided', + __( 'Wrong Parameter Provided : secondary => value', 'wp-module-onboarding' ), + array( 'status' => 404 ) + ); + } + } + } + $flow_data = array_replace_recursive( $flow_data, $params ); // update timestamp once data is updated $flow_data['updatedAt'] = time(); - // Update Blog Information from Basic Info + // Update Blog Information from Basic Info if ( ( ! empty( $flow_data['data']['blogName'] ) ) ) { - \update_option( Options::get_option_name( 'blog_name', false ), $flow_data['data']['blogName'] ); + \update_option( Options::get_option_name( 'blog_name', false ), $flow_data['data']['blogName'] ); } if ( ( ! empty( $flow_data['data']['blogDescription'] ) ) ) { - \update_option( Options::get_option_name( 'blog_description', false ), $flow_data['data']['blogDescription'] ); + \update_option( Options::get_option_name( 'blog_description', false ), $flow_data['data']['blogDescription'] ); } if ( ( ! empty( $flow_data['data']['siteLogo'] ) ) && ! empty( $flow_data['data']['siteLogo']['id'] ) ) { - \update_option( Options::get_option_name( 'site_icon', false ), $flow_data['data']['siteLogo']['id'] ); - \update_option( Options::get_option_name( 'site_logo', false ), $flow_data['data']['siteLogo']['id'] ); + \update_option( Options::get_option_name( 'site_icon', false ), $flow_data['data']['siteLogo']['id'] ); + \update_option( Options::get_option_name( 'site_logo', false ), $flow_data['data']['siteLogo']['id'] ); } else { - \update_option( Options::get_option_name( 'site_icon', false ), 0 ); - \delete_option( Options::get_option_name( 'site_logo', false ) ); + \update_option( Options::get_option_name( 'site_icon', false ), 0 ); + \delete_option( Options::get_option_name( 'site_logo', false ) ); } // save data to database diff --git a/includes/RestApi/SiteClassificationController.php b/includes/RestApi/SiteClassificationController.php index 863e764b3..07aa0ef35 100644 --- a/includes/RestApi/SiteClassificationController.php +++ b/includes/RestApi/SiteClassificationController.php @@ -3,7 +3,7 @@ namespace NewfoldLabs\WP\Module\Onboarding\RestApi; use NewfoldLabs\WP\Module\Onboarding\Permissions; -use NewfoldLabs\WP\Module\Data\SiteClassification; +use NewfoldLabs\WP\Module\Data\SiteClassification\SiteClassification; /** * Class SiteClassificationController @@ -47,10 +47,9 @@ public function register_routes() { * @return array */ public function get() { - if ( ! class_exists( 'NewfoldLabs\WP\Module\Data\SiteClassification' ) ) { + if ( ! class_exists( 'NewfoldLabs\WP\Module\Data\SiteClassification\SiteClassification' ) ) { return array(); } - $classification = new SiteClassification(); - return $classification->get(); + return SiteClassification::get(); } }