diff --git a/includes/Data/Data.php b/includes/Data/Data.php index f481d3730..def2d711d 100644 --- a/includes/Data/Data.php +++ b/includes/Data/Data.php @@ -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, + ); } /** @@ -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(); diff --git a/includes/Data/Flows.php b/includes/Data/Flows.php index 200b3e2e7..e83acfd59 100644 --- a/includes/Data/Flows.php +++ b/includes/Data/Flows.php @@ -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(), @@ -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; } } diff --git a/includes/Data/Options.php b/includes/Data/Options.php index c3991fcb8..a33c01860 100644 --- a/includes/Data/Options.php +++ b/includes/Data/Options.php @@ -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', @@ -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, @@ -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 @@ -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; } diff --git a/includes/ModuleController.php b/includes/ModuleController.php index 23be63dc5..d1cec8210 100644 --- a/includes/ModuleController.php +++ b/includes/ModuleController.php @@ -10,11 +10,10 @@ /** * Class ModuleController - * */ class ModuleController { - /** + /** * Initialize the Module Controller functionality. */ public static function init() { @@ -22,72 +21,72 @@ public static function init() { 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; } } @@ -95,4 +94,4 @@ public static function is_ecom_customer( $customer_data ) { // If the Customer is not a Ecommerce Customer or is an Old Customer return false; } -} \ No newline at end of file +}