Skip to content

Instantly share code, notes, and snippets.

@atultiwari
Last active November 29, 2017 04:33
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/4bc9fd36c5fb2a18081762189fd6a347 to your computer and use it in GitHub Desktop.
Save atultiwari/4bc9fd36c5fb2a18081762189fd6a347 to your computer and use it in GitHub Desktop.
<?php
/**
* Process custom question form submission.
* Original work by - Rahul Aryan
* Modified by - Dr. Atul
* @return void
* @since 4.1.0
*/
function at_submit_question_form() {
$editing = false;
$form = anspress()->get_form( 'at_question_form' );
/**
* Action triggered before processing question form.
*
* @since 4.1.0
*/
do_action( 'ap_submit_question_form' );
$values = $form->get_values();
// Check nonce and is valid form.
if ( ! $form->is_submitted() ) {
echo __( 'Holla, you trying to cheat!' );
return;
}
$question_args = array(
'post_title' => $values['post_title']['value'],
'post_content' => $values['post_content']['value'],
);
if ( ! empty( $values['post_id']['value'] ) ) {
$question_args['ID'] = $values['post_id']['value'];
$editing = true;
$_post = ap_get_post( $question_args['ID'] );
// Check if valid post type and user can edit.
if ( 'question' !== $_post->post_type || ! ap_user_can_edit_question( $_post ) ) {
ap_ajax_json( 'something_wrong' );
}
}
// Add default arguments if not editing.
if ( ! $editing ) {
$question_args = wp_parse_args( $question_args, array(
'post_author' => get_current_user_id(),
'post_name' => '',
'comment_status' => 'open',
) );
}
// Post status.
$question_args['post_status'] = ap_new_edit_post_status( false, 'question', $editing );
if ( $form->have_errors() ) {
ap_ajax_json([
'success' => false,
'snackbar' => [ 'message' => __( 'Unable to post question.', 'anspress-question-answer' ) ],
'form_errors' => $form->errors,
'fields_errors' => $form->get_fields_errors(),
] );
}
// Set post parent.
// @TODO: Check nonce for post parent.
if ( isset( $values['post_parent'] ) && $values['post_parent']['value'] ) {
$question_args['post_parent'] = $values['post_parent']['value'];
}
/**
* Filter question description before saving.
*
* @param string $content Post content.
* @since unknown
* @since @3.0.0 Moved from process-form.php
*/
$question_args['post_content'] = apply_filters( 'ap_form_contents_filter', $question_args['post_content'] );
$question_args['post_name'] = ap_remove_stop_words_post_name( $question_args['post_title'] );
if ( $editing ) {
/**
* Can be used to modify `$args` before updating question
*
* @param array $question_args Question arguments.
* @since 2.0.1
* @since 4.1.0 Moved from includes/ask-form.php.
*/
$question_args = apply_filters( 'ap_pre_update_question', $question_args );
} else {
/**
* Can be used to modify args before inserting question
*
* @param array $question_args Question arguments.
* @since 2.0.1
* @since 4.1.0 Moved from includes/ask-form.php.
*/
$question_args = apply_filters( 'ap_pre_insert_question', $question_args );
}
if ( ! $editing ) {
$question_args['post_type'] = 'question';
$post_id = wp_insert_post( $question_args, true );
} else {
$post_id = wp_update_post( $question_args, true );
}
// If error return and send error message.
if ( is_wp_error( $post_id ) ) {
ap_ajax_json([
'success' => false,
'snackbar' => array(
'message' => sprintf(
// Translators: placeholder contain error message.
__( 'Unable to post question. Error: %s', 'anspress-question-answer' ),
$post_id->get_error_message()
),
),
] );
}
$form->after_save( false, array(
'post_id' => $post_id,
) );
// Clear temporary images.
if ( $post_id ) {
ap_clear_unattached_media();
}
if ( isset( $question_args['ID'] ) ) {
$message = __( 'Question updated successfully, you\'ll be redirected in a moment.', 'anspress-question-answer' );
} else {
$message = __( 'Your question is posted successfully, you\'ll be redirected in a moment.', 'anspress-question-answer' );
}
if ( ! is_wp_error( $post_id ) ) {
$form = anspress()->get_form( 'at_question_form' );
$values = $form->get_values();
$qameta = array(
'last_updated' => current_time( 'mysql' ), // When last question was active.
'answers' => ap_count_published_answers( $post_id ), // numbers of answers.
'anonymous_name' => sanitize_text_field( $values['anonymous_name']['value'] ), // Anonymous user name.
'selected' => false, // Answer id if have a selected answer.
'views' => 10000, // Numbers of views.
'closed' => false, // Is question closed?
'comments' => 10, // Numbers of comments.
'featured' => false, // is featured?
'votes_up' => 5, // Numbers of vote up.
'votes_down' => 0, // Numbers of vote down.
'subscribers' => 10, // Numbers of subscribers.
'flags' => 0, // Numbers of flags.
);
ap_insert_qameta( $post_id, $qameta );
$activity_type = 'new_question';
/** Following should have worked, but not working -
ap_activity_add( array(
'q_id' => $post_id,
'action' => $activity_type,
) );
**/
// Following function appears to be deprecated, but it was the only thing working for me -
ap_update_post_activity_meta( $post_id, $activity_type, get_current_user_id() );
echo get_permalink( $post_id );
}
}
add_action( 'init', 'at_submit_question_form' );
/**
* Register a custom form by "at_question_form" name in AnsPress.
*
* @return array
*/
function at_custom_question_form_in_ap() {
$form = array(
'fields' => array(
'post_title' => array(
'label' => __('Demo post title'),
'desc' => __('A TinyMce editor field.'),
'type' => 'editor'
),
'post_content' => array(
'label' => __('Demo post content'),
'desc' => __('A TinyMce editor field.'),
'type' => 'editor'
),
)
);
// I am trying to figure out that how to reset fields value once form is submitted. Following doesnt work -
//$form['fields']['post_content']['value'] = '';
return $form;
}
// ap_form_ followed by form name.
add_filter('ap_form_at_question_form', 'at_custom_question_form_in_ap');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment