Skip to content

Commit

Permalink
Merge pull request #153 from newfold-labs/fix/flow-param-fallback
Browse files Browse the repository at this point in the history
Fallback to customer data if flow param/option does not exist
  • Loading branch information
arunshenoy99 authored Jan 17, 2023
2 parents d10f871 + 1854e70 commit a4b230d
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 68 deletions.
33 changes: 16 additions & 17 deletions includes/Data/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,19 +58,21 @@ public static function current_brand() {
public static function current_plan() {
$customer_data = self::customer_data();

if ( isset( $customer_data['plan_type'] ) && isset( $customer_data['plan_subtype'] ) ) {
$current_flow = Flows::get_flow_from_customer_data( $customer_data );
if ( false !== $current_flow ) {
return array(
'flow' => Flows::get_flow_from_plan_subtype( $customer_data['plan_subtype'] ),
'flow' => $current_flow,
'subtype' => $customer_data['plan_subtype'],
'type' => $customer_data['plan_type'],
);
}

return array(
'flow' => self::current_flow(),
'subtype' => null,
'type' => null,
);
$current_flow = Flows::get_flow_from_params();
return array(
'flow' => ( false !== $current_flow ) ? $current_flow : Flows::get_default_flow(),
'subtype' => null,
'type' => null,
);
}

/**
Expand All @@ -79,19 +81,16 @@ public static function current_plan() {
* @return string
*/
public static function current_flow() {
$flows = Flows::get_flows();

if ( isset( $_GET['flow'] ) ) {
$current_flow_type = \sanitize_text_field( $_GET['flow'] );
$current_flow = Flows::get_flow_from_params();
if ( false !== $current_flow ) {
return $current_flow;
}

if ( ! empty( $current_flow_type ) && isset( $flows[ $current_flow_type ] ) ) {
return $current_flow_type;
}

$current_flow_type = \get_option( 'nfd_onboarding_flow_preset', false );
if ( $current_flow_type && isset( $flows[ $current_flow_type ] ) ) {
return $current_flow_type;
$customer_data = self::customer_data();
$current_flow = Flows::get_flow_from_customer_data( $customer_data );
if ( false !== $current_flow ) {
return $current_flow;
}

return Flows::get_default_flow();
Expand Down
67 changes: 60 additions & 7 deletions includes/Data/Flows.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
* Contains Onboarding Flow information.
*/
final class Flows {

/**
* Flow data blueprint.
*
* @var array
*/
protected static $data = array(
// Each time step is viewed, insert GMT timestamp to array.
'isViewed' => array(),
Expand Down Expand Up @@ -168,16 +172,65 @@ public static function get_flows() {
}

/**
* @param string $plan_subtype
* Check if a plan is of flow type ecommerce.
*
* Get the corresponding flow given a hosting plan_subtype.
* @param string $plan The hosting plan.
* @return boolean
*/
public static function is_ecommerce_plan( $plan ) {
if ( preg_match( '/^(wc_standard|wc_premium)$/i', $plan ) ) {
return true;
}
return false;
}

/**
* Get the type of flow from the flow query param or the flow preset option.
*
* @return string
* @return string|boolean
*/
public static function get_flow_from_params() {
$flows = self::get_flows();

if ( isset( $_GET['flow'] ) ) {
$current_flow_type = \sanitize_text_field( $_GET['flow'] );
}

if ( ! empty( $current_flow_type ) && isset( $flows[ $current_flow_type ] ) ) {
return $current_flow_type;
}

$current_flow_type = \get_option( Options::get_option_name( 'flow_preset' ), false );
if ( $current_flow_type && isset( $flows[ $current_flow_type ] ) ) {
return $current_flow_type;
}

return false;
}

/**
* Get the flow type given customer data in a particular shape.
*
* @param array $customer_data The customer data to parse.
* @return string|boolean
*/
public static function get_flow_from_customer_data( $customer_data = array() ) {
if ( isset( $customer_data['plan_type'] ) && isset( $customer_data['plan_subtype'] ) ) {
return self::get_flow_from_plan_subtype( $customer_data['plan_subtype'] );
}
return false;
}

/**
* Get the corresponding flow type given a hosting plan_subtype.
*
* @param string $plan_subtype The hosting plan_subtype.
* @return string|boolean
*/
public static function get_flow_from_plan_subtype( $plan_subtype ) {
if ( preg_match( '/^(wc_standard|wc_premium)$/i', $plan_subtype ) ) {
return isset( self::get_flows()['ecommerce'] ) ? 'ecommerce' : self::get_default_flow();
if ( self::is_ecommerce_plan( $plan_subtype ) ) {
return isset( self::get_flows()['ecommerce'] ) ? 'ecommerce' : false;
}
return self::get_default_flow();
return false;
}
}
52 changes: 35 additions & 17 deletions includes/Data/Options.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,20 @@
* Stores all the WordPress Options used in the module.
*/
final class Options {
/**
* @var string Prefix for options in the module.
*/
/**
* Prefix for options in the module.
*
* @var string
*/

protected static $prefix = 'nfd_module_onboarding_';

/**
* @var array List of all the options
*/
/**
* List of all the options.
*
* @var array
*/

protected static $options = array(
'redirect' => 'redirect',
'redirect_param' => 'redirect_param',
Expand Down Expand Up @@ -41,8 +47,14 @@ final class Options {
'show_on_front' => 'show_on_front',
'page_on_front' => 'page_on_front',
'theme_settings' => 'theme_settings',
'flow_preset' => 'flow_preset',
);

/**
* Contains all the options and their values to be initialized by onboarding.
*
* @var array
*/
protected static $initialization_options = array(
'close_comments_for_old_posts' => 1,
'close_comments_days_old' => 28,
Expand All @@ -55,14 +67,13 @@ final class Options {
'permalink_structure' => '/%postname%/',
);

/**
* Get option name for a given key.
*
* @param string $option_key The key for the Options::$options array.
* @param bool $attach_prefix Attach the module prefix.
*
* @return string
*/
/**
* Get option name for a given key.
*
* @param string $option_key The key for the Options::$options array.
* @param boolean $attach_prefix Attach the module prefix.
* @return string|boolean
*/
public static function get_option_name( $option_key, $attach_prefix = true ) {
return isset( self::$options[ $option_key ] )
? ( $attach_prefix
Expand All @@ -72,13 +83,20 @@ public static function get_option_name( $option_key, $attach_prefix = true ) {
: false;
}

/**
* @return array List of all options.
*/
/**
* Get the list of all options.
*
* @return array
*/
public static function get_all_options() {
return self::$options;
}

/**
* Get the list of all initialization options with their values.
*
* @return array
*/
public static function get_initialization_options() {
return self::$initialization_options;
}
Expand Down
53 changes: 26 additions & 27 deletions includes/ModuleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,89 +10,88 @@

/**
* Class ModuleController
*
*/
class ModuleController {

/**
/**
* Initialize the Module Controller functionality.
*/
public static function init() {
// Check the conditions after the step_theme loads as only after that the moudle had been registered prior
add_action( 'after_setup_theme', array( __CLASS__, 'module_switcher' ), 10, 0 );
}

/**
/**
* Check if the user is a valid Ecommerce and subsequently enable/disable modules
*/
public static function module_switcher() {

$module_name = 'onboarding';
$customer_data = Data::customer_data();
$module_name = 'onboarding';
$customer_data = Data::customer_data();

// Sample data for Testing
// $customer_data['plan_subtype'] = 'wc_standard';
// $customer_data['signup_date'] = '2022-08-18T15:30:00.000Z';
// $customer_data['plan_subtype'] = 'wc_standard';
// $customer_data['signup_date'] = '2022-08-18T15:30:00.000Z';

// Check if he is a Non-Ecom Cust and Disable Redirect and Module
if( !self::is_ecom_customer( $customer_data ) ){
if ( ! self::is_ecom_customer( $customer_data ) ) {

// Check if the Module Does Exist
if( ModuleRegistry::get( $module_name ) ){
if ( ModuleRegistry::get( $module_name ) ) {

// Disable the Redirect for Onboarding SPA
LoginRedirect::disable_redirect();
LoginRedirect::disable_redirect();

// Deactivate the Module
deactivate( $module_name );
}
}
else {
} else {

// Check if the Module Does Exist
if( ModuleRegistry::get( $module_name ) ){
if ( ModuleRegistry::get( $module_name ) ) {

// Enable the Redirect for Onboarding SPA
LoginRedirect::enable_redirect();
LoginRedirect::enable_redirect();

// Activate the Module
activate( $module_name );
}
}

}

/**
* Get the current customer data using the Bluehost customer data module.
*
* @return bool
* @param array $customer_data The customer data to be parsed.
* @return boolean
*/
public static function is_ecom_customer( $customer_data ) {

if ( isset( $_GET['flow'] ) && 'ecommerce' === \sanitize_text_field( $_GET['flow'] ) ) {
return true;
return true;
}

// August 18
$new_cust_date = date("Y-m-d H:i:s", strtotime('2022-08-18T15:30:00.000Z'));
$new_cust_date = gmdate( 'Y-m-d H:i:s', strtotime( '2022-08-18T15:30:00.000Z' ) );

if ( isset( $customer_data['plan_subtype'] ) && isset( $customer_data['signup_date'] ) ) {

// Convert the Customer Signup Date to a Php known format
$cust_signup_date = date("Y-m-d H:i:s", strtotime( $customer_data['signup_date'] ));
$cust_signup_date = gmdate( 'Y-m-d H:i:s', strtotime( $customer_data['signup_date'] ) );

// Check if the Customer is a new Customer
$is_new_cust = $cust_signup_date >= $new_cust_date;

// Check if the Customer has an Ecom Plan
$has_ecom_plan = Flows::get_flow_from_plan_subtype( $customer_data['plan_subtype'] ) === "ecommerce";
$has_ecom_plan = Flows::is_ecommerce_plan( $customer_data['plan_subtype'] );

if( $has_ecom_plan && $is_new_cust ){
if ( $has_ecom_plan && $is_new_cust ) {
return true;
}
}

// If the Customer is not a Ecommerce Customer or is an Old Customer
return false;
}
}
}

0 comments on commit a4b230d

Please sign in to comment.