AnsPress_Process_Form::process_answer_form()
Description #
Process answer form
Source #
File: includes/process-form.php
public function process_answer_form() {
$question_id = (int) ap_sanitize_unslash( 'form_question_id', 'r' );
// Do security check, if fails then return.
if ( ! ap_user_can_answer( $question_id ) || ! ap_verify_nonce( 'nonce_answer_' . $question_id ) ) {
ap_ajax_json( array(
'success' => false,
'snackbar' => [ 'message' => __( 'Sorry, you cannot asnwer on this question', 'anspress-question-answer' ) ],
) );
}
global $ap_errors, $validate;
$question = ap_get_post( $question_id );
$editing_post_id = ap_isset_post_value( 'edit_post_id', false );
/**
* 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', ap_get_answer_form_fields( $question->ID, $editing_post_id ) );
$validate = new AnsPress_Validation( $args );
/**
* ACTION
* ap_process_ask_form
*
* @since 4.0.0
*/
do_action( 'ap_process_answer_form', $validate );
// Bail if there is error in validating form.
ap_form_validation_error_response( $validate );
$fields = $validate->get_sanitized_fields();
$this->fields = $fields;
if ( ! empty( $fields['edit_post_id'] ) ) {
$this->edit_answer( $question );
return;
}
// Check if duplicate.
if ( false !== ap_find_duplicate_post( $fields['description'], 'answer', $question->ID ) ) {
ap_ajax_json( array(
'success' => false,
'form' => ap_sanitize_unslash( 'ap_form_action' ),
'snackbar' => [ 'message' => __( 'This seems to be a duplicate answer. An answer with same content already exists.', 'anspress-question-answer' ) ],
) );
}
$filter = apply_filters( 'ap_before_inserting_answer', false, $fields['description'] );
if ( true === $filter || is_array( $filter ) ) {
if ( is_array( $filter ) ) {
$this->result = $filter;
}
return;
}
$user_id = get_current_user_id();
$answer_array = array(
'post_author' => $user_id,
'post_content' => $fields['description'],
'attach_uploads' => true,
);
$answer_array['post_status'] = ap_new_edit_post_status( $user_id, 'answer', false );
if ( isset( $this->fields['is_private'] ) && $this->fields['is_private'] ) {
$answer_array['is_private'] = true;
}
// Check if anonymous post and have name.
if ( ! is_user_logged_in() && ap_opt( 'allow_anonymous' ) && ! empty( $fields['anonymous_name'] ) ) {
$answer_array['anonymous_name'] = $fields['anonymous_name'];
}
$answer_id = ap_save_answer( $question->ID, $answer_array );
if ( $answer_id ) {
ap_clear_unattached_media();
ap_answer_post_ajax_response( $question->ID, $answer_id );
}
}
Expand full source code Collapse full source code View on GitHub: includes/process-form.php:282
Add your comment