settings = $settings; $this->helper = $helper; $this->config_handler = $config_handler; $this->survey_questions = $survey_questions; $this->surveys_rest = $surveys_rest; } public function is_woocommerce_admin_page(): bool { if ( ! isset( $_SERVER['REQUEST_URI'] ) ) { return false; } $current_uri = sanitize_text_field( $_SERVER['REQUEST_URI'] ); if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) { return false; } if ( isset( $current_uri ) && strpos( $current_uri, '/wp-json/' ) !== false ) { return false; } foreach ( self::WOOCOMMERCE_PAGES as $page ) { if ( strpos( $current_uri, $page ) !== false ) { return true; } } return false; } public function is_content_generation_survey_enabled(): bool { if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) { return false; } $not_submitted = ! get_transient( self::SUBMITTED_SURVEY_TRANSIENT ); $not_completed = ! $this->settings->get_setting( 'feedback_survey_completed' ); $content_published = $this->settings->get_setting( 'content_published' ); $is_client_eligible = $this->is_client_eligible(); $is_hostinger_page = $this->helper->is_hostinger_admin_page(); if ( ! $is_hostinger_page || empty( $this->get_survey_questions() ) ) { return false; } return $not_submitted && $not_completed && $content_published && $is_client_eligible; } public function is_woocommerce_survey_enabled(): bool { if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) { return false; } $not_submitted = ! get_transient( self::SUBMITTED_SURVEY_TRANSIENT ); $not_completed = ! $this->settings->get_setting( 'woocommerce_survey_completed' ); $is_woocommerce_page = $this->is_woocommerce_admin_page(); $default_woocommerce_completed = $this->helper->default_woocommerce_survey_completed(); $oldest_product_date = $this->get_oldest_product_date(); $seven_days_ago = strtotime( '-7 days' ); $is_client_eligible = $this->is_client_eligible(); if ( empty( $this->get_survey_questions() ) || $oldest_product_date < $seven_days_ago ) { return false; } return $not_submitted && $not_completed && $is_woocommerce_page && $default_woocommerce_completed && $is_client_eligible; } public function is_ai_onboarding_survey_enabled(): bool { if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) { return false; } $first_login_at = strtotime( get_option( 'hostinger_first_login_at', time() ) ); $not_submitted = ! get_transient( self::SUBMITTED_SURVEY_TRANSIENT ); $not_completed = ! $this->settings->get_setting( 'ai_onboarding_survey_completed' ); $is_client_eligible = $this->is_client_eligible(); $is_ai_onboarding_passed = $this->settings->get_setting( 'ai_onboarding' ); $is_hostinger_admin_page = $this->helper->is_hostinger_admin_page(); if ( ! $is_ai_onboarding_passed || empty( $this->get_survey_questions() ) ) { return false; } if ( $first_login_at && ! $this->is_time_elapsed( $first_login_at, self::TIME_15_MINUTES ) ) { return false; } return $not_submitted && $not_completed && $is_client_eligible && $is_hostinger_admin_page; } public function is_affiliate_survey_enabled(): bool { if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) { return false; } $not_submitted = ! get_transient( self::SUBMITTED_SURVEY_TRANSIENT ); $not_completed = ! $this->settings->get_setting( 'affiliate_survey_completed' ); $affiliate_links_exsists = $this->settings->get_setting( 'affiliate_links_created' ); $is_hostinger_admin_page = $this->helper->is_hostinger_admin_page(); $is_client_eligible = $this->is_client_eligible(); if ( ! $is_hostinger_admin_page || empty( $this->get_survey_questions() ) ) { return false; } return $not_submitted && $not_completed && $affiliate_links_exsists && $is_client_eligible; } public function is_prebuild_website_survey_enabled(): bool { if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) { return false; } $not_submitted = ! get_transient( self::SUBMITTED_SURVEY_TRANSIENT ); $not_completed = ! $this->settings->get_setting( 'prebuild_website_survey_completed' ); $is_hostinger_admin_page = $this->helper->is_hostinger_admin_page(); $is_client_eligible = $this->is_client_eligible(); $astra_templates_active = $this->helper->is_plugin_active( 'astra-sites' ); if ( ! $is_hostinger_admin_page || empty( $this->get_survey_questions() ) ) { return false; } return $not_submitted && $not_completed && $is_client_eligible && $astra_templates_active; } public function is_client_eligible(): bool { $transient_request_key = self::IS_CLIENT_ELIGIBLE_TRANSIENT_REQUEST; $transient_response_key = self::IS_CLIENT_ELIGIBLE_TRANSIENT_RESPONSE; $cached_eligibility = get_transient( $transient_response_key ); // Check if a request is already in progress if ( get_transient( 'hts_eligible_request' ) ) { return false; } // Check if transient response exists if ( $cached_eligibility && ( $cached_eligibility === 'eligible' || $cached_eligibility === 'not_eligible' ) ) { return $cached_eligibility === 'eligible'; } // Attempt to set transient request if ( ! $this->helper->check_transient_eligibility( $transient_request_key, self::CACHE_THREE_HOURS ) ) { return false; } try { // Set transient flag to indicate request in progress set_transient( 'hts_eligible_request', 'in_progress', 60 ); $is_eligible = $this->surveys_rest->is_client_eligible(); // Clear the request transient flag delete_transient( 'hts_eligible_request' ); if ( has_action( 'litespeed_purge_all' ) ) { do_action( 'litespeed_purge_all' ); } if ( $is_eligible ) { set_transient( $transient_response_key, 'eligible', self::CACHE_THREE_HOURS ); return $is_eligible; } set_transient( $transient_response_key, 'not_eligible', self::CACHE_THREE_HOURS ); return false; } catch ( Exception $exception ) { $this->helper->error_log( 'Error checking eligibility: ' . $exception->getMessage() ); return false; } } public function get_survey_questions(): array { $transient_request_key = self::SURVEY_QUESTIONS_TRANSIENT_REQUEST; $transient_response_key = self::SURVEY_QUESTIONS_TRANSIENT_RESPONSE; // Check if a request is already in progress if ( get_transient( 'hts_questions_request' ) ) { return array(); } // Check if transient response exists $cached_questions = get_transient( $transient_response_key ); if ( false !== $cached_questions ) { return $cached_questions; } // Attempt to set transient request if ( ! $this->helper->check_transient_eligibility( $transient_request_key, self::CACHE_THREE_HOURS ) ) { return array(); } try { // Set transient flag to indicate request in progress set_transient( 'hts_questions_request', 'in_progress', 60 ); $survey_questions = $this->surveys_rest->get_survey_questions(); // Clear the request transient flag delete_transient( 'hts_questions_request' ); set_transient( $transient_response_key, $survey_questions, self::CACHE_THREE_HOURS ); return $survey_questions; } catch ( Exception $exception ) { $this->helper->error_log( 'Error getting survey questions: ' . $exception->getMessage() ); return array(); } } public function submit_survey_answers( array $answers, string $survey_type ): void { $required_survey_items = $this->get_required_survey_items( $survey_type ); $data = array( 'identifier' => self::CLIENT_SURVEY_IDENTIFIER, 'answers' => $required_survey_items, ); $answers = $this->add_user_answers( $data, $answers ); $success = $this->surveys_rest->submit_survey_data( $answers, $survey_type ); set_transient( self::SUBMITTED_SURVEY_TRANSIENT, true, self::TIME_24_HOURS ); if ( $success ) { delete_transient( self::IS_CLIENT_ELIGIBLE_TRANSIENT_RESPONSE ); switch ( $survey_type ) { case 'woo_survey': $setting_key = 'woocommerce_survey_completed'; break; case 'ai_onboarding_survey': $setting_key = 'ai_onboarding_survey_completed'; break; case 'prebuild_website_survey': $setting_key = 'prebuild_website_survey_completed'; break; case 'affiliate_plugin_survey': $setting_key = 'affiliate_survey_completed'; break; default: $setting_key = 'feedback_survey_completed'; } $this->settings->update_setting( $setting_key, true ); wp_send_json( __( 'Survey completed', 'hostinger' ) ); if ( has_action( 'litespeed_purge_all' ) ) { do_action( 'litespeed_purge_all' ); } } } public function get_wp_survey_questions(): array { $all_questions = $this->get_survey_questions(); $question_slugs = array( 'score', 'comment' ); return $this->filter_questions_by_slug( $all_questions, $question_slugs ); } public function get_specified_survey_questions( array $survey_questions, string $survey_type = 'ai_survey' ): array { $specified_survey_questions = array( 'pages' => array(), 'showQuestionNumbers' => 'off', 'showTOC' => false, 'pageNextText' => __( 'Next', 'hostinger' ), 'pagePrevText' => __( 'Previous', 'hostinger' ), 'completeText' => __( 'Submit', 'hostinger' ), 'completedHtml' => __( 'Thank you for completing the survey !', 'hostinger' ), 'requiredText' => '*', ); foreach ( $survey_questions as $question ) { $title = ''; switch ( $survey_type ) { case 'woo_survey': $title = $this->survey_questions->map_survey_questions( $question['slug'] )['woo_question']; break; case 'prebuild_website_survey': $title = $this->survey_questions->map_survey_questions( $question['slug'] )['prebuild_website_question']; break; case 'ai_onboarding_survey': $title = $this->survey_questions->map_survey_questions( $question['slug'] )['ai_question']; break; case 'affiliate_plugin_survey': $title = $this->survey_questions->map_survey_questions( $question['slug'] )['affiliate_question']; break; default: $title = $this->survey_questions->map_survey_questions( $question['slug'] )['question']; break; } $element = array( 'type' => $this->survey_questions->map_survey_questions( $question['slug'] )['type'], 'name' => $question['slug'], 'title' => $title, 'requiredErrorText' => __( 'Response required.', 'hostinger' ), ); if ( $question['slug'] == 'comment' ) { $element['maxLength'] = 250; } if ( isset( $question['rules'] ) ) { $between_rule = $this->get_between_rule_values( $question['rules'] ); if ( $between_rule ) { $element['rateMin'] = $between_rule[0]; $element['rateMax'] = $between_rule[1]; $element['minRateDescription'] = __( 'Poor', 'hostinger' ); $element['maxRateDescription'] = __( 'Excellent', 'hostinger' ); } if ( $this->is_survey_question_required( $question ) ) { $element['isRequired'] = true; } } $question_data = array( 'name' => $question['slug'], 'elements' => array( $element ), ); $specified_survey_questions['pages'][] = $question_data; } return $specified_survey_questions; } public function get_between_rule_values( array $rules ): array { foreach ( $rules as $rule ) { $position = strpos( $rule, 'between:' ); if ( $position !== false ) { $between_values = $this->extract_between_values( $rule, $position ); if ( count( $between_values ) === 2 ) { return $between_values; } } } return array(); } public function generate_survey_html( string $survey_class ): string { ob_start(); ?>