AnsPress_Comment_Hooks()

Description #

Comments class

Source #

File: includes/comments.php

class AnsPress_Comment_Hooks {

	/**
	 * Filter comments array to include only comments which user can read.
	 *
	 * @param array $comments Comments.
	 * @return array
	 * @since 4.1.0
	 */
	public static function the_comments( $comments ) {
		foreach ( $comments as $k => $c ) {
			if ( 'anspress' === $c->comment_type && ! ap_user_can_read_comment( $c ) ) {
				unset( $comments[ $k ] );
			}
		}

		return $comments;
	}

	/**
	 * Ajax callback for loading comments.
	 *
	 * @since 2.0.1
	 * @since 3.0.0 Moved from AnsPress_Ajax class.
	 *
	 * @category haveTest
	 */
	public static function load_comments() {
		global $avatar_size;
		$paged      = 1;
		$comment_id = ap_sanitize_unslash( 'comment_id', 'r' );

		if ( ! empty( $comment_id ) ) {
			$_comment = get_comment( $comment_id );
			if ( isset( $_comment->comment_post_ID ) && $_comment->comment_post_ID ) {
				$post_id = $_comment->comment_post_ID;
			}
		} else {
			$post_id = ap_sanitize_unslash( 'post_id', 'r' );
			$paged   = max( 1, ap_isset_post_value( 'paged', 1 ) );
		}

		if ( isset( $post_id ) && $post_id ) {
			$_post = ap_get_post( $post_id );
		}

		$args = array(
			'show_more' => false,
		);

		if ( ! empty( $_comment ) ) {
			$avatar_size         = 60;
			$args['comment__in'] = $_comment->comment_ID;
		}

		ob_start();
		if ( isset( $post_id ) && $post_id ) {
			ap_the_comments( $post_id, $args );
		} else {
			ap_the_comments( -1, $args );
		}
		$html = ob_get_clean();

		if ( isset( $_post ) && $_post->post_type ) {
			$type = 'question' === $_post->post_type ? __( 'Question', 'anspress-question-answer' ) : __( 'Answer', 'anspress-question-answer' );
		}

		$result = array(
			'success' => true,
			'html'    => $html,
		);

		ap_ajax_json( $result );
	}

	/**
	 * Modify comment query args for showing pending comments to moderator.
	 *
	 * @param  array $args Comment args.
	 * @return array
	 * @since  3.0.0
	 */
	public static function comments_template_query_args( $args ) {
		global $question_rendered;

		if ( true === $question_rendered && is_singular( 'question' ) ) {
			return false;
		}

		if ( ap_user_can_approve_comment() ) {
			$args['status'] = 'all';
		}

		return $args;
	}

	/**
	 * Ajax callback to approve comment.
	 */
	public static function approve_comment() {
		$comment_id = (int) ap_sanitize_unslash( 'comment_id', 'r' );

		if ( ! anspress_verify_nonce( 'approve_comment_' . $comment_id ) || ! ap_user_can_approve_comment() ) {
			ap_ajax_json(
				array(
					'success'  => false,
					'snackbar' => array( 'message' => __( 'Sorry, unable to approve comment', 'anspress-question-answer' ) ),
				)
			);
		}

		$success  = wp_set_comment_status( $comment_id, 'approve' );
		$_comment = get_comment( $comment_id );
		$count    = get_comment_count( $_comment->comment_post_ID );

		if ( $success ) {
			$_comment = get_comment( $comment_id );
			ap_ajax_json(
				array(
					'success'       => true,
					'cb'            => 'commentApproved',
					'comment_ID'    => $comment_id,
					'post_ID'       => $_comment->comment_post_ID,
					'commentsCount' => array(
						'text'       => sprintf(
							// translators: %d is comments count.
							_n( '%d Comment', '%d Comments', $count['all'], 'anspress-question-answer' ),
							$count['all']
						),
						'number'     => $count['all'],
						'unapproved' => $count['awaiting_moderation'],
					),
					'snackbar'      => array( 'message' => __( 'Comment approved successfully.', 'anspress-question-answer' ) ),
				)
			);
		}
	}

	/**
	 * Manipulate question and answer comments link.
	 *
	 * @param string     $link    The comment permalink with '#comment-$id' appended.
	 * @param WP_Comment $comment The current comment object.
	 * @param array      $args    An array of arguments to override the defaults.
	 */
	public static function comment_link( $link, $comment, $args ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed
		$_post = ap_get_post( $comment->comment_post_ID );

		if ( ! in_array( $_post->post_type, array( 'question', 'answer' ), true ) ) {
			return $link;
		}

		$permalink = get_permalink( $_post );
		return $permalink . '#/comment/' . $comment->comment_ID;
	}

	/**
	 * Change comment_type while adding comments for question or answer.
	 *
	 * @param array $commentdata Comment data array.
	 * @return array
	 * @since 4.1.0
	 */
	public static function preprocess_comment( $commentdata ) {
		if ( ! empty( $commentdata['comment_post_ID'] ) ) {
			$post_type = get_post_type( $commentdata['comment_post_ID'] );

			if ( in_array( $post_type, array( 'question', 'answer' ), true ) ) {
				$commentdata['comment_type'] = 'anspress';
			}
		}

		return $commentdata;
	}

	/**
	 * Override comments template for single question page.
	 * This will prevent post comments below single question.
	 *
	 * @param string $template Template.
	 * @return string
	 *
	 * @since 4.1.11
	 */
	public static function comments_template( $template ) {
		if ( is_singular( 'question' ) || is_anspress() ) {
			$template = ap_get_theme_location( 'post-comments.php' );
		}

		return $template;
	}
}

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