AnsPress_Process_Form::process_ask_form()

Description #

Process ask form.

Changelog #

VersionDescription
2.0.1Introduced.

Source #

File: includes/process-form.php

	public function process_ask_form() {
		// Do security check, if fails then return.
		if ( check_ajax_referer( 'ask_form', false, false ) || ! ap_user_can_ask() ) {
			ap_ajax_json( array(
				'success' => false,
				'snackbar' => [ 'message' => __( 'Sorry, unable to post new question', 'anspress-question-answer' ) ],
			) );
		}

		global $ap_errors, $validate;
		$editing_post_id = ap_isset_post_value( 'edit_post_id', false );

		/**
		 * FILTER: ap_ask_fields_validation
		 * Filter can be used to modify ask question fields.
		 * @param array $args Ask form validation arguments.
		 * @since 2.0.1
		 */
		$args = apply_filters( 'ap_ask_fields_validation', ap_get_ask_form_fields( $editing_post_id ) );

		$validate = new AnsPress_Validation( $args );

		/**
		 * ACTION
		 * ap_process_ask_form
		 *
		 * @since 4.0.0
		 */
		do_action( 'ap_process_ask_form', $validate );

		// If error in form then bail.
		ap_form_validation_error_response( $validate );
		$fields = $validate->get_sanitized_fields();
		$this->fields = $fields;

		if ( ! empty( $fields['edit_post_id'] ) ) {
			$this->edit_question();
			return;
		}

		// Check if duplicate.
		if ( false !== ap_find_duplicate_post( $fields['description'], 'question' ) ) {
			ap_ajax_json( array(
				'success' 	=> false,
				'form' 	 	=> $_POST['ap_form_action'],
				'snackbar' 	=> [ 'message' => __( 'This seems to be a duplicate question. A question with same content already exists.', 'anspress-question-answer' ) ],
			) );
		}

		$filter = apply_filters( 'ap_before_inserting_question', false, $fields['description'] );
		if ( true === $filter || is_array( $filter ) ) {
			if ( is_array( $filter ) ) {
				$this->result = $filter;
			}
			return;
		}

		$user_id = get_current_user_id();

		$question_array = array(
			'post_title'		=> $fields['title'],
			'post_author'		=> $user_id,
			'post_content' 		=> $fields['description'],
			'attach_uploads' 	=> true,
		);

		$question_array['post_status'] = ap_new_edit_post_status( $user_id, 'question', false );

		if ( isset( $this->fields['is_private'] ) && $this->fields['is_private'] ) {
			$question_array['is_private'] = true;
		}

		// Check if anonymous post and have name.
		if ( ! is_user_logged_in() && ap_opt( 'allow_anonymous' ) && ! empty( $fields['anonymous_name'] ) ) {
			$question_array['anonymous_name'] = $fields['anonymous_name'];
		}

		if ( isset( $fields['parent_id'] ) ) {
			$question_array['post_parent'] = (int) $fields['parent_id'];
		}

		$post_id = ap_save_question( $question_array, true );

		if ( $post_id ) {
			ap_clear_unattached_media();

			ap_ajax_json( array(
				'success'  => true,
				'action'   => 'new_question',
				'redirect' => get_permalink( $post_id ),
				'snackbar' => [
					'message' => __( 'Question posted successfully. Redirecting to question', 'anspress-question-answer' ),
				],
			) );
		}
	}

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Add your comment