AP_Form_Hooks::submit_answer_form( boolean $manual = false )

Description #

Process question form submission.

Parameters #

  • $manual
    boolean (Optional) Is form submitted manually. Default value: false

Changelog #

VersionDescription
4.1.5Added new argument $manual for allowing form to be submitted manually.
4.1.0Introduced.

Source #

File: includes/class-form-hooks.php

	public static function submit_answer_form( $manual = false ) {
		$editing     = false;
		$question_id = ap_sanitize_unslash( 'question_id', 'r' );
		$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();
		// Store current values in session.
		$form->save_values_session( $question_id );

		// Check nonce and is valid form.
		if ( false === $manual && ( ! $form->is_submitted() || ! ap_user_can_answer( $question_id ) ) ) {
			ap_ajax_json(
				array(
					'success'  => false,
					'snackbar' => array( '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 ( $form->have_errors() ) {
			if ( false === $manual ) {
				ap_ajax_json(
					array(
						'success'       => false,
						'snackbar'      => array( 'message' => __( 'Unable to post answer.', 'anspress-question-answer' ) ),
						'form_errors'   => $form->errors,
						'fields_errors' => $form->get_fields_errors(),
					)
				);
			} else {
				return new WP_Error( 'failed', __( 'Please check field', 'anspress-question-answer' ) );
			}
		}

		// If private override status.
		if ( isset( $values['is_private']['value'] ) && true === $values['is_private']['value'] ) {
			$answer_args['post_status'] = 'private_post';
		}

		// Create user if enabled.
		if ( ! $editing && ! is_user_logged_in() && ap_opt( 'create_account' ) ) {
			self::create_user( $values, $answer_args, $manual );
		}

		/**
		 * 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(
					array(
						'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;
			}
		}

		$post = ap_get_post( $post_id );

		$form->delete_values_session( $question_id );

		$activity_type = $editing ? 'edit_a' : 'new_a';

		// Insert activity.
		ap_activity_add(
			array(
				'q_id'   => $post->post_parent,
				'a_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 answer form. This is triggered
		 * only for answer submitted from frontend.
		 *
		 * @param integer $post_id Answer id just created /updated.
		 *
		 * @since 4.1.11
		 */
		do_action( 'ap_after_answer_form_processed', $post_id );

		if ( ! $editing ) {
			anspress()->session->set_answer( $post_id );
			ap_answer_post_ajax_response( $question_id, $post_id );
		}

		if ( $editing ) {
			$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' => array(
						'message' => $message,
					),
					'redirect' => get_permalink( $question_id ),
					'post_id'  => $post_id,
				)
			);
		}

		return $post_id;
	}

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