I'm trying to customize a plugin to check the current user's IP against previous IP values collected and stored by the plugin.
The plugin collects user votes for a custom post type called "competitors". The plugin also collects the IP of the user for each vote to prevent multiple votes by the user on a given competitor.
This is the code the plugin uses to store:
- the most recent voter IP into a $key called "lastIP" in the wp_postmeta database table
- all IPs that voted on the competitor into the next $key called "history" in the wp_postmeta database table (so there could be dozens of IPs stored in the "history" key array, along with a timestamp for each, and one of two other values depending on the vote type)
Existing code to store the values - works great:
function wps_update_history ( $thiscompetitor, $field, $value, $lastIP ) {
update_post_meta($thiscompetitor, 'lastIP', $lastIP);
$current_history = get_post_meta($thiscompetitor, 'history', true);
$current_history_array = unserialize($current_history);
//push new values on
$current_history_array[] = array ( current_time( "timestamp" ), $lastIP, $field, $value );
update_post_meta($thiscompetitor, 'history', serialize( $current_history_array ));
}
I've tried this to get the IP history list (to check if current user is in the list) but it is not working (within the post loop, with wp_query
as orderby=>random
) - from this post here
$hasIP = false;
$histMeta = get_post_meta($post->ID, 'history', false);
foreach($histMeta as $array) {
if(isset($array['lastIP'])) {
$hasIP = $array['lastIP'];
break;
}
}
echo $hasIP;
I've even tried to just echo the entire history array (for debugging) but can't even get this working.
echo $histMeta;
Ultimately what I'd like to do is this logic:
if (current_user has their IP in this $post=>ID wp_postmeta 'history' key array) {
// don't show or let them vote on this $post=>ID
} else {
// try another $post=>ID
}
Any suggestions how I can achieve this logic above?
a:6:{i:0;a:4:{i:0;i:1424637516;i:1;s:13:"65.92.235.251";i:2;s:3:"won";i:3;s:0:"";}i:1;a:4: {i:0;i:1424637581;i:1;s:13:"24.114.65.160";i:2;s:6:"points";i:3;i:9;}i:4;a:4:{i:0;i:1424638574;i:1;s:13:"24.114.65.160";i:2;s:3:"won";i:3;s:0:"";}i:5;a:4:{i:0;i:1424640181;i:1;s:12:"24.138.123.7";i:2;s:3:"won";i:3;s:0:"";}}