ap_user_can_view_future_post( integer $post_id = null, integer $user_id = false )

Description #

Check if user can view a future post.

Parameters #

  • $post_id
    integer (Optional) Post ID. Default value: null
  • $user_id
    integer (Optional) User ID. Default value: false

Changelog #

VersionDescription
4.1.5Let user view if post is in their session. This is useful for anonymous users.
2.4.6Introduced.

Source #

File: includes/class/roles-cap.php

function ap_user_can_view_future_post( $post_id = 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_future' ) ) {
		return true;
	}

	$_post = ap_get_post( $post_id );

	// Bail if not answer or question.
	if ( ! $_post || ! ap_is_cpt( $_post ) ) {
		return false;
	}

	if ( is_user_logged_in() && $_post && $_post->post_author == $user_id ) { // phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison
		return true;
	}

	$session_type  = 'answer' === $_post->post_type ? 'answers' : 'questions';
	$session_posts = anspress()->session->get( $session_type );

	if ( is_array( $session_posts ) && ! is_user_logged_in() && '0' === $_post->post_author && in_array( $_post->ID, $session_posts ) ) { // phpcs:ignore WordPress.PHP.StrictInArray.MissingTrueStrict
		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