api调用后,Wordpress无效cf7 [英] Wordpress invalidate cf7 after api call

查看:374
本文介绍了api调用后,Wordpress无效cf7的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的问题,我已经安装了wordpress的联系表单7,在wpcf7_before_send_mail期间,我调用了一个API,如果API返回错误,我需要使表单无效,然后我需要使请求无效并返回错误从API调用传回。



我在API失败时将标志设置为false,并且错误消息也被存储,但是尽管失败,我的表单仍然成功诱导。

  add_action(wpcf7_before_send_mail,wpcf7_send_contact_builder); 
函数wpcf7_send_contact_builder($ form){
$ submission = WPCF7_Submission :: get_instance();
$ wpcf7_data = $ submission-> get_posted_data();
... api调用并将$ success设置为true,如果不是,则返回false
if(!$ success){
$ form-> status ='validation_failed(statuscode :'。$ xml-> status-> statuscode [0]。')。';
$ form-> valid = false;
$ form-> response = $ xml-> status-> statusdesc [0];
return $ forml
}
}

我也是尝试使用:

  $ form-> invalidate('validation_failed(statuscode:'。$ xml-> status-> statuscode [0]。')。',$ xml-> status-> statusdesc [0]); 

但无论如何我无法阻止发送成功电子邮件并且表单被验证为成功。调试证明if语句中的!成功正在工作,并且包含的​​代码被添加到变量中。我也尝试好像表单是一个数组($ form ['valid'] = false),但是这也不起作用,表单提交成功。任何想法,我在这里失踪?我省略了API调用本身的代码,并确定了正确的表单ID,这两个工作都正常,只有我解析后的表单被解析,并且API调用正在返回预期的数据。

解决方案

我需要相同的功能。通过CF7插件文件后,我发现了以下解决方案:

  //为了使它工作,我们至少需要CF7-V5.0; 
add_action('wpcf7_before_send_mail','cf7_validate_api',15,3);

函数cf7_validate_api($ cf7,& $ abort,$ submission){

if($ cf7-> id()!== 789)// CF7 post -id从管理员设置;
return;

$ errMsg ='';

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

// ----- API发帖------
$ url =http://my-web.com/wp-admin/admin-ajax.php ?行动=获得出头;
$ username ='apiUserName';
$ password ='apiUserPass';

$ args = [
'headers'=> [
'授权'=> Basic.base64_encode($ username。':'。$ password),
'Accept'=> 应用程序/ JSON; charset = utf-8',// API返回JSON
//'Content-Type'=> 应用程序/ JSON; charset = utf-8'
],
'body'=> $ postedData
];
$ response = wp_remote_post($ url,$ args);
// ------------------

if(is_wp_error($ response)){
$ error_message = $ response - > get_error_message();
$ errMsg =出错了:\\\
{$ error_message};

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

if(empty($ data)|| $ data-> status == 0){// API验证错误!
$ errMsg = $ data-> msg-> title。\\\
。$ data-> msg-> description;
}
}

if($ errMsg){//不发送邮件;
// $ cf7-> skip_mail = true; //为旧版本!
$ abort = true; // ==>在这里,自CF7-v5.0起'用引用调用':)
$ submission-> set_status('validation_failed');
// $ submission-> set_response($ cf7-> message('validation_error')); //来自管理员设置的msg;
$ submission-> set_response($ cf7-> filter_message($ errMsg)); //自定义味精;
}
}

希望它能帮助别人。快乐编码:)

Here's my issue, I have contact form 7 for wordpress installed and during the wpcf7_before_send_mail I make a call to an API, I need to invalidate the form if the API returns an error then I need to invalidate the request and return the error passed back from the API call.

I set a flag to false on API failure and the error message is also stored but my form is going through as success despite the failure I induce.

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
    }
}

I've also tried using:

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

But whichever way I am unable to prevent the success email being sent and the form validates as successful. Debugging proved that the !success in the if statement is working and the code contained is added to the variable. I also tried as if form was an array ($form['valid'] = false) but this also didn't work and the form submits as successful. Any ideas of what I'm missing here? I've omitted the code for the API call itself and the determining of the correct form id, both of these work correctly, only the form I'm after is parsed and the API call is returning the expected data.

解决方案

I needed the same. After going through the CF7 plugin files, I found the following solution:

//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;
    }
}

Hopefully, it will help someone. Happy Coding :)

这篇关于api调用后,Wordpress无效cf7的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆