Example #1
0
 /**
  * If questions is restored then restore its answers too.
  * @param  integer $post_id Post ID.
  * @since 2.0.0
  */
 public function untrash_ans_on_question_untrash($post_id)
 {
     $post = get_post($post_id);
     if ($post->post_type == 'question') {
         do_action('ap_untrash_question', $post->ID);
         $ans = get_posts(array('post_type' => 'answer', 'post_status' => 'trash', 'post_parent' => $post_id, 'showposts' => -1));
         if ($ans > 0) {
             foreach ($ans as $p) {
                 do_action('ap_untrash_answer', $p->ID);
                 wp_untrash_post($p->ID);
             }
         }
         // Restore question history.
         ap_restore_question_history($post_id);
     }
     if ($post->post_type == 'answer') {
         $ans = ap_count_published_answers($post->post_parent);
         do_action('untrash_answer', $post->ID, $post->post_author);
         // Update answer count.
         update_post_meta($post->post_parent, ANSPRESS_ANS_META, $ans + 1);
     }
 }
Example #2
0
 /**
  * Process answer form
  */
 public function process_answer_form()
 {
     global $ap_errors, $validate;
     if (ap_show_captcha_to_user() && !$this->check_recaptcha()) {
         $this->result = array('form' => $_POST['ap_form_action'], 'message' => 'captcha_error', 'errors' => array('captcha' => __('Bot verification failed.', 'ap')));
         return;
     }
     $question = get_post((int) $_POST['form_question_id']);
     $args = array('description' => array('sanitize' => array('remove_more', 'encode_pre_code', 'wp_kses'), 'validate' => array('required' => true, 'length_check' => ap_opt('minimum_question_length'))), 'is_private' => array('sanitize' => array('only_boolean')), 'name' => array('sanitize' => array('strip_tags', 'sanitize_text_field')), 'form_question_id' => array('sanitize' => array('only_int')), 'edit_post_id' => array('sanitize' => array('only_int')));
     /**
      * FILTER: ap_answer_fields_validation
      * Filter can be used to modify answer form fields.
      * @var void
      * @since 2.0.1
      */
     $args = apply_filters('ap_answer_fields_validation', $args);
     $validate = new AnsPress_Validation($args);
     $ap_errors = $validate->get_errors();
     // if error in form then return
     if ($validate->have_error()) {
         $this->result = array('form' => $_POST['ap_form_action'], 'message_type' => 'error', 'message' => __('Check missing fields and then re-submit.', 'ap'), 'errors' => $ap_errors);
         return;
     }
     $fields = $validate->get_sanitized_fields();
     $this->fields = $fields;
     if (!empty($fields['edit_post_id'])) {
         $this->edit_answer($question);
         return;
     }
     // Do security check, if fails then return
     if (!ap_user_can_answer($question->ID) || !isset($_POST['__nonce']) || !wp_verify_nonce($_POST['__nonce'], 'nonce_answer_' . $question->ID)) {
         $this->result = ap_ajax_responce('no_permission');
         return;
     }
     $user_id = get_current_user_id();
     $status = 'publish';
     if (ap_opt('new_answer_status') == 'moderate' || ap_opt('new_answer_status') == 'point' && ap_get_points($user_id) < ap_opt('new_answer_status')) {
         $status = 'moderate';
     }
     if (isset($this->fields['is_private']) && $this->fields['is_private']) {
         $status = 'private_post';
     }
     $answer_array = array('post_title' => $question->post_title, 'post_author' => $user_id, 'post_content' => apply_filters('ap_form_contents_filter', $fields['description']), 'post_parent' => $question->ID, 'post_type' => 'answer', 'post_status' => $status, 'comment_status' => 'open');
     /**
      * FILTER: ap_pre_insert_answer
      * Can be used to modify args before inserting answer
      * @var array
      * @since 2.0.1
      */
     $answer_array = apply_filters('ap_pre_insert_answer', $answer_array);
     $post_id = wp_insert_post($answer_array);
     if ($post_id) {
         // get existing answer count
         $current_ans = ap_count_published_answers($question->ID);
         if (!is_user_logged_in() && ap_opt('allow_anonymous') && isset($fields['name'])) {
             update_post_meta($post_id, 'anonymous_name', $fields['name']);
         }
         if ($this->is_ajax) {
             if ($current_ans == 1) {
                 global $post;
                 $post = $question;
                 setup_postdata($post);
             } else {
                 global $post;
                 $post = get_post($post_id);
                 setup_postdata($post);
             }
             ob_start();
             global $answers;
             if ($current_ans == 1) {
                 $answers = ap_get_answers(array('question_id' => $question->ID));
                 ap_get_template_part('answers');
             } else {
                 $answers = ap_get_answers(array('p' => $post_id));
                 while (ap_have_answers()) {
                     ap_the_answer();
                     ap_get_template_part('answer');
                 }
             }
             $html = ob_get_clean();
             $count_label = sprintf(_n('1 Answer', '%d Answers', $current_ans, 'ap'), $current_ans);
             $result = array('postid' => $post_id, 'action' => 'new_answer', 'div_id' => '#answer_' . get_the_ID(), 'can_answer' => ap_user_can_answer($post->ID), 'html' => $html, 'message' => 'answer_submitted', 'do' => 'clearForm', 'view' => array('answer_count' => $current_ans, 'answer_count_label' => $count_label));
             $this->result = $result;
         }
     }
     $this->process_image_uploads($post_id, $user_id);
 }
Example #3
0
 /**
  * Handle Ajax callback for permanent delete of post.
  */
 public function permanent_delete_post()
 {
     $post_id = (int) $_POST['post_id'];
     $action = 'delete_post_' . $post_id;
     if (!ap_verify_nonce($action) || !ap_user_can_permanent_delete()) {
         $this->something_wrong();
     }
     $post = get_post($post_id);
     wp_trash_post($post_id);
     if ($post->post_type == 'question') {
         do_action('ap_wp_trash_question', $post_id);
     } else {
         do_action('ap_wp_trash_answer', $post_id);
     }
     wp_delete_post($post_id, true);
     if ($post->post_type == 'question') {
         $this->send(array('action' => 'delete_question', 'do' => array('redirect' => ap_base_page_link()), 'message' => 'question_deleted_permanently'));
     } else {
         $current_ans = ap_count_published_answers($post->post_parent);
         $count_label = sprintf(_n('1 Answer', '%d Answers', $current_ans, 'anspress-question-answer'), $current_ans);
         $remove = !$current_ans ? true : false;
         $this->send(array('action' => 'delete_answer', 'div_id' => '#answer_' . $post_id, 'count' => $current_ans, 'count_label' => $count_label, 'remove' => $remove, 'message' => 'answer_deleted_permanently', 'view' => array('answer_count' => $current_ans, 'answer_count_label' => $count_label)));
     }
 }
Example #4
0
 /**
  * if questions is restored then restore its answers too.
  * @param  int
  * @return void
  * @since 2.0.0
  */
 public function untrash_ans_on_question_untrash($post_id)
 {
     $post = get_post($post_id);
     if ($post->post_type == 'question') {
         do_action('ap_untrash_question', $post->ID);
         //ap_add_parti($post->ID, $post->post_author, 'question');
         $arg = array('post_type' => 'answer', 'post_status' => 'trash', 'post_parent' => $post_id, 'showposts' => -1);
         $ans = get_posts($arg);
         if ($ans > 0) {
             foreach ($ans as $p) {
                 do_action('ap_untrash_answer', $p->ID);
                 //ap_add_parti($p->ID, $p->post_author, 'answer');
                 wp_untrash_post($p->ID);
             }
         }
     }
     if ($post->post_type == 'answer') {
         $ans = ap_count_published_answers($post->post_parent);
         do_action('untrash_answer', $post->ID, $post->post_author);
         //ap_add_parti($post->post_parent, $post->post_author, 'answer');
         //update answer count
         update_post_meta($post->post_parent, ANSPRESS_ANS_META, $ans + 1);
     }
 }