Add support for LIKE operator to Tax_Query in Tainacan API #266

This commit is contained in:
leogermani 2019-07-29 15:06:05 -03:00
parent 5ed051103e
commit 1c1f6d3e35
3 changed files with 206 additions and 74 deletions

View File

@ -11,6 +11,7 @@ class REST_Controller extends \WP_REST_Controller {
public function __construct() { public function __construct() {
$this->namespace = TAINACAN_REST_NAMESPACE; $this->namespace = TAINACAN_REST_NAMESPACE;
add_action('rest_api_init', array($this, 'register_routes')); add_action('rest_api_init', array($this, 'register_routes'));
add_filter('tainacan-api-prepare-items-args', array($this, 'add_support_to_tax_query_like'));
} }
/** /**
@ -144,9 +145,45 @@ class REST_Controller extends \WP_REST_Controller {
$args['perm'] = 'readable'; $args['perm'] = 'readable';
return apply_filters('tainacan-api-prepare-items-args', $args, $request);
}
public function add_support_to_tax_query_like($args) {
if (!isset($args['tax_query']) || !is_array($args['tax_query'])) {
return $args; return $args;
} }
$new_tax_query = [];
foreach ($args['tax_query'] as $index => $tax_query) {
if ( isset($tax_query['operator']) && $tax_query['operator'] == 'LIKE' &&
isset($tax_query['terms']) && is_string($tax_query['terms']) ) {
$terms = get_terms([
'taxonomy' => $tax_query['taxonomy'],
'fields' => 'ids',
'search' => $tax_query['terms']
]);
$new_tax_query[] = [
'taxonomy' => $tax_query['taxonomy'],
'terms' => $terms,
];
} else {
$new_tax_query[] = $tax_query;
}
}
$args['tax_query'] = $new_tax_query;
return $args;
}
/** /**
* @param $mapped * @param $mapped
* @param $request * @param $request

View File

@ -286,11 +286,6 @@ class Items extends Repository {
$args['post_type'] = $cpt; $args['post_type'] = $cpt;
// In progress...
// if (isset($args['meta_query'])) {
// $args = $this->transform_meta_query_to_tax_query($args);
// }
$args = apply_filters( 'tainacan_fetch_args', $args, 'items' ); $args = apply_filters( 'tainacan_fetch_args', $args, 'items' );
$wp_query = new \WP_Query( $args ); $wp_query = new \WP_Query( $args );
@ -381,75 +376,6 @@ class Items extends Repository {
return $where; return $where;
} }
public function transform_meta_query_to_tax_query($args) {
if (!isset($args['meta_query']) || !is_array($args['meta_query'])) {
return $args;
}
$metas = [];
foreach ($args['meta_query'] as $i => $meta_q) {
if (is_array($meta_q) && isset($meta_q['key']) && is_numeric($meta_q['key'])) {
$metas[$meta_q['key']] = $i;
}
}
$metadata = Metadata::get_instance()->fetch([
'metadata_type' => 'Tainacan\Metadata_Types\Taxonomy',
'post__in' => array_keys($metas)
], 'OBJECT');
if (empty($metadata)) {
return $args;
}
if (!isset($args['tax_query'])) {
$args['tax_query'] = [];
}
foreach ($metadata as $metadatum) {
if ( isset($metas[$metadatum->get_id()]) ) {
$index = $metas[$metadatum->get_id()];
$metaquery = $args['meta_query'][$index];
$options = $metadatum->get_metadata_type_options();
$tax_id = $options['taxonomy_id'];
$tax_slug = Taxonomies::get_instance()->get_db_identifier_by_id($tax_id);
if (!isset($metaquery['compare']) || $metaquery['compare'] == '=' || $metaquery['compare'] == 'IN' ) {
$args['tax_query'][] = [
'taxonomy' => $tax_slug,
'field' => 'name',
'terms' => $terms,
];
} elseif ( strtoupper($metaquery['compare']) == 'LIKE') {
$search['search'] = $metaquery['value'];
$terms = get_terms([
'taxonomy' => $tax_slug,
'fields' => 'ids',
'search' => $metaquery['value']
]);
$args['tax_query'][] = [
'taxonomy' => $tax_slug,
'terms' => $terms,
];
} else {
continue;
}
unset( $args['meta_query'][$index] );
}
}
return $args;
}
/** /**
* Get a default thumbnail ID from the item document. * Get a default thumbnail ID from the item document.

View File

@ -697,6 +697,175 @@ class TAINACAN_REST_Items_Controller extends TAINACAN_UnitApiTestCase {
}
/*
* @group tax_query
*/
function test_support_tax_query_like() {
$collection = $this->tainacan_entity_factory->create_entity(
'collection',
array(
'name' => 'Agile',
'description' => 'Agile methods',
'status' => 'publish'
),
true
);
$taxonomy = $this->tainacan_entity_factory->create_entity(
'taxonomy',
array(
'name' => 'taxonomy_public',
'description' => 'taxonomy_public',
'status' => 'publish'
),
true
);
$tax_meta = $this->tainacan_entity_factory->create_entity(
'metadatum',
array(
'name' => 'metadata-public',
'status' => 'publish',
'collection' => $collection,
'metadata_type' => 'Tainacan\Metadata_Types\Taxonomy',
'metadata_type_options' => [
'allow_new_terms' => 'yes',
'taxonomy_id' => $taxonomy->get_id()
],
'multiple' => 'yes'
),
true
);
$item1 = $this->tainacan_entity_factory->create_entity(
'item',
array(
'title' => 'Lean Startup',
'description' => 'Um processo ágil de criação de novos negócios.',
'collection' => $collection,
'status' => 'publish'
),
true
);
$itemMetaRepo = \Tainacan\Repositories\Item_Metadata::get_instance();
$newMeta = new \Tainacan\Entities\Item_Metadata_Entity($item1, $tax_meta);
$newMeta->set_value(['blue', 'mellon']);
$newMeta->validate();
$itemMetaRepo->insert($newMeta);
$item2 = $this->tainacan_entity_factory->create_entity(
'item',
array(
'title' => 'XXLean Startup',
'description' => 'Um processo ágil de criação de novos negócios.',
'collection' => $collection,
'status' => 'publish'
),
true
);
$newMeta = new \Tainacan\Entities\Item_Metadata_Entity($item2, $tax_meta);
$newMeta->set_value(['red', 'watermellon']);
$newMeta->validate();
$itemMetaRepo->insert($newMeta);
$item3 = $this->tainacan_entity_factory->create_entity(
'item',
array(
'title' => 'XXLean Startup',
'description' => 'Um processo ágil de criação de novos negócios.',
'collection' => $collection,
'status' => 'publish'
),
true
);
$newMeta = new \Tainacan\Entities\Item_Metadata_Entity($item3, $tax_meta);
$newMeta->set_value(['red', 'blue']);
$newMeta->validate();
$itemMetaRepo->insert($newMeta);
####################################################
$request = new \WP_REST_Request('GET', $this->namespace . '/collection/' . $collection->get_id() . '/items');
$attributes = [
'taxquery' => [
[
'taxonomy' => $taxonomy->get_db_identifier(),
'operator' => 'LIKE',
'terms' => 'mellon'
]
]
];
$request->set_query_params($attributes);
$response = $this->server->dispatch($request);
$this->assertEquals(200, $response->get_status());
$data = $response->get_data()['items'];
$this->assertEquals(2, count($data));
$ids = array_map(function($e) { return $e['id']; }, $data);
$this->assertContains($item1->get_id(), $ids);
$this->assertContains($item2->get_id(), $ids);
####################################################
$request = new \WP_REST_Request('GET', $this->namespace . '/collection/' . $collection->get_id() . '/items');
$attributes = [
'taxquery' => [
[
'taxonomy' => $taxonomy->get_db_identifier(),
'operator' => 'LIKE',
'terms' => 'red'
]
]
];
$request->set_query_params($attributes);
$response = $this->server->dispatch($request);
$this->assertEquals(200, $response->get_status());
$data = $response->get_data()['items'];
$this->assertEquals(2, count($data));
$ids = array_map(function($e) { return $e['id']; }, $data);
$this->assertContains($item2->get_id(), $ids);
$this->assertContains($item3->get_id(), $ids);
####################################################
$request = new \WP_REST_Request('GET', $this->namespace . '/collection/' . $collection->get_id() . '/items');
$attributes = [
'taxquery' => [
[
'taxonomy' => $taxonomy->get_db_identifier(),
'operator' => 'LIKE',
'terms' => 'water'
]
]
];
$request->set_query_params($attributes);
$response = $this->server->dispatch($request);
$this->assertEquals(200, $response->get_status());
$data = $response->get_data()['items'];
$this->assertEquals(1, count($data));
$ids = array_map(function($e) { return $e['id']; }, $data);
$this->assertContains($item2->get_id(), $ids);
} }
} }