AnsPress_Flag::action_flag()

Description #

Ajax callback to process post flag button

Changelog #

VersionDescription
2.0.0Introduced.

Source #

File: includes/flag.php

	public static function action_flag() {
		$post_id = (int) ap_sanitize_unslash( 'post_id', 'r' );

		if ( ! anspress_verify_nonce( 'flag_' . $post_id ) || ! is_user_logged_in() ) {
			ap_ajax_json( 'something_wrong' );
		}

		$post       = ap_get_post( $post_id );
		$userid     = get_current_user_id();
		$is_flagged = ap_is_user_flagged( $post_id );

		// Die if already flagged.
		if ( $is_flagged ) {
			ap_ajax_json(
				array(
					'success'  => false,
					'snackbar' => array(
						'message' => sprintf(
							/* Translators: %s Question or Answer post type label for already reported question or answer. */
							__( 'You have already reported this %s.', 'anspress-question-answer' ),
							( 'question' === $post->post_type ) ? esc_html__( 'question', 'anspress-question-answer' ) : esc_html__( 'answer', 'anspress-question-answer' )
						),
					),
				)
			);
		}

		ap_add_flag( $post_id );
		$count = ap_update_flags_count( $post_id );

		ap_ajax_json(
			array(
				'success'  => true,
				'action'   => array(
					'count'  => $count,
					'active' => true,
				),
				'snackbar' => array(
					'message' => sprintf(
						/* Translators: %s Question or Answer post type label for reported question or answer. */
						__( 'Thank you for reporting this %s.', 'anspress-question-answer' ),
						( 'question' === $post->post_type ) ? esc_html__( 'question', 'anspress-question-answer' ) : esc_html__( 'answer', '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