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 547b70978..bd8099772 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 @@ -100,8 +100,8 @@ class REST_Metadatum_Mappers_Controller extends REST_Controller { * @return \WP_Error|\WP_REST_Response */ public function get_items( $request ) { - $collection_id = isset($request['collection_id']) ? $request['collection_id'] : ''; - $disabled_mappers = get_post_meta($collection_id, 'disabled_mappers'); + $collection_id = isset($request['collection_id']) ? $request['collection_id'] : 'default'; + $disabled_mappers = get_post_meta($collection_id, 'disabled_mappers', false); $Tainacan_Mappers = \Tainacan\Mappers_Handler::get_instance(); $metadatum_mappers = $Tainacan_Mappers->get_mappers( 'OBJECT' ); @@ -110,7 +110,7 @@ class REST_Metadatum_Mappers_Controller extends REST_Controller { foreach ($metadatum_mappers as $metadatum_mapper){ if($metadatum_mapper->show_ui) { $mapper = $this->prepare_item_for_response($metadatum_mapper, $request); - $mapper['disabled'] = in_array($metadatum_mapper->slug, $disabled_mappers) ? true : false; + $mapper['disabled'] = !empty($disabled_mappers) && in_array($metadatum_mapper->slug, $disabled_mappers) ? true : false; array_push($prepared, $mapper); } } @@ -123,16 +123,29 @@ class REST_Metadatum_Mappers_Controller extends REST_Controller { * @return \WP_Error|\WP_REST_Response */ public function get_item( $request ) { + if( !isset( $request['slug'] ) ) { + return new \WP_REST_Response([ + 'error_message' => __('Slug mapper not informed', 'tainacan'), + ], 422); + } + + $collection_id = isset($request['collection_id']) ? $request['collection_id'] : 'default'; + $disabled_mappers = get_post_meta($collection_id, 'disabled_mappers', false); $Tainacan_Mappers = \Tainacan\Mappers_Handler::get_instance(); - $metadatum_mappers = $Tainacan_Mappers->get_mappers( 'OBJECT' ); - $prepared = []; - foreach ($metadatum_mappers as $metadatum_mapper){ - if($metadatum_mapper->show_ui) array_push($prepared, $this->prepare_item_for_response($metadatum_mapper, $request)); + $slug_mapper = $request['slug']; + $mapper_key = array_search($slug_mapper, array_column($metadatum_mappers, 'slug') ); + if( $mapper_key === false ) { + return new \WP_REST_Response([ + 'error_message' => __('Mapper not found', 'tainacan'), + ], 400); } + $mapper = $metadatum_mappers[$mapper_key]; + $prepared_mapper = $this->prepare_item_for_response($mapper, $request); + $prepared_mapper['disabled'] = !empty($disabled_mappers) && in_array($slug_mapper, $disabled_mappers) ? true : false; - return new \WP_REST_Response($prepared, 200); + return new \WP_REST_Response($prepared_mapper, 200); } /** diff --git a/src/classes/mappers/class-tainacan-dublin-core.php b/src/classes/mappers/class-tainacan-dublin-core.php index 6563765ae..4870094b4 100644 --- a/src/classes/mappers/class-tainacan-dublin-core.php +++ b/src/classes/mappers/class-tainacan-dublin-core.php @@ -10,6 +10,7 @@ namespace Tainacan\Mappers; class Dublin_Core extends Mapper { public $slug = 'dublin-core'; public $name = 'Dublin Core'; + public $description = 'The mapper of the Dublin Core standard'; public $allow_extra_metadata = true; public $context_url = 'http://dublincore.org/documents/dcmi-terms/'; public $header = ''; diff --git a/src/classes/mappers/class-tainacan-mapper.php b/src/classes/mappers/class-tainacan-mapper.php index 16b54689a..d9e8f7543 100644 --- a/src/classes/mappers/class-tainacan-mapper.php +++ b/src/classes/mappers/class-tainacan-mapper.php @@ -4,7 +4,8 @@ namespace Tainacan\Mappers; abstract class Mapper { public $slug = null; // Slug of Mapper, used as option on api call - public $name = null; // Public name do mapper + public $name = null; // Public name of the mapper + public $description = null; // Public descript of the mapper public $allow_extra_metadata = true; // Allow more metadatum to be register public $context_url = null; // URL of mapper documentation public $type = ''; // The Class of the ontology that this mapping refers to. For example `CreativeWork`, which is a class of Schema.org if applied @@ -39,6 +40,7 @@ abstract class Mapper { return [ 'slug' => $this->slug, 'name' => $this->name, + 'description' => $this->description, 'allow_extra_metadata' => $this->allow_extra_metadata, 'context_url' => $this->context_url, 'metadata' => $this->metadata, diff --git a/src/classes/mappers/class-tainacan-wiki-data.php b/src/classes/mappers/class-tainacan-wiki-data.php index dbe88f942..780881c9f 100644 --- a/src/classes/mappers/class-tainacan-wiki-data.php +++ b/src/classes/mappers/class-tainacan-wiki-data.php @@ -10,6 +10,7 @@ namespace Tainacan\Mappers; class Wiki_Data extends Mapper { public $slug = 'wiki-data'; public $name = 'WikiData'; + public $description = 'The model mapper using the WikiData source'; public $allow_extra_metadata = true; public $context_url = 'http://??????'; public $header = '??????';