allow change output in fetch methods
This commit is contained in:
parent
023ef0f15a
commit
c15cb6464f
|
@ -221,7 +221,7 @@ class Collection extends Entity {
|
||||||
*/
|
*/
|
||||||
function get_metadata() {
|
function get_metadata() {
|
||||||
$Tainacan_Metadatas = new \Tainacan\Repositories\Metadatas();
|
$Tainacan_Metadatas = new \Tainacan\Repositories\Metadatas();
|
||||||
return $Tainacan_Metadatas->fetch_by_collection( $this );
|
return $Tainacan_Metadatas->fetch_by_collection( $this, [], 'OBJECT' );
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -127,7 +127,7 @@ class Item extends Entity {
|
||||||
$collection = $this->get_collection();
|
$collection = $this->get_collection();
|
||||||
$all_metadata = [];
|
$all_metadata = [];
|
||||||
if ($collection) {
|
if ($collection) {
|
||||||
$meta_list = $Tainacan_Metadatas->fetch_by_collection( $collection );
|
$meta_list = $Tainacan_Metadatas->fetch_by_collection( $collection, [], 'OBJECT' );
|
||||||
|
|
||||||
foreach ($meta_list as $meta) {
|
foreach ($meta_list as $meta) {
|
||||||
$all_metadata[$meta->get_id()] = new Item_Metadata_Entity($this, $meta);
|
$all_metadata[$meta->get_id()] = new Item_Metadata_Entity($this, $meta);
|
||||||
|
|
|
@ -31,7 +31,7 @@ class Collections extends Repository {
|
||||||
//'validation' => v::stringType(),
|
//'validation' => v::stringType(),
|
||||||
],
|
],
|
||||||
'parent' => [
|
'parent' => [
|
||||||
'map' => 'parent',
|
'map' => 'post_parent',
|
||||||
'name' => __('Parent Collection', 'tainacan'),
|
'name' => __('Parent Collection', 'tainacan'),
|
||||||
'description'=> __('Parent collection ID', 'tainacan'),
|
'description'=> __('Parent collection ID', 'tainacan'),
|
||||||
//'validation' => v::stringType(),
|
//'validation' => v::stringType(),
|
||||||
|
@ -138,6 +138,7 @@ class Collections extends Repository {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Tainacan\Entities\Collection $collection
|
* @param Tainacan\Entities\Collection $collection
|
||||||
|
* @return \Tainacan\Entities\Collection
|
||||||
* {@inheritDoc}
|
* {@inheritDoc}
|
||||||
* @see \Tainacan\Repositories\Repository::insert()
|
* @see \Tainacan\Repositories\Repository::insert()
|
||||||
*/
|
*/
|
||||||
|
@ -162,9 +163,10 @@ class Collections extends Repository {
|
||||||
* to learn all args accepted in the $args parameter
|
* to learn all args accepted in the $args parameter
|
||||||
*
|
*
|
||||||
* @param array $args WP_Query args || int $args the collection id
|
* @param array $args WP_Query args || int $args the collection id
|
||||||
* @return \WP_Query an instance of wp query
|
* @param string $output One of 2 pre-defined constants 'WP_Query' | 'OBJECT' . Defaults to WP_Query
|
||||||
|
* @return \WP_Query|Array an instance of wp query OR array of entities;
|
||||||
*/
|
*/
|
||||||
public function fetch($args = []){
|
public function fetch($args = [], $output = 'WP_Query'){
|
||||||
if(is_numeric( $args )){
|
if(is_numeric( $args )){
|
||||||
return new Entities\Collection($args);
|
return new Entities\Collection($args);
|
||||||
} elseif(is_array($args)) {
|
} elseif(is_array($args)) {
|
||||||
|
@ -177,7 +179,8 @@ class Collections extends Repository {
|
||||||
|
|
||||||
// TODO: Pegar coleções registradas via código
|
// TODO: Pegar coleções registradas via código
|
||||||
|
|
||||||
return new \WP_Query($args);
|
$wp_query = new \WP_Query($args);
|
||||||
|
return $this->fetch_output($wp_query, $output);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -141,15 +141,10 @@ class Filters extends Repository {
|
||||||
* to learn all args accepted in the $args parameter
|
* to learn all args accepted in the $args parameter
|
||||||
*
|
*
|
||||||
* @param array $args WP_Query args || int $args the filter id
|
* @param array $args WP_Query args || int $args the filter id
|
||||||
* @rreturn new \WP_Query($args);
|
* @param string $output One of 2 pre-defined constants 'WP_Query' | 'OBJECT' . Defaults to WP_Query
|
||||||
|
* @return \WP_Query|Array an instance of wp query OR array of entities;
|
||||||
*/
|
*/
|
||||||
public function fetch($args = []){
|
public function fetch($args = [], $output = 'WP_Query'){
|
||||||
/**
|
|
||||||
* Se for numérico retorna o objeto filtro
|
|
||||||
* Se não, mas se há valor em $object e $args retorna filtro de coleção especifica
|
|
||||||
* Se não, mas se for string retorna os filtros pelo tipo de metadado
|
|
||||||
* Se não, retorna todos os filtros
|
|
||||||
*/
|
|
||||||
if( is_numeric($args) ){
|
if( is_numeric($args) ){
|
||||||
return new Entities\Filter($args);
|
return new Entities\Filter($args);
|
||||||
} elseif (!empty($args)) {
|
} elseif (!empty($args)) {
|
||||||
|
@ -161,30 +156,9 @@ class Filters extends Repository {
|
||||||
|
|
||||||
$args['post_type'] = Entities\Filter::get_post_type();
|
$args['post_type'] = Entities\Filter::get_post_type();
|
||||||
|
|
||||||
return new \WP_Query($args);;
|
$wp_query = new \WP_Query($args);
|
||||||
|
return $this->fetch_output($wp_query, $output);
|
||||||
}
|
}
|
||||||
// elseif(is_string($object)) {
|
|
||||||
// $filters = array();
|
|
||||||
// $filters_type = $this->fetch();
|
|
||||||
//
|
|
||||||
// foreach ( $filters_type as $filter_type ){
|
|
||||||
// if( in_array( $object, $filter_type->get_supported_types() ) ){
|
|
||||||
// $filters[] = $filter_type;
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// return $filters;
|
|
||||||
// } else {
|
|
||||||
// $filters = array();
|
|
||||||
//
|
|
||||||
// foreach (get_declared_classes() as $class) {
|
|
||||||
// if (is_subclass_of($class, '\Tainacan\Filter_Types\Filter_Type')){
|
|
||||||
// $filters[] = new $class();
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// return $filters;
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -54,7 +54,7 @@ class Item_Metadata extends Repository {
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
$meta_list = $Tainacan_Metadatas->fetch_by_collection($collection);
|
$meta_list = $Tainacan_Metadatas->fetch_by_collection($collection, [], 'OBJECT' );
|
||||||
|
|
||||||
$return = [];
|
$return = [];
|
||||||
|
|
||||||
|
|
|
@ -124,9 +124,10 @@ class Items extends Repository {
|
||||||
*
|
*
|
||||||
* @param array $args WP_Query args || int $args the item id
|
* @param array $args WP_Query args || int $args the item id
|
||||||
* @param array $collections Array Entities\Collection || Array int collections IDs || int collection id || Entities\Collection collection object
|
* @param array $collections Array Entities\Collection || Array int collections IDs || int collection id || Entities\Collection collection object
|
||||||
* @return \WP_Query an instance of wp query
|
* @param string $output One of 2 pre-defined constants 'WP_Query' | 'OBJECT' . Defaults to WP_Query
|
||||||
|
* @return \WP_Query|Array an instance of wp query OR array of entities;
|
||||||
*/
|
*/
|
||||||
public function fetch($args = [],$collections = []){
|
public function fetch($args = [],$collections = [], $output = 'WP_Query'){
|
||||||
|
|
||||||
global $Tainacan_Collections;
|
global $Tainacan_Collections;
|
||||||
|
|
||||||
|
@ -178,7 +179,8 @@ class Items extends Repository {
|
||||||
|
|
||||||
$args['post_type'] = $cpt;
|
$args['post_type'] = $cpt;
|
||||||
|
|
||||||
return new \WP_Query($args);
|
$wp_query = new \WP_Query($args);
|
||||||
|
return $this->fetch_output($wp_query, $output);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function update($object){
|
public function update($object){
|
||||||
|
|
|
@ -116,9 +116,10 @@ class Logs extends Repository {
|
||||||
* to learn all args accepted in the $args parameter
|
* to learn all args accepted in the $args parameter
|
||||||
*
|
*
|
||||||
* @param array $args WP_Query args || int $args the log id
|
* @param array $args WP_Query args || int $args the log id
|
||||||
* @return Array of Entities\Log objects || Entities\Log
|
* @param string $output One of 2 pre-defined constants 'WP_Query' | 'OBJECT' . Defaults to WP_Query
|
||||||
|
* @return \WP_Query|Array an instance of wp query OR array of entities;
|
||||||
*/
|
*/
|
||||||
public function fetch($args = []){
|
public function fetch($args = [], $output = 'WP_Query'){
|
||||||
if(is_numeric($args)){
|
if(is_numeric($args)){
|
||||||
return new Entities\Log($args);
|
return new Entities\Log($args);
|
||||||
} else {
|
} else {
|
||||||
|
@ -127,16 +128,9 @@ class Logs extends Repository {
|
||||||
], $args);
|
], $args);
|
||||||
|
|
||||||
$args['post_type'] = Entities\Log::get_post_type();
|
$args['post_type'] = Entities\Log::get_post_type();
|
||||||
|
|
||||||
$posts = get_posts($args);
|
|
||||||
|
|
||||||
$logs = [];
|
|
||||||
|
|
||||||
foreach ($posts as $post) {
|
|
||||||
$logs[] = new Entities\Log($post);
|
|
||||||
}
|
|
||||||
|
|
||||||
return $logs;
|
$wp_query = new \WP_Query($args);
|
||||||
|
return $this->fetch_output($wp_query, $output);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -10,6 +10,7 @@ defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
|
||||||
*/
|
*/
|
||||||
class Metadatas extends Repository {
|
class Metadatas extends Repository {
|
||||||
protected $entities_type = '\Tainacan\Entities\Metadata';
|
protected $entities_type = '\Tainacan\Entities\Metadata';
|
||||||
|
protected $default_metadata = 'default';
|
||||||
|
|
||||||
public function get_map() {
|
public function get_map() {
|
||||||
return [
|
return [
|
||||||
|
@ -117,6 +118,15 @@ class Metadatas extends Repository {
|
||||||
register_post_type(Entities\Metadata::get_post_type(), $args);
|
register_post_type(Entities\Metadata::get_post_type(), $args);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* constant used in default metadata in attribute collection_id
|
||||||
|
*
|
||||||
|
* @return string the value of constant
|
||||||
|
*/
|
||||||
|
public function get_default_metadata_attribute(){
|
||||||
|
return $this->default_metadata;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* fetch metadata based on ID or WP_Query args
|
* fetch metadata based on ID or WP_Query args
|
||||||
|
@ -125,9 +135,10 @@ class Metadatas extends Repository {
|
||||||
* to learn all args accepted in the $args parameter
|
* to learn all args accepted in the $args parameter
|
||||||
*
|
*
|
||||||
* @param array $args WP_Query args || int $args the metadata id
|
* @param array $args WP_Query args || int $args the metadata id
|
||||||
* @return \WP_Query an instance of wp query
|
* @param string $output One of 2 pre-defined constants 'WP_Query' | 'OBJECT' . Defaults to WP_Query
|
||||||
|
* @return \WP_Query|Array an instance of wp query OR array of entities;
|
||||||
*/
|
*/
|
||||||
public function fetch( $args ) {
|
public function fetch( $args, $output = 'WP_Query' ) {
|
||||||
|
|
||||||
if( is_numeric($args) ){
|
if( is_numeric($args) ){
|
||||||
return new Entities\Metadata($args);
|
return new Entities\Metadata($args);
|
||||||
|
@ -140,37 +151,43 @@ class Metadatas extends Repository {
|
||||||
|
|
||||||
$args['post_type'] = Entities\Metadata::get_post_type();
|
$args['post_type'] = Entities\Metadata::get_post_type();
|
||||||
|
|
||||||
return new \WP_Query($args);
|
$wp_query = new \WP_Query($args);
|
||||||
|
return $this->fetch_output($wp_query, $output);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* fetch metadata by collection
|
* fetch metadata by collection, searches all metadata available
|
||||||
*
|
*
|
||||||
* @param Entities\Collection $collection
|
* @param Entities\Collection $collection
|
||||||
* @param array $args
|
* @param array $args
|
||||||
* @return Array Entities\Metadata
|
* @return Array Entities\Metadata
|
||||||
*/
|
*/
|
||||||
public function fetch_by_collection(Entities\Collection $collection, $args = []){
|
public function fetch_by_collection(Entities\Collection $collection, $args = [], $output = 'WP_Query'){
|
||||||
$metadata = [];
|
|
||||||
$collection_id = $collection->get_id();
|
$collection_id = $collection->get_id();
|
||||||
|
|
||||||
$args = array_merge([
|
//get parent collections
|
||||||
'meta_key' => 'collection_id',
|
$parents = get_post_ancestors( $collection_id );
|
||||||
'meta_value' => $collection_id
|
|
||||||
], $args);
|
|
||||||
|
|
||||||
$wp_query = $this->fetch($args);
|
//insert the actual collection
|
||||||
|
$parents[] = $collection_id;
|
||||||
|
|
||||||
if ( $wp_query->have_posts() ){
|
//search for default metadata
|
||||||
while ( $wp_query->have_posts() ) {
|
$parents[] = $this->get_default_metadata_attribute();
|
||||||
$wp_query->the_post();
|
|
||||||
$metadata[] = new Entities\Metadata( get_the_ID() );
|
$meta_query = array(
|
||||||
}
|
'key' => 'collection_id',
|
||||||
|
'value' => $parents,
|
||||||
|
'compare' => 'IN',
|
||||||
|
);
|
||||||
|
|
||||||
|
if( isset( $args['meta_query'] ) ){
|
||||||
|
$args['meta_query'][] = $meta_query;
|
||||||
|
}else{
|
||||||
|
$args['meta_query'] = array( $meta_query );
|
||||||
}
|
}
|
||||||
|
|
||||||
return $metadata;
|
return $this->fetch( $args, $output );
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function update($object){
|
public function update($object){
|
||||||
|
|
|
@ -71,6 +71,28 @@ abstract class Repository {
|
||||||
return new $this->entities_type($obj->WP_Post);
|
return new $this->entities_type($obj->WP_Post);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param \WP_Query $WP_Query
|
||||||
|
* @param string $output
|
||||||
|
* @return array|\WP_Query
|
||||||
|
*/
|
||||||
|
public function fetch_output(\WP_Query $WP_Query, $output = 'WP_Query' ){
|
||||||
|
if( $output === 'WP_Query'){
|
||||||
|
return $WP_Query;
|
||||||
|
}else if( $output === 'OBJECT' ) {
|
||||||
|
$result = [];
|
||||||
|
|
||||||
|
if ( $WP_Query->have_posts() ){
|
||||||
|
while ( $WP_Query->have_posts() ) {
|
||||||
|
$WP_Query->the_post();
|
||||||
|
$result[] = new $this->entities_type( get_the_ID() );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $object
|
* @param $object
|
||||||
* @return mixed
|
* @return mixed
|
||||||
|
|
|
@ -145,10 +145,11 @@ class Taxonomies extends Repository {
|
||||||
* Taxonomies are stored as posts. Check WP_Query docs
|
* Taxonomies are stored as posts. Check WP_Query docs
|
||||||
* to learn all args accepted in the $args parameter
|
* to learn all args accepted in the $args parameter
|
||||||
*
|
*
|
||||||
* @param array $args WP_Query args || int $args the taxonomy id
|
* @param array $args WP_Query args | int $args the taxonomy id
|
||||||
* @return Array of Entities\Taxonomy objects || Entities\Taxonomy
|
* @param string $output One of 2 pre-defined constants 'WP_Query' | 'OBJECT' . Defaults to WP_Query
|
||||||
|
* @return \WP_Query|Array an instance of wp query OR array of entities;
|
||||||
*/
|
*/
|
||||||
public function fetch( $args ) {
|
public function fetch( $args, $output = 'WP_Query' ) {
|
||||||
|
|
||||||
if( is_numeric($args) ){
|
if( is_numeric($args) ){
|
||||||
return new Entities\Taxonomy($args);
|
return new Entities\Taxonomy($args);
|
||||||
|
@ -161,7 +162,8 @@ class Taxonomies extends Repository {
|
||||||
|
|
||||||
$args['post_type'] = Entities\Taxonomy::get_post_type();
|
$args['post_type'] = Entities\Taxonomy::get_post_type();
|
||||||
|
|
||||||
return new \WP_Query($args);;
|
$wp_query = new \WP_Query($args);
|
||||||
|
return $this->fetch_output($wp_query, $output);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -3,18 +3,18 @@
|
||||||
namespace Tainacan\Tests;
|
namespace Tainacan\Tests;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class TestCollections
|
* Class Metadata
|
||||||
*
|
*
|
||||||
* @package Test_Tainacan
|
* @package Test_Tainacan
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sample test case.
|
* Metadata test case.
|
||||||
*/
|
*/
|
||||||
class Metadata extends \WP_UnitTestCase {
|
class Metadata extends \WP_UnitTestCase {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Teste da insercao de um metadado simples sem o tipo
|
* Test insert a regular metadata with type
|
||||||
*/
|
*/
|
||||||
function test_add() {
|
function test_add() {
|
||||||
global $Tainacan_Collections, $Tainacan_Metadatas;
|
global $Tainacan_Collections, $Tainacan_Metadatas;
|
||||||
|
@ -42,9 +42,9 @@ class Metadata extends \WP_UnitTestCase {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Teste da insercao de um metadado simples com o tipo
|
* Test insert a regular metadata with type
|
||||||
*/
|
*/
|
||||||
function teste_add_type(){
|
function test_add_type(){
|
||||||
global $Tainacan_Collections, $Tainacan_Metadatas;
|
global $Tainacan_Collections, $Tainacan_Metadatas;
|
||||||
|
|
||||||
$collection = new \Tainacan\Entities\Collection();
|
$collection = new \Tainacan\Entities\Collection();
|
||||||
|
@ -70,4 +70,65 @@ class Metadata extends \WP_UnitTestCase {
|
||||||
$this->assertEquals('Tainacan\Field_Types\Text', $test->get_field_type());
|
$this->assertEquals('Tainacan\Field_Types\Text', $test->get_field_type());
|
||||||
$this->assertEquals($test->get_field_type_object(), $type);
|
$this->assertEquals($test->get_field_type_object(), $type);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
function test_hierarchy_metadata(){
|
||||||
|
global $Tainacan_Collections, $Tainacan_Metadatas;
|
||||||
|
|
||||||
|
$metadata_default = new \Tainacan\Entities\Metadata();
|
||||||
|
$collection_grandfather = new \Tainacan\Entities\Collection();
|
||||||
|
$metadata_grandfather = new \Tainacan\Entities\Metadata();
|
||||||
|
$collection_father = new \Tainacan\Entities\Collection();
|
||||||
|
$metadata_father = new \Tainacan\Entities\Metadata();
|
||||||
|
$collection_son = new \Tainacan\Entities\Collection();
|
||||||
|
$metadata_son = new \Tainacan\Entities\Metadata();
|
||||||
|
$type = new \Tainacan\Field_Types\Text();
|
||||||
|
|
||||||
|
//creating metadata default
|
||||||
|
$metadata_default->set_name('metadata default');
|
||||||
|
$metadata_default->set_collection_id( $Tainacan_Metadatas->get_default_metadata_attribute() );
|
||||||
|
$metadata_default->set_field_type_object( $type );
|
||||||
|
$Tainacan_Metadatas->insert($metadata_default);
|
||||||
|
|
||||||
|
//creating collection grandfather
|
||||||
|
$collection_grandfather->set_name('collection grandfather');
|
||||||
|
$collection_grandfather = $Tainacan_Collections->insert($collection_grandfather);
|
||||||
|
|
||||||
|
//creating metadata grandfather
|
||||||
|
$metadata_grandfather->set_name('metadata grandfather');
|
||||||
|
$metadata_grandfather->set_collection_id( $collection_grandfather->get_id() );
|
||||||
|
$metadata_grandfather->set_field_type_object( $type );
|
||||||
|
$Tainacan_Metadatas->insert($metadata_grandfather);
|
||||||
|
|
||||||
|
//creating collection father
|
||||||
|
$collection_father->set_name('collection father');
|
||||||
|
$collection_father->set_parent( $collection_grandfather->get_id() );
|
||||||
|
$collection_father = $Tainacan_Collections->insert( $collection_father );
|
||||||
|
|
||||||
|
$this->assertEquals( $collection_grandfather->get_id(), $collection_father->get_parent() );
|
||||||
|
|
||||||
|
//creating metadata father
|
||||||
|
$metadata_father->set_name('metadata father');
|
||||||
|
$metadata_father->set_collection_id( $collection_father->get_id() );
|
||||||
|
$metadata_father->set_field_type_object( $type );
|
||||||
|
$Tainacan_Metadatas->insert($metadata_father);
|
||||||
|
|
||||||
|
//creating collection son
|
||||||
|
$collection_son->set_name('collection son');
|
||||||
|
$collection_son->set_parent( $collection_father->get_id() );
|
||||||
|
$collection_son = $Tainacan_Collections->insert($collection_son);
|
||||||
|
|
||||||
|
$this->assertEquals( $collection_father->get_id(), $collection_son->get_parent() );
|
||||||
|
|
||||||
|
//creating metadata son
|
||||||
|
$metadata_son->set_name('metadata son');
|
||||||
|
$metadata_son->set_collection_id( $collection_son->get_id() );
|
||||||
|
$metadata_son->set_field_type_object( $type );
|
||||||
|
$Tainacan_Metadatas->insert($metadata_son);
|
||||||
|
|
||||||
|
$retrieve_metadata = $Tainacan_Metadatas->fetch_by_collection( $collection_son, [], 'OBJECT' );
|
||||||
|
|
||||||
|
$this->assertEquals( 4, sizeof( $retrieve_metadata ) );
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue