示例#1
0
 /**
  * Process voting button
  * @return void
  * @since 2.0.1.1
  */
 public function vote()
 {
     $post_id = (int) $_POST['post_id'];
     if (!wp_verify_nonce($_POST['__nonce'], 'vote_' . $post_id)) {
         ap_send_json(ap_ajax_responce('something_wrong'));
         return;
     }
     if (!is_user_logged_in()) {
         ap_send_json(ap_ajax_responce('please_login'));
         return;
     }
     $post = get_post($post_id);
     if ($post->post_author == get_current_user_id()) {
         ap_send_json(ap_ajax_responce('cannot_vote_own_post'));
         return;
     }
     $type = sanitize_text_field($_POST['type']);
     $type = $type == 'up' ? 'vote_up' : 'vote_down';
     $userid = get_current_user_id();
     $is_voted = ap_is_user_voted($post_id, 'vote', $userid);
     if (is_object($is_voted) && $is_voted->count > 0) {
         // if user already voted and click that again then reverse
         if ($is_voted->type == $type) {
             ap_remove_vote($type, $userid, $post_id, $post->post_author);
             $counts = ap_post_votes($post_id);
             //update post meta
             update_post_meta($post_id, ANSPRESS_VOTE_META, $counts['net_vote']);
             do_action('ap_undo_vote', $post_id, $counts);
             $action = 'undo';
             $count = $counts['net_vote'];
             do_action('ap_undo_' . $type, $post_id, $counts);
             ap_send_json(ap_ajax_responce(array('action' => $action, 'type' => $type, 'count' => $count, 'message' => 'undo_vote')));
         } else {
             ap_send_json(ap_ajax_responce('undo_vote_your_vote'));
         }
     } else {
         ap_add_vote($userid, $type, $post_id, $post->post_author);
         $counts = ap_post_votes($post_id);
         //update post meta
         update_post_meta($post_id, ANSPRESS_VOTE_META, $counts['net_vote']);
         do_action('ap_' . $type, $post_id, $counts);
         $action = 'voted';
         $count = $counts['net_vote'];
         ap_send_json(ap_ajax_responce(array('action' => $action, 'type' => $type, 'count' => $count, 'message' => 'voted')));
     }
 }
示例#2
0
 /**
  * Process voting button.
  * @since 2.0.1.1
  */
 public function vote()
 {
     $post_id = (int) $_POST['post_id'];
     if (!ap_verify_nonce('vote_' . $post_id)) {
         $this->something_wrong();
     }
     if (!is_user_logged_in()) {
         $this->send('please_login');
     }
     $post = get_post($post_id);
     if ($post->post_author == get_current_user_id() && !is_super_admin()) {
         $this->send('cannot_vote_own_post');
     }
     $type = sanitize_text_field($_POST['type']);
     $type = $type == 'up' ? 'vote_up' : 'vote_down';
     if ('question' == $post->post_type && ap_opt('disable_down_vote_on_question') && 'vote_down' == $type) {
         $this->send('voting_down_disabled');
     } elseif ('answer' === $post->post_type && ap_opt('disable_down_vote_on_answer') && 'vote_down' === $type) {
         $this->send('voting_down_disabled');
     }
     $userid = get_current_user_id();
     $is_voted = ap_is_user_voted($post_id, 'vote', $userid);
     if (is_object($is_voted) && $is_voted->count > 0) {
         // If user already voted and click that again then reverse.
         if ($is_voted->type == $type) {
             ap_remove_vote($type, $userid, $post_id, $post->post_author);
             $counts = ap_post_votes($post_id);
             // Update post meta.
             update_post_meta($post_id, ANSPRESS_VOTE_META, $counts['net_vote']);
             do_action('ap_undo_vote', $post_id, $counts);
             $action = 'undo';
             $count = $counts['net_vote'];
             do_action('ap_undo_' . $type, $post_id, $counts);
             $this->send(array('action' => $action, 'type' => $type, 'count' => $count, 'message' => 'undo_vote'));
         } else {
             $this->send('undo_vote_your_vote');
         }
     } else {
         ap_add_vote($userid, $type, $post_id, $post->post_author);
         $counts = ap_post_votes($post_id);
         // Update post meta.
         update_post_meta($post_id, ANSPRESS_VOTE_META, $counts['net_vote']);
         do_action('ap_' . $type, $post_id, $counts);
         $action = 'voted';
         $count = $counts['net_vote'];
         $this->send(array('action' => $action, 'type' => $type, 'count' => $count, 'message' => 'voted'));
     }
 }
示例#3
0
 function ap_vote_on_post()
 {
     $args = explode('-', sanitize_text_field($_POST['args']));
     if (wp_verify_nonce($args[2], 'vote_' . $args[1])) {
         $value = $args[0] == 'up' ? 1 : -1;
         $type = $args[0] == 'up' ? 'vote_up' : 'vote_down';
         $userid = get_current_user_id();
         $is_voted = ap_is_user_voted($args[1], $type, $userid);
         if ($is_voted) {
             // if user already voted and click that again then reverse
             if ($is_voted == $value) {
                 $row = ap_remove_vote($type, $userid, $args[1]);
                 $counts = ap_post_votes($args[1]);
                 //update post meta
                 update_post_meta($args[1], ANSPRESS_VOTE_META, $counts['net_vote']);
                 do_action('ap_undo_vote', $args[1], $counts, $value);
                 $action = 'undo';
                 $count = $counts['net_vote'];
                 $message = __('Your vote has been removed', 'ap');
                 $result = apply_filters('ap_undo_vote_result', array('row' => $row, 'action' => $action, 'type' => $args[0], 'count' => $count, 'message' => $message));
                 ap_do_event('undo_' . $type, $args[1], $counts);
             } else {
                 $result = array('action' => false, 'message' => __('Undo your vote first', 'ap'));
             }
         } else {
             $row = ap_add_vote($userid, $type, $args[1]);
             $counts = ap_post_votes($args[1]);
             //update post meta
             update_post_meta($args[1], ANSPRESS_VOTE_META, $counts['net_vote']);
             do_action('ap_voted_' . $type, $args[1], $counts);
             $action = 'voted';
             $count = $counts['net_vote'];
             $message = __('Thank you for voting', 'ap');
             $result = apply_filters('ap_cast_vote_result', array('row' => $row, 'action' => $action, 'type' => $args[0], 'count' => $count, 'message' => $message));
             ap_do_event($type, $args[1], $counts);
         }
     } else {
         $result = array('action' => false, 'message' => __('Unable to process your vote, try again', 'ap'));
     }
     die(json_encode($result));
 }