I'm using a plugin that allows users to vote on posts, I'd like to add each vote that's cast to the authors user_meta so I can rank users the same way I rank posts.
There's a couple of different functions in the plugin that update and save the post_meta.
add_action( 'wp_ajax_nopriv_epicred_vote', 'epicred_vote' );
add_action( 'wp_ajax_epicred_vote', 'epicred_vote' );
function epicred_vote(){
global $wpdb, $current_user;
get_currentuserinfo();
$wpdb->myo_ip = $wpdb->prefix . 'epicred';
$option = (int)$_POST['option'];
$current = (int)$_POST['current'];
//if we are locked via IP set the fid variable to be the IP address, otherwise log the member ID
if(get_option('epicred_ip') == 'yes'){
$ipAddr = isset($_SERVER['HTTP_X_CLUSTER_CLIENT_IP']) ? $_SERVER['HTTP_X_CLUSTER_CLIENT_IP'] : $_SERVER['REMOTE_ADDR'];
$fid = "'" . $ipAddr . "'";
}else{
$fid = $current_user->ID;
}
$postid = (int)$_POST['poll'];
$query = "SELECT epicred_option FROM $wpdb->myo_ip WHERE epicred_ip = $fid AND epicred_id = $postid";
$al = $wpdb->get_var($query);
if($al == NULL){
$query = "INSERT INTO $wpdb->myo_ip ( epicred_id , epicred_ip, epicred_option) VALUES ( $postid, $fid, $option)";
$wpdb->query($query);
}else{
$query = "UPDATE $wpdb->myo_ip SET epicred_option = $option WHERE epicred_ip = $fid AND epicred_id = $postid";
$wpdb->query($query);
}
$vote = get_post_meta($postid,'epicredvote',true);
if($option == 1){
if($al != 1){
if($al == -1){
$vote = $vote+2;
}else{
$vote = $vote+1;
}
}
}
if($option == -1){
if($al != -1){
if($al == 1){
$vote = $vote-2;
}else{
$vote = $vote-1;
}
}
}
update_post_meta($postid,'epicredvote',$vote);
$response['poll'] = $postid;
$response['vote'] = $vote;
echo json_encode($response);
// IMPORTANT: don't forget to "exit"
exit;
}
And
function wpt_save_wpeddit() {
global $wp, $wpdb, $post;
// unhook this function so it doesn't loop infinitely
remove_action('save_post', 'wpt_save_wpeddit');
// Is the user allowed to edit the post or page?
if ( !current_user_can( 'edit_post', $post->ID ))
return $post->ID;
// OK, we're authenticated: we need to find and save the data. Make sure we don't add an image every time the post is
// saved as a draft - so keep track of the external URL in a custom field.
$vote = get_post_meta($post->ID,'epicredvote',true);
$rank = get_post_meta($post->ID,'epicredrank',true);
if($vote == ""){
$vote = 0;
}
if($rank == ""){
$rank = 0;
}
update_post_meta($post->ID, 'epicredvote', $vote );
update_post_meta($post->ID,'epicredrank',$rank);
add_action('save_post', 'wpt_save_wpeddit');
}
Then my goal would be to use something like this in order to rank the users:-
EDIT:- loop I'm using to output users/scores.
<?php
$args = array(
'meta_key' => 'epicredvote',
'orderby' => 'meta_value',
'order' => 'DESC',
'number' => 10,
);
add_action( 'pre_user_query', 'wpse_149342_pre_user_query' );
$user_query = new WP_User_Query( $args );
// User Loop
if ( ! empty( $user_query->results ) ) {
foreach ( $user_query->results as $user ) {
echo '<p>' . $user->display_name . '</p>';
echo '<span>' . $user->epicredvote . '</span>';
}
} else {
echo 'No users found.';
}
?>
So one vote on a post = one vote for that user essentially.
Possible, or am I completely off track here?