有人告诉我,管理ajax现在已经过时了,最好改用REST API,但我想知道如何才能获得与此代码相同的结果,该代码使用REST API生成一个按钮,其中包含基于一些变量和自定义字段的链接?
另外,有人告诉我,这段代码不安全,因为我使用的是HTML响应,而不是ajax中的数据,但我想知道如何为这段代码使用数据?
很抱歉,我对编码还是很陌生
add_action( \'woocommerce_before_add_to_cart_button\', \'simple_amz_link_ajax\', 10);
function simple_amz_link_ajax() {
?>
<script>
jQuery(document).ready(function(){
jQuery.ajax({
url: "<?php echo admin_url(\'admin-ajax.php\'); ?>",
type: \'POST\',
data: {
action: \'getmyfunctionform1\',
postId: <?php echo get_post()->ID; ?>
},
dataType: \'html\',
success: function(response) {
jQuery("#buy_amz_btn_wrap").html(response);
}
});
});
</script>
<!-- end Ajax call to getmyfunctionform1 smc 11-22-2013 -->
<div id="buy_amz_btn_wrap"> Loading ... </div>
<?php
}
// Amazon Simple Button Function
add_action(\'wp_ajax_getmyfunctionform1\', \'myfunctionform1\');
add_action(\'wp_ajax_nopriv_getmyfunctionform1\', \'myfunctionform1\');
function myfunctionform1() {
$postId = filter_input( INPUT_POST, \'postId\', FILTER_SANITIZE_NUMBER_INT );
$country_code = $_SERVER ["HTTP_CF_IPCOUNTRY"];
$de_asin = get_post_meta( $postId, "wccaf_de_asin", true );
$uk_asin = get_post_meta( $postId, "wccaf_uk_asin", true );
$not_avilable_country = \'<div id="amz_not_avilable" class="amz_not_avilable">This product is not avilable in your country yet</div>\';
//////////////////////////////////////////////
if ($country_code=="DE" or $country_code=="DE") {
$amazon_domain = \'https://www.amazon.de\';
$associate_id = \'bonstato-21\';
$asin = $de_asin;
}
else if ($country_code=="GB" && $uk_asin!=="") {
$amazon_domain = \'https://www.amazon.co.uk\';
$associate_id = \'bonmedico-21\';
$asin = $uk_asin;
}
///////////////////
global $post;
$product = wc_get_product( $postId );
$type = $product->get_type();
if( $type == \'simple\' && $asin!="" ){
// Amazon Link For Mobile
?>
<script>
function amzGo(){
window.location=\'<?php echo $amazon_domain ?>/dp/<?php echo $asin ?>/?tag=<?php echo $associate_id ?>\';
}
</script>
<?php
?>
<div class="buy_amz_btn_wrap" >
<button type="button" id="buy_amz_btn" class="buy_amz_btn" onclick="amzGo();"><i class="fa fa-amazon fa-amz"></i><?php echo $amz_btn_title ?></button>
</div>
<?php
}
else if( $type == \'simple\' && $asin=="" ){
echo $not_avilable_country;
}
die();
}
?>