个性化阅读
专注于IT技术分析

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开发的新手, 请帮助。


#1


发现了错误。这与支付网关如何处理手机号码有关。我在数字前面添加了一行, 以添加” +”, 并且复制停止了。

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;
        }
    }
}
赞(0)
未经允许不得转载:srcmini » WordPress wp_insert_post在移动设备上多次触发

评论 抢沙发

评论前必须登录!