ap_answer_form( mixed $question_id, boolean $editing = false )

Description #

Generate answer form.

Parameters #

  • $question_id
    mixed (Required) Question iD.
  • $editing
    boolean (Optional) true if post is being edited. Default value: false

Changelog #

VersionDescription
unknownunknown
4.1.6Fixed: editing answer creates new answer.
4.1.5Don't use ap_ajax as action.
4.1.0Introduced.

Source #

File: includes/functions.php

function ap_answer_form( $question_id, $editing = false ) {
	$editing    = false;
	$editing_id = ap_sanitize_unslash( 'id', 'r' );

	// If post_id is empty then its not editing.
	if ( ! empty( $editing_id ) ) {
		$editing = true;
	}

	if ( $editing && ! ap_user_can_edit_answer( $editing_id ) ) {
		echo '<p>' . esc_attr__( 'You cannot edit this answer.', 'anspress-question-answer' ) . '</p>';
		return;
	}

	if ( ! $editing && ! ap_user_can_answer( $question_id ) ) {
		echo '<p>' . esc_attr__( 'You do not have permission to answer this question.', 'anspress-question-answer' ) . '</p>';
		return;
	}

	$args = array(
		'hidden_fields' => array(
			array(
				'name'  => 'action',
				'value' => 'ap_form_answer',
			),
			array(
				'name'  => 'question_id',
				'value' => (int) $question_id,
			),
		),
	);

	$values         = array();
	$session_values = anspress()->session->get( 'form_answer_' . $question_id );

	// Add value when editing post.
	if ( $editing ) {
		$answer = ap_get_post( $editing_id );

		$form['editing']      = true;
		$form['editing_id']   = $editing_id;
		$form['submit_label'] = __( 'Update Answer', 'anspress-question-answer' );

		$values['post_title']   = $answer->post_title;
		$values['post_content'] = $answer->post_content;
		$values['is_private']   = 'private_post' === $answer->post_status ? true : false;

		if ( isset( $values['anonymous_name'] ) ) {
			$fields = ap_get_post_field( 'fields', $answer );

			$values['anonymous_name'] = ! empty( $fields['anonymous_name'] ) ? $fields['anonymous_name'] : '';
		}

		$args['hidden_fields'][] = array(
			'name'  => 'post_id',
			'value' => (int) $editing_id,
		);
	} elseif ( ! empty( $session_values ) ) {
		// Set last session values if not editing.
		$values = $session_values;
	}

	anspress()->get_form( 'answer' )->set_values( $values )->generate( $args );
}

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