ap_answer_post_ajax_response( integer|object $question_id, integer|object $answer_id )

Description #

Send ajax response after posting an answer.

Parameters #

  • $question_id
    integer | object (Required) Question ID or object.
  • $answer_id
    integer | object (Required) Answer ID or object.

Changelog #

VersionDescription
4.1.0Introduced.

Source #

File: includes/functions.php

function ap_answer_post_ajax_response( $question_id, $answer_id ) {
	$question = ap_get_post( $question_id );
	// Get existing answer count.
	$current_ans = ap_count_published_answers( $question_id );

	global $post;
	$post = ap_get_post( $answer_id ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
	setup_postdata( $post );

	ob_start();
	global $withcomments;
	$withcomments = true; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited

	ap_get_template_part( 'answer' );

	$html = ob_get_clean();

	// translators: %d is answer count.
	$count_label = sprintf( _n( '%d Answer', '%d Answers', $current_ans, 'anspress-question-answer' ), $current_ans );

	$result = array(
		'success'      => true,
		'ID'           => $answer_id,
		'form'         => 'answer',
		'div_id'       => '#post-' . get_the_ID(),
		'can_answer'   => ap_user_can_answer( $post->ID ),
		'html'         => $html,
		'snackbar'     => array( 'message' => __( 'Answer submitted successfully', 'anspress-question-answer' ) ),
		'answersCount' => array(
			'text'   => $count_label,
			'number' => $current_ans,
		),
	);

	ap_ajax_json( $result );
}

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