AnsPress_Comment_Hooks::edit_comment()

Description #

Updates comment.

Changelog #

VersionDescription
3.0.0Introduced.

Source #

File: includes/comments.php

	public static function edit_comment() {
		$comment_id = ap_sanitize_unslash( 'comment_ID', 'r' );
		$content = ap_sanitize_unslash( 'content', 'r' );

		if ( ! is_user_logged_in() || ! ap_verify_nonce( 'edit-comment-' . $comment_id ) || ! ap_user_can_edit_comment( $comment_id ) ) {
			ap_ajax_json( array(
				'success'  => false,
				'snackbar' => [ 'message' => __( 'Sorry, you cannot edit this comment', 'anspress-question-answer' ) ],
			) );
		}

		$comment = get_comment( $comment_id );

		// Check if content is changed.
		if ( $content === $comment->comment_content || empty( $content ) ) {
			ap_ajax_json( [
				'success' => false,
				'snackbar' => [ 'message' => __( 'No change detected, edit comment and then try', 'anspress-question-answer' ) ],
			] );
		}

		$updated = wp_update_comment( array(
			'comment_ID'      => $comment_id,
			'comment_content' => $content,
		) );

		if ( $updated ) {
			$c = get_comment( $comment_id );
			$count = get_comment_count( $c->comment_post_ID );
			$result = array(
				'success'       => true,
				'comment'       => ap_comment_ajax_data( $c ),
				'action' 		     => 'edit-comment',
				'commentsCount' => [ 'text' => sprintf( _n( '%d Comment', '%d Comments', $count['all'], 'anspress-question-answer' ), $count['all'] ), 'number' => $count['all'], 'unapproved' => $count['awaiting_moderation'] ],
				'snackbar'      => [ 'message' => __( 'Comment updated successfully', 'anspress-question-answer' ) ],
			);
			ap_ajax_json( $result );
		}

		ap_ajax_json( array(
			'success'  => false,
			'snackbar' => [ 'message' => __( 'Unable to update comment', '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