Exemplo n.º 1
0
 public function comment_inserted($comment_id, $comment_object)
 {
     if ($comment_object->comment_approved == '1') {
         $post = get_post($comment_object->comment_post_ID);
         if ($post->post_type == 'question') {
             ap_do_event('new_comment', $comment_object, 'question', '');
             // set updated meta for sorting purpose
             update_post_meta($comment_object->comment_post_ID, ANSPRESS_UPDATED_META, current_time('mysql'));
             // add participant
             ap_add_parti($comment_object->comment_post_ID, $comment_object->user_id, 'comment', $comment_id);
         } elseif ($post->post_type == 'answer') {
             ap_do_event('new_comment', $comment_object, 'answer', $post->post_parent);
             $post_id = wp_get_post_parent_id($comment_object->comment_post_ID);
             // set updated meta for sorting purpose
             update_post_meta($post_id, ANSPRESS_UPDATED_META, current_time('mysql'));
             // add participant only
             ap_add_parti($post_id, $comment_object->user_id, 'comment', $comment_id);
         }
     }
 }
Exemplo n.º 2
0
 public function ap_set_best_answer()
 {
     $args = explode('-', sanitize_text_field($_POST['args']));
     if (wp_verify_nonce($args[1], 'answer-' . $args[0])) {
         $post = get_post($args[0]);
         $user_id = get_current_user_id();
         if (ap_is_answer_selected($post->post_parent)) {
             ap_do_event('unselect_answer', $user_id, $post->post_parent, $post->ID);
             update_post_meta($post->ID, ANSPRESS_BEST_META, 0);
             update_post_meta($post->post_parent, ANSPRESS_SELECTED_META, false);
             $html = ap_select_answer_btn_html($args[0]);
             $result = array('action' => 'unselected', 'message' => __('Uselected the answer', 'ap'), 'html' => $html);
         } else {
             ap_do_event('select_answer', $user_id, $post->post_parent, $post->ID);
             update_post_meta($post->ID, ANSPRESS_BEST_META, 1);
             update_post_meta($post->post_parent, ANSPRESS_SELECTED_META, $post->ID);
             $html = ap_select_answer_btn_html($args[0]);
             $result = array('action' => 'selected', 'message' => __('Thank you for awarding best answer', 'ap'), 'html' => $html);
         }
     } else {
         $result = array('action' => false, 'message' => __('Please try again', 'ap'));
     }
     die(json_encode($result));
 }
Exemplo n.º 3
0
 public function post_delete_action($post_id)
 {
     remove_action('after_delete_post', array($this, 'post_delete_action'));
     /* trashed item post type is revison so we have to get its post parent */
     $post = get_post($post_id);
     if ($post) {
         /* if questions was deleted then delete its answers as well */
         $post_type = get_post_type($post->post_parent);
         if ($post_type == 'question') {
             ap_do_event('delete_question', $post->post_parent, $post->post_author);
             // remove question participant
             ap_remove_parti($post->post_parent);
             $arg = array('post_type' => 'answer', 'post_status' => 'trash', 'post_parent' => $post->post_parent, 'showposts' => -1, 'caller_get_posts' => 1, 'post__not_in' => '');
             $ans = get_posts($arg);
             if (count($ans) > 0) {
                 foreach ($ans as $p) {
                     ap_remove_parti($p->post_parent, $p->post_author, 'answer');
                     ap_do_event('delete_answer', $ans->ID, $ans->post_author);
                     wp_delete_post($p->ID, true);
                 }
             }
         }
         /* remove participant answer */
         if ($post_type == 'answer') {
             $ans = get_post($post->post_parent);
             ap_remove_parti($ans->post_parent, $ans->post_author, 'answer');
             ap_do_event('delete_answer', $ans->ID, $ans->post_author);
         }
     }
     add_action('after_delete_post', array($this, 'post_delete_action'));
 }
Exemplo n.º 4
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));
 }