0

下面有简单的帖子评分系统的代码(来自本网站的教程 TUTORIAL) - 它工作正常,但我需要稍微改变一下。现在,在用户投票的地方,他无法在 $timebeforevote(1440 分钟)内再次为同一个帖子投票。我想阻止用户对所有帖子的投票,而不仅仅是这个帖子。

我认为问题是函数正在使用以下代码在当前帖子元字段中查找用户 ip:

$meta_IP = get_post_meta($post_id, "voted_IP");
$voted_IP = $meta_IP[0];

返回$voted_IP并将其与$ip = $_SERVER['REMOTE_ADDR'];我尝试使用查询修改它进行比较(其中查询在所有帖子的元字段中查找 ip),因此它会在所有帖子中搜索 ip 但它不起作用。我试图让它工作 2 天,但没有任何效果。

邮政评级系统代码:

$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;
}

这是查询用户是否已经投票的查询代码,但问题是在 $timebeforerevote IP 值保留在元字段中之后,我无法使用它。我暂时没有更多的想法...

$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
}
4

1 回答 1

0

这段代码:

        $voted_IP[$ip] = time();

        update_post_meta($post_id, "voted_IP", $voted_IP);
        update_post_meta($post_id, "votes_count", ++$meta_count);

...表示投票尝试以数组voted_IP的形式存储在帖子的元数据中。ip=>timestamp这使'meta_value' => $ip您的查询无效,因为您正在尝试将字符串与数组匹配。相反,只需查询具有此元键的所有帖子,然后进行一些迭代:

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;
于 2013-11-01T16:58:33.073 回答