From d9eb5a628501d66fb70b98ae7269fdba750886b6 Mon Sep 17 00:00:00 2001 From: vnmedeiros Date: Fri, 29 Sep 2023 00:51:00 -0300 Subject: [PATCH] feat: remove disabled mapper in exposers #783 --- ...lass-tainacan-rest-exposers-controller.php | 19 +++++++++++++++++-- ...acan-rest-metadatum-mappers-controller.php | 2 +- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/classes/api/endpoints/class-tainacan-rest-exposers-controller.php b/src/classes/api/endpoints/class-tainacan-rest-exposers-controller.php index ca31cc2fe..a943e7552 100644 --- a/src/classes/api/endpoints/class-tainacan-rest-exposers-controller.php +++ b/src/classes/api/endpoints/class-tainacan-rest-exposers-controller.php @@ -42,6 +42,13 @@ class REST_Exposers_Controller extends REST_Controller { 'permission_callback' => array($this, 'get_items_permissions_check') ) )); + register_rest_route($this->namespace, '/collection/(?P[\d]+)/' . $this->rest_base, array( + array( + 'methods' => \WP_REST_Server::READABLE, + 'callback' => array($this, 'get_items'), + 'permission_callback' => array($this, 'get_items_permissions_check') + ) + )); } /** @@ -52,14 +59,22 @@ class REST_Exposers_Controller extends REST_Controller { public function get_items( $request ) { $exposers = $this->exposers->get_exposers(); + + $collection_id = isset($request['collection_id']) ? $request['collection_id'] : 'default'; + $collection = \Tainacan\Repositories\Collections::get_instance()->fetch($collection_id); + $disabled_mappers = isset($collection) ? $collection->get_disabled_mappers() : []; $response = []; foreach ($exposers as $exposer) { if ( class_exists($exposer) ) { - $e = new $exposer(); - $response[] = $e->_toArray(); + $e = (new $exposer())->_toArray(); + $e['mappers'] = array_filter($e['mappers'], function($n) use ($disabled_mappers) { + return !in_array($n, $disabled_mappers); + }); + $response[] = $e; + } } diff --git a/src/classes/api/endpoints/class-tainacan-rest-metadatum-mappers-controller.php b/src/classes/api/endpoints/class-tainacan-rest-metadatum-mappers-controller.php index a7605ac2f..7f4512ff9 100644 --- a/src/classes/api/endpoints/class-tainacan-rest-metadatum-mappers-controller.php +++ b/src/classes/api/endpoints/class-tainacan-rest-metadatum-mappers-controller.php @@ -260,7 +260,7 @@ class REST_Metadatum_Mappers_Controller extends REST_Controller { if ($disabled == true && !in_array($slug_mapper, $disabled_mappers)) { $disabled_mappers[] = $slug_mapper; } elseif ( - ($disabled == false && $key = array_search($slug_mapper, $disabled_mappers)) !== false + $disabled == false && (($key = array_search($slug_mapper, $disabled_mappers)) !== false) ) { unset($disabled_mappers[$key]); }