ap_user_can_read_comment( integer|object $_comment = false, boolean|integer $user_id = false )

Description #

Check if a user can read a comment.

Parameters #

  • $_comment
    integer | object (Optional) Comment id or object. Default value: false
  • $user_id
    boolean | integer (Optional) User ID. Default value: false

Changelog #

VersionDescription
4.1.0Introduced.

Source #

File: includes/class/roles-cap.php

function ap_user_can_read_comment( $_comment = false, $user_id = false ) {
	$_comment = get_comment( $_comment );

	if ( false === $user_id ) {
		$user_id = get_current_user_id();
	}

	if ( is_super_admin( $user_id ) ) {
		return true;
	}

	/**
	 * Filter to hijack ap_user_can_read_comment.
	 *
	 * @param  boolean|string   $apply_filter   Apply current filter, empty string by default.
	 * @param  integer|object   $_comment         Comment object.
	 * @param  integer              $user_id            User ID.
	 * @since  4.1.0
	 */
	$filter = apply_filters( 'ap_user_can_read_comment', '', $_comment, $user_id );

	if ( true === $filter ) {
		return true;
	} elseif ( false === $filter ) {
		return false;
	}

	// If user cannot read post then return from here.
	if ( ! ap_user_can_read_post( $_comment->comment_post_ID, $user_id ) ) {
		return false;
	}

	if ( '1' != $_comment->comment_approved && ! ap_user_can_approve_comment( $user_id ) ) { // phpcs:ignore Universal.Operators.StrictComparisons.LooseNotEqual
		return false;
	}

	$option = ap_opt( 'read_comment_per' );
	if ( 'have_cap' === $option && is_user_logged_in() && user_can( $user_id, 'ap_read_comment' ) ) {
		return true;
	} elseif ( 'logged_in' === $option && is_user_logged_in() ) {
		return true;
	} elseif ( 'anyone' === $option ) {
		return true;
	}

	return false;
}

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