对于我编写的插件,AJAX请求位于前端works properly if i am admin, 但当我试着normal user, 它总是返回0。
这是插件的核心php文件,我正在其中完成所有排队和本地化工作:
require_once ( plugin_dir_path(__FILE__) . \'like-user-request.php\' );
function lr_enqueue_ui_scripts() {
wp_enqueue_script( \'core-likeranker-js\', plugins_url( \'js/like.js\', __FILE__ ), array( \'jquery\', \'jquery-ui-datepicker\' ), false, true );
wp_enqueue_style( \'jquery-style\', \'http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.2/themes/smoothness/jquery-ui.css\' );
wp_localize_script( \'core-likeranker-js\', \'my_ajax_object\', array( \'ajax_url\' => admin_url(\'admin-ajax.php\'), \'security\' => wp_create_nonce(\'user-like\') ) );
}
add_action( \'wp_enqueue_scripts\', \'lr_enqueue_ui_scripts\');
这是javascript文件
jQuery(document).ready(function($){
var state = $(\'#post-like-count\').data(\'user-state\');
$(\'.like-button\').on(\'click\', function(){
var count = $(\'#post-like-count\').data(\'id\');
var postId = $(\'#post-like-count\').data(\'post-id\');
state == 1 ? count++ : count--;
$.ajax({
url: my_ajax_object.ajax_url,
type: \'POST\',
dataType: \'json\',
data: {
action: \'save_like\',
likeCount: count,
id: postId,
userState: state,
security: my_ajax_object.security
},
success: function(response) {
if(response.success === true){
$(\'#post-like-count\').html(count + \' Likes\');
$(\'#post-like-count\').data(\'id\', count);
if(state == 1){
state = 2;
$(\'.like-button\').html(\'Dislike\');
}
else {
state = 1;
$(\'.like-button\').html(\'Like !\');
}
$(\'#post-like-count\').data(\'user-state\', state);
}
},
error:function(error) {
$(\'.like-box\').after(\'Error\');
}
});
})
});
处理AJAX请求的Php文件
<?php
function lr_save_like_request() {
if ( ! check_ajax_referer( \'user-like\', \'security\' ) ) {
wp_send_json_error( \'Invalid Nonce !\' );
}
$count = $_POST[\'likeCount\'];
$postId = $_POST[\'id\'];
$ip;
if ( ! empty( $_SERVER[\'HTTP_CLIENT_IP\'] ) ) {
//check ip from share internet
$ip = $_SERVER[\'HTTP_CLIENT_IP\'];
} elseif ( ! empty( $_SERVER[\'HTTP_X_FORWARDED_FOR\'] ) ) {
//to check ip is pass from proxy
$ip = $_SERVER[\'HTTP_X_FORWARDED_FOR\'];
} else {
$ip = $_SERVER[\'REMOTE_ADDR\'];
}
$voters_ip = get_post_meta( $postId, \'_voters_ip\', false );
$flag = empty( $voters_ip ) ? 1 : 0;
if( $_POST[\'userState\'] == 1 ) {
if( $flag == 1 ) {
$voters_ip_buffer = $voters_ip;
$voters_ip_buffer[] = $ip;
} else {
$voters_ip_buffer = $voters_ip[0];
$voters_ip_buffer[] = $ip;
}
} else {
$voters_ip_buffer = $voters_ip[0];
for ( $i=0; $i < count($voters_ip_buffer); $i++ ) {
if( $ip == $voters_ip_buffer[$i] ) {
unset( $voters_ip_buffer );
}
}
}
if( ! update_post_meta( $postId, \'_Like\', $count ) ) {
add_post_meta ( $postId, \'_Like\', $count, true );
}
if( ! update_post_meta( $postId, \'_voters_ip\', $voters_ip_buffer ) ) {
add_post_meta ( $postId, \'_voters_ip\', $voters_ip_buffer, true );
}
wp_send_json_success( array(
\'state\' => $_POST[\'userState\']
));
wp_die();
}
add_action( \'wp_ajax_save_like\', \'lr_save_like_request\' );