ap_user_can_read_comments( mixed $_post = null, boolean|integer $user_id = false )

Description #

Check if a user can read a comments.

Parameters #

  • $_post
    mixed (Optional) Post ID or object. Default value: null
  • $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_comments( $_post = null, $user_id = false ) {
	$_post = ap_get_post( $_post );

	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_comments.
	 *
	 * @param  boolean|string   $apply_filter   Apply current filter, empty string by default.
	 * @param  object               $_post            Post ID or object.
	 * @param  integer              $user_id            User ID.
	 * @since  4.1.0
	 */
	$filter = apply_filters( 'ap_user_can_read_comments', '', $_post, $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( $_post, $user_id ) ) {
		return false;
	}

	$option = ap_opt( 'read_comment_per' );

	if ( 'have_cap' === $option && is_user_logged_in() && get_user_by( 'ID', $user_id )->has_cap( '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