ap_user_can_read_post( integer|object $_post = null, boolean|integer $user_id = false, string|integer $post_type = false )

Description #

Check if a user can read post.

Parameters #

  • $_post
    integer | object (Optional) Post ID. Default value: null
  • $user_id
    boolean | integer (Optional) User ID. Default value: false
  • $post_type
    string | integer (Optional) Post type. Default value: false

Changelog #

VersionDescription
4.1.0Check for options read_question_per and read_answer_per.
2.4.6Introduced.

Source #

File: includes/class/roles-cap.php

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

	$post_o = ap_get_post( $_post );

	// Return false if post is not found.
	if ( ! $post_o ) {
		return false;
	}

	// Get post type.
	$post_type = $post_o->post_type;

	// If not question or answer then return true.
	if ( ! in_array( $post_type, array( 'question', 'answer' ), true ) ) {
		return true;
	}

	/**
	 * Allow overriding of ap_user_can_read_post.
	 *
	 * @param  boolean|string   $apply_filter Default is empty string.
	 * @param  integer          $post_id      Question ID.
	 * @param  integer          $user_id      User ID.
	 * @param  string           $post_type    Post type.
	 * @return boolean
	 * @since  2.4.6
	 */
	$filter = apply_filters( 'ap_user_can_read_post', '', $post_o->ID, $user_id, $post_type );

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

	// Show if post in user's session.
	if ( ! is_user_logged_in() && '0' === $post_o->post_author && anspress()->session->post_in_session( $post_o ) ) {
		return true;
	}

	// Also return true if user have capability to edit others question.
	if ( user_can( $user_id, 'ap_edit_others_' . $post_type ) ) {
		return true;
	}

	// Do not allow to read trash post.
	if ( 'trash' === $post_o->post_status ) {
		return false;
	}

	// If Answer, check if user can read parent question.
	if ( 'answer' === $post_type ) {
		$answer = ap_get_post( $post_o->post_parent );
		if ( 'private_post' === $answer->post_status && ! ap_user_can_view_private_post( $answer->ID, $user_id ) ) {
			return false;
		} elseif ( 'moderate' === $answer->post_status && ! ap_user_can_view_moderate_post( $answer->ID, $user_id ) ) {
			return false;
		}
	}

	if ( 'private_post' === $post_o->post_status && ! ap_user_can_view_private_post( $post_o->ID, $user_id ) ) {
		return false;
	} elseif ( 'moderate' === $post_o->post_status && ! ap_user_can_view_moderate_post( $post_o->ID, $user_id ) ) {
		return false;
	}

	$option = ap_opt( 'read_' . $post_type . '_per' );

	if ( 'have_cap' === $option && is_user_logged_in() && user_can( $user_id, 'ap_read_' . $post_type ) ) {
		return true;
	} elseif ( 'logged_in' === $option && is_user_logged_in() ) {
		return true;
	} elseif ( 'anyone' === $option ) {
		return true;
	}

	// Finally return false. And break the heart :p.
	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