There's code below for simple post rating system (tutorial from this site TUTORIAL ) - it works fine but i need to change it a bit. Now where user votes, he is not able to vote again for same post for $timebeforevote (1440 minutes). I want to block user voting for all post not just for this one.
I think the problem is function is looking for user ip in current post meta field with this code:
$meta_IP = get_post_meta($post_id, "voted_IP");
$voted_IP = $meta_IP[0];
Returns $voted_IP
and compares it with $ip = $_SERVER['REMOTE_ADDR'];
I tried to modify it with query (where query looks for ip in meta field for all posts) so it would search for ip in all posts but it didn't work. I am trying to make it work for 2 days and nothing works.
POST RATING SYSTEM CODE:
$timebeforerevote = 1440;
add_action('wp_ajax_nopriv_post-like', 'post_like');
add_action('wp_ajax_post-like', 'post_like');
wp_enqueue_script('like_post', get_template_directory_uri().'/js/post-like.js', array('jquery'), '1.0', 1 );
wp_localize_script('like_post', 'ajax_var', array(
'url' => admin_url('admin-ajax.php'),
'nonce' => wp_create_nonce('ajax-nonce')
));
function post_like()
{
$nonce = $_POST['nonce'];
if ( ! wp_verify_nonce( $nonce, 'ajax-nonce' ) )
die ( 'Busted!');
if(isset($_POST['post_like']))
{
$ip = $_SERVER['REMOTE_ADDR'];
$post_id = $_POST['post_id'];
$meta_IP = get_post_meta($post_id, "voted_IP");
$voted_IP = $meta_IP[0];
if(!is_array($voted_IP))
$voted_IP = array();
$meta_count = get_post_meta($post_id, "votes_count", true);
if(!hasAlreadyVoted($post_id))
{
$voted_IP[$ip] = time();
update_post_meta($post_id, "voted_IP", $voted_IP);
update_post_meta($post_id, "votes_count", ++$meta_count);
echo $meta_count;
}
else
echo "already";
}
exit;
}
function hasAlreadyVoted($post_id)
{
global $timebeforerevote;
$meta_IP = get_post_meta($post_id, "voted_IP");
$voted_IP = $meta_IP[0];
if(!is_array($voted_IP))
$voted_IP = array();
$ip = $_SERVER['REMOTE_ADDR'];
if(in_array($ip, array_keys($voted_IP)))
{
$time = $voted_IP[$ip];
$now = time();
if(round(($now - $time) / 60) > $timebeforerevote)
return false;
return true;
}
return false;
}
function getPostLikeLink($post_id)
{
$themename = "twentyeleven";
$vote_count = get_post_meta($post_id, "votes_count", true);
$output = '<p class="post-like">';
if ($vote_count == 0)
$output .= '<span class="count">0<span class="glosow"><br>głosów w rankingu</span></span>';
else
$output .= '<span class="count">'.$vote_count.'<span class="glosow"><br>głosów w rankingu</span></span>';
if(hasAlreadyVoted($post_id))
$output .= '<span title="Już głosowałeś" class="qtip alreadyvoted"></span>';
else
$output .= '<a href="#" data-post_id="'.$post_id.'">
<span title="'.__('Głosuj', $themename).'"class="qtip like"></span></a>';
$output .= '</p>';
return $output;
}
Heres code for query that finds if user already voted but problem is that after $timebeforerevote IP value stays in meta field so i can't use it. I have no more ideas for now...
$ip = $_SERVER['REMOTE_ADDR'];
$query = new WP_Query( array(
'ignore_sticky_posts' => true,
'meta_key' => 'voted_IP',
'meta_value' => $ip
) );
if (0 !== $query->found_posts) {
//do something
}
This code:
$voted_IP[$ip] = time();
update_post_meta($post_id, "voted_IP", $voted_IP);
update_post_meta($post_id, "votes_count", ++$meta_count);
...indicates that the voting attempts are stored in the post's voted_IP
meta, in the form of an ip=>timestamp
array. That makes 'meta_value' => $ip
in your query invalid, because you are trying to match a string with an array. Instead, just query for all posts that have this meta key and then do some iterations:
global $post;
$tmp=$post;
$recently_voted=false;
$ip = $_SERVER['REMOTE_ADDR'];
$query = new WP_Query( array(
'ignore_sticky_posts' => true,
'meta_key' => 'voted_IP',
// 'meta_value' => $ip // invalid...
) );
if (0 !== $query->found_posts) {
$now=time();
while($query->have_posts())
{
$id=$post->id;
$meta_IP=get_post_meta($id,"voted_IP"); // this is an array
if(isset($meta_IP[$ip])&&($now-$meta_IP[$ip]<1440*60))
{
$recently_voted=true; // found at least one recent vote
break;
}
}
}
$post=$tmp;