Prevent duplicate catching of ap_vote_up and ap_undo_vote_up hooks with myCred

Solved2.87K viewsCoreap_undo_vote_up ap_vote_up mycred points reputation
0

What I need: Some way to check whether a vote has been added to a post by a particular user before so they won’t be awarded again with points if that vote is removed and added again.
If anyone with a bit of technical knowledge or even better Rahul could give me some input here that would be amazing. I have managed to write code to reward a user with myCred points if they get their post upvoted by using ap_vote_up hook (See code below)

//Registers a myCred custom hook
add_filter( 'mycred_setup_hooks', 'register_my_custom_hook' );
function register_my_custom_hook( $installed )
{
 $installed['upvoted_anspress_comment'] = array(
  'title'       => __( 'Upvoted Anspress Comment', 'textdomain' ),
  'description' => __( 'Every time a user has a comment upvoted in the Anspress-powered discussion forum.', 'textdomain' ),
  'callback'    => array( 'anspress_upvote_class' )
 );
}
 //Anspress Comment Upvote Class
class anspress_upvote_class extends myCRED_Hook{
 function __construct( $hook_prefs, $type ) {
  parent::__construct( array(
   'id'       => 'upvoted_anspress_comment',
   'defaults' => array(
    'creds'   => 5  ,
    'log'     => '%plural% for getting an upvote on your comment'
   )
  ), $hook_prefs, $type );
 }
    //Giving Points
 public function run(){
     add_action('ap_vote_up', array($this, 'points_for_anspress_upvotes'), 10, 2);
 }
 public function points_for_anspress_upvotes($post_id, $counts){
     $user_id = get_the_author_meta('ID', get_post($post_id)->post_author);
     $this->core->add_creds(
         'upvoted_anspress_comment',
         $user_id,
         $this->prefs['creds'],
   $this->prefs['log'],
   0,
   '',
   $m
         );
 }
}


. However I am currently trying to deal with an issue where if the person who awards the upvote removes their vote and votes again, points would be awarded twice. To combat this, I used the ap_undo_vote_up hook to remove the same number of points whenever a vote is removed. The only issue is if I do this, the total lifetime earned points still registers duplicate points (i.e. user rewarded for being upvoted both times).
What I need: Some way to check whether a vote has been added to a post by a particular user before so they won’t be awarded again with points if that vote is removed and added again.

Question is closed for new answers.
Selected answer as best
1

Hello Matt,
If a vote is undone then there is not way to know if user has already voted. But I see some workarounds.
First thing you can do is you can update total lifetime point count. I am not sure where it is stored but most usually it may be a user meta. I am pretty sure this will be most easier.
Secondly you can also add another vote reaction to record user vote. This reaction is not removed on undo vote. Below code will be inside ap_vote_up hook.

ap_vote_insert( $post_id, $user_id, 'user_voted_once',  $user_id, 1 );


Then later you can check if user already voted by

ap_is_user_voted( $post_id, 'user_voted_once', $user_id )


Lemme know if that helps.
Cheers.

Posted new comment

Thanks Rahul! Got it all working thanks to your workaround. Appreciate the help and cheers for a great plugin!

You are viewing 1 out of 1 answers, click here to view all answers.