Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 44
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
ModuleController
0.00% covered (danger)
0.00%
0 / 44
0.00% covered (danger)
0.00%
0 / 5
650
0.00% covered (danger)
0.00%
0 / 1
 init
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 module_switcher
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
20
 verify_onboarding_criteria
0.00% covered (danger)
0.00%
0 / 22
0.00% covered (danger)
0.00%
0 / 1
110
 is_brand_eligible
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
20
 is_commerce_signup
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
42
1<?php
2namespace NewfoldLabs\WP\Module\Onboarding;
3
4use NewfoldLabs\WP\Module\Onboarding\Data\Data;
5use NewfoldLabs\WP\Module\Onboarding\Data\Flows\Flows;
6use NewfoldLabs\WP\Module\Onboarding\Data\Options;
7use NewfoldLabs\WP\Module\Onboarding\Data\Brands;
8use NewfoldLabs\WP\ModuleLoader\ModuleRegistry;
9use function NewfoldLabs\WP\ModuleLoader\activate;
10use function NewfoldLabs\WP\ModuleLoader\deactivate;
11use function NewfoldLabs\WP\ModuleLoader\container;
12
13
14/**
15 * Class ModuleController
16 */
17class ModuleController {
18
19    /**
20     * Initialize the Module Controller functionality.
21     */
22    public static function init() {
23        // Enable/Disable the module after_setup_theme.
24        \add_action( 'after_setup_theme', array( __CLASS__, 'module_switcher' ), 10, 0 );
25    }
26
27    /**
28     * Enable/Disable Onboarding based on certain checks.
29     */
30    public static function module_switcher() {
31        $module_name = 'onboarding';
32        // Set brand context for the module.
33        $current_brand = Brands::set_current_brand( container() );
34
35        $enable_onboarding = self::verify_onboarding_criteria( $current_brand );
36
37        // Check if he is a Non-Ecommerce Customer and Disable Redirect and Module
38        if ( ! $enable_onboarding ) {
39
40            // Check if the Module Does Exist
41            if ( ModuleRegistry::get( $module_name ) ) {
42
43                // Deactivate the Module
44                deactivate( $module_name );
45            }
46        } elseif ( ModuleRegistry::get( $module_name ) ) {
47                // Activate the Module
48                activate( $module_name );
49        }
50    }
51
52    /**
53     * Verify all the necessary criteria to enable Onboarding for the site.
54     *
55     * @param string $brand_name The kebab cased name of the brand.
56     * @return boolean
57     */
58    public static function verify_onboarding_criteria( $brand_name ) {
59        // Check if nfd_module_onboarding_activate query param was passed previously.
60        $activate_transient_name = Options::get_option_name( 'activate_param' );
61        if ( '1' === get_transient( $activate_transient_name ) ) {
62            return true;
63        }
64
65        // If the transient does not exist, check if nfd_module_onboarding_activate query param has been passed.
66        $activate_param_name = Options::get_option_name( 'activate' );
67        if ( isset( $_GET[ $activate_param_name ] ) && Permissions::is_authorized_admin() ) {
68                // Set a 30 day transient that reflects this parameter being passed.
69                set_transient( $activate_transient_name, '1', 2592000 );
70                return true;
71        }
72
73        $eligible_brand = self::is_brand_eligible( $brand_name );
74        if ( ! $eligible_brand ) {
75            return false;
76        }
77
78        $customer_data       = Data::customer_data();
79        $brand_enabled_flows = Flows::get_flows();
80        foreach ( $brand_enabled_flows as $flow => $enabled ) {
81            if ( ! $enabled ) {
82                continue;
83            }
84
85            switch ( $flow ) {
86                case 'ecommerce':
87                    if ( self::is_commerce_signup( $customer_data ) ) {
88                        return true;
89                    }
90                    break;
91                // The Sitegen flow always starts with wp-setup, regardless of whether the AI Sitegen capability is set.
92                case 'wp-setup':
93                    return true;
94            }
95        }
96
97        return false;
98    }
99
100    /**
101     * Checks if a particular brand name is eligible for Onboarding.
102     *
103     * @param string $brand_name The kebab cased name of the brand.
104     * @return boolean
105     */
106    public static function is_brand_eligible( $brand_name ) {
107        if ( false !== strpos( $brand_name, 'hostgator' ) && 'hostgator-br' !== $brand_name && 'hostgator-us' !== $brand_name ) {
108            return false;
109        }
110
111        return true;
112    }
113
114    /**
115     * Determine if the install is a new commerce signup
116     *
117     * @param array $customer_data The site's customer data.
118     * @return boolean
119     */
120    public static function is_commerce_signup( $customer_data ) {
121        // Determine if the flow=ecommerce param is set.
122        if ( isset( $_GET['flow'] ) && 'ecommerce' === \sanitize_text_field( $_GET['flow'] ) ) {
123            return true;
124        }
125
126        // Determine if the install is on a commerce plan (or) has Woocommerce active (commerce priority).
127        $is_commerce = false;
128        if ( isset( $customer_data['plan_subtype'] ) ) {
129            $is_commerce = Flows::is_ecommerce_plan( $customer_data['plan_subtype'] );
130        }
131        if ( ! $is_commerce ) {
132            $is_commerce = Flows::is_commerce_priority();
133        }
134        if ( ! $is_commerce ) {
135            return false;
136        }
137
138        return true;
139    }
140}