diff --git a/src/api/endpoints/class-tainacan-rest-terms-controller.php b/src/api/endpoints/class-tainacan-rest-terms-controller.php index 90e9cb06d..a267f895d 100644 --- a/src/api/endpoints/class-tainacan-rest-terms-controller.php +++ b/src/api/endpoints/class-tainacan-rest-terms-controller.php @@ -105,9 +105,7 @@ class TAINACAN_REST_Terms_Controller extends TAINACAN_REST_Controller { $this->prepare_item_for_database($to_prepare); if($this->term->validate()){ - $term_id = $this->terms_repository->insert($this->term); - - $term_inserted = $this->terms_repository->fetch($term_id, $taxonomy); + $term_inserted = $this->terms_repository->insert($this->term); return new WP_REST_Response($this->prepare_item_for_response($term_inserted, $request), 200); } else { diff --git a/src/classes/entities/class-tainacan-term.php b/src/classes/entities/class-tainacan-term.php index bf5f8bfc6..173452f8c 100644 --- a/src/classes/entities/class-tainacan-term.php +++ b/src/classes/entities/class-tainacan-term.php @@ -53,6 +53,9 @@ class Term extends Entity { * @return integer */ function get_id() { + return $this->get_term_id(); + } + function get_term_id() { return $this->get_mapped_property('term_id'); } diff --git a/src/classes/repositories/class-tainacan-terms.php b/src/classes/repositories/class-tainacan-terms.php index 5f1c79498..e0d04acfd 100644 --- a/src/classes/repositories/class-tainacan-terms.php +++ b/src/classes/repositories/class-tainacan-terms.php @@ -35,6 +35,7 @@ class Terms extends Repository { 'title' => __('Parent', 'tainacan'), 'type' => 'integer', 'description'=> __('The parent of the term', 'tainacan'), + 'default' => 0, 'validation' => '' ], 'description' => [ @@ -86,9 +87,9 @@ class Terms extends Repository { } // save post and get its ID - $term_inserted = wp_insert_term( $term->WP_Term->name, $term->WP_Term->taxonomy, [ - 'parent' => ( isset( $term->WP_Term->parent ) ) ? $term->WP_Term->parent : 0, - 'description' => ( isset( $term->WP_Term->description ) ) ? $term->WP_Term->description : '', + $term_inserted = wp_insert_term( $term->get_name(), $term->get_taxonomy(), [ + 'parent' => $term->get_parent(), + 'description' => $term->get_description(), ]); // Now run through properties stored as postmeta @@ -100,8 +101,8 @@ class Terms extends Repository { do_action('tainacan-insert', $term); do_action('tainacan-insert-Term', $term); - - return $term_inserted['term_id']; + //var_dump($term); + return new Entities\Term($term_inserted['term_id'], $term->get_taxonomy()); } /** @@ -161,7 +162,7 @@ class Terms extends Repository { } public function update($object, $tax_name = null){ - return new Entities\Term($this->insert($object), $tax_name); + return $this->insert($object); } public function delete($args){ diff --git a/tests/test-api-queries.php b/tests/test-api-queries.php index d11de41ba..62c2fab87 100644 --- a/tests/test-api-queries.php +++ b/tests/test-api-queries.php @@ -283,8 +283,8 @@ class TAINACAN_REST_Queries extends TAINACAN_UnitApiTestCase { ] ]; - wp_set_post_terms($itemA1->get_id(), $termA, $taxonomyA->get_db_identifier()); - wp_set_post_terms($itemA2->get_id(), $termA, $taxonomyA->get_db_identifier()); + wp_set_post_terms($itemA1->get_id(), $termA->get_term_id(), $taxonomyA->get_db_identifier()); + wp_set_post_terms($itemA2->get_id(), $termA->get_term_id(), $taxonomyA->get_db_identifier()); $tax_query_request_collections = new \WP_REST_Request('GET', $this->namespace . '/collection/' . $collectionA->get_id() . '/items'); $tax_query_request_collections->set_query_params($tax_query); diff --git a/tests/test-api-terms.php b/tests/test-api-terms.php index 2ddca5e3e..e8cf31f5d 100644 --- a/tests/test-api-terms.php +++ b/tests/test-api-terms.php @@ -61,7 +61,7 @@ class TAINACAN_REST_Terms extends TAINACAN_UnitApiTestCase { true ); - $request = new \WP_REST_Request('DELETE', $this->namespace . '/taxonomy/' . $taxonomy->get_id() . '/terms/' . $term); + $request = new \WP_REST_Request('DELETE', $this->namespace . '/taxonomy/' . $taxonomy->get_id() . '/terms/' . $term->get_term_id()); $response = $this->server->dispatch($request); @@ -69,7 +69,7 @@ class TAINACAN_REST_Terms extends TAINACAN_UnitApiTestCase { $this->assertTrue($data); - $term = get_term($term); + $term = get_term($term->get_term_id()); $this->assertNull($term); } @@ -101,7 +101,7 @@ class TAINACAN_REST_Terms extends TAINACAN_UnitApiTestCase { ]); $request = new \WP_REST_Request( - 'PATCH', $this->namespace . '/taxonomy/' . $taxonomy->get_id() . '/terms/' . $term + 'PATCH', $this->namespace . '/taxonomy/' . $taxonomy->get_id() . '/terms/' . $term->get_term_id() ); $request->set_body($new_attributes); @@ -169,7 +169,7 @@ class TAINACAN_REST_Terms extends TAINACAN_UnitApiTestCase { #### FETCH A TERM #### $request = new \WP_REST_Request( - 'GET', $this->namespace . '/taxonomy/' . $taxonomy->get_id() . '/terms/' . $term2 + 'GET', $this->namespace . '/taxonomy/' . $taxonomy->get_id() . '/terms/' . $term2->get_term_id() ); $response = $this->server->dispatch($request); diff --git a/tests/test-taxonomies.php b/tests/test-taxonomies.php index 5ee7de57b..dd76849b1 100644 --- a/tests/test-taxonomies.php +++ b/tests/test-taxonomies.php @@ -64,7 +64,7 @@ class Taxonomies extends TAINACAN_UnitTestCase { ); //retorna um objeto da classe Tainacan_Term - $test = $Tainacan_Terms->fetch($term, $taxonomy_test); + $test = $Tainacan_Terms->fetch($term->get_term_id(), $taxonomy_test); $this->assertEquals( $test->get_name(), 'Rock' ); $this->assertEquals( $test->get_user(), 56 ); }