Now simple query are working and meta and date query are started

This commit is contained in:
weryques 2018-02-02 16:05:59 -02:00
parent fb4149c476
commit a55e15865e
8 changed files with 61 additions and 85 deletions

View File

@ -45,21 +45,66 @@ class TAINACAN_REST_Controller extends WP_REST_Controller {
}
/**
* @param $filters
* @param $map
* @param $request
*
* @return array
*/
protected function unmap_filters($filters, $map){
$unmapped = [];
protected function prepare_filters($request){
$map = [
'name' => 'title',
'title' => 'title',
'id' => 'p',
'pageid' => 'page_id',
'authorid' => 'author_id',
'authorname' => 'author_name',
'search' => 's',
'posttype' => 'post_type',
'poststatus' => 'post_status',
'offset' => 'offset',
'metaquery' => 'meta_query',
'datequery' => 'date_query',
'order' => 'order',
'orderby' => 'orderby',
'metakey' => 'meta_key',
'hide_empty' => 'hide_empty',
];
if(!empty($filters)) {
foreach ( $filters as $filter => $value ) {
$unmapped[ $map[ $filter ]['map'] ] = $value;
$meta_query = [
'key' => 'key',
'value' => 'value',
'compare' => 'compare',
'relation' => 'relation',
];
$date_query = [
'year' => 'year',
'month' => 'month',
'day' => 'month',
'week' => 'week',
'hour' => 'hour',
'minute' => 'minute',
'second' => 'second'
];
$args = [];
foreach ($map as $mapped => $mapped_v){
if(isset($request[$mapped])){
if($mapped === 'metaquery'){
foreach ($meta_query as $mapped_meta => $meta_v){
$args[$mapped_v][$meta_v] = $request[$mapped][$mapped_meta];
}
} elseif ($mapped === 'datequery') {
foreach ($date_query as $date_meta => $date_v){
$args[$mapped_v][$date_v] = $request[$mapped][$date_meta];
}
} else {
$args[ $mapped_v ] = $request[ $mapped ];
}
}
}
return $unmapped;
return $args;
}
}

View File

@ -78,17 +78,7 @@ class TAINACAN_REST_Collections_Controller extends TAINACAN_REST_Controller {
* @return WP_Error|WP_REST_Response
*/
public function get_items($request){
$args = [];
$map = $this->collections_repository->get_map();
foreach ($map as $key => $value){
if(isset($request[$key])){
$args[$value['map']] = $request[$key];
}
}
//$args = $this->unmap_filters($args, $map);
$args = $this->prepare_filters($request);
$collections = $this->collections_repository->fetch($args);
@ -368,7 +358,7 @@ class TAINACAN_REST_Collections_Controller extends TAINACAN_REST_Controller {
public function get_item_schema() {
$args = $this->collections_repository->get_map();
return apply_filters("rest_{$this->collection->get_post_type()}_collection_params", $args, $this->collection->get_post_type());
return $args;
}
}

View File

@ -174,17 +174,7 @@ class TAINACAN_REST_Fields_Controller extends TAINACAN_REST_Controller {
public function get_items( $request ) {
$collection_id = $request['collection_id'];
$args = [];
$map = $this->field_repository->get_map();
foreach ($map as $key => $value){
if(isset($request[$key], $map[$key])){
$args[$value['map']] = $request[$key];
}
}
//$args = $this->unmap_filters($args, $map);
$args = $this->prepare_filters($request);
$collection = new Entities\Collection($collection_id);

View File

@ -273,17 +273,7 @@ class TAINACAN_REST_Filters_Controller extends TAINACAN_REST_Controller {
* @return WP_Error|WP_REST_Response
*/
public function get_items( $request ) {
$args = [];
$map = $this->filter_repository->get_map();
foreach ($map as $key => $value){
if(isset($request[$key], $map[$key])){
$args[$value['map']] = $request[$key];
}
}
//$args = $this->unmap_filters($args, $map);
$args = $this->prepare_filters($request);
$filters = $this->filter_repository->fetch($args, 'OBJECT');

View File

@ -132,17 +132,7 @@ class TAINACAN_REST_Items_Controller extends TAINACAN_REST_Controller {
* @return WP_Error|WP_REST_Response
*/
public function get_items( $request ) {
$args = [];
$map = $this->items_repository->get_map();
foreach ($map as $key => $value){
if(isset($request[$key], $map[$key])){
$args[$value['map']] = $request[$key];
}
}
//$args = $this->unmap_filters($args, $map);
$args = $this->prepare_filters($request);
$collection_id = $request['collection_id'];
$items = $this->items_repository->fetch($args, $collection_id, 'WP_Query');

View File

@ -72,16 +72,7 @@ class TAINACAN_REST_Logs_Controller extends TAINACAN_REST_Controller {
* @return WP_Error|WP_REST_Response
*/
public function get_items( $request ) {
$args = [];
$map = $this->logs_repository->get_map();
foreach ($map as $key => $value){
if(isset($request[$key], $map[$key])){
$args[$value['map']] = $request[$key];
}
}
$args = $this->prepare_filters($request);
$logs = $this->logs_repository->fetch($args, 'OBJECT');

View File

@ -204,17 +204,7 @@ class TAINACAN_REST_Taxonomies_Controller extends TAINACAN_REST_Controller {
* @return WP_Error|WP_REST_Response
*/
public function get_items( $request ) {
$args = [];
$map = $this->taxonomy_repository->get_map();
foreach ($map as $key => $value){
if(isset($request[$key], $map[$key])){
$args[$value['map']] = $request[$key];
}
}
//$args = $this->unmap_filters($args, $map);
$args = $this->prepare_filters($request);
$taxonomies = $this->taxonomy_repository->fetch($args, 'OBJECT');

View File

@ -262,17 +262,7 @@ class TAINACAN_REST_Terms_Controller extends TAINACAN_REST_Controller {
$taxonomy = $this->taxonomy_repository->fetch($taxonomy_id);
$args = [];
$map = $this->terms_repository->get_map();
foreach ($map as $key => $value){
if(isset($request[$key], $map[$key])){
$args[$value['map']] = $request[$key];
}
}
//$args = $this->unmap_filters($args, $map);
$args = $this->prepare_filters($request);
$terms = $this->terms_repository->fetch($args, $taxonomy);