AP_Form_Hooks::submit_question_form( boolean $manual = false )
Description #
Process question form submission.
Parameters #
- $manualboolean (Optional) Is form submitted manually. Default value: false
Changelog #
Source #
File: includes/class-form-hooks.php
public static function submit_question_form( $manual = false ) { $editing = false; $form = anspress()->get_form( 'question' ); /** * Action triggered before processing question form. * * @since 4.1.0 */ do_action( 'ap_submit_question_form' ); $values = $form->get_values(); // Store current values in session. $form->save_values_session(); // Check nonce and is valid form. Do not check if `$manual` is true. if ( ! $form->is_submitted() && false === $manual ) { ap_ajax_json( array( 'success' => false, 'snackbar' => array( 'message' => __( 'Trying to cheat?!', 'anspress-question-answer' ) ), ) ); } $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 ( false !== $manual && ( '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() ) { if ( false === $manual ) { ap_ajax_json( array( 'success' => false, 'snackbar' => array( 'message' => __( 'Unable to post question.', 'anspress-question-answer' ) ), 'form_errors' => $form->errors, 'fields_errors' => $form->get_fields_errors(), ) ); } else { return new WP_Error( 'failed', __( 'Failed to insert question', 'anspress-question-answer' ) ); } } // Set post parent. $post_parent = ap_sanitize_unslash( 'post_parent', 'r' ); if ( ! empty( $post_parent ) && wp_verify_nonce( ap_sanitize_unslash( '__nonce_pp', 'r' ), 'post_parent_' . $post_parent ) ) { $question_args['post_parent'] = (int) $post_parent; } // If private override status. if ( isset( $values['is_private']['value'] ) && true === $values['is_private']['value'] ) { $question_args['post_status'] = 'private_post'; } // Create user if enabled. if ( ! $editing && ! is_user_logged_in() && ap_opt( 'create_account' ) ) { self::create_user( $values, $question_args, $manual ); } // Check if duplicate. if ( ! $editing && ap_opt( 'duplicate_check' ) && false !== ap_find_duplicate_post( $question_args['post_content'], 'question' ) ) { $form->add_error( 'duplicate-question', __( 'You are trying to post a duplicate question. Please search existing questions before posting a new one.', 'anspress-question-answer' ) ); if ( false === $manual ) { ap_ajax_json( array( 'success' => false, 'snackbar' => array( 'message' => __( 'Unable to post question.', 'anspress-question-answer' ) ), 'form_errors' => $form->errors, 'fields_errors' => $form->get_fields_errors(), ) ); } else { return new WP_Error( 'failed', __( 'Failed to insert question', 'anspress-question-answer' ) ); } } /** * 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 ) ) { if ( false === $manual ) { ap_ajax_json( array( '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() ), ), ) ); } else { return $post_id; } } $activity_type = $editing ? 'edit_q' : 'new_q'; // Insert activity. ap_activity_add( array( 'q_id' => $post_id, 'action' => $activity_type, ) ); $form->after_save( false, array( 'post_id' => $post_id, ) ); // Clear temporary images. if ( $post_id ) { ap_clear_unattached_media(); } /** * Action called after processing question form. This is triggred * only for question submitted from frontend. * * @param integer $post_id Question id just created /updated. * * @since 4.1.11 */ do_action( 'ap_after_question_form_processed', $post_id ); 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 ( false === $manual ) { anspress()->session->set_question( $post_id ); ap_ajax_json( array( 'success' => true, 'snackbar' => array( 'message' => $message, ), 'redirect' => get_permalink( $post_id ), 'post_id' => $post_id, ) ); } return $post_id; }
Expand full source code Collapse full source code View on GitHub: includes/class-form-hooks.php:318
// Create user if enabled.
if ( ! $editing && ! is_user_logged_in() && ap_opt( ‘create_account’ ) ) {
self::create_user( $values, $question_args, $manual );
}
ap_opt( ‘create_account’ ) where is it activated?