Skip to content

Commit

Permalink
Merge pull request #237 from newfold-labs/add/PRESS2-114-implement-si…
Browse files Browse the repository at this point in the history
…te-classification-api

Update primary and secondary type data using helpers from the data module
  • Loading branch information
arunshenoy99 authored May 9, 2023
2 parents ef05532 + 16c8f5d commit 6b739e5
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 16 deletions.
58 changes: 46 additions & 12 deletions includes/RestApi/FlowController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;


/**
Expand Down Expand Up @@ -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 ) {
Expand All @@ -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
Expand Down
7 changes: 3 additions & 4 deletions includes/RestApi/SiteClassificationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();
}
}

0 comments on commit 6b739e5

Please sign in to comment.