Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 68 |
|
0.00% |
0 / 7 |
CRAP | |
0.00% |
0 / 1 |
| StatusService | |
0.00% |
0 / 68 |
|
0.00% |
0 / 7 |
812 | |
0.00% |
0 / 1 |
| handle_started | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
12 | |||
| handle_abandoned | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| handle_completed | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| is_site_created_within_last_9_months | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| is_onboarding_restart_eligible | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
30 | |||
| update_onboarding_restart_status | |
0.00% |
0 / 37 |
|
0.00% |
0 / 1 |
90 | |||
| track | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
30 | |||
| 1 | <?php |
| 2 | namespace NewfoldLabs\WP\Module\Onboarding\Services; |
| 3 | |
| 4 | use NewfoldLabs\WP\Module\Onboarding\Data\Brands; |
| 5 | use NewfoldLabs\WP\Module\Onboarding\Data\Options; |
| 6 | use NewfoldLabs\WP\Module\Onboarding\WP_Admin; |
| 7 | use NewfoldLabs\WP\Module\Onboarding\Data\Config; |
| 8 | use NewfoldLabs\WP\Module\Onboarding\Data\Flows\Flows; |
| 9 | use NewfoldLabs\WP\Module\Onboarding\Data\Services\SiteGenService; |
| 10 | |
| 11 | /** |
| 12 | * Tracks the Status of Onboarding. |
| 13 | */ |
| 14 | class StatusService { |
| 15 | |
| 16 | /** |
| 17 | * Handle Onboarding started event. |
| 18 | * |
| 19 | * @return bool True if the onboarding started was marked, false if it was already marked. |
| 20 | */ |
| 21 | public static function handle_started(): bool { |
| 22 | $status = get_option( Options::get_option_name( 'status' ) ); |
| 23 | if ( 'started' !== $status && 'completed' !== $status ) { |
| 24 | update_option( Options::get_option_name( 'status' ), 'started' ); |
| 25 | |
| 26 | // Store start time when onboarding begins |
| 27 | update_option( Options::get_option_name( 'start_time' ), time() ); |
| 28 | |
| 29 | do_action( 'newfold/onboarding/started' ); |
| 30 | return true; |
| 31 | } |
| 32 | return false; |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Handles Onboarding abandoned event. |
| 37 | * |
| 38 | * @return void |
| 39 | */ |
| 40 | public static function handle_abandoned(): void { |
| 41 | if ( 'started' === get_option( Options::get_option_name( 'status' ) ) ) { |
| 42 | update_option( Options::get_option_name( 'status' ), 'abandoned' ); |
| 43 | |
| 44 | // Clean up time tracking when onboarding is abandoned |
| 45 | delete_option( Options::get_option_name( 'start_time' ) ); |
| 46 | delete_option( Options::get_option_name( 'completed_time' ) ); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Handles Onboarding completed event. |
| 52 | * |
| 53 | * @return void |
| 54 | */ |
| 55 | public static function handle_completed(): void { |
| 56 | if ( 'started' === get_option( Options::get_option_name( 'status' ) ) ) { |
| 57 | update_option( Options::get_option_name( 'status' ), 'completed' ); |
| 58 | |
| 59 | // Store completion time |
| 60 | update_option( Options::get_option_name( 'completed_time' ), time() ); |
| 61 | |
| 62 | /** |
| 63 | * We're disabling the restart onboarding feature for now. |
| 64 | */ |
| 65 | // self::update_onboarding_restart_status(); |
| 66 | do_action( 'newfold/onboarding/completed' ); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Checks if the WordPress site was created within the last 9 months (275 days) |
| 72 | * using the 'bluehost_plugin_install_date' option. |
| 73 | * |
| 74 | * @return bool True if the site was created within the last 275 days, false otherwise. |
| 75 | */ |
| 76 | private static function is_site_created_within_last_9_months(): bool { |
| 77 | $install_date_timestamp = get_option( Options::get_option_name( 'bluehost_plugin_install_date', false ) ); |
| 78 | |
| 79 | // If the option is not set or is invalid, return false |
| 80 | if ( ! $install_date_timestamp ) { |
| 81 | return false; |
| 82 | } |
| 83 | |
| 84 | // Calculate the timestamp for 275 days ago (9 months) |
| 85 | $nine_months_ago = time() - ( 275 * 24 * 60 * 60 ); |
| 86 | |
| 87 | return $install_date_timestamp >= $nine_months_ago; |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Checks if the user is eligible to restart onboarding based on brand configuration and AI SiteGen capability. |
| 92 | * |
| 93 | * @return bool True if eligible, false otherwise. |
| 94 | */ |
| 95 | public static function is_onboarding_restart_eligible(): bool { |
| 96 | // Check if the brand is eligible for Restarting Onboarding |
| 97 | $brand_config = Brands::get_brands()[ NFD_ONBOARDING_PLUGIN_BRAND ]['config'] ?? array(); |
| 98 | if ( empty( $brand_config['canRestartOnboarding'] ) || ! $brand_config['canRestartOnboarding'] ) { |
| 99 | return false; |
| 100 | } |
| 101 | |
| 102 | // Check if AI SiteGen Hiive capability is active and the site was created in the last 9 months |
| 103 | if ( ! Config::has_ai_sitegen() || ! self::is_site_created_within_last_9_months() ) { |
| 104 | return false; |
| 105 | } |
| 106 | |
| 107 | return true; |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Handles the flow data and updates the restart eligibility status based on total onboarding tries. |
| 112 | * |
| 113 | * @return void |
| 114 | */ |
| 115 | public static function update_onboarding_restart_status(): void { |
| 116 | if ( isset( $_GET['page'] ) && \sanitize_text_field( wp_unslash( $_GET['page'] ) ) === 'nfd-onboarding' ) { |
| 117 | return; |
| 118 | } |
| 119 | |
| 120 | // Don't do anything if the customer is not eligible |
| 121 | if ( ! self::is_onboarding_restart_eligible() ) { |
| 122 | return; |
| 123 | } |
| 124 | |
| 125 | // Get flow data |
| 126 | $flow_data = get_option( Options::get_option_name( 'flow' ) ); |
| 127 | $active_flow = $flow_data['activeFlow']; |
| 128 | $homepages = $flow_data['sitegen']['homepages']; |
| 129 | |
| 130 | if ( isset( $flow_data['onboardingRetries'] ) && ! empty( $flow_data['onboardingRetries'] ) ) { |
| 131 | // Increment the total onboarding tries |
| 132 | $flow_data['onboardingRetries']['retryCount'] = ( $flow_data['onboardingRetries']['retryCount'] ?? 0 ) + 1; |
| 133 | |
| 134 | // Update the flow data with the incremented total onboarding tries count |
| 135 | update_option( Options::get_option_name( 'flow' ), $flow_data ); |
| 136 | |
| 137 | // Determine eligibility for restarting onboarding |
| 138 | $current_retry_count = $flow_data['onboardingRetries']['retryCount']; |
| 139 | $can_restart = $current_retry_count < $flow_data['onboardingRetries']['maxRetryCount']; |
| 140 | |
| 141 | // Update the eligibility status in wp_option |
| 142 | update_option( Options::get_option_name( 'can_restart' ), $can_restart ); |
| 143 | |
| 144 | if ( $can_restart ) { |
| 145 | // Module AI prefix |
| 146 | $prefix = 'nfd-ai-site-gen-'; |
| 147 | // Sitemeta Options |
| 148 | $enabled_identifiers = array_keys( array_filter( SiteGenService::enabled_identifiers() ) ); |
| 149 | |
| 150 | // Delete enabled identifiers options |
| 151 | foreach ( $enabled_identifiers as $identifier ) { |
| 152 | delete_option( $prefix . SiteGenService::get_identifier_name( $identifier ) ); |
| 153 | } |
| 154 | |
| 155 | // Extra NFD-AI Options |
| 156 | $sitegen_identifiers = array( |
| 157 | 'keywords', |
| 158 | 'homepages', |
| 159 | 'generatedpatterns', |
| 160 | 'contentstructures', |
| 161 | 'siteclassificationmapping', |
| 162 | 'refinedsitedescription', |
| 163 | ); |
| 164 | foreach ( $sitegen_identifiers as $identifier ) { |
| 165 | delete_option( $prefix . $identifier ); |
| 166 | } |
| 167 | |
| 168 | delete_option( Options::get_option_name( 'flow' ) ); |
| 169 | $flow_data_copy = Flows::get_data(); |
| 170 | $flow_data_copy['activeFlow'] = $active_flow; |
| 171 | $flow_data_copy['sitegen']['homepages'] = $homepages; |
| 172 | $flow_data_copy['onboardingRetries']['retryCount'] = $current_retry_count; |
| 173 | |
| 174 | // Update the flow data with the incremented total onboarding tries count and add to db |
| 175 | add_option( Options::get_option_name( 'flow' ), $flow_data_copy ); |
| 176 | |
| 177 | delete_option( Options::get_option_name( 'start_date' ) ); |
| 178 | delete_option( Options::get_option_name( 'status' ) ); |
| 179 | delete_option( Options::get_option_name( 'sitegen_regenerated_homepages' ) ); |
| 180 | } |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * Begin tracking the Onboarding status in an option. |
| 186 | * |
| 187 | * @return void |
| 188 | */ |
| 189 | public static function track(): void { |
| 190 | // Ignore if the request is an AJAX request. |
| 191 | if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) { |
| 192 | return; |
| 193 | } |
| 194 | |
| 195 | // Ignore if the request is not for the onboarding page. |
| 196 | if ( isset( $_GET['page'] ) && \sanitize_text_field( $_GET['page'] ) === WP_Admin::$slug ) { |
| 197 | return; |
| 198 | } |
| 199 | |
| 200 | // Handle abandoned event. |
| 201 | self::handle_abandoned(); |
| 202 | } |
| 203 | } |