AP_Form_Hooks::submit_comment_form()

Description #

Process comment form.

Changelog #

VersionDescription
4.1.0Introduced.

Source #

File: includes/class-form-hooks.php

	public static function submit_comment_form() {
		global $comment;

		$editing = false;
		$form    = anspress()->get_form( 'comment' );

		/**
		 * Action triggered before processing comment form.
		 *
		 * @since 4.1.0
		 */
		do_action( 'ap_submit_comment_form' );

		$values  = $form->get_values();
		$post_id = ap_sanitize_unslash( 'post_id', 'r' );

		// Check nonce and is valid form.
		if ( ! $form->is_submitted() || ! ap_user_can_comment( $post_id ) ) {
			ap_ajax_json(
				array(
					'success'  => false,
					'snackbar' => array( 'message' => __( 'Trying to cheat?!', 'anspress-question-answer' ) ),
				)
			);
		}

		if ( $form->have_errors() ) {
			ap_ajax_json(
				array(
					'success'       => false,
					'snackbar'      => array( 'message' => __( 'Unable to post comment.', 'anspress-question-answer' ) ),
					'form_errors'   => $form->errors,
					'fields_errors' => $form->get_fields_errors(),
				)
			);
		}

		$comment_id = ap_sanitize_unslash( 'comment_id', 'r' );
		if ( ! empty( $comment_id ) ) {
			$comment = get_comment( $comment_id ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited

			if ( 'anspress' !== $comment->comment_type || ! ap_user_can_edit_comment( $comment_id ) ) {
				ap_ajax_json(
					array(
						'success'  => false,
						'snackbar' => array( 'message' => __( 'You cannot edit this comment.', 'anspress-question-answer' ) ),
					)
				);
			}

			// Check if content is changed.
			if ( $values['content']['value'] === $comment->comment_content ) {
				ap_ajax_json(
					array(
						'success'  => false,
						'snackbar' => array( 'message' => __( 'There is no change in your comment.', 'anspress-question-answer' ) ),
					)
				);
			}

			$updated = wp_update_comment(
				array(
					'comment_ID'      => $comment_id,
					'comment_content' => $values['content']['value'],
				)
			);

			if ( ! is_wp_error( $updated ) ) {
				/**
				 * Fires immediately after a comment is updated in the database.
				 *
				 * @since 4.1.2
				 *
				 * @param int   $comment_id The comment ID.
				 */
				do_action( 'ap_edit_comment', $comment_id );

				$c     = get_comment( $comment_id );
				$count = get_comment_count( $c->comment_post_ID );

				ob_start();
				$comment = $c; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
				ap_get_template_part( 'comment' );
				$html = ob_get_clean();

				$result = array(
					'success'       => true,
					'action'        => 'edit-comment',
					'commentsCount' => array(
						// translators: %d is count of comments.
						'text'       => sprintf( _n( '%d Comment', '%d Comments', $count['all'], 'anspress-question-answer' ), $count['all'] ),
						'number'     => $count['all'],
						'unapproved' => $count['awaiting_moderation'],
					),
					'snackbar'      => array( 'message' => __( 'Comment updated successfully', 'anspress-question-answer' ) ),
					'html'          => $html,
					'post_id'       => $c->comment_post_ID,
					'comment_id'    => $c->comment_ID,
					'hide_modal'    => 'comment',
				);

				ap_ajax_json( $result );
			}

			ap_ajax_json(
				array(
					'success'  => false,
					'snackbar' => array( 'message' => $updated->get_error_message() ),
				)
			);
		}

		$_post = ap_get_post( $post_id );

		$type = 'question' === $_post->post_type ? __( 'question', 'anspress-question-answer' ) : __( 'answer', 'anspress-question-answer' );

		// Check if not restricted post type.
		if ( in_array( $_post->post_status, array( 'draft', 'pending', 'trash' ), true ) ) {
			ap_ajax_json(
				array(
					'success'  => false,
					'snackbar' => array(
						'message' => sprintf(
							// Translators: %s contain post type name.
							__( 'Commenting is not allowed on draft, pending or deleted %s', 'anspress-question-answer' ),
							$type
						),
					),
				)
			);
		}

		if ( is_user_logged_in() ) {
			$user    = wp_get_current_user();
			$user_id = $user->ID;
			$author  = wp_slash( $user->display_name );
			$email   = wp_slash( $user->user_email );
			$url     = wp_slash( $user->user_url );
		} else {
			$user_id = 0;
			$author  = $values['author']['value'];
			$email   = $values['email']['value'];
			$url     = $values['url']['value'];
		}

		$commentdata = array(
			'comment_post_ID'      => $_post->ID,
			'comment_author'       => wp_slash( $author ),
			'comment_author_email' => wp_slash( $email ),
			'comment_author_url'   => wp_slash( $url ),
			'comment_content'      => trim( $values['content']['value'] ),
			'comment_type'         => 'anspress',
			'comment_parent'       => 0,
			'user_id'              => $user_id,
		);

		/**
		 * Filter comment content before inserting to DB.
		 *
		 * @param bool      $apply_filter  Apply this filter.
		 * @param string    $content           Un-filtered comment content.
		 * @since 3.0.0
		 */
		$commentdata = apply_filters( 'ap_pre_insert_comment', $commentdata );

		// Insert new comment and get the comment ID.
		$comment_id = wp_new_comment( $commentdata, true );

		if ( ! is_wp_error( $comment_id ) && false !== $comment_id ) {
			$c = get_comment( $comment_id );
			do_action( 'ap_after_new_comment', $c );

			$count = get_comment_count( $c->comment_post_ID );

			ob_start();
			ap_the_comments( $c->comment_post_ID );
			$html = ob_get_clean();

			$result = array(
				'success'       => true,
				'action'        => 'new-comment',
				'commentsCount' => array(
					'text'       => sprintf(
						// Translators: %d contains count of comments.
						_n( '%d Comment', '%d Comments', $count['all'], 'anspress-question-answer' ),
						$count['all']
					),
					'number'     => $count['all'],
					'unapproved' => $count['awaiting_moderation'],
				),
				'snackbar'      => array( 'message' => __( 'Comment successfully posted', 'anspress-question-answer' ) ),
				'html'          => $html,
				'post_id'       => $c->comment_post_ID,
				'comment_id'    => $c->comment_ID,
				'hide_modal'    => 'comment',
			);

			ap_ajax_json( $result );
		}

		// Lastly output error message.
		ap_ajax_json(
			array(
				'success'  => false,
				'snackbar' => array( 'message' => $comment_id->get_error_message() ),
			)
		);
	}

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