如何跟踪投票时间?

时间:2012-10-03 作者:Frank Morrison

我在我的网站上设置了一个很好的投票系统,但由于我想按周、月和所有时间对排名靠前的帖子进行排序。。。我现在需要能够跟踪投票的时间。有人能帮我/推动我朝正确的方向走吗?

我遵循了本教程:

http://wp.tutsplus.com/tutorials/how-to-create-a-simple-post-rating-system-with-wordpress-and-jquery/

JQuery:

    jQuery(document).ready(function() {  
    jQuery(".post-like a").click(function(){  
        heart = jQuery(this);  
        // Retrieve post ID from data attribute  
        post_id = heart.data("post_id");  
        // Ajax call  
        jQuery.ajax({  
            type: "post",  
            url: ajax_var.url,  
            data: "action=post-like&nonce="+ajax_var.nonce+"&post_like=&post_id="+post_id,  
            success: function(count){  
                // If vote successful  
                if(count != "already")  
                {  
                    heart.addClass("voted");  
                    heart.siblings(".count").text(count);  
                }  
            }  
        });  
        return false;  
    })  
})  
函数挂钩。php

add_action(\'wp_ajax_nopriv_post-like\', \'post_like\');  
add_action(\'wp_ajax_post-like\', \'post_like\');  
类帖子功能:

    function post_like()  
{  
    // Check for nonce security  
    $nonce = $_POST[\'nonce\'];  
    if ( ! wp_verify_nonce( $nonce, \'ajax-nonce\' ) )  
        die ( \'Busted!\');  
    if(isset($_POST[\'post_like\']))  
    {  
        // Retrieve user IP address  
        $ip = $_SERVER[\'REMOTE_ADDR\'];  
        $post_id = $_POST[\'post_id\'];  
        // Get voters\'IPs for the current post  
        $meta_IP = get_post_meta($post_id, "voted_IP");  
        $voted_IP = $meta_IP[0];  
        if(!is_array($voted_IP))  
            $voted_IP = array();  
        // Get votes count for the current post  
        $meta_count = get_post_meta($post_id, "votes_count", true);  
        // Use has already voted ?  
        if(!hasAlreadyVoted($post_id))  
        {  
            $voted_IP[$ip] = time();  
            // Save IP and increase votes count  
            update_post_meta($post_id, "voted_IP", $voted_IP);  
            update_post_meta($post_id, "votes_count", ++$meta_count);  
            // Display count (ie jQuery return value)  
            echo $meta_count;  
        }  
        else  
            echo "already";  
    }  
    exit;  
} 
已投票:

    function hasAlreadyVoted($post_id)  
{  
    global $timebeforerevote;  
    // Retrieve post votes IPs  
    $meta_IP = get_post_meta($post_id, "voted_IP");  
    $voted_IP = $meta_IP[0];  
    if(!is_array($voted_IP))  
        $voted_IP = array();  
    // Retrieve current user IP  
    $ip = $_SERVER[\'REMOTE_ADDR\'];  
    // If user has already voted  
    if(in_array($ip, array_keys($voted_IP)))  
    {  
        $time = $voted_IP[$ip];  
        $now = time();  
        // Compare between current time and vote time  
        if(round(($now - $time) / 60) > $timebeforerevote)  
            return false;  
        return true;  
    }  
    return false;  
} 
用我已有的代码,这可行吗?任何帮助或建议都会很棒,非常感谢。

1 个回复
最合适的回答,由SO网友:Adam 整理而成

以下代码行都存储IP地址和投票时间,

// $ip holds $_SERVER[\'REMOTE_ADDR\']
$voted_IP[$ip] = time();  
// Save IP and increase votes count  
update_post_meta($post_id, "voted_IP", $voted_IP);
投票时间已存储在post_meta 该人员正在投票的给定职位的表格。

要检索给定帖子的投票和相应时间,您可以这样做,

  $mykey_values = get_post_custom_values(\'voted_IP\', $post_id);
  foreach ( $mykey_values as $key => $value ) {
    echo "$key  => $value <br />"; 
  }
将以类似的格式返回列表,

192.166.342.8 => 1349260674
202.646.100.3 => 1349260674
^ IP numbers     ^ unix time stamps

结束