/* 
 Theme Name:   divichild
 Theme URI:    
 Description:  divichild
 Author:       rbkadam
 Author URI:   https://rbk.markets
 Template:     Divi
 Version:      1.0
 License:      GNU General Public License v2 or later
 License URI:  http://www.gnu.org/licenses/gpl-2.0.html

 /* == Add your own styles below this line ==
--------------------------------------------*/
add_filter('fluentform/validate_input_item_input_email', function ($errorMessage, $field, $formData, $form) {
    // 1. Change '12' to match your specific Fluent Form ID
    if ($form->id != 12) {
        return $errorMessage;
    }

    // Extract the email address from the form submission
    $fieldName = $field['name'];
    $email = isset($formData[$fieldName]) ? trim($formData[$fieldName]) : '';

    if (empty($email)) {
        return $errorMessage;
    }

    // 2. Replace with your actual EmailListVerify API Key
    $apiKey = '5y6Za2Um2GRTCXV11zw899hpMlWZH4sy'; 
    $apiUrl = 'https://emaillistverify.com' . urlencode($apiKey) . '&email=' . urlencode($email);

    // Make a secure, non-blocking remote request to the API
    $response = wp_remote_get($apiUrl, array('timeout' => 3));

    if (is_wp_error($response)) {
        return $errorMessage; // Safety fallback: Fail open if API is down
    }

    $result = trim(wp_remote_retrieve_body($response));

    // List of statuses that mean the email is fake or unsafe
    $invalidStatuses = array('fail', 'disposable', 'spam_traps', 'bad_syntax');

    if (in_array($result, $invalidStatuses)) {
        return __('Please enter a valid, active email address.', 'fluentform');
    }

    return $errorMessage;
}, 10, 4);

