AnsPress_Ajax::toggle_featured()

Description #

Handle toggle featured question ajax callback

Changelog #

VersionDescription
unknownunknown
4.1.2Introduced.

Source #

File: includes/ajax-hooks.php

	public static function toggle_featured() {
		$post_id = (int) ap_sanitize_unslash( 'post_id', 'request' );

		if ( ! ap_user_can_toggle_featured() || ! anspress_verify_nonce( 'set_featured_' . $post_id ) ) {
			ap_ajax_json(
				array(
					'success'  => false,
					'snackbar' => array( 'message' => __( 'Sorry, you cannot toggle a featured question', 'anspress-question-answer' ) ),
				)
			);
		}

		$post = ap_get_post( $post_id );

		// Do nothing if post type is not question.
		if ( 'question' !== $post->post_type ) {
			ap_ajax_json(
				array(
					'success'  => false,
					'snackbar' => array( 'message' => __( 'Only question can be set as featured', 'anspress-question-answer' ) ),
				)
			);
		}

		// Check if current question ID is in featured question array.
		if ( ap_is_featured_question( $post ) ) {
			ap_unset_featured_question( $post->ID );

			// Update activity.
			ap_activity_add(
				array(
					'q_id'   => $post->ID,
					'action' => 'unfeatured',
				)
			);

			ap_ajax_json(
				array(
					'success'  => true,
					'action'   => array(
						'active' => false,
						'title'  => __( 'Mark this question as featured', 'anspress-question-answer' ),
						'label'  => __( 'Feature', 'anspress-question-answer' ),
					),
					'snackbar' => array( 'message' => __( 'Question is unmarked as featured.', 'anspress-question-answer' ) ),
				)
			);
		}

		ap_set_featured_question( $post->ID );

		// Update activity.
		ap_activity_add(
			array(
				'q_id'   => $post->ID,
				'action' => 'featured',
			)
		);

		ap_ajax_json(
			array(
				'success'  => true,
				'action'   => array(
					'active' => true,
					'title'  => __( 'Unmark this question as featured', 'anspress-question-answer' ),
					'label'  => __( 'Unfeature', 'anspress-question-answer' ),
				),
				'snackbar' => array( 'message' => __( 'Question is marked as featured.', '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