api调用后Wordpress使CF7无效

Cra*_*eld 6 php forms wordpress invalidation contact-form-7

这是我的问题,我已经安装了Wordpress的联系表格7,并且在wpcf7_before_send_mail期间我对API进行了调用,如果API返回错误,则需要使表格无效,然后我需要使请求无效并返回从传回的错误API调用。

我在API失败时将标志设置为false,并且还存储了错误消息,但是尽管我诱发了失败,但我的表单仍在成功处理。

add_action("wpcf7_before_send_mail", "wpcf7_send_contact_builder");
function wpcf7_send_contact_builder($form) {
    $submission = WPCF7_Submission::get_instance();
    $wpcf7_data = $submission->get_posted_data();
    ... api call and set $success to true if ok and false if not ...
    if (!$success) {
        $form->status = 'validation_failed (statuscode:' . $xml->status->statuscode[0] . ').';
        $form->valid = false;
        $form->response = $xml->status->statusdesc[0];
        return $forml
    }
}
Run Code Online (Sandbox Code Playgroud)

我也尝试过使用:

$form->invalidate('validation_failed (statuscode:' . $xml->status->statuscode[0] . ').', $xml->status->statusdesc[0]);
Run Code Online (Sandbox Code Playgroud)

但是无论哪种方式,我都无法阻止发送成功的电子邮件,并且表单验证成功。调试证明if语句中的!success成功,并且包含的​​代码已添加到变量中。我也尝试过,好像表单是一个数组($ form ['valid'] = false),但这也不起作用,表单提交成功。关于我在这里缺少什么的任何想法?我已经省略了API调用本身的代码以及确定正确的表单ID的代码,两者都可以正常工作,仅解析了我要使用的表单,并且API调用返回了预期的数据。

Rez*_*mun 6

我需要同样的。在浏览了 CF7 插件文件后,我找到了以下解决方案:

//To make it working, we must need at least CF7-v5.0;
add_action( 'wpcf7_before_send_mail', 'cf7_validate_api', 15, 3 );

function cf7_validate_api($cf7, &$abort, $submission){

    if ( $cf7->id() !== 789 ) //CF7 post-id from admin settings;
        return;

    $errMsg = '';

    //$submission = WPCF7_Submission::get_instance();
    $postedData = $submission->get_posted_data();
    //$postedData['more-data'] = 'something';
    unset($postedData['not-sending-data']);

    //-----API posting------
    $url = "http://my-web.com/wp-admin/admin-ajax.php?action=get-something";
    $username = 'apiUserName';
    $password = 'apiUserPass';

    $args = [
        'headers' => [
            'Authorization' => "Basic ".base64_encode( $username . ':' . $password ),
            'Accept' => 'application/json; charset=utf-8', // The API returns JSON
            //'Content-Type' => 'application/json; charset=utf-8'
        ],
        'body' => $postedData
    ];
    $response = wp_remote_post( $url, $args );
    //------------------

    if( is_wp_error( $response ) ){
        $error_message = $response->get_error_message();
        $errMsg = "Something went wrong:\n{$error_message}";

    } else {
        $response_body = wp_remote_retrieve_body( $response );
        $data = json_decode( $response_body );

        if( empty($data) || $data->status==0 ){ //API validation error!
            $errMsg = $data->msg->title."\n".$data->msg->description;
        }
    }

    if( $errMsg ){ //do not send mail;
        //$cf7->skip_mail = true; //for older versions!
        $abort = true; //==> Here, it is with 'called by reference' since CF7-v5.0 :)
        $submission->set_status( 'validation_failed' );
        //$submission->set_response( $cf7->message( 'validation_error' ) ); //msg from admin settings;
        $submission->set_response( $cf7->filter_message($errMsg) ); //custom msg;
    }
}
Run Code Online (Sandbox Code Playgroud)

希望它会帮助某人。快乐编码:)