/** * Generate answer form * @param integer $question_id Question iD. * @param boolean $editing true if post is being edited. * @return void */ function ap_answer_form($question_id, $editing = false) { if (!ap_user_can_answer($question_id) && !$editing) { return; } global $editing_post; $is_private = isset($_POST['is_private']) ? (bool) $_POST['is_private'] : false; if ($editing) { $is_private = $editing_post->post_status == 'private_post' ? true : false; } $args = array('name' => 'answer_form', 'is_ajaxified' => true, 'submit_button' => $editing ? __('Update answer', 'anspress-question-answer') : __('Post answer', 'anspress-question-answer'), 'nonce_name' => 'nonce_answer_' . $question_id, 'fields' => array(array('name' => 'description', 'type' => 'editor', 'value' => $editing ? apply_filters('the_content', $editing_post->post_content) : wp_kses_post(@$_POST['description']), 'settings' => apply_filters('ap_answer_form_editor_settings', array('textarea_rows' => 8, 'tinymce' => ap_opt('answer_text_editor') ? false : true, 'quicktags' => ap_opt('answer_text_editor') ? true : false, 'media_buttons' => false)), 'placeholder' => __('Your answer..', 'anspress-question-answer')), array('name' => 'form_question_id', 'type' => 'hidden', 'value' => $editing ? $editing_post->post_parent : $question_id, 'order' => 20))); if (!is_user_logged_in() && ap_opt('allow_anonymous')) { $args['fields'][] = array('name' => 'name', 'label' => __('Name', 'anspress-question-answer'), 'type' => 'text', 'placeholder' => __('Enter your name to display', 'anspress-question-answer'), 'value' => sanitize_text_field(@$_POST['name']), 'order' => 12); } // If private posts is allowed then show the checkbox. if (ap_opt('allow_private_posts')) { $args['fields'][] = array('name' => 'is_private', 'type' => 'checkbox', 'desc' => __('Only visible to admin and moderator.', 'anspress-question-answer'), 'value' => $is_private, 'order' => 12, 'show_desc_tip' => false); } if (ap_show_captcha_to_user()) { // Show recpatcha if key exists and enabled. if (ap_opt('recaptcha_site_key') == '') { $reCaptcha_html = '<div class="ap-notice red">' . __('reCaptach keys missing, please add keys', 'anspress-question-answer') . '</div>'; } else { $reCaptcha_html = '<div class="g-recaptcha" id="recaptcha" data-sitekey="' . ap_opt('recaptcha_site_key') . '"></div>'; $reCaptcha_html .= '<script type="text/javascript" src="https://www.google.com/recaptcha/api.js?hl=' . get_locale() . '&onload=onloadCallback&render=explicit" async defer></script>'; $reCaptcha_html .= '<script type="text/javascript">'; $reCaptcha_html .= 'var onloadCallback = function() {'; $reCaptcha_html .= 'widgetId1 = grecaptcha.render("recaptcha", {'; $reCaptcha_html .= '"sitekey" : "' . ap_opt('recaptcha_site_key') . '"'; $reCaptcha_html .= '});'; $reCaptcha_html .= '};</script>'; } $args['fields'][] = array('name' => 'captcha', 'type' => 'custom', 'order' => 100, 'html' => $reCaptcha_html); } $args['fields'][] = array('name' => 'ap_upload', 'type' => 'custom', 'html' => ap_post_upload_form(), 'order' => 11); /** * FILTER: ap_ask_form_fields * Filter for modifying $args * @var array * @since 2.0 */ $args = apply_filters('ap_answer_form_fields', $args, $editing); if ($editing) { $args['fields'][] = array('name' => 'edit_post_id', 'type' => 'hidden', 'value' => $editing_post->ID, 'order' => 20); } anspress()->form = new AnsPress_Form($args); echo anspress()->form->get_form(); // Post image upload form. echo ap_post_upload_hidden_form(); }
public function process_answer_form() { if (!is_user_logged_in()) { return false; } if (isset($_POST['is_answer']) && isset($_POST['submitted']) && isset($_POST['nonce']) && wp_verify_nonce($_POST['nonce'], 'post_nonce_' . $_POST['form_question_id'])) { $fields = $this->get_answer_fields_to_process(); $validate = $this->validate_ans_form(); if ($validate['has_error']) { if ($_POST['action'] == 'ap_submit_answer') { $result = array('action' => 'validation_falied', 'message' => __('Answer not submitted, please check the form fields.', 'ap'), 'error' => $validate); return json_encode($result); } return; } if (!isset($fields['question_id']) && !is_int($fields['question_id']) && 'question' !== get_post_type($fields['question_id'])) { return; } $question = get_post($fields['question_id']); if (!ap_user_can_answer($question->ID)) { return; } do_action('process_answer_form'); $logged_in = false; $user_id = get_current_user_id(); $ans_array = array('post_author' => $user_id, 'post_content' => wp_kses($fields['post_content'], ap_form_allowed_tags()), 'post_type' => 'answer', 'post_status' => 'publish', 'post_parent' => $question->ID); $post_id = wp_insert_post($ans_array); if ($post_id) { // set default value for meta update_post_meta($post_id, ANSPRESS_VOTE_META, '0'); // set updated meta for sorting purpose update_post_meta($question->ID, ANSPRESS_UPDATED_META, current_time('mysql')); ap_add_parti($question->ID, $user_id, 'answer'); // get existing answer count $current_ans = ap_count_ans($question->ID); //update answer count update_post_meta($question->ID, ANSPRESS_ANS_META, $current_ans); update_post_meta($post_id, ANSPRESS_BEST_META, 0); // redirect if just logged in if ($logged_in && $_POST['action'] != 'ap_submit_answer') { wp_redirect(get_permalink($question->ID)); exit; } $result = array(); if ($_POST['action'] == 'ap_submit_answer') { if ($current_ans == 1) { global $post; $post = $question; setup_postdata($post); } else { global $post; $post = get_post($post_id); setup_postdata($post); } ob_start(); if ($current_ans == 1) { ap_answers_list($post->ID, 'voted'); } else { include ap_get_theme_location('answer.php'); } $html = ob_get_clean(); $count_label = sprintf(_n('1 Answer', '%d Answers', $current_ans, 'ap'), $current_ans); $result = apply_filters('ap_ajax_answer_submit_result', array('postid' => $post_id, 'action' => 'new_answer', 'div_id' => '#answer_' . get_the_ID(), 'count' => $current_ans, 'count_label' => $count_label, 'can_answer' => ap_user_can_answer($post->ID), 'html' => $html, 'message' => __('Answer submitted successfully!', 'ap'))); if ($logged_in) { $result['redirect_to'] = get_permalink($post->ID); } } do_action('ap_after_inserting_answer', $post_id); ap_do_event('new_answer', $post_id, $user_id, $question->ID, $result); if ($_POST['action'] == 'ap_submit_answer') { return json_encode($result); } } } elseif ($_POST['action'] == 'ap_submit_answer') { $result = array('postid' => $post_id, 'action' => false, 'message' => __('Please try again, answer submission failed!', 'ap')); return json_encode($result); } }
/** * 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); }
</div> <?php comments_template(); ?> </div> </div> </div> <?php if (ap_have_ans(get_the_ID())) { ap_answers_list(get_the_ID(), 'voted'); } ?> <?php if (ap_user_can_answer(get_question_id())) { include ap_get_theme_location('answer-form.php'); } ?> </div> </div> <div class="ap-question-right"> <div class="ap-question-right-inner"> <?php ap_question_side_tab(get_question_id()); ?> <!-- Start labels --> <div class="ap-question-side"> <h3 class="ap-question-side-title">