php - Wordpress wp_insert_post 在移动设备上多次触发
问题描述
我在 Wordpress 上构建了一个自定义的前端多页捐赠表单,通过页面间的会话变量保存数据。然后,我使用 save_post 挂钩运行一个函数,在提交表单后将用户重定向到在线支付门户。问题是,当用户通过手机访问表单时,函数 wp_insert_post 会触发多次。
这是我在用于处理表单数据的 php 页面上的代码。
<?php
header('Cache-Control: no cache'); //no cache
session_cache_limiter('private_no_expire'); // works
//session_cache_limiter('public'); // works too
//let's start the session
session_start();
require_once 'library/HTMLPurifier.auto.php';
$config = HTMLPurifier_Config::createDefault();
$purifier = new HTMLPurifier($config);
//LISTING ALL VARIABLES AVAILABLE FROM FRONTEND FORM
$negara = $_SESSION['negara'];
$binatang = $_SESSION['binatang'];
if($_SESSION['ekor']) {
$ekorBahagian = $_SESSION['ekor'];
} else {
$ekorBahagian = $_SESSION['bahagian'];
}
$nama1 = $_SESSION['nama'];
$kp1 = $_SESSION['kp'];
$telefon1 = $_SESSION['telefon'];
$emel1 = $_SESSION['emel'];
$alamat11 = $_SESSION['alamat1'];
$alamat21 = $_SESSION['alamat2'];
$poskod1 = $_SESSION['poskod'];
$bandar1 = $_SESSION['bandar'];
$negeri1 = $_SESSION['negeri'];
$peserta1 = $_SESSION['peserta'];
$kempen = $_POST['kempen'];
$bank = $_POST['bank'];
if($telefon1) {
$mobile = preg_replace("/[^0-9]/", "", $telefon1);
$custTel = $mobile;
$custTel2 = substr($mobile, 0, 1);
if ($custTel2 == '+') {
$custTel3 = substr($mobile, 1, 1);
if ($custTel3 != '6') {
$custTel = "+6" . $mobile;
}
} elseif ($custTel2 == '6') {
} else {
if ($custTel != '') {
$custTel = "+6" . $mobile;
}
}
}
//purifying the texts
$nama = $purifier->purify($nama1);
$kp = $purifier->purify($kp1);
$telefon = $purifier->purify($custTel);
$emel = $purifier->purify($emel1);
$alamat1 = $purifier->purify($alamat11);
$alamat2 = $purifier->purify($alamat21);
$poskod = $purifier->purify($poskod1);
$bandar = $purifier->purify($bandar1);
$negeri = $purifier->purify($negeri1);
$peserta = $purifier->purify($peserta1);
if($_SESSION['ekor']) {
$bil = $_SESSION['ekor']; //capturing bilangan ekor into a var
switch ($_SESSION['negara']){
case 'Malaysia':
$jumlahHarga = $bil*(650*7);
break;
case 'ASEAN':
$jumlahHarga = $bil*(450*7);
break;
case 'Timur Tengah':
$jumlahHarga = $bil*(1300*7);
break;
case 'Afrika':
$jumlahHarga = $bil*(350*7);
break;
default:
}
} else {
$bil = $_SESSION['bahagian']; //capturing bilangan bahagian into a var
switch ($_SESSION['negara']){
case 'Malaysia':
$jumlahHarga = $bil*650;
break;
case 'ASEAN':
$jumlahHarga = $bil*450;
break;
case 'Timur Tengah':
$jumlahHarga = $bil*1300;
break;
case 'Afrika':
$jumlahHarga = $bil*350;
break;
default:
}
}
$post = array(
'post_title' => wp_strip_all_tags( $nama ),
'post_status' => 'publish',
'post_type' => 'qurban',
'meta_input' => array(
'pilihan_negara' => $negara,
'pilihan_lembu' => $binatang,
'bilangan_ekorbahagian' => $ekorBahagian,
'jumlah_bayaran' => $jumlahHarga,
'nama_penuh' => $nama,
'nombor_kad_pengenalan' => $kp,
'nombor_telefon' => $telefon,
'emel' => $emel,
'alamat_rumah_1' => $alamat1,
'alamat_rumah_2' => $alamat2,
'poskod' => $poskod,
'bandar' => $bandar,
'negeri' => $negeri,
'senarai_nama_peserta' => $peserta,
'bank' => $bank,
'kempen' => $kempen
)
);
$post_id = wp_insert_post($post);
get_header();
?>
<?php
get_footer();
?>
下面是使用 save_post 将用户重定向到外部支付网站的代码:
?php
require_once "Mobile_Detect.php";
////////////////////////////
//// PAYMENT REDIRECTION FUNCTION
////////////////////////////
function my_save_post_iq( $post_id ) {
debug_to_console( "save post function fired" );
$billplzApi = get_field('iq_secret_key', 'option');
$billplzId = get_field('iq_collection_id', 'option');
// bail early if not a donation post
if( get_post_type($post_id) !== 'qurban' ) {
return;
}
// bail early if editing in admin
if( is_admin() ) {
return;
}
$post = get_post( $post_id);
$jumlah_bayaran_iq = get_field('jumlah_bayaran', $post_id);
//check & update user device type
$detect = new Mobile_Detect;
if($detect->isMobile()){
update_field('devices', 'mobile' , $post);
} else {
update_field('devices', 'desktop', $post);
}
$name = get_field('nama_penuh', $post);
$email = get_field('emel', $post);
$mobile = get_field('nombor_telefon', $post);
$bank = get_field('bank', $post);
$billplz_data = array(
'amount' => $jumlah_bayaran_iq * 100,
'name' => $name,
'mobile' => $mobile,
'email' => $email,
'collection_id' => $billplzId,
'deliver' => false,
'reference_1_label' => 'Bank Code',
'reference_1' => $bank,
'reference_2_label' => 'Post ID',
'reference_2' => $post_id,
'description' => 'xxx',
'redirect_url' => home_url('qurbanv2/paymentredirectv2'),
'callback_url' => home_url('paymentcallback')
);
$process = curl_init('https://www.billplz.com/api/v3/bills/');
curl_setopt($process, CURLOPT_HEADER, 0);
curl_setopt($process, CURLOPT_USERPWD, $billplzApi . ":");
curl_setopt($process, CURLOPT_TIMEOUT, 30);
curl_setopt($process, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($process, CURLOPT_POSTFIELDS, http_build_query($billplz_data));
$return = curl_exec($process);
curl_close($process);
$arr = json_decode($return, true);
$billplz_url = $arr['url'];
$billplz_id = $arr['id'];
//update payment status
update_field('billplz_iq', $billplz_id , $post);
//$hzpost_name = array(
// 'ID' = $post,
// 'post_name' = $billplz_url
//);
//
//wp_update_post($hzpost_name);
header('Location: '. $billplz_url . '?auto_submit=true');
exit();
}
add_action('save_post', 'my_save_post_iq', 10, 1);
我是 Wordpress 开发的新手,所以请帮忙。
解决方案
发现了错误。这与支付网关如何处理手机号码有关。我在数字前面添加了一行以添加“+”,然后重复停止。
if($telefon1) {
$mobile = preg_replace("/[^0-9]/", "", $telefon1);
$custTel = $mobile;
$custTel2 = substr($mobile, 0, 1);
if ($custTel2 == '+') {
$custTel3 = substr($mobile, 1, 1);
if ($custTel3 != '6') {
$custTel = "+6" . $mobile;
}
} elseif ($custTel2 == '6') {
$custTel = "+" . $mobile; //added this line.
} else {
if ($custTel != '') {
$custTel = "+6" . $mobile;
}
}
}
推荐阅读
- mongodb - 如何聚合 MongoDB 嵌套查询?
- r - 如何识别句子之间的共同文本?
- c++ - 结构定义错误,但前提是先前定义了函数
- react-native - react native中useEffect中的数据更新后如何加载UI
- r - 为什么我的 Rmarkdown 文档在编织到 pdf 时不显示任何 unicode 字符?
- android - 在 Android 11 中,我无法使用相机意图获取结果代码 0 如何解决此错误?
- javascript - 当召唤它的用户离开时,我如何断开机器人的连接?
- c# - 封面页没有显示页眉
- mysql - 在 MySQL 中,可以使用一个过程将查询的结果集包含到另一个中吗?
- arrays - 如何设置记录数组?(德尔福)