0

これはプロモーションモーダルの実用的なコードですが、これは ajax リクエストで wordpress nonce を使用する安全で適切な方法ですか?

tmp で nonce を作成します: $ajax_nonce = wp_create_nonce( "mynonce" ); URL の例: www.mysite.com/#345345

var asdf = location.hash.match(/^#?(.*)$/)[1];
if (asdf) {
    $productID = asdf; 
    if ($.cookie("modalshow") === undefined) {
        var expiryDate = new Date();
        var minutes = 0.20; //12 seconds
        expiryDate.setTime(expiryDate.getTime() + (minutes * 60 * 1000)); 
        $.cookie("modalshow", "exists", { path: '/', expires: expiryDate });
        var data = {
            action: 'my_action',
            product_id: $productID,
            security: '<?php echo $ajax_nonce; ?>'
        };
        var ajaxurl = '/wp-admin/admin-ajax.php';
        $.post(ajaxurl, data, function(response) {
                $(".modal-content").append(response);
                $('#Modal').modal({ show:true });
        });
    } //no cookie
};//if match

プラグイン:

add_action( 'wp_ajax_my_action', 'my_action_function' );
function my_action_function() {
    check_ajax_referer( 'mynonce', 'security' );
    if( isset($_POST['product_id']) ) {
        $my_report_num = $_POST['product_id']; // splash 443
        $myposttype = get_post_type( $my_product_num );
        if ( get_post_status( $my_product_num ) == 'publish' && $myposttype == "df_product" ) {
            //we're good
            $theid = $my_product_num;
        } else {
            $theid = "876";
        }
        //fedtch content using $theid
        die();
        } // end if
    die;
}

上記の php では、get_post_type() は、sanitize_post() を使用する get_post() を使用しています。そのため、いくつかの検証が行われています。現在、私の計画では、悪意のあるものが URL に追加されたり、クライアントで他の手段によって送信されたりした場合、その $theid は私のホワイトリスト番号「876」に設定されます-クライアントまたはphpで追加の検証を行う必要がありますか?

どんな援助でも大歓迎です、ありがとう!

4

0 に答える 0