后评级系统功能

时间:2013-11-01 14:58:52

标签: php wordpress post rating

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

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

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

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

POST RATING系统代码:

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

1 个答案:

答案 0 :(得分: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;