WP_REST_Posts_Controller::check_read_permission()publicWP 4.7.0

Checks if a post can be read.

Correctly handles posts with the inherit status.

Метод класса: WP_REST_Posts_Controller{}

Хуков нет.

Возвращает

true|false. Whether the post can be read.

Использование

$WP_REST_Posts_Controller = new WP_REST_Posts_Controller();
$WP_REST_Posts_Controller->check_read_permission( $post );
$post(WP_Post) (обязательный)
Post object.

Список изменений

С версии 4.7.0 Введена.

Код WP_REST_Posts_Controller::check_read_permission() WP 6.5.2

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;
	}

	// Is the post readable?
	if ( 'publish' === $post->post_status || current_user_can( '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 = get_post( $post->post_parent );
		if ( $parent ) {
			return $this->check_read_permission( $parent );
		}
	}

	/*
	 * If there isn't 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;
}