Now tax query is working
This commit is contained in:
parent
516de78640
commit
466c58e221
|
@ -92,7 +92,7 @@ class TAINACAN_REST_Controller extends WP_REST_Controller {
|
|||
'offset' => 'offset',
|
||||
'metaquery' => 'meta_query',
|
||||
'datequery' => 'date_query',
|
||||
'taxquery' => 'taxquery',
|
||||
'taxquery' => 'tax_query',
|
||||
'order' => 'order',
|
||||
'orderby' => 'orderby',
|
||||
'metakey' => 'meta_key',
|
||||
|
|
|
@ -78,6 +78,21 @@ class TAINACAN_REST_Items_Controller extends TAINACAN_REST_Controller {
|
|||
));
|
||||
}
|
||||
|
||||
|
||||
private function add_terms_to_item($item_object, $item_array){
|
||||
$item_terms = $item_object->get_terms();
|
||||
|
||||
foreach ($item_terms as $index => $term){
|
||||
$term_id = $term['term_id'];
|
||||
|
||||
$item_array['terms'][$term_id]['name'] = $term['name'];
|
||||
$item_array['terms'][$term_id]['description'] = $term['description'];
|
||||
$item_array['terms'][$term_id]['taxonomy'] = $term['taxonomy'];
|
||||
}
|
||||
|
||||
return $item_array;
|
||||
}
|
||||
|
||||
private function add_metadata_to_item($item_object, $item_array){
|
||||
$item_metadata = $item_object->get_fields();
|
||||
|
||||
|
@ -85,8 +100,8 @@ class TAINACAN_REST_Items_Controller extends TAINACAN_REST_Controller {
|
|||
$field = $me->get_field();
|
||||
$slug = $field->get_slug();
|
||||
|
||||
$item_array['metadata'][$slug]['name'] = $field->get_name();
|
||||
$item_array['metadata'][$slug]['value'] = $me->get_value();
|
||||
$item_array['metadata'][$slug]['name'] = $field->get_name();
|
||||
$item_array['metadata'][$slug]['value'] = $me->get_value();
|
||||
$item_array['metadata'][$slug]['multiple'] = $field->get_multiple();
|
||||
}
|
||||
|
||||
|
@ -107,7 +122,8 @@ class TAINACAN_REST_Items_Controller extends TAINACAN_REST_Controller {
|
|||
$item_arr['current_user_can_edit'] = $item->can_edit();
|
||||
}
|
||||
|
||||
return $this->add_metadata_to_item($item, $item_arr);
|
||||
$prep = $this->add_metadata_to_item($item, $item_arr);
|
||||
return $this->add_terms_to_item($item, $prep);
|
||||
}
|
||||
|
||||
return $item;
|
||||
|
@ -150,6 +166,7 @@ class TAINACAN_REST_Items_Controller extends TAINACAN_REST_Controller {
|
|||
|
||||
$limited_item = $this->get_only_needed_attributes($item, $map, $request['context']);
|
||||
$limited_item = $this->add_metadata_to_item($item, $limited_item);
|
||||
$limited_item = $this->add_terms_to_item($item, $limited_item);
|
||||
|
||||
array_push($response, $limited_item);
|
||||
}
|
||||
|
|
|
@ -28,6 +28,20 @@ class Item extends Entity {
|
|||
return 'Hello, my name is '. $this->get_title();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $value
|
||||
*/
|
||||
function set_terms($value){
|
||||
$this->set_mapped_property('terms', $value);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed|null
|
||||
*/
|
||||
function get_terms(){
|
||||
return $this->get_mapped_property('terms');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed|null
|
||||
*/
|
||||
|
|
|
@ -76,6 +76,12 @@ class Items extends Repository {
|
|||
'type' => 'array',
|
||||
'description' => __('The item attachments')
|
||||
],
|
||||
'terms' => [
|
||||
'map' => 'terms',
|
||||
'title' => __('Term IDs', 'tainacan'),
|
||||
'type' => 'array',
|
||||
'description' => __('The item term IDs', 'tainacan'),
|
||||
],
|
||||
//'collection' => 'relation...',
|
||||
// field .. field...
|
||||
]);
|
||||
|
@ -127,7 +133,7 @@ class Items extends Repository {
|
|||
|
||||
// iterate through the native post properties
|
||||
foreach ($map as $prop => $mapped) {
|
||||
if ($mapped['map'] != 'meta' && $mapped['map'] != 'meta_multi') {
|
||||
if ($mapped['map'] != 'meta' && $mapped['map'] != 'meta_multi' && $mapped['map'] != 'terms') {
|
||||
$item->WP_Post->{$mapped['map']} = $item->get_mapped_property($prop);
|
||||
}
|
||||
}
|
||||
|
@ -153,7 +159,22 @@ class Items extends Repository {
|
|||
add_post_meta($id, $prop, wp_slash( $value ));
|
||||
}
|
||||
}
|
||||
}
|
||||
} elseif($mapped['map'] == 'terms'){
|
||||
$values = $item->get_mapped_property($prop);
|
||||
|
||||
if($values) {
|
||||
$res = [];
|
||||
|
||||
foreach ($values as $value){
|
||||
$taxonomy = get_term($value)->taxonomy;
|
||||
$res[] = wp_set_post_terms( $item->WP_Post->ID, $value, $taxonomy );
|
||||
}
|
||||
|
||||
if ( ! is_array( $res ) ) {
|
||||
throw new \InvalidArgumentException( 'The id of post or taxonomy name or term name may be invalid. Response = ' . $res );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
do_action('tainacan-insert', $item);
|
||||
|
|
|
@ -69,12 +69,12 @@ abstract class Repository {
|
|||
throw new \Exception('Entities must be validated before you can save them');
|
||||
// TODO: Throw Warning saying you must validate object before insert()
|
||||
}
|
||||
|
||||
|
||||
$map = $this->get_map();
|
||||
|
||||
// First iterate through the native post properties
|
||||
foreach ($map as $prop => $mapped) {
|
||||
if ($mapped['map'] != 'meta' && $mapped['map'] != 'meta_multi') {
|
||||
if ($mapped['map'] != 'meta' && $mapped['map'] != 'meta_multi' && $mapped['map'] != 'terms') {
|
||||
$obj->WP_Post->{$mapped['map']} = $obj->get_mapped_property($prop);
|
||||
}
|
||||
}
|
||||
|
@ -92,7 +92,6 @@ abstract class Repository {
|
|||
|
||||
// Now run through properties stored as postmeta
|
||||
foreach ($map as $prop => $mapped) {
|
||||
|
||||
if ($mapped['map'] == 'meta' || $mapped['map'] == 'meta_multi') {
|
||||
$this->insert_metadata($obj, $prop);
|
||||
}
|
||||
|
@ -286,11 +285,9 @@ abstract class Repository {
|
|||
$property = get_term_meta($entity->WP_Term->term_id, $prop, true);
|
||||
} elseif ( isset( $entity->WP_Post )) {
|
||||
if($mapped == 'thumbnail'){
|
||||
if(isset($entity->WP_Post->ID)) {
|
||||
$property = get_the_post_thumbnail_url($entity->WP_Post->ID, 'full');
|
||||
}
|
||||
$property = isset($entity->WP_Post->ID) ? get_the_post_thumbnail_url($entity->WP_Post->ID, 'full') : null;
|
||||
} elseif($mapped == 'attachments'){
|
||||
if(isset($entity->WP_Post) && isset($entity->WP_Post->ID)){
|
||||
if(isset($entity->WP_Post->ID)){
|
||||
$attachments_query = [
|
||||
'post_type' => 'attachment',
|
||||
'post_per_page' => -1,
|
||||
|
@ -317,6 +314,24 @@ abstract class Repository {
|
|||
|
||||
$property = $attachments_prepared;
|
||||
}
|
||||
} elseif ($mapped === 'terms'){
|
||||
$taxonomies = get_taxonomies('', 'names');
|
||||
|
||||
$terms_prepared = [];
|
||||
foreach($taxonomies as $taxonomy){
|
||||
if(stristr($taxonomy, 'tnc_tax_')) {
|
||||
$terms = isset( $entity->WP_Post->ID ) ? wp_get_post_terms( $entity->WP_Post->ID, $taxonomy ) : null;
|
||||
|
||||
if ( is_array( $terms ) ) {
|
||||
foreach ( $terms as $term ) {
|
||||
$term = new Entities\Term( $term->term_id, $term->taxonomy );
|
||||
array_push( $terms_prepared, $term->__toArray() );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$property = $terms_prepared;
|
||||
} else {
|
||||
$property = isset($entity->WP_Post->$mapped) ? $entity->WP_Post->$mapped : null;
|
||||
}
|
||||
|
@ -532,15 +547,16 @@ abstract class Repository {
|
|||
}
|
||||
return user_can($user, $entity_cap->publish_posts, $entity->get_id());
|
||||
}
|
||||
|
||||
/**
|
||||
* Compare two repository entities
|
||||
*
|
||||
* @param Entity|integer|\WP_Post $old default ($which = 0) to self compare with stored entity
|
||||
* @param Entity|integer|\WP_Post $new
|
||||
*
|
||||
* @return array List of diff values
|
||||
*/
|
||||
|
||||
/**
|
||||
* Compare two repository entities
|
||||
*
|
||||
* @param Entity|integer|\WP_Post $old default ($which = 0) to self compare with stored entity
|
||||
* @param Entity|integer|\WP_Post $new
|
||||
*
|
||||
* @return array List of diff values
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function diff($old = 0, $new) {
|
||||
$old_entity = null;
|
||||
if($old === 0) { // self diff or other entity?
|
||||
|
|
|
@ -33,7 +33,7 @@ trait Entity_Collections_Relation {
|
|||
}
|
||||
|
||||
public function set_collections_ids(Array $value) {
|
||||
$this->set_mapped_property('collection_id', $value);
|
||||
$this->set_mapped_property('collections_ids', $value);
|
||||
$this->collections = null;
|
||||
}
|
||||
|
||||
|
|
|
@ -3,13 +3,34 @@
|
|||
namespace Tainacan\Tests;
|
||||
|
||||
/**
|
||||
* @group api
|
||||
* @group queries
|
||||
* **/
|
||||
class TAINACAN_REST_Queries extends TAINACAN_UnitApiTestCase {
|
||||
|
||||
public function test_queries(){
|
||||
|
||||
// Populate the database
|
||||
|
||||
$collectionB = $this->tainacan_entity_factory->create_entity(
|
||||
'collection',
|
||||
[
|
||||
'name' => 'B',
|
||||
'description' => 'Collection B',
|
||||
'status' => 'publish'
|
||||
],
|
||||
true
|
||||
);
|
||||
|
||||
$collectionC = $this->tainacan_entity_factory->create_entity(
|
||||
'collection',
|
||||
[
|
||||
'name' => 'C',
|
||||
'description' => 'Collection C',
|
||||
'status' => 'private'
|
||||
],
|
||||
true
|
||||
);
|
||||
|
||||
$collectionA = $this->tainacan_entity_factory->create_entity(
|
||||
'collection',
|
||||
[
|
||||
|
@ -20,6 +41,35 @@ class TAINACAN_REST_Queries extends TAINACAN_UnitApiTestCase {
|
|||
true
|
||||
);
|
||||
|
||||
// Create Taxonomy
|
||||
$taxonomyA = $this->tainacan_entity_factory->create_entity(
|
||||
'taxonomy',
|
||||
array(
|
||||
'name' => 'Tax A',
|
||||
'description' => 'A taxonomy',
|
||||
'allow_insert' => 'yes',
|
||||
'status' => 'publish',
|
||||
'collections_ids' => [
|
||||
$collectionA->get_id(),
|
||||
$collectionC->get_id(),
|
||||
$collectionB->get_id()
|
||||
]
|
||||
),
|
||||
true
|
||||
);
|
||||
// Create Term
|
||||
|
||||
$termA = $this->tainacan_entity_factory->create_entity(
|
||||
'term',
|
||||
array(
|
||||
'taxonomy' => $taxonomyA->get_db_identifier(),
|
||||
'name' => 'Term A',
|
||||
'user' => get_current_user_id(),
|
||||
),
|
||||
true
|
||||
);
|
||||
//
|
||||
|
||||
// Create Items
|
||||
$itemA1 = $this->tainacan_entity_factory->create_entity(
|
||||
'item',
|
||||
|
@ -27,7 +77,8 @@ class TAINACAN_REST_Queries extends TAINACAN_UnitApiTestCase {
|
|||
'title' => 'Item A-1',
|
||||
'description' => 'Item in collection A',
|
||||
'status' => 'publish',
|
||||
'collection' => $collectionA
|
||||
'collection' => $collectionA,
|
||||
'terms' => [$termA]
|
||||
],
|
||||
true
|
||||
);
|
||||
|
@ -38,7 +89,8 @@ class TAINACAN_REST_Queries extends TAINACAN_UnitApiTestCase {
|
|||
'title' => 'Item A-2',
|
||||
'description' => 'Item in collection A',
|
||||
'status' => 'private',
|
||||
'collection' => $collectionA
|
||||
'collection' => $collectionA,
|
||||
'terms' => [$termA]
|
||||
],
|
||||
true
|
||||
);
|
||||
|
@ -110,27 +162,6 @@ class TAINACAN_REST_Queries extends TAINACAN_UnitApiTestCase {
|
|||
|
||||
//
|
||||
|
||||
$collectionB = $this->tainacan_entity_factory->create_entity(
|
||||
'collection',
|
||||
[
|
||||
'name' => 'B',
|
||||
'description' => 'Collection B',
|
||||
'status' => 'publish'
|
||||
],
|
||||
true
|
||||
);
|
||||
|
||||
$collectionC = $this->tainacan_entity_factory->create_entity(
|
||||
'collection',
|
||||
[
|
||||
'name' => 'C',
|
||||
'description' => 'Collection C',
|
||||
'status' => 'private'
|
||||
],
|
||||
true
|
||||
);
|
||||
|
||||
|
||||
// Fetch a collection with a specific name
|
||||
$name_query = ['name' => 'B'];
|
||||
|
||||
|
@ -237,6 +268,36 @@ class TAINACAN_REST_Queries extends TAINACAN_UnitApiTestCase {
|
|||
$data5 = $date_query_response_collections->get_data();
|
||||
|
||||
$this->assertCount(0, $data5);
|
||||
|
||||
/* Tax Query
|
||||
*
|
||||
* Fetch items under a taxonomy with a specific term
|
||||
*
|
||||
* */
|
||||
|
||||
$tax_query = [
|
||||
'taxquery' => [
|
||||
[
|
||||
'taxonomy' => $taxonomyA->get_db_identifier(),
|
||||
'field' => 'slug',
|
||||
'terms' => 'term-a'
|
||||
]
|
||||
]
|
||||
];
|
||||
|
||||
$tax_query_request_collections = new \WP_REST_Request('GET', $this->namespace . '/collection/' . $collectionA->get_id() . '/items');
|
||||
$tax_query_request_collections->set_query_params($tax_query);
|
||||
|
||||
$tax_query_response_collections = $this->server->dispatch($tax_query_request_collections);
|
||||
$data6 = $tax_query_response_collections->get_data();
|
||||
|
||||
$this->assertCount(2, $data6);
|
||||
|
||||
$itemsA1_A2 = [$data6[0]['title'], $data6[1]['title']];
|
||||
|
||||
$this->assertContains('Item A-1', $itemsA1_A2);
|
||||
$this->assertContains('Item A-2', $itemsA1_A2);
|
||||
$this->assertNotContains('Item A-3', $itemsA1_A2);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue