From f3f7752f2100ae6d1dcccc2faaa67e882e4b9ee4 Mon Sep 17 00:00:00 2001 From: leogermani Date: Thu, 12 Dec 2019 16:50:35 -0300 Subject: [PATCH] allow term creatino if allow_new_terms is yes #274 --- .../class-tainacan-rest-terms-controller.php | 31 ++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/src/api/endpoints/class-tainacan-rest-terms-controller.php b/src/api/endpoints/class-tainacan-rest-terms-controller.php index 5715912e5..0fe7a3788 100644 --- a/src/api/endpoints/class-tainacan-rest-terms-controller.php +++ b/src/api/endpoints/class-tainacan-rest-terms-controller.php @@ -138,7 +138,22 @@ class REST_Terms_Controller extends REST_Controller { $taxonomy = $this->taxonomy_repository->fetch($request['taxonomy_id']); if ($taxonomy instanceof Entities\Taxonomy) { - return $taxonomy->can_edit(); + if ( $taxonomy->can_edit() ) { + return true; + } + + $body = json_decode($request->get_body(), true); + + if ( isset( $body['metadatum_id'] ) && isset( $body['item_id'] ) ) { + $metadatum = \tainacan_metadata()->fetch( $body['metadatum_id'] ); + $item = \tainacan_items()->fetch( $body['item_id'] ); + if ( $metadatum instanceof Entities\Metadatum ) { + $options = $metadatum->get_metadata_type_options(); + if ( isset( $options['allow_new_terms'] ) && $options['allow_new_terms'] == 'yes' ) { + return $item->can_edit() && $taxonomy->get_allow_insert() == 'yes'; + } + } + } } return false; @@ -430,6 +445,20 @@ class REST_Terms_Controller extends REST_Controller { } $endpoint_args = $map; + + if ($method === \WP_REST_Server::CREATABLE) { + $endpoint_args['metadatum_id'] = [ + 'required' => false, + 'type' => 'integer', + 'description' => __('If term is being created in the context of a Taxonomy metadatum, inform its ID') + ]; + $endpoint_args['item_id'] = [ + 'required' => false, + 'type' => 'integer', + 'description' => __('If term is being created in the context of a Taxonomy metadatum, inform the ID of the item being edited') + ]; + } + } return $endpoint_args;