联系表 7 允许在 24 小时后重复

Contact Form 7 allow duplicate after 24 hours

我正在使用 Contact Form 7 来捕获我们网站上的潜在客户。 我还使用 CFDB 插件添加验证规则,以防止网站上的所有重复使用。

function is_already_submitted($formName, $fieldName, $fieldValue) {
    require_once(ABSPATH . 'wp-content/plugins/contact-form-7-to-database-extension/CFDBFormIterator.php');
    $exp = new CFDBFormIterator();
    $atts = array();
    $atts['show'] = $fieldName;
    $atts['filter'] = "$fieldName=$fieldValue";
    $atts['unbuffered'] = 'true';
    $exp->export($formName, $atts);
    $found = false;
    while ($row = $exp->nextRow()) {
        $found = true;
    }
    return $found;
}
function my_validate_email($result, $tag) {
    $formName = 'email_form'; // Change to name of the form containing this field
    $fieldName = 'email_123'; // Change to your form's unique field name
    $errorMessage = 'Email has already been submitted'; // Change to your error message
    $name = $tag['name'];
    if ($name == $fieldName) {
        if (is_already_submitted($formName, $fieldName, $_POST[$name])) {
            $result->invalidate($tag, $errorMessage);
        }
    }
    return $result;
}

如果用户在 24 小时后重试,我们现在需要允许重复输入。

我们的提议是 运行 一个 cron 作业来标记超过 24 小时的条目,然后允许用户继续。我们包含了一个新的 table 列 (allow_duplicate) 来标记该条目。

任何有关如何构建 functions.php 验证的建议都将不胜感激。

所以我个人...我会使用 Transient API。使用 Transients,您可以将到期时间设置为 24 小时,并且不再需要 cron 任务。首先...使用挂钩 wpcf7_before_send_mail 设置瞬态,然后使用验证过滤器检查瞬态是否存在。瞬态将在初次提交后 24 小时过期。

在函数中设置您的联系表单 ID dd_handle_form_submission

// capture data after contact form submit
add_action( 'wpcf7_before_send_mail', 'dd_handle_form_submission' ); 
function dd_handle_form_submission( $contact_form ) {
    // Check Form ID
    if( 2744 !== $contact_form->id() ) return; // Use your form ID here
    $submission = WPCF7_Submission::get_instance();
    if ( $submission ) {
        $posted_data = $submission->get_posted_data();
        // Set the Transient
        $transient_name = 'cf7_has_submitted_' . $posted_data['email_123'];
        set_transient($transient_name, true, 1 * DAY_IN_SECONDS);
    }
    return;
}

// Custom Validation Filter
add_filter( 'wpcf7_validate_email*', 'my_validate_email', 20, 2);
function my_validate_email($result, $tag) {
    $fieldName = 'email_123'; // Change to your form's unique field name
    $errorMessage = 'Email has already been submitted'; // Change to your error message
    $name = $tag['name'];
    if ($name == $fieldName) {
        // Create the Transient Name to lookup with the email field
        $transient = 'cf7_has_submitted_' . $_POST[$name];
        // Lookup transient
        if (false !== get_transient($transient)) $result->invalidate($tag, $errorMessage);
    }
    return $result;
}