From f7bdd1c7fe9b5d8053464e0bf502361e70546e9e Mon Sep 17 00:00:00 2001 From: arunshenoy99 Date: Mon, 16 Jan 2023 14:31:44 +0530 Subject: [PATCH 1/2] improve flow detection logic --- includes/Data/Data.php | 33 +++++++++++------------ includes/Data/Flows.php | 41 +++++++++++++++++++++++++--- includes/Data/Options.php | 1 + includes/ModuleController.php | 50 +++++++++++++++++------------------ 4 files changed, 78 insertions(+), 47 deletions(-) diff --git a/includes/Data/Data.php b/includes/Data/Data.php index f481d3730..33248f16d 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 ( $current_flow !== false ) { 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' => ( $current_flow !== false ) ? $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 ( $current_flow !== false ) { + 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 ( $current_flow !== false ) { + return $current_flow; } return Flows::get_default_flow(); diff --git a/includes/Data/Flows.php b/includes/Data/Flows.php index 200b3e2e7..e0b5391a4 100644 --- a/includes/Data/Flows.php +++ b/includes/Data/Flows.php @@ -167,17 +167,50 @@ public static function get_flows() { ); } + public static function is_ecommerce_plan( $plan ) { + if ( preg_match( '/^(wc_standard|wc_premium)$/i', $plan ) ) { + return true; + } + return false; + } + + 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; + } + + public static function get_flow_from_customer_data( $customer_data ) { + if ( isset( $customer_data['plan_type'] ) && isset( $customer_data['plan_subtype'] ) ) { + return self::get_flow_from_plan_subtype( $customer_data['plan_subtype'] ); + } + return false; + } + /** * @param string $plan_subtype * * Get the corresponding flow given a hosting plan_subtype. * - * @return string + * @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..e7600d893 100644 --- a/includes/Data/Options.php +++ b/includes/Data/Options.php @@ -41,6 +41,7 @@ final class Options { 'show_on_front' => 'show_on_front', 'page_on_front' => 'page_on_front', 'theme_settings' => 'theme_settings', + 'flow_preset' => 'flow_preset', ); protected static $initialization_options = array( diff --git a/includes/ModuleController.php b/includes/ModuleController.php index 23be63dc5..267121f19 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,44 +21,43 @@ 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 ); } } - + } /** @@ -68,26 +66,26 @@ public static function module_switcher() { * @return bool */ 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 = date( '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 = date( '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 +93,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 +} From 1854e702bab192e249244210d272e959baf2ea56 Mon Sep 17 00:00:00 2001 From: arunshenoy99 Date: Mon, 16 Jan 2023 15:08:16 +0530 Subject: [PATCH 2/2] add docblocks, switch to gmdate instead of date --- includes/Data/Data.php | 8 +++--- includes/Data/Flows.php | 32 +++++++++++++++++----- includes/Data/Options.php | 51 +++++++++++++++++++++++------------ includes/ModuleController.php | 7 ++--- 4 files changed, 68 insertions(+), 30 deletions(-) diff --git a/includes/Data/Data.php b/includes/Data/Data.php index 33248f16d..def2d711d 100644 --- a/includes/Data/Data.php +++ b/includes/Data/Data.php @@ -59,7 +59,7 @@ public static function current_plan() { $customer_data = self::customer_data(); $current_flow = Flows::get_flow_from_customer_data( $customer_data ); - if ( $current_flow !== false ) { + if ( false !== $current_flow ) { return array( 'flow' => $current_flow, 'subtype' => $customer_data['plan_subtype'], @@ -69,7 +69,7 @@ public static function current_plan() { $current_flow = Flows::get_flow_from_params(); return array( - 'flow' => ( $current_flow !== false ) ? $current_flow : Flows::get_default_flow(), + 'flow' => ( false !== $current_flow ) ? $current_flow : Flows::get_default_flow(), 'subtype' => null, 'type' => null, ); @@ -83,13 +83,13 @@ public static function current_plan() { public static function current_flow() { $current_flow = Flows::get_flow_from_params(); - if ( $current_flow !== false ) { + if ( false !== $current_flow ) { return $current_flow; } $customer_data = self::customer_data(); $current_flow = Flows::get_flow_from_customer_data( $customer_data ); - if ( $current_flow !== false ) { + if ( false !== $current_flow ) { return $current_flow; } diff --git a/includes/Data/Flows.php b/includes/Data/Flows.php index e0b5391a4..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(), @@ -167,6 +171,12 @@ public static function get_flows() { ); } + /** + * Check if a plan is of flow type ecommerce. + * + * @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; @@ -174,6 +184,11 @@ public static function is_ecommerce_plan( $plan ) { return false; } + /** + * Get the type of flow from the flow query param or the flow preset option. + * + * @return string|boolean + */ public static function get_flow_from_params() { $flows = self::get_flows(); @@ -193,7 +208,13 @@ public static function get_flow_from_params() { return false; } - public static function get_flow_from_customer_data( $customer_data ) { + /** + * 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'] ); } @@ -201,11 +222,10 @@ public static function get_flow_from_customer_data( $customer_data ) { } /** - * @param string $plan_subtype - * - * Get the corresponding flow given a hosting plan_subtype. + * Get the corresponding flow type given a hosting plan_subtype. * - * @return string/boolean + * @param string $plan_subtype The hosting plan_subtype. + * @return string|boolean */ public static function get_flow_from_plan_subtype( $plan_subtype ) { if ( self::is_ecommerce_plan( $plan_subtype ) ) { diff --git a/includes/Data/Options.php b/includes/Data/Options.php index e7600d893..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', @@ -44,6 +50,11 @@ final class Options { '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, @@ -56,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 @@ -73,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 267121f19..d1cec8210 100644 --- a/includes/ModuleController.php +++ b/includes/ModuleController.php @@ -63,7 +63,8 @@ public static function module_switcher() { /** * 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 ) { @@ -72,12 +73,12 @@ public static function is_ecom_customer( $customer_data ) { } // 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;