Skip to content

Instantly share code, notes, and snippets.

@atultiwari
Last active December 18, 2017 11:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save atultiwari/53471873ac622b2cf039efebf897c2b5 to your computer and use it in GitHub Desktop.
Save atultiwari/53471873ac622b2cf039efebf897c2b5 to your computer and use it in GitHub Desktop.
<?php
/**
* Function orignally created by Rahul Aryan,
* Modified by - Dr. Atul
**/
function at_submit_answer_form( $manual = false, $question_post_id) {
$editing = false;
$question_id = $question_post_id;
@$form = anspress()->get_form( 'answer' );
/**
* Action triggered before processing answer form.
*
* @since 4.1.0
*/
do_action( 'ap_submit_answer_form' );
$values = $form->get_values();
if ( false === $manual && ( ! $form->is_submitted() || ! ap_user_can_answer( $question_id ) ) ) {
ap_ajax_json( [
'success' => false,
'snackbar' => [ 'message' => __( 'Trying to cheat?!', 'anspress-question-answer' ) ],
] );
}
$answer_args = array(
'post_title' => $question_id,
'post_name' => $question_id,
'post_content' => $values['post_content']['value'],
'post_parent' => $question_id,
);
if ( ! empty( $values['post_id']['value'] ) ) {
$answer_args['ID'] = $values['post_id']['value'];
$editing = true;
$_post = ap_get_post( $answer_args['ID'] );
// Check if valid post type and user can edit.
if ( 'answer' !== $_post->post_type || ! ap_user_can_edit_answer( $_post ) ) {
ap_ajax_json( 'something_wrong' );
}
}
// Add default arguments if not editing.
if ( ! $editing ) {
$answer_args = wp_parse_args( $answer_args, array(
'post_author' => get_current_user_id(),
'post_name' => '',
'comment_status' => 'open',
) );
}
// Post status.
$answer_args['post_status'] = ap_new_edit_post_status( false, 'answer', $editing );
// If private override status.
if ( true === $values['is_private']['value'] ) {
$answer_args['post_status'] = 'private_post';
}
/**
* Filter question description before saving.
*
* @param string $content Post content.
* @since unknown
* @since @3.0.0 Moved from process-form.php
*/
$answer_args['post_content'] = apply_filters( 'ap_form_contents_filter', $answer_args['post_content'] );
$answer_args['post_name'] = ap_remove_stop_words_post_name( $answer_args['post_title'] );
if ( $editing ) {
/**
* Can be used to modify `$args` before updating answer
*
* @param array $answer_args Answer arguments.
* @since 2.0.1
* @since 4.1.0 Moved from includes/answer-form.php.
*/
$answer_args = apply_filters( 'ap_pre_update_answer', $answer_args );
} else {
/**
* Can be used to modify args before inserting answer
*
* @param array $answer_args Answer arguments.
* @since 2.0.1
* @since 4.1.0 Moved from includes/answer-form.php.
*/
$answer_args = apply_filters( 'ap_pre_insert_answer', $answer_args );
}
if ( ! $editing ) {
$answer_args['post_type'] = 'answer';
$post_id = wp_insert_post( $answer_args, true );
} else {
$post_id = wp_update_post( $answer_args, true );
}
// If error return and send error message.
if ( is_wp_error( $post_id ) ) {
if ( false === $manual ) {
ap_ajax_json([
'success' => false,
'snackbar' => array(
'message' => sprintf(
// Translators: placeholder contain error message.
__( 'Unable to post answer. Error: %s', 'anspress-question-answer' ),
$post_id->get_error_message()
),
),
] );
} else {
return $post_id;
}
}
if ( isset( $answer_args['ID'] ) ) {
$message = __( 'Answer updated successfully. Redirecting you to question page.', 'anspress-question-answer' );
} else {
$message = __( 'Your answer is posted successfully.', 'anspress-question-answer' );
}
if ( false === $manual ) {
ap_ajax_json( array(
'success' => true,
'snackbar' => [
'message' => $message,
],
'redirect' => get_permalink( $question_id ),
'post_id' => $post_id,
) );
}
return $post_id;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment