Check if we can read a post.

Correctly handles posts with the inherit status.

@param object $post Post object.
@return boolean Can we read it?
771395
public function check_read_permission( $post ) {
		$post_type = get_post_type_object( $post->post_type );
		if ( ! $this->check_is_post_type_allowed( $post_type ) ) {
			return false;
		}

		// Can we read the post?
		if ( 'publish' === $post->post_status || current_user_can( $post_type->cap->read_post, $post->ID ) ) {
			return true;
		}

		$post_status_obj = get_post_status_object( $post->post_status );
		if ( $post_status_obj && $post_status_obj->public ) {
			return true;
		}

		// Can we read the parent if we're inheriting?
		if ( 'inherit' === $post->post_status && $post->post_parent > 0 ) {
			$parent = $this->get_post( $post->post_parent );
			return $this->check_read_permission( $parent );
		}

		// If we don't have a parent, but the status is set to inherit, assume
		// it's published (as per get_post_status()).
		if ( 'inherit' === $post->post_status ) {
			return true;
		}

		return false;
	}
1165913
private function hasTemplateForPost(\WP_Post $post): bool
    {
        $postTemplate = get_post_meta($post->ID, '_wp_page_template', true);

        $templates = isset($this->template[$post->post_type]) ? $this->template[$post->post_type] : [];

        if (empty($templates)) {
            // No templates exist for the current post type so we let
            // pass through either in order to display the metabox.
            return true;
        }

        return in_array($postTemplate, $templates, true);
    }
994116
public function can_split_post(stdClass $user, discussion_entity $discussion, post_entity $post) : bool {
        if ($post->is_private_reply()) {
            // It is not possible to create a private discussion.
            return false;
        }

        return $this->can_split_discussions($user) && $post->has_parent();
    }
213071
public function performAjaxValidation($post=null)
    {
        $request = Yii::$app->getRequest();

        /**
         * @var $this \yii\base\Model
         */
        if ($request->isAjax && $this->load(isset($post) ? $post : $request->post())) {
            $response = Yii::$app->getResponse();
            $response->format = $response::FORMAT_JSON;
            if (is_null($this->_performAjaxValidationResult)){
                $this->_performAjaxValidationResult = ActiveForm::validate($this);
            }
            return $this->_performAjaxValidationResult;
        }
        return null;
    }
524940
protected function isViewGranted($object)
    {
        if ($object instanceof FieldVote) {
            $eventName = ObjectFilterEvents::OBJECT_FIELD_VIEW_GRANTED;
            $event = new ObjectFieldViewGrantedEvent($object);
            $permission = 'perm_read';
        } else {
            $eventName = ObjectFilterEvents::OBJECT_VIEW_GRANTED;
            $event = new ObjectViewGrantedEvent($object);
            $permission = 'perm_view';
        }

        $this->dispatcher->dispatch($eventName, $event);

        if ($event->isSkipAuthorizationChecker()) {
            return $event->isGranted();
        }

        return $this->ac->isGranted($permission, $object);
    }
886388
public function isPosted($key = null)
    {
        if (isset($key)) {
            $value = $this->request->post($key, null);
            return isset($value);
        }

        return $this->server->requestMethod() === 'POST';
    }
238994
public function canPost()
    {
        $viewer = get_viewer();

        $actor = $this->actor;

        if (!$actor) {
            return false;
        }

        if ($viewer->eql($actor)) {
            return false;
        }

        if (!$actor->isFollowable()) {
            return false;
        }

        if (!$actor->isSubscribable()) {
            return false;
        }

        return $viewer->following($actor);
    }
69186
protected function get_post_id( $post = null ) {

		return is_a( 'WP_Post', $post ) ? $post->ID : ( is_numeric( $post ) ? absint( $post ) : get_queried_object_id() );

	}
146961
public function setLastPost(Post $post)
    {
        $this->last_posted_at = $post->created_at;
        $this->last_posted_user_id = $post->user_id;
        $this->last_post_id = $post->id;
        $this->last_post_number = $post->number;

        return $this;
    }
810231
protected function check_read_permission( $status ) {
		if ( true === $status->public ) {
			return true;
		}
		if ( false === $status->internal || 'trash' === $status->name ) {
			$types = get_post_types( array( 'show_in_rest' => true ), 'objects' );
			foreach ( $types as $type ) {
				if ( current_user_can( $type->cap->edit_posts ) ) {
					return true;
				}
			}
		}
		return false;
	}
697108
public function iShouldNotBeAbleToViewThePost($post_title)
    {
        try {
            $this->getContentFromTitle($post_title);
        } catch (UnexpectedValueException $e) {
            // This is expected to fail.
            return;
        }

        throw new RuntimeException(sprintf('[W811] Content "%s" exists, but it should not.', $post_title));
    }
929219
public function delete(Post $post)
    {
        $this->authorize(PostsPolicy::PERMISSION_DELETE);

        try {
            $post->trashed() ? $post->forceDelete() : $post->delete();

            return $this->jsonResponseSuccess([
                'message' => $this->transNotification('deleted', ['title' => $post->title], $post->toArray())
            ]);
        }
        catch(\Exception $e) {
            return $this->jsonResponseError(['message' => $e->getMessage()], 500);
        }
    }
1300739
public function canRead()
    {
        $actor = pick($this->actor, get_viewer());

        //if repository is ownable then ask the actor if viewer can publish things
        if (
            in_array($this->getRequest()->get('layout'), array('add', 'edit', 'form', 'composer'))
        ) {
            $result = ($this->getItem()) ? $this->canEdit() : $this->canAdd();

            return $result;
        }

        if (!$this->getItem()) {
            return false;
        }

        //check if an entiy authorize access
        return $this->getItem()->authorize('access');
    }
766879
public function can_view_post(stdClass $user, discussion_entity $discussion, post_entity $post) : bool {
        if (!$this->can_view_post_shell($user, $post)) {
            return false;
        }

        $forum = $this->get_forum();
        $forumrecord = $this->get_forum_record();
        $discussionrecord = $this->get_discussion_record($discussion);
        $postrecord = $this->get_post_record($post);
        $coursemodule = $forum->get_course_module_record();
        return forum_user_can_see_post($forumrecord, $discussionrecord, $postrecord, $user, $coursemodule, false);
    }
64232
public static function isChildPost($postOrPostId = null)
    {
        $post = self::getPost($postOrPostId);
        if (empty($post)) {
            return false;
        }

        return $post->post_parent > 0;
    }
1282384
public static function get( $post ) {
		$post = is_a( $post, 'WP_Post' ) ? $post : get_post( $post );
		$type = $post->post_type;
		if ( 'page' === $type ) {
			$template_slug = get_page_template_slug( $post->ID );
			if ( ! empty( $template_slug ) ) {
				$type .= '-' . wp_basename( $template_slug, '.php' );
			}
		}
		return $type;
	}
430969
public function has_user_read_post(stdClass $user, post_entity $post) : bool {
        global $CFG;
        $isoldpost = ($post->get_time_modified() < (time() - ($CFG->forum_oldpostdays * 24 * 3600)));

        if ($isoldpost) {
            return true;
        }

        $receipts = isset($this->receiptsbypostid[$post->get_id()]) ? $this->receiptsbypostid[$post->get_id()] : [];

        foreach ($receipts as $receipt) {
            if ($receipt->userid == $user->id) {
                return true;
            }
        }

        return false;
    }