From 2fd4bb0ad6d7fcdc2a52c075914d64ccc4da5ea8 Mon Sep 17 00:00:00 2001 From: Leo Germani Date: Tue, 30 Jul 2019 15:35:23 -0300 Subject: [PATCH] add support to NOT LIKE operator in tax_query #266 --- src/api/class-tainacan-rest-controller.php | 17 ++++++ tests/test-api-items.php | 68 ++++++++++++++++++++++ 2 files changed, 85 insertions(+) diff --git a/src/api/class-tainacan-rest-controller.php b/src/api/class-tainacan-rest-controller.php index ed121ad74..6e24dab82 100644 --- a/src/api/class-tainacan-rest-controller.php +++ b/src/api/class-tainacan-rest-controller.php @@ -172,6 +172,23 @@ class REST_Controller extends \WP_REST_Controller { 'terms' => $terms, ]; + } elseif ( isset($tax_query['operator']) && $tax_query['operator'] == 'NOT LIKE' && + isset($tax_query['terms']) && is_string($tax_query['terms']) ) { + + $terms = get_terms([ + 'taxonomy' => $tax_query['taxonomy'], + 'fields' => 'ids', + 'search' => $tax_query['terms'] + ]); + if ($terms) { + $new_tax_query[] = [ + 'taxonomy' => $tax_query['taxonomy'], + 'terms' => $terms, + 'operator' => 'NOT IN' + ]; + } + + } else { $new_tax_query[] = $tax_query; } diff --git a/tests/test-api-items.php b/tests/test-api-items.php index b707d4520..f9a1f93c2 100644 --- a/tests/test-api-items.php +++ b/tests/test-api-items.php @@ -866,6 +866,74 @@ class TAINACAN_REST_Items_Controller extends TAINACAN_UnitApiTestCase { $ids = array_map(function($e) { return $e['id']; }, $data); $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' => 'NOT 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(1, count($data)); + $ids = array_map(function($e) { return $e['id']; }, $data); + $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' => 'NOT LIKE', + 'terms' => '__does_not_exists' + ] + ] + ]; + + $request->set_query_params($attributes); + $response = $this->server->dispatch($request); + + $this->assertEquals(200, $response->get_status()); + $data = $response->get_data()['items']; + + $this->assertEquals(3, count($data)); + + #################################################### + + $request = new \WP_REST_Request('GET', $this->namespace . '/collection/' . $collection->get_id() . '/items'); + + $attributes = [ + 'taxquery' => [ + [ + 'taxonomy' => $taxonomy->get_db_identifier(), + 'operator' => 'LIKE', + 'terms' => '__does_not_exists' + ] + ] + ]; + + $request->set_query_params($attributes); + $response = $this->server->dispatch($request); + + $this->assertEquals(200, $response->get_status()); + $data = $response->get_data()['items']; + + $this->assertEquals(0, count($data)); + } }