Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update primary and secondary type data using helpers from the data module #237

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
}
}