<?php
if ( ! defined( 'ABSPATH' ) ) {
	die( 'You are not allowed to call this page directly.' );
}

class FormShieldForms {

	/**
	 * If auto-protection is on, get and protect all forms with HP
	 *
	 * @return Void
	 */
	public static function init() {

	}

	/**
	 * Save configuration to enable/disable forms to be protected with HP
	 *
	 * @param String $form_id - Form ID to enable/disable
	 * @param Integer $enabled - Boolean Int (1 === enabled)
	 * @param Boolean $unattended - If unattended, don't output HTML on enabling forms
	 *
	 * @return Void
	 */
	public static function enable_forms( $form_id, $enabled, $unattended = false ) {
		$options = FormShield::get_options();
		
				// Handle unprotect request or allow protection of unlimited forms
		if ( ! $enabled ) {
			// Disable form protection (don't remove it completely)
			if ( isset( $options['formshield_forms'][ $form_id ] ) ) {
				$options['formshield_forms'][ $form_id ]['form_enabled'] = 0;
				$alert_type = 'success';
			} else {
				$alert_type = 'error';
			}
		} else {
			// Add or enable form protection
			if ( ! isset( $options['formshield_forms'][ $form_id ] ) ) {
				// New form - try to determine type from detected forms
				$form_type = 'Unknown';
				$form_name = 'Form #' . $form_id;
				
				// Check if this form exists in detected_forms
				if (isset($options['detected_forms']) && is_array($options['detected_forms'])) {
					foreach ($options['detected_forms'] as $detected_form) {
						if ($detected_form['id'] === $form_id) {
							$form_type = $detected_form['type'];
							$form_name = $detected_form['name'];
							break;
						}
					}
				}
				
				// If still unknown, try to determine from form ID
				if ($form_type === 'Unknown') {
					if (strpos($form_id, 'cf7-') === 0) {
						$form_type = 'скенер';
					} elseif (strpos($form_id, 'ninja-') === 0) {
						$form_type = 'ninja_forms';
					} elseif (strpos($form_id, 'wpforms-') === 0) {
						$form_type = 'wpforms';
					} elseif (strpos($form_id, 'gravity_') === 0) {
						$form_type = 'gravityforms';
					} elseif (strpos($form_id, 'divi_') === 0) {
						$form_type = 'divi';
					} elseif (strpos($form_id, 'comments_') === 0) {
						$form_type = 'comments';
					}
				}
				
				$options['formshield_forms'][ $form_id ] = array(
					'form_enabled' => 1,
					'form_type'    => $form_type,
					'form_name'    => $form_name,
					'form_date'    => current_time( 'mysql' )
				);
			} else {
				// Existing form - enable it
				$options['formshield_forms'][ $form_id ]['form_enabled'] = 1;
			}
			$alert_type = 'success';
		}
		
		// Update options
		FormShield::update_options($options);
		
		// Show admin notice
		$current_action = $enabled ? 'enable' : 'disable';
		FormShieldUtils::show_wp_admin_notice( $current_action . 'd form #' . $form_id, 'notice-' . $alert_type . ' is-dismissible' );
	}

	/**
	 * Get a count of all protected forms
	 *
	 * @return Integer
	 */
	public static function get_protected_forms_ct() {
		$options = form_shield()->get_options();
		$protected_forms_ct = 0;
		
		if ( isset( $options['formshield_forms'] ) && is_array( $options['formshield_forms'] ) ) {
			foreach ( $options['formshield_forms'] as $formshield_form ) {
				if ( isset( $formshield_form['form_enabled'] ) && $formshield_form['form_enabled'] ) {
					// Increment the count for protected forms
					$protected_forms_ct++;
				}
			}
			// Store the count
			$options['protected_forms_ct'] = $protected_forms_ct;
			form_shield()->update_options( $options );
		}

		return $protected_forms_ct;
	}

	/**
	 * Get form submissions count
	 *
	 * @param String $form_id - form id
	 * @param Integer $human_ct - Number of valid form entries
	 * @param Boolean $no_ct_tracked - When a form builder doesn't track submissions
	 *
	 * @return String (HTML)
	 */
	public static function get_form_submissions_ct( $form_id, $human_ct, $no_ct_tracked = false ) {
		$options = FormShield::get_options();
		if ( is_array( $options ) && array_key_exists( 'formshield_forms', $options ) ) {
			$recorded_human_ct   = $no_ct_tracked ? 'N/A' : $human_ct;
			$recorded_suspicious = 0;
			if ( isset( $options['formshield_forms'][ $form_id ]['suspicious_ct'] ) ) {
				$recorded_suspicious = ( null == $options['formshield_forms'][ $form_id ]['suspicious_ct'] ) ? 0 : $options['formshield_forms'][ $form_id ]['suspicious_ct'];
			}
			$recorded_total = $no_ct_tracked ? 'N/A' : ( $human_ct + $recorded_suspicious );

			return '<div class="label-row text-center row"><div class="col-lg-4"><span class="label label-success"><span class="fa fa-check"></span> <span>Human</span> <span class="badge">' . $recorded_human_ct . '</span></span></div><div class="col-lg-4"><span class="label label-danger"><span class="fa fa-exclamation-circle"></span> <span>Suspicious</span> <span class="badge">' . $recorded_suspicious . '</span></span></div><div class="col-lg-4"><span class="label label-default"><span class="fa fa-balance-scale"></span> <span>Total</span> <span class="badge">' . $recorded_total . '</span></span></div></div>';
		}
	}

	/**
	 * Get latest form activity
	 *
	 * @param String $form_id - form id
	 * @param String $last_entry - Date/Time string of the last human/valid form entry
	 *
	 * @return String
	 */
	public static function get_latest_form_activity( $form_id, $last_entry ) {
		$options = FormShield::get_options();
		if ( is_array( $options ) && array_key_exists( 'formshield_forms', $options ) ) {
			$last_suspicious_activity = isset( $options['formshield_forms'][ $form_id ]['last_suspicious_activity'] ) ? $options['formshield_forms'][ $form_id ]['last_suspicious_activity'] : null;
			if ( null != $last_suspicious_activity ) {
				if ( $last_suspicious_activity > $last_entry ) {
					// If suspicious activity is the latest
					return FormShieldUtils::time_ago( $last_suspicious_activity );
				} else {
					// Otherwise show the latest human activity
					return FormShieldUtils::time_ago( $last_entry );
				}
			} else {
				// Otherwise show the latest human activity if there is any
				return ( null != $last_entry ) ? FormShieldUtils::time_ago( $last_entry ) : 'never';
			}
		}
	}

	/**
	 * Build the table data for the forms.
	 *
	 * @return array
	 */
	public static function forms_list() {

		$forms_list = apply_filters( 'formshield_forms_list', array() );
		
		$options = FormShield::get_options();

		// Get a count of all initially protected forms
		if ( isset( $options['formshield_forms'] ) && ! array_key_exists( 'protected_forms_ct', $options ) ) {
			self::get_protected_forms_ct();
		}

		return $forms_list;
	}

	/**
	 * Check if a plugin is installed and active
	 *
	 * @param string $plugin_type
	 * @return bool
	 */
	private static function is_plugin_installed($plugin_type) {
		switch ($plugin_type) {
			case 'ninja_forms':
			case 'ninja-forms':
				return is_plugin_active('ninja-forms/ninja-forms.php');
			case 'скенер':
			case 'contact form 7':
			case 'contact-form-7':
			case 'wpcf7':
				return is_plugin_active('contact-form-7/wp-contact-form-7.php');
			case 'wpforms':
				return is_plugin_active('wpforms-lite/wpforms.php') || is_plugin_active('wpforms/wpforms.php');
			case 'gravityforms':
			case 'gravity-forms':
				return is_plugin_active('gravityforms/gravityforms.php');
			case 'comments':
			case 'wordpress-comments':
				return true; // WordPress comments are always available
			case 'divi':
			case 'divi-contact-form':
				return true; // Divi forms are part of Divi theme
			default:
				return true; // Default to true for unknown types
		}
	}

	/**
	 * Get all forms for the dashboard
	 *
	 * @return array
	 */
	public static function get_forms() {
		$forms = array();
		$options = FormShield::get_options();
		
		// Секция 1: Скенер (винаги включен)
		$scanner_id = 'frontend_detector';
		$scanner_blocked_attempts = 0;
		if (isset($options['formshield_forms'][$scanner_id]['blocked_attempts'])) {
			$scanner_blocked_attempts = intval($options['formshield_forms'][$scanner_id]['blocked_attempts']);
		}
		
		$forms['scanner'] = array(
			'id' => $scanner_id,
			'name' => __('Скенер на сайта', 'formshield'),
			'plugin' => 'FormShield Detector',
			'type' => 'скенер',
			'enabled' => 1, // Винаги включен
			'blocked_attempts' => $scanner_blocked_attempts,
			'page_id' => 0,
			'location' => 'FormShield Detection System',
			'is_scanner' => true // Маркер че това е скенер
		);
		
		// Секция 2: Коментари (винаги включени)
		$forms['comments'] = array();
		
		// Секция 3: Плъгини и форми (могат да се включват/изключват)
		$forms['plugins'] = array();
		
		// Прилагаме филтъра за интеграции (включително WordPress коментари)
		$integration_forms = apply_filters('formshield_forms_list', array());
		
		// Организираме формите по секции
		foreach ($integration_forms as $integration_form) {
			$form_type = $integration_form['type'];
			$form_id = isset($integration_form['id']) ? $integration_form['id'] : '';
			
			// Извличаме реалните blocked_attempts от опциите
			if (!empty($form_id) && isset($options['formshield_forms'][$form_id]['blocked_attempts'])) {
				$integration_form['blocked_attempts'] = intval($options['formshield_forms'][$form_id]['blocked_attempts']);
			} else {
				$integration_form['blocked_attempts'] = 0;
			}
			
			// Определяме в коя секция да добавим формата
			if (in_array($form_type, ['comments', 'wordpress-comments'])) {
				// Коментари - винаги включени
				$integration_form['enabled'] = 1;
				$integration_form['is_comments'] = true;
				$forms['comments'][] = $integration_form;
			} else {
				// Плъгини и форми - могат да се включват/изключват
				$forms['plugins'][] = $integration_form;
			}
		}
		
		// Ако няма коментари, добавяме поне един примерен
		if (empty($forms['comments'])) {
			$comments_id = 'wordpress_comments';
			$comments_blocked_attempts = 0;
			if (isset($options['formshield_forms'][$comments_id]['blocked_attempts'])) {
				$comments_blocked_attempts = intval($options['formshield_forms'][$comments_id]['blocked_attempts']);
			}
			
			$forms['comments'][] = array(
				'id' => $comments_id,
				'name' => 'WordPress Comments',
				'plugin' => 'WordPress Core',
				'type' => 'comments',
				'enabled' => 1, // Винаги включени
				'blocked_attempts' => $comments_blocked_attempts,
				'page_id' => 0,
				'location' => 'All pages with comments enabled',
				'is_comments' => true
			);
		}
		
		return $forms;
	}
	
	    /**
     * Get the actual location (page title) for a form
     *
     * @param string $form_id
     * @param array $form_data
     * @return array Returns array with 'title' and 'page_id'
     */
    private static function get_form_location($form_id, $form_data) {
        $result = array(
            'title' => isset($form_data['form_name']) ? $form_data['form_name'] : 'Unknown Page',
            'page_id' => 0
        );
        
        // If we have a page_id, get the page title
        if (isset($form_data['page_id']) && !empty($form_data['page_id'])) {
            $page = get_post($form_data['page_id']);
            if ($page) {
                $result['title'] = $page->post_title;
                $result['page_id'] = $page->ID;
                return $result;
            }
        }
        
        // For Divi forms, extract page ID from form ID
        if (strpos($form_id, 'divi_') !== false) {
            $page_id = str_replace('divi_', '', $form_id);
            if (is_numeric($page_id)) {
                $page = get_post($page_id);
                if ($page) {
                    $result['title'] = $page->post_title;
                    $result['page_id'] = $page->ID;
                    return $result;
                }
            }
        }
        
        // For Contact Form 7, search for where it's used
        if (strpos($form_id, 'cf7-') !== false) {
            $form_id_number = str_replace('cf7-', '', $form_id);
            $page_info = self::find_contact_form_7_page($form_id_number);
            if ($page_info && isset($page_info['page_id'])) {
                $page = get_post($page_info['page_id']);
                if ($page) {
                    $result['title'] = $page->post_title;
                    $result['page_id'] = $page->ID;
                    return $result;
                }
            }
        }
        
        // For WPForms, search for where it's used
        if (strpos($form_id, 'wpforms_') !== false) {
            $form_id_number = str_replace('wpforms_', '', $form_id);
            $page_url = self::find_wpforms_page($form_id_number);
            if ($page_url) {
                $page_id = url_to_postid($page_url);
                if ($page_id) {
                    $page = get_post($page_id);
                    if ($page) {
                        $result['title'] = $page->post_title;
                        $result['page_id'] = $page_id;
                        return $result;
                    }
                }
            }
        }
        
        // For Gravity Forms, search for where it's used
        if (strpos($form_id, 'gravity_') !== false) {
            $form_id_number = str_replace('gravity_', '', $form_id);
            $page_url = self::find_gravity_forms_page($form_id_number);
            if ($page_url) {
                $page_id = url_to_postid($page_url);
                if ($page_id) {
                    $page = get_post($page_id);
                    if ($page) {
                        $result['title'] = $page->post_title;
                        $result['page_id'] = $page_id;
                        return $result;
                    }
                }
            }
        }
        
        // For Ninja Forms, search for where it's used
        if (strpos($form_id, 'ninja_') !== false) {
            $form_id_number = str_replace('ninja_', '', $form_id);
            $page_url = self::find_ninja_forms_page($form_id_number);
            if ($page_url) {
                $page_id = url_to_postid($page_url);
                if ($page_id) {
                    $page = get_post($page_id);
                    if ($page) {
                        $result['title'] = $page->post_title;
                        $result['page_id'] = $page_id;
                        return $result;
                    }
                }
            }
        }
        
        return $result;
    }
	
	    /**
     * Find where a Contact Form 7 form is used
     *
     * @param int $form_id
     * @return array|false Array with page_id and page_url if found, false otherwise
     */
    public static function find_contact_form_7_page($form_id) {
        // Search in all pages and posts for Contact Form 7 shortcode
        $args = array(
            'post_type' => array('page', 'post'),
            'post_status' => 'publish',
            'posts_per_page' => -1,
            's' => '[contact-form-7 id="' . $form_id . '"'
        );
        
        $query = new WP_Query($args);
        
        if ($query->have_posts()) {
            while ($query->have_posts()) {
                $query->the_post();
                $post = get_post();
                $content = $post->post_content;
                
                // Check for Contact Form 7 shortcode variations
                if (strpos($content, '[contact-form-7 id="' . $form_id . '"') !== false ||
                    strpos($content, '[contact-form-7 id="' . $form_id . '"]') !== false ||
                    strpos($content, 'contact-form-7 id="' . $form_id . '"') !== false ||
                    strpos($content, '[contact-form-7 id=' . $form_id . ']') !== false ||
                    strpos($content, 'contact-form-7 id=' . $form_id) !== false) {
                    
                    wp_reset_postdata();
                    return array(
                        'page_id' => $post->ID,
                        'page_url' => get_permalink($post->ID)
                    );
                }
            }
        }
        
        wp_reset_postdata();
        return false;
    }
	
	/**
	 * Find where a WPForms form is used
	 *
	 * @param int $form_id
	 * @return string|false
	 */
	private static function find_wpforms_page($form_id) {
		// Search in all pages and posts for WPForms shortcode
		$args = array(
			'post_type' => array('page', 'post'),
			'post_status' => 'publish',
			'posts_per_page' => -1
		);
		
		$posts = get_posts($args);
		
		foreach ($posts as $post) {
			$content = $post->post_content;
			
			// Check for WPForms shortcode variations
			if (strpos($content, '[wpforms id="' . $form_id . '"') !== false ||
				strpos($content, '[wpforms id="' . $form_id . '"]') !== false ||
				strpos($content, 'wpforms id="' . $form_id . '"') !== false ||
				strpos($content, '[wpforms id=' . $form_id . ']') !== false ||
				strpos($content, 'wpforms id=' . $form_id) !== false) {
				
				return get_permalink($post->ID);
			}
		}
		
		return false;
	}
	
	/**
	 * Find where a Gravity Forms form is used
	 *
	 * @param int $form_id
	 * @return string|false
	 */
	private static function find_gravity_forms_page($form_id) {
		// Search in all pages and posts for Gravity Forms shortcode
		$args = array(
			'post_type' => array('page', 'post'),
			'post_status' => 'publish',
			'posts_per_page' => -1
		);
		
		$posts = get_posts($args);
		
		foreach ($posts as $post) {
			$content = $post->post_content;
			
			// Check for Gravity Forms shortcode variations
			if (strpos($content, '[gravityform id="' . $form_id . '"') !== false ||
				strpos($content, '[gravityform id="' . $form_id . '"]') !== false ||
				strpos($content, 'gravityform id="' . $form_id . '"') !== false ||
				strpos($content, '[gravityform id=' . $form_id . ']') !== false ||
				strpos($content, 'gravityform id=' . $form_id) !== false) {
				
				return get_permalink($post->ID);
			}
		}
		
		return false;
	}
	
	/**
	 * Find where a Ninja Forms form is used
	 *
	 * @param int $form_id
	 * @return string|false
	 */
	private static function find_ninja_forms_page($form_id) {
		// Search in all pages and posts for Ninja Forms shortcode
		$args = array(
			'post_type' => array('page', 'post'),
			'post_status' => 'publish',
			'posts_per_page' => -1
		);
		
		$posts = get_posts($args);
		
		foreach ($posts as $post) {
			$content = $post->post_content;
			
			// Check for Ninja Forms shortcode variations
			if (strpos($content, '[ninja_form id="' . $form_id . '"') !== false ||
				strpos($content, '[ninja_form id="' . $form_id . '"]') !== false ||
				strpos($content, 'ninja_form id="' . $form_id . '"') !== false ||
				strpos($content, '[ninja_form id=' . $form_id . ']') !== false ||
				strpos($content, 'ninja_form id=' . $form_id) !== false) {
				
				return get_permalink($post->ID);
			}
		}
		
		return false;
	}

	/**
	 * Makes form validation fail if Human Presence is not 100% confident.
	 *
	 * @return Boolean
	 */
	public static function fail_form_validation() {
		$options = FormShield::get_options();
		// Fetch the HP check response.
		$response = form_shield()->check_session();
		// If response is not sure the user is human or the confidence is below the desired threshold, fail the detection check.
		$failed = false;
		$latest_submissions = !empty( $options['latest_submissions'] ) ? $options['latest_submissions'] : [];
		
		// Get minimum confidence threshold (default to 70 if not set)
		$min_confidence = isset($options['wp_hp_min_confidence']) ? intval($options['wp_hp_min_confidence']) : 70;
		
		if ( empty( $response ) || ! isset( $response['signal'] ) || ! isset( $response['confidence'] ) ) {
			$failed = true;
		} else {
			if ( 'HUMAN' !== $response['signal'] ) {
				$failed = true;
			}
			if ( intval($response['confidence']) < $min_confidence ) {
				$failed = true;
			}
		}
		if( ( $options['wp_hp_debug'] ) ){
			if( count($latest_submissions) >= FORMSHIELD_LOG_LIMIT ) {
				array_pop($latest_submissions);
			}
		// Sanitize request data before logging
		$sanitized_request = array();
		// phpcs:ignore WordPress.Security.NonceVerification.Missing -- This is logging function, not form processing
		foreach ($_POST as $key => $value) {
			if (in_array($key, ['_wpcf7', '_wpnonce', 'form_id'])) {
				$sanitized_request[$key] = sanitize_text_field($value);
			} else {
				$sanitized_request[$key] = '***REDACTED***';
			}
		}
			
			array_unshift($latest_submissions, array(
				'request'  => $sanitized_request,
				'response' => $response,
				'session_id' => formshield_get_cookie_string('ellipsis_sessionid'),
				'failed_validation' => $failed ? 'Yes' : 'No',
			));
			$options['latest_submissions'] = $latest_submissions;
			form_shield()->update_options( $options );
		}
		return $failed;
	}

	/**
	 * Record suspicious form activity
	 *
	 * @param String $form_id - form id
	 *
	 * @return Void
	 */
	public static function handle_suspicious_form_activity( $form_id ) {
		$options = FormShield::get_options();
		if ( is_array( $options ) && array_key_exists( 'formshield_forms', $options ) ) {
			// Increment suspicious activity
			$options['formshield_forms'][ $form_id ]['suspicious_ct'] = ( null == $options['formshield_forms'][ $form_id ]['suspicious_ct'] ) ? 1 : ++ $options['formshield_forms'][ $form_id ]['suspicious_ct'];
			// Save the timestamp
			$options['formshield_forms'][ $form_id ]['last_suspicious_activity'] = gmdate( 'Y-m-d H:i:s' );
			
			// Record attack statistics for Analytics Overview
			self::record_attack_statistics($form_id, 'other');
			
			form_shield()->update_options( $options );
		}
	}

	/**
	 * Record attack statistics for Analytics Overview
	 *
	 * @param String $form_id - Form ID
	 * @param String $attack_type - Type of attack (spam, bot, injection, other)
	 *
	 * @return Void
	 */
	public static function record_attack_statistics( $form_id, $attack_type = 'other' ) {
		$options = FormShield::get_options();
		
		// Initialize attack statistics if they don't exist
		if ( !isset( $options['blocked_attempts'] ) ) {
			$options['blocked_attempts'] = 0;
		}
		if ( !isset( $options['attack_types'] ) ) {
			$options['attack_types'] = array(
				'spam' => 0,
				'bot' => 0,
				'injection' => 0,
				'other' => 0
			);
		}
		if ( !isset( $options['last_attack'] ) ) {
			$options['last_attack'] = '';
		}
		
		// Increment total blocked attempts
		$options['blocked_attempts']++;
		
		// Increment specific attack type
		if ( isset( $options['attack_types'][ $attack_type ] ) ) {
			$options['attack_types'][ $attack_type ]++;
		} else {
			$options['attack_types']['other']++;
		}
		
		// Update last attack timestamp
		$options['last_attack'] = current_time( 'mysql' );
		
		// Update form-specific blocked attempts
		if ( isset( $options['formshield_forms'][ $form_id ] ) ) {
			if ( !isset( $options['formshield_forms'][ $form_id ]['blocked_attempts'] ) ) {
				$options['formshield_forms'][ $form_id ]['blocked_attempts'] = 0;
			}
			$options['formshield_forms'][ $form_id ]['blocked_attempts']++;
		}
		
		// Save updated options
		form_shield()->update_options( $options );
		
	}

	/**
	 * Record spam attack
	 *
	 * @param String $form_id - Form ID
	 * @param String $email - Email address (optional)
	 * @param String $content - Content sample (optional)
	 *
	 * @return Void
	 */
	public static function record_spam_attack( $form_id, $email = '', $content = '' ) {
		self::record_attack_statistics( $form_id, 'spam' );
		
	}

	/**
	 * Record bot attack
	 *
	 * @param String $form_id - Form ID
	 * @param String $user_agent - User agent string (optional)
	 * @param String $ip - IP address (optional)
	 *
	 * @return Void
	 */
	public static function record_bot_attack( $form_id, $user_agent = '', $ip = '' ) {
		self::record_attack_statistics( $form_id, 'bot' );
		
	}

	/**
	 * Record injection attack
	 *
	 * @param String $form_id - Form ID
	 * @param String $payload - Malicious payload (optional)
	 * @param String $field - Field name (optional)
	 *
	 * @return Void
	 */
	public static function record_injection_attack( $form_id, $payload = '', $field = '' ) {
		self::record_attack_statistics( $form_id, 'injection' );
		
	}

	/**
	 * Get uptime percentage based on last attack
	 *
	 * @return Integer - Uptime percentage (0-100)
	 */
	public static function get_uptime_percentage() {
		$options = FormShield::get_options();
		$last_attack = isset( $options['last_attack'] ) ? $options['last_attack'] : '';
		
		if ( empty( $last_attack ) ) {
			return 100; // No attacks recorded, assume 100% uptime
		}
		
		$last_attack_time = strtotime( $last_attack );
		$current_time = current_time( 'timestamp' );
		$time_since_attack = $current_time - $last_attack_time;
		
		// If no attack in last 24 hours, uptime is 100%
		if ( $time_since_attack > DAY_IN_SECONDS ) {
			return 100;
		}
		
		// Calculate uptime based on time since last attack
		// More recent attacks = lower uptime
		$uptime = max( 85, 100 - ( $time_since_attack / DAY_IN_SECONDS ) * 15 );
		
		return round( $uptime );
	}

	/**
	 * Returns the failed form validation error message.
	 *
	 * @return String
	 */
	public static function fail_form_error_message() {
		return apply_filters( 'formshield_fail_validation_message', 'Sorry, we could not process your submission at this time. Please try again later.' );
	}

	/**
	 * Show admin notice for forms in protected forms table
	 *
	 * @param String $form_id - Form ID to enable/disable
	 * @param Integer $enabled - Boolean Int (1 === enabled)
	 * @param String $alert_type - success/warning class for enable/disable form notices
	 *
	 * @return Void
	 */
	public static function show_protected_forms_admin_notice( $form_id, $enabled, $alert_type ) {
		$options                     = FormShield::get_options();
		$current_action              = $enabled ? 'enable' : 'disable';
		$hp_comm_forms_limit_reached = isset( $options['protected_forms_ct'] ) ? $options['protected_forms_ct'] > 0 : false;
		if ( ( isset( $options['wp_hp_premium_license'] ) && 1 == $options['wp_hp_premium_license'] ) || ! $enabled || ( $enabled && ! $hp_comm_forms_limit_reached ) ) {
			// Handle unprotect request or premium license request to protect more than 1 form
			FormShieldUtils::show_wp_admin_notice( $current_action . 'd form #' . $form_id, 'notice-' . $alert_type . ' is-dismissible' );
		} else {
			// Handle community plan forms limit reached
			FormShieldUtils::show_wp_admin_notice( 'Community version limit reached. Protecting more than one form requires a premium license.', 'notice-error is-dismissible' );
		}
	}

	/**
	 * AJAX handler for recording suspicious activity from Divi forms
	 */
	public static function record_suspicious_activity_ajax() {
		// Verify nonce
		formshield_verify_nonce_or_bail('formshield_form_enabled_change');

		$form_id = formshield_get_post_string('form_id');
		
		if ( $form_id ) {
			self::handle_suspicious_form_activity( $form_id );
			wp_send_json_success( 'Activity recorded' );
		} else {
			wp_send_json_error( 'Invalid form ID' );
		}
	}

}
