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

Description #

Check if user have permission to view post.

Parameters #

  • $_post
    mixed (Optional) Post. Default value: null
  • $user_id
    integer | false (Optional) user ID. Default value: false

Changelog #

VersionDescription
2.0.1Introduced.

Source #

File: includes/class/roles-cap.php

function ap_user_can_view_private_post( $_post = null, $user_id = false ) {
	if ( false === $user_id ) {
		$user_id = get_current_user_id();
	}

	if ( is_super_admin( $user_id ) || user_can( $user_id, 'ap_view_private' ) ) {
		return true;
	}

	$post_o = is_object( $_post ) ? $_post : ap_get_post( $_post );

	if ( ! $post_o || 0 == $user_id || ! ap_is_cpt( $post_o ) ) { // phpcs:ignore Universal.Operators.StrictComparisons.LooseEqual
		return false;
	}

	// Return false if the post status is not set to private_post.
	if ( 'private_post' !== $post_o->post_status ) {
		return false;
	}

	if ( $post_o->post_author == $user_id ) { // phpcs:ignore Universal.Operators.StrictComparisons.LooseEqual
		return true;
	}

	// Also allow question author to see all private answers.
	if ( 'answer' === $post_o->post_type ) {
		$question = ap_get_post( $post_o->post_parent );

		if ( $question->post_author == $user_id ) { // phpcs:ignore Universal.Operators.StrictComparisons.LooseEqual
			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