AnsPress_Process_Form::edit_answer( object $question )

Description #

Process edit answer form.

Parameters #

  • $question
    object (Required) Parent question object.

Source #

File: includes/process-form.php

	public function edit_answer( $question ) {
		global $ap_errors, $validate;

		// Return if user do not have permission to edit this answer.
		if ( ! ap_user_can_edit_answer( $this->fields['edit_post_id'] ) ) {
			ap_ajax_json( array(
				'success'  => false,
				'snackbar' => [ 'message' => __( 'Sorry, you cannot edit this answer', 'anspress-question-answer' ) ],
			) );
		}

		$filter = apply_filters( 'ap_before_updating_answer', false, $this->fields['description'] );
		if ( true === $filter || is_array( $filter ) ) {
			if ( is_array( $filter ) ) {
				$this->result = $filter;
			}
			return;
		}

		$answer = ap_get_post( $this->fields['edit_post_id'] );
		$answer_array = array(
			'ID'				        => $this->fields['edit_post_id'],
			'post_author'		    => $answer->post_author,
			'post_content' 		  => $this->fields['description'],
			'attach_uploads' 	  => true,
		);

		$answer_array['post_status'] = ap_new_edit_post_status( get_current_user_id(), 'answer', true );

		if ( isset( $this->fields['is_private'] ) && $this->fields['is_private'] ) {
			$answer_array['is_private'] = true;
		}

		$answer_id = ap_save_answer( $question->ID, $answer_array );

		if ( $answer_id ) {
			ap_clear_unattached_media();

			// Trigger update hook.
			ap_trigger_qa_update_hook( $answer_id, 'edited' );

			ap_ajax_json( array(
				'success'  => true,
				'action'   => 'answer_edited',
				'snackbar' => [ 'message' => __( 'Answer updated successfully', 'anspress-question-answer' ) ],
				'redirect' => get_permalink( $answer->post_parent ),
			));
		}
	}

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