/** * Check if a user has already voted for a post * @param type $post_id * @param type $user_id * @return bool */ function bbpvotes_has_user_voted_for_post($post_id = null, $user_id = 0) { $votes_up = bbpvotes_has_user_voted_up_for_post($post_id, $user_id); $votes_down = bbpvotes_has_user_voted_down_for_post($post_id, $user_id); if (!$votes_up && !$votes_down) { return false; } return true; }
/** * * @param type $post_id * @param type $vote MUST BE defined, MUST BE a boolean * @return boolean|\WP_Error */ function do_post_vote($post_id, $vote = null) { //check vote value if (is_bool($vote) === false) { return new WP_Error('vote_is_not_bool', __('Vote is not a boolean', 'bbpvotes')); } $voteplus = $vote === true; $voteminus = $vote === false; if (!($post = get_post($post_id))) { return false; } if ($voteplus && !bbpvotes_can_user_vote_up_for_post($post->ID) || $voteminus && !bbpvotes_can_user_vote_down_for_post($post->ID)) { return new WP_Error('missing_capability', __("You don't have the required capability to vote", 'bbpvotes')); } $user_id = get_current_user_id(); //check user is not post author if ($post->post_author == $user_id) { return new WP_Error('user_is_author', __('You cannot vote for your own post', 'bbpvotes')); } //get current votes $post_score = bbpvotes_get_votes_score_for_post($post->ID); $votes_count = bbpvotes_get_votes_count_for_post($post->ID); $user_voted_up = bbpvotes_has_user_voted_up_for_post($post->ID, $user_id); $user_voted_down = bbpvotes_has_user_voted_down_for_post($post->ID, $user_id); $toggle_vote = $voteplus && $user_voted_down || $voteminus && $user_voted_up; $unvote = bbpvotes()->options['unvote_enabled'] && ($voteplus && $user_voted_up || $voteminus && $user_voted_down); //remove old vote first if any if ($toggle_vote || $unvote) { //get previous vote meta key if ($user_voted_down) { $meta_previous_vote = $this->metaname_post_vote_down; } else { $meta_previous_vote = $this->metaname_post_vote_up; } if ($removed_previous_vote = delete_post_meta($post->ID, $meta_previous_vote, $user_id)) { //successfully deleted $votes_count--; //restore score if ($user_voted_down) { $post_score++; } else { $post_score--; } } } if ($voteplus && $user_voted_up || $voteminus && $user_voted_down) { //vote duplicate : remove or block it if (bbpvotes()->options['unvote_enabled']) { //remove vote if (!bbpvotes_can_user_unvote_for_post($post->ID)) { return new WP_Error('missing_capability', __("You don't have the required capability to unvote", 'bbpvotes')); } else { if ($removed_previous_vote) { update_post_meta($post->ID, $this->metaname_post_vote_score, $post_score); update_post_meta($post->ID, $this->metaname_post_vote_count, $votes_count); do_action('bbpvotes_do_post_unvote', $post->ID, $user_id); } else { return new WP_Error('unvoting_error', __('Error while unvoting for this post', 'bbpvotes')); } } } else { //block vote return new WP_Error('already_voted', __('You have already voted for this post', 'bbpvotes')); } } else { //process vote //get new vote meta key if ($voteplus) { $meta_vote = $this->metaname_post_vote_up; } else { $meta_vote = $this->metaname_post_vote_down; } if ($result = add_post_meta($post->ID, $meta_vote, $user_id)) { $votes_count++; //update score if ($voteplus) { $post_score++; } else { $post_score--; } update_post_meta($post->ID, $this->metaname_post_vote_score, $post_score); update_post_meta($post->ID, $this->metaname_post_vote_count, $votes_count); } else { return new WP_Error('voting_error', __('Error while voting for this post', 'bbpvotes')); } do_action('bbpvotes_do_post_vote', $post->ID, $user_id, $vote); } return true; }