Toggle_Best_Answer::logged_in()

Description #

Handle ajax for logged in users.

Source #

File: ajax/toggle-best-answer.php

	public function logged_in() {
		$_post = ap_get_post( $this->req( 'answer_id' ) );

		// Unselect best answer if already selected.
		if ( ap_have_answer_selected( $_post->post_parent ) ) {
			ap_unset_selected_answer( $_post->post_parent );
			$this->set_success();
			$this->add_res( 'selected', false );
			$this->add_res( 'label', __( 'Select', 'anspress-question-answer' ) );
			$this->snackbar( __( 'Best answer is unselected for your question.', 'anspress-question-answer' ) );

			$this->send();
		}

		// Do not allow answer to be selected as best if status is moderate.
		if ( in_array( $_post->post_status, array( 'moderate', 'trash', 'private' ), true ) ) {
			$this->set_fail();
			$this->snackbar( __( 'This answer cannot be selected as best, update status to select as best answer.', 'anspress-question-answer' ) );

			$this->send();
		}

		// Update question qameta.
		ap_set_selected_answer( $_post->post_parent, $_post->ID );

		$this->set_success();
		$this->add_res( 'selected', true );
		$this->add_res( 'label', __( 'Unselect', 'anspress-question-answer' ) );
		$this->snackbar( __( 'Best answer is selected for your 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