refactor Entity constructor and repository fetch
This commit is contained in:
parent
61cd18d2d0
commit
d292ae828d
|
@ -269,7 +269,7 @@ class REST_Collections_Controller extends REST_Controller {
|
|||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
|
|
@ -70,8 +70,12 @@ class Entity {
|
|||
public $cap;
|
||||
|
||||
/**
|
||||
* Create an instance of Entity and get post data from database or create a new StdClass if $which is 0
|
||||
* Create an instance of Entity
|
||||
*
|
||||
* If ID or WP Post is passed, it retrieves the object from the database
|
||||
*
|
||||
* Attention: If the ID or Post provided do not match the Entity post type, an Exception will be thrown
|
||||
*
|
||||
* @param integer|\WP_Post optional $which Entity ID or a WP_Post object for existing Entities. Leave empty to create a new Entity.
|
||||
*
|
||||
* @throws \Exception
|
||||
|
@ -79,10 +83,11 @@ class Entity {
|
|||
function __construct($which = 0) {
|
||||
if (is_numeric($which) && $which > 0) {
|
||||
$post = get_post($which);
|
||||
|
||||
if ($post instanceof \WP_Post) {
|
||||
if ($post instanceof \WP_Post) {
|
||||
$this->WP_Post = get_post($which);
|
||||
}
|
||||
} else {
|
||||
throw new \Exception( 'No entity was found with ID ' . $which );
|
||||
}
|
||||
|
||||
} elseif ($which instanceof \WP_Post) {
|
||||
$this->WP_Post = $which;
|
||||
|
@ -98,18 +103,21 @@ class Entity {
|
|||
} else {
|
||||
$this->WP_Post = new \StdClass();
|
||||
}
|
||||
|
||||
$collection_pt_pattern = '/' . Collection::$db_identifier_prefix . '\d+' . Collection::$db_identifier_sufix . '/';
|
||||
|
||||
if(
|
||||
is_int($which) &&
|
||||
$this->WP_Post instanceof \WP_Post &&
|
||||
$which != 0 &&
|
||||
isset( $this->WP_Post->ID ) &&
|
||||
(
|
||||
( $this->get_post_type() !== false && $this->WP_Post->post_type != $this->get_post_type() ) ||
|
||||
// Lets check if it is a collection and have rigth post_type
|
||||
( $this->get_post_type() === false && $this->WP_Post->post_type != Collection::$db_identifier_prefix.$this->get_db_identifier().Collection::$db_identifier_sufix ) // TODO check if we can use only get_db_identifier for this
|
||||
( $this->get_post_type() === false && $this->WP_Post->post_type && ! preg_match($collection_pt_pattern, $this->WP_Post->post_type) )
|
||||
)
|
||||
) {
|
||||
if($this->get_post_type() === false) {
|
||||
throw new \Exception('the returned post is not the same type of the entity! expected: '.Collection::$db_identifier_prefix.$this->get_db_identifier().Collection::$db_identifier_sufix.' and actual: '.$this->WP_Post->post_type );
|
||||
|
||||
throw new \Exception('the returned post is not the same type of the entity! expected: '.Collection::$db_identifier_prefix.$this->get_db_identifier().Collection::$db_identifier_sufix.' and actual: '.$this->WP_Post->post_type );
|
||||
}
|
||||
else {
|
||||
throw new \Exception('the returned post is not the same type of the entity! expected: '.$this->get_post_type().' and actual: '.$this->WP_Post->post_type );
|
||||
|
|
|
@ -335,6 +335,9 @@ class Collections extends Repository {
|
|||
* to learn all args accepted in the $args parameter (@see https://developer.wordpress.org/reference/classes/wp_query/)
|
||||
* You can also use a mapped property, such as name and description, as an argument and it will be mapped to the
|
||||
* appropriate WP_Query argument
|
||||
*
|
||||
* If a number is passed to $args, it will return a \Tainacan\Entities\Collection object. But if the post is not found or
|
||||
* does not match the entity post type, it will return an empty array
|
||||
*
|
||||
* @param array $args WP_Query args || int $args the collection id
|
||||
* @param string $output The desired output format (@see \Tainacan\Repositories\Repository::fetch_output() for possible values)
|
||||
|
@ -345,7 +348,11 @@ class Collections extends Repository {
|
|||
if ( is_numeric( $args ) ) {
|
||||
$existing_post = get_post( $args );
|
||||
if ( $existing_post instanceof \WP_Post ) {
|
||||
return new Entities\Collection( $existing_post );
|
||||
try {
|
||||
return new Entities\Collection( $existing_post );
|
||||
} catch (\Exception $e) {
|
||||
return [];
|
||||
}
|
||||
} else {
|
||||
return [];
|
||||
}
|
||||
|
|
|
@ -232,6 +232,9 @@ class Filters extends Repository {
|
|||
* to learn all args accepted in the $args parameter (@see https://developer.wordpress.org/reference/classes/wp_query/)
|
||||
* You can also use a mapped property, such as name and description, as an argument and it will be mapped to the
|
||||
* appropriate WP_Query argument
|
||||
*
|
||||
* If a number is passed to $args, it will return a \Tainacan\Entities\Filter object. But if the post is not found or
|
||||
* does not match the entity post type, it will return an empty array
|
||||
*
|
||||
* @param array $args WP_Query args || int $args the filter id
|
||||
* @param string $output The desired output format (@see \Tainacan\Repositories\Repository::fetch_output() for possible values)
|
||||
|
@ -240,15 +243,19 @@ class Filters extends Repository {
|
|||
*/
|
||||
public function fetch( $args = [], $output = null ) {
|
||||
if ( is_numeric( $args ) ) {
|
||||
|
||||
$existing_post = get_post( $args );
|
||||
if ( $existing_post instanceof \WP_Post ) {
|
||||
return new Entities\Filter( $existing_post );
|
||||
try {
|
||||
return new Entities\Filter( $existing_post );
|
||||
} catch (\Exception $e) {
|
||||
return [];
|
||||
}
|
||||
} else {
|
||||
return [];
|
||||
}
|
||||
|
||||
} elseif ( is_array( $args ) ) {
|
||||
// TODO: get filters from parent collections
|
||||
$args = array_merge( [
|
||||
'posts_per_page' => - 1,
|
||||
], $args );
|
||||
|
|
|
@ -194,6 +194,9 @@ class Items extends Repository {
|
|||
* to learn all args accepted in the $args parameter (@see https://developer.wordpress.org/reference/classes/wp_query/)
|
||||
* You can also use a mapped property, such as name and description, as an argument and it will be mapped to the
|
||||
* appropriate WP_Query argument
|
||||
*
|
||||
* If a number is passed to $args, it will return a \Tainacan\Entities\Item object. But if the post is not found or
|
||||
* does not match the entity post type, it will return an empty array
|
||||
*
|
||||
* The second paramater specifies from which collections item should be fetched.
|
||||
* You can pass the Collection ID or object, or an Array of IDs or collection objects
|
||||
|
@ -209,9 +212,14 @@ class Items extends Repository {
|
|||
$Tainacan_Collections = \Tainacan\Repositories\Collections::get_instance();
|
||||
|
||||
if ( is_numeric( $args ) ) {
|
||||
|
||||
$existing_post = get_post( $args );
|
||||
if ( $existing_post instanceof \WP_Post ) {
|
||||
return new Entities\Item( $existing_post );
|
||||
try {
|
||||
return new Entities\Item( $existing_post );
|
||||
} catch (\Exception $e) {
|
||||
return [];
|
||||
}
|
||||
} else {
|
||||
return [];
|
||||
}
|
||||
|
|
|
@ -175,6 +175,9 @@ class Logs extends Repository {
|
|||
* to learn all args accepted in the $args parameter (@see https://developer.wordpress.org/reference/classes/wp_query/)
|
||||
* You can also use a mapped property, such as name and description, as an argument and it will be mapped to the
|
||||
* appropriate WP_Query argument
|
||||
*
|
||||
* If a number is passed to $args, it will return a \Tainacan\Entities\Log object. But if the post is not found or
|
||||
* does not match the entity post type, it will return an empty array
|
||||
*
|
||||
* @param array $args WP_Query args || int $args the log id
|
||||
* @param string $output The desired output format (@see \Tainacan\Repositories\Repository::fetch_output() for possible values)
|
||||
|
@ -183,9 +186,14 @@ class Logs extends Repository {
|
|||
*/
|
||||
public function fetch( $args = [], $output = null ) {
|
||||
if ( is_numeric( $args ) ) {
|
||||
|
||||
$existing_post = get_post( $args );
|
||||
if ( $existing_post instanceof \WP_Post ) {
|
||||
return new Entities\Log( $existing_post );
|
||||
try {
|
||||
return new Entities\Log( $existing_post );
|
||||
} catch (\Exception $e) {
|
||||
return [];
|
||||
}
|
||||
} else {
|
||||
return [];
|
||||
}
|
||||
|
|
|
@ -300,6 +300,9 @@ class Metadata extends Repository {
|
|||
* You can also use a mapped property, such as name and description, as an argument and it will be mapped to the
|
||||
* appropriate WP_Query argument
|
||||
*
|
||||
* If a number is passed to $args, it will return a \Tainacan\Entities\Metadatum object. But if the post is not found or
|
||||
* does not match the entity post type, it will return an empty array
|
||||
*
|
||||
* @param array $args WP_Query args || int $args the metadatum id
|
||||
* @param string $output The desired output format (@see \Tainacan\Repositories\Repository::fetch_output() for possible values)
|
||||
*
|
||||
|
@ -311,7 +314,11 @@ class Metadata extends Repository {
|
|||
if ( is_numeric( $args ) ) {
|
||||
$existing_post = get_post( $args );
|
||||
if ( $existing_post instanceof \WP_Post ) {
|
||||
return new Entities\Metadatum( $existing_post );
|
||||
try {
|
||||
return new Entities\Metadatum( $existing_post );
|
||||
} catch (\Exception $e) {
|
||||
return [];
|
||||
}
|
||||
} else {
|
||||
return [];
|
||||
}
|
||||
|
@ -736,6 +743,11 @@ class Metadata extends Repository {
|
|||
* @throws \Exception
|
||||
*/
|
||||
public function disable_delete_core_metadata( $before, $post ) {
|
||||
|
||||
if ( Entities\Metadatum::get_post_type() != $post->post_type ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$metadatum = $this->fetch( $post->ID );
|
||||
|
||||
if ( $metadatum && in_array( $metadatum->get_metadata_type(), $this->core_metadata ) && is_numeric( $metadatum->get_collection_id() ) ) {
|
||||
|
@ -755,6 +767,11 @@ class Metadata extends Repository {
|
|||
* @internal param The $post_id post ID which is deleting
|
||||
*/
|
||||
public function force_delete_core_metadata( $before, $post, $force_delete ) {
|
||||
|
||||
if ( Entities\Metadatum::get_post_type() != $post->post_type ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$metadatum = $this->fetch( $post->ID );
|
||||
|
||||
if ( $metadatum && in_array( $metadatum->get_metadata_type(), $this->core_metadata ) && is_numeric( $metadatum->get_collection_id() ) ) {
|
||||
|
|
|
@ -158,6 +158,9 @@ class Taxonomies extends Repository {
|
|||
* to learn all args accepted in the $args parameter (@see https://developer.wordpress.org/reference/classes/wp_query/)
|
||||
* You can also use a mapped property, such as name and description, as an argument and it will be mapped to the
|
||||
* appropriate WP_Query argument
|
||||
*
|
||||
* If a number is passed to $args, it will return a \Tainacan\Entities\Taxonomy object. But if the post is not found or
|
||||
* does not match the entity post type, it will return an empty array
|
||||
*
|
||||
* @param array $args WP_Query args | int $args the taxonomy id
|
||||
* @param string $output The desired output format (@see \Tainacan\Repositories\Repository::fetch_output() for possible values)
|
||||
|
@ -171,10 +174,15 @@ class Taxonomies extends Repository {
|
|||
if ( is_numeric( $args ) ) {
|
||||
$existing_post = get_post( $args );
|
||||
if ( $existing_post instanceof \WP_Post ) {
|
||||
return new Entities\Taxonomy( $existing_post );
|
||||
try {
|
||||
return new Entities\Taxonomy( $existing_post );
|
||||
} catch (\Exception $e) {
|
||||
return [];
|
||||
}
|
||||
} else {
|
||||
return [];
|
||||
}
|
||||
|
||||
} elseif ( is_array( $args ) ) {
|
||||
|
||||
$args = array_merge( [
|
||||
|
|
|
@ -0,0 +1,119 @@
|
|||
<?php
|
||||
|
||||
namespace Tainacan\Tests;
|
||||
|
||||
/**
|
||||
* Class TestCollections
|
||||
*
|
||||
* @package Test_Tainacan
|
||||
*/
|
||||
|
||||
/**
|
||||
* Sample test case.
|
||||
*/
|
||||
class Entities extends TAINACAN_UnitTestCase {
|
||||
|
||||
|
||||
function setUp() {
|
||||
parent::setUp();
|
||||
$this->collection = $this->tainacan_entity_factory->create_entity(
|
||||
'collection',
|
||||
array(
|
||||
'name' => 'teste',
|
||||
'description' => 'Filter teste colletion'
|
||||
),
|
||||
true
|
||||
);
|
||||
|
||||
$this->collection2 = $this->tainacan_entity_factory->create_entity(
|
||||
'collection',
|
||||
array(
|
||||
'name' => 'teste2',
|
||||
'description' => 'Filter teste colletion'
|
||||
),
|
||||
true
|
||||
);
|
||||
|
||||
$this->filter = $this->tainacan_entity_factory->create_entity(
|
||||
'filter',
|
||||
array(
|
||||
'name' => 'filtro',
|
||||
'collection' => $this->collection,
|
||||
'description' => 'Teste Filtro'
|
||||
),
|
||||
true
|
||||
);
|
||||
|
||||
$this->item = $this->tainacan_entity_factory->create_entity(
|
||||
'item',
|
||||
array(
|
||||
'title' => 'testeItem',
|
||||
'collection' => $this->collection,
|
||||
),
|
||||
true
|
||||
);
|
||||
|
||||
$this->id_filter = $this->filter->get_id();
|
||||
$this->id_collection = $this->collection->get_id();
|
||||
|
||||
$this->post_filter = get_post($this->filter->get_id());
|
||||
$this->post_collection = get_post($this->collection->get_id());
|
||||
|
||||
}
|
||||
|
||||
|
||||
public function test_construct_with_id() {
|
||||
|
||||
|
||||
$test_filter = new \Tainacan\Entities\Filter( $this->id_filter );
|
||||
|
||||
$this->assertTrue( $test_filter instanceof \Tainacan\Entities\Filter );
|
||||
|
||||
}
|
||||
|
||||
public function test_contruct_with_post() {
|
||||
|
||||
$new_test_filter = new \Tainacan\Entities\Filter( $this->post_filter );
|
||||
|
||||
$this->assertTrue( $new_test_filter instanceof \Tainacan\Entities\Filter );
|
||||
|
||||
}
|
||||
|
||||
public function test_construct_with_wrong_id() {
|
||||
|
||||
$this->expectException(\Exception::class);
|
||||
|
||||
$test_wrong = new \Tainacan\Entities\Filter( $this->id_collection );
|
||||
|
||||
}
|
||||
|
||||
public function test_construct_with_wrong_post() {
|
||||
|
||||
$this->expectException(\Exception::class);
|
||||
|
||||
$test_wrong = new \Tainacan\Entities\Filter( $this->post_collection );
|
||||
|
||||
|
||||
}
|
||||
|
||||
public function test_contruct_item() {
|
||||
|
||||
$item = new \Tainacan\Entities\Item( $this->item->get_id() );
|
||||
|
||||
$this->assertTrue( $item instanceof \Tainacan\Entities\Item );
|
||||
|
||||
$this->assertEquals( $item->get_collection_id(), $this->id_collection );
|
||||
|
||||
}
|
||||
|
||||
public function test_contruct_wrong_item() {
|
||||
|
||||
$this->expectException(\Exception::class);
|
||||
|
||||
$item = new \Tainacan\Entities\Item( $this->collection2->get_id() );
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -379,4 +379,39 @@ class Items extends TAINACAN_UnitTestCase {
|
|||
|
||||
$this->assertFalse(comments_open($item->get_id()));
|
||||
}
|
||||
|
||||
public function test_delete_item() {
|
||||
|
||||
$collection = $this->tainacan_entity_factory->create_entity(
|
||||
'collection',
|
||||
array(
|
||||
'name' => 'collectionComments',
|
||||
'allow_comments' => 'closed'
|
||||
),
|
||||
true,
|
||||
true
|
||||
);
|
||||
$item = $this->tainacan_entity_factory->create_entity(
|
||||
'item',
|
||||
array(
|
||||
'title' => 'itemComments1',
|
||||
'collection' => $collection,
|
||||
'comment_status' => 'open'
|
||||
),
|
||||
true,
|
||||
true
|
||||
);
|
||||
|
||||
$item_id = $item->get_id();
|
||||
|
||||
$items = \Tainacan\Repositories\Items::get_instance();
|
||||
|
||||
$items->delete($item_id);
|
||||
|
||||
$fetch_item = $items->fetch($item_id);
|
||||
|
||||
$this->assertEmpty($fetch_item);
|
||||
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue