AnsPress_Post_Status::change_post_status()

Description #

Handle change post status ajax request.

Changelog #

VersionDescription
2.1Introduced.

Source #

File: includes/post-status.php

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

		// Check if user has permission else die.
		if ( ! is_user_logged_in() || ! in_array( $status, array( 'publish', 'moderate', 'private_post', 'trash' ), true ) || ! anspress_verify_nonce( 'change-status-' . $status . '-' . $post_id ) || ! ap_user_can_change_status( $post_id ) ) {
			ap_ajax_json(
				array(
					'success'  => false,
					'snackbar' => array( 'message' => __( 'You are not allowed to change post status', 'anspress-question-answer' ) ),
				)
			);
		}

		$post                       = ap_get_post( $post_id );
		$update_data                = array();
		$update_data['post_status'] = $status;
		$update_data['ID']          = $post->ID;

		wp_update_post( $update_data );

		// Unselect as best answer if moderate.
		if ( 'answer' === $post->post_type && 'moderate' === $status && ap_have_answer_selected( $post->post_parent ) ) {
			ap_unset_selected_answer( $post->ID );
		}

		do_action( 'ap_post_status_updated', $post->ID );

		$activity_type = 'moderate' === $post->post_status ? 'approved_' . $post->post_type : 'changed_status';
		ap_update_post_activity_meta( $post_id, $activity_type, get_current_user_id() );

		ap_ajax_json(
			array(
				'success'     => true,
				'snackbar'    => array(
					'message' => sprintf(
						/* Translators: %s Question or Answer post type label for post status change */
						__( '%s status updated successfully', 'anspress-question-answer' ),
						( 'question' === $post->post_type ) ? esc_html__( 'Question', 'anspress-question-answer' ) : esc_html__( 'Answer', 'anspress-question-answer' )
					),
				),
				'action'      => array( 'active' => true ),
				'postmessage' => ap_get_post_status_message( $post->ID ),
				'newStatus'   => $status,
			)
		);
	}

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