From b832a785befcfe96544fcabaac845b33c8ce2d1b Mon Sep 17 00:00:00 2001 From: vnmedeiros Date: Wed, 9 Mar 2022 01:14:55 -0300 Subject: [PATCH] fix: get public item of the private collection using internal API --- src/classes/repositories/class-tainacan-collections.php | 6 +++++- src/classes/repositories/class-tainacan-items.php | 7 ++++++- src/classes/repositories/class-tainacan-repository.php | 5 ++++- 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/classes/repositories/class-tainacan-collections.php b/src/classes/repositories/class-tainacan-collections.php index 8ce0b589b..fad050a4a 100644 --- a/src/classes/repositories/class-tainacan-collections.php +++ b/src/classes/repositories/class-tainacan-collections.php @@ -343,7 +343,11 @@ class Collections extends Repository { $existing_post = get_post( $args ); if ( $existing_post instanceof \WP_Post ) { try { - return new Entities\Collection( $existing_post ); + $col = new Entities\Collection( $existing_post ); + if ( $col->can_read() ) { + return $col; + } + return []; } catch (\Exception $e) { return []; } diff --git a/src/classes/repositories/class-tainacan-items.php b/src/classes/repositories/class-tainacan-items.php index 90a9c564c..8871e9683 100644 --- a/src/classes/repositories/class-tainacan-items.php +++ b/src/classes/repositories/class-tainacan-items.php @@ -252,7 +252,12 @@ class Items extends Repository { $existing_post = get_post( $args ); if ( $existing_post instanceof \WP_Post ) { try { - return new Entities\Item( $existing_post ); + $item = new Entities\Item( $existing_post ); + $collection = $item->get_collection(); + if (isset($collection) && $collection->can_read()) { + return $item; + } + return []; } catch (\Exception $e) { return []; } diff --git a/src/classes/repositories/class-tainacan-repository.php b/src/classes/repositories/class-tainacan-repository.php index daabc21ed..0da2ea5c9 100644 --- a/src/classes/repositories/class-tainacan-repository.php +++ b/src/classes/repositories/class-tainacan-repository.php @@ -725,7 +725,10 @@ abstract class Repository { $entity_cap = $entity->get_capabilities(); if ( ! isset( $entity_cap->read ) ) { - if ( $entity->get_post_type() === false ) { // Allow read of not post entities + $prefix = Entities\Collection::$db_identifier_prefix; + $sufix = Entities\Collection::$db_identifier_sufix; + $is_a_item = preg_match('/^'. $prefix . '[0-9]*' . $sufix . '$/i', $entity->WP_Post->post_type); + if ( $entity->get_post_type() === false && !$is_a_item) { // Allow read of not post entities return true; }