Hello Ross
Thank you very much for your help.
I’m guessing my problem would be that the transients may not have expired, I just want to delete the IP addresses that are older than 1st January this year.
You are dealing with a total amateur here but I copied the following code from the “Edit Plugin’ option in WordPress, post-ratings.php file.
Does this mean it will only store 20 IP addresses and they should expire after 90 days?
If I change the expiry to 35 days and then run your code, would that work and if I then wanted to increase these numbers to improve security against multiple votes, could I just change them?
I really do appreciate any advice you can give and I might just add that post ratings is the only plugin that includes a ‘top 20’ sidebar widget, and the plugin has been out of support for ages. If you were to write and support a new ratings plugin I’m sure there must be a market – including me!
Cheers
Pete
/*
* Attempt to get the visitor's IP address
*
* @since 2.3
* @return string
*/
private function getIP(){
if(isset($_SERVER['HTTP_CLIENT_IP']))
return $_SERVER['HTTP_CLIENT_IP'];
if(isset($_SERVER['HTTP_X_FORWARDED_FOR']))
return $_SERVER['HTTP_X_FORWARDED_FOR'];
if(isset($_SERVER['HTTP_X_FORWARDED']))
return $_SERVER['HTTP_X_FORWARDED'];
if(isset($_SERVER['HTTP_FORWARDED_FOR']))
return $_SERVER['HTTP_FORWARDED_FOR'];
if(isset($_SERVER['HTTP_FORWARDED']))
return $_SERVER['HTTP_FORWARDED'];
return $_SERVER['REMOTE_ADDR'];
}
/*
* Process rating, or set up plugin hooks if this is not a rate request
*
* @since 1.0
*/
public function Run(){
$options = $this->getOptions();
extract($options);
if(!isset($_GET['rate'])){
if($custom_filter)
add_filter($custom_filter, array($this, 'ControlBlockHook'));
if($before_post || $after_post){
// post content
add_filter('the_content', array($this, 'ControlBlockHook'), 20);
// bbpress
add_filter('bbp_get_topic_content', array($this, 'ControlBlockHook'));
add_filter('bbp_get_reply_content', array($this, 'ControlBlockHook'));
}
add_action('wp_enqueue_scripts', array($this, 'assets'));
// this is our $.ajax request
}else{
defined('DOING_AJAX') or define('DOING_AJAX', true);
$post_id = (int)$_GET['post_id'];
$voted = min(max((int)$_GET['rate'], 1), $max_rating);
$error = '';
$post = &get_post($post_id);
$rating = 0;
$votes = 0;
if(!$post){
$error = __("Invalid vote! Cheatin' uh?", self::ID);
}else{
// get current post rating and vote count
extract($this->getRating($post->ID));
// vote seems valid, register it
if($this->currentUserCanRate($post_id)){
// increase global post rate count if this is the first vote
if($votes < 1)
$options['num_rated_posts']++;
// global vote count
$options['num_votes']++;
// update post rating and vote count
$votes++;
$rating = (($rating * ($votes - 1)) + $voted) / $votes;
update_post_meta($post->ID, 'rating', $rating);
update_post_meta($post->ID, 'votes', $votes);
// update global stats
$options['avg_rating'] = ($options['num_votes'] > 0) ? ((($options['avg_rating'] * ($options['num_votes'] - 1)) + $voted) / $options['num_votes']) : 0;
update_option(self::ID, $options);
$ip_cache = get_transient('post_ratings_ip_cache');
if(!$ip_cache)
$ip_cache = array();
$posts_rated = isset($_COOKIE[$this->getRecordsKey('posts_rated')]) ? explode('-', $_COOKIE[$this->getRecordsKey('posts_rated')]) : array();
$posts_rated = array_map('intval', array_filter($posts_rated));
// add user's IP to the cache
$ip_cache[$post_id][] = $this->getIP();
// keep it light, only 10 records per post and maximum 10 post records (=> max. 100 ip entries)
// also, the data gets deleted after 2 weeks if there's no activity during this time...
if(count($ip_cache[$post_id]) > 10)
array_shift($ip_cache[$post_id]);
if(count($ip_cache) > 10)
array_shift($ip_cache);
set_transient('post_ratings_ip_cache', $ip_cache, 60 * 60 * 24 * 14);
// update user meta
if(is_user_logged_in()){
$user = wp_get_current_user();
$current_user_ratings = get_user_meta($user->ID, $this->getRecordsKey('posts_rated'), true);
if(!$current_user_ratings)
$current_user_ratings = array();
$posts_rated = array_unique(array_merge($posts_rated, array_filter($current_user_ratings)));
update_user_meta($user->ID, $this->getRecordsKey('posts_rated'), $posts_rated);
}
// update cookie
$posts_rated = array_slice($posts_rated, -20); // keep it under 20 entries
$posts_rated[] = $post_id;
setcookie($this->getRecordsKey('posts_rated'), implode('-', $posts_rated), time() + 60 * 60 * 24 * 90, '/'); // expires in 90 days
$this->rated_posts[] = $post_id;
do_action('rated_post', $post_id);
$this->clearQueryCache();
}else{
$error = __('You cannot rate this post!', self::ID);
}