AnsPress_Ajax::close_question()

Description #

Close question callback.

Changelog #

VersionDescription
unknownunknown
4.1.2Introduced.

Source #

File: includes/ajax-hooks.php

	public static function close_question() {
		$post_id = ap_sanitize_unslash( 'post_id', 'p' );

		// Check permission and nonce.
		if ( ! is_user_logged_in() || ! check_ajax_referer( 'close_' . $post_id, 'nonce', false ) || ! ap_user_can_close_question() ) {
			ap_ajax_json(
				array(
					'success'  => false,
					'snackbar' => array( 'message' => __( 'You cannot close a question', 'anspress-question-answer' ) ),
				)
			);
		}

		$_post       = ap_get_post( $post_id );
		$toggle      = ap_toggle_close_question( $post_id );
		$close_label = $_post->closed ? __( 'Close', 'anspress-question-answer' ) : __( 'Open', 'anspress-question-answer' );
		$close_title = $_post->closed ? __( 'Close this question for new answer.', 'anspress-question-answer' ) : __( 'Open this question for new answers', 'anspress-question-answer' );

		$message = 1 === $toggle ? __( 'Question closed', 'anspress-question-answer' ) : __( 'Question is opened', 'anspress-question-answer' );

		// Log in activity table.
		if ( 1 === $toggle ) {
			ap_activity_add(
				array(
					'q_id'   => $_post->ID,
					'action' => 'closed_q',
				)
			);
		} else {
			ap_activity_add(
				array(
					'q_id'   => $_post->ID,
					'action' => 'open_q',
				)
			);
		}

		$results = array(
			'success'     => true,
			'action'      => array(
				'label' => $close_label,
				'title' => $close_title,
			),
			'snackbar'    => array( 'message' => $message ),
			'postmessage' => ap_get_post_status_message( $post_id ),
		);

		ap_ajax_json( $results );
	}

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