Update term

This commit is contained in:
weryques 2018-03-27 14:54:28 -03:00
parent f3aeab8154
commit 444f78dad7
3 changed files with 35 additions and 12 deletions

View File

@ -93,8 +93,13 @@ class Terms extends Repository {
unset($defaults['id']);
return $defaults;
}
public function insert($term){
/**
* @param Entities\Entity $term
*
* @return Entities\Entity|Entities\Term
*/
public function insert($term){
// First iterate through the native post properties
$map = $this->get_map();
foreach ($map as $prop => $mapped) {
@ -104,22 +109,39 @@ class Terms extends Repository {
}
// save post and get its ID
$term_inserted = wp_insert_term( $term->get_name(), $term->get_taxonomy(), [
'parent' => $term->get_parent(),
'description' => $term->get_description(),
]);
if(isset($term->WP_Term->term_id)){
$args = [];
foreach ($map as $prop => $mapped) {
if ($mapped['map'] != 'termmeta') {
$get_ = 'get_'. $prop;
if( !empty($term->WP_Term->{$mapped['map']}) ){
$args[$mapped['map']] = $term->$get_();
}
}
}
$term_saved = wp_update_term( $term->get_id(), $term->get_taxonomy(), $args);
} else {
$term_saved = wp_insert_term( $term->get_name(), $term->get_taxonomy(), [
'parent' => $term->get_parent(),
'description' => $term->get_description(),
] );
}
// Now run through properties stored as postmeta
foreach ($map as $prop => $mapped) {
if ($mapped['map'] == 'termmeta') {
update_term_meta($term_inserted['term_id'], $prop, wp_slash( $term->get_mapped_property($prop) ));
update_term_meta($term_saved['term_id'], $prop, wp_slash( $term->get_mapped_property($prop) ));
}
}
do_action('tainacan-insert', $term);
do_action('tainacan-insert-Term', $term);
//var_dump($term);
return new Entities\Term($term_inserted['term_id'], $term->get_taxonomy());
return new Entities\Term($term_saved['term_id'], $term->get_taxonomy());
}
/**
@ -178,7 +200,7 @@ class Terms extends Repository {
}
}
public function update($object, $tax_name = null){
public function update($object, $args = null){
return $this->insert($object);
}

View File

@ -6,7 +6,6 @@ import { eventBus } from './event-bus-web-components';
import { eventFilterBus } from './event-bus-filters';
import Buefy from 'buefy'
Vue.use(Buefy);
Vue.use(VueCustomElement);

View File

@ -97,7 +97,8 @@ class TAINACAN_REST_Terms extends TAINACAN_UnitApiTestCase {
);
$new_attributes = json_encode([
'name' => 'Trap'
'name' => 'Trap',
'user' => 7
]);
$request = new \WP_REST_Request(
@ -112,6 +113,7 @@ class TAINACAN_REST_Terms extends TAINACAN_UnitApiTestCase {
$this->assertNotEquals('Rock', $data['name']);
$this->assertEquals('Trap', $data['name']);
$this->assertEquals(7, $data['user']);
$this->assertEquals($taxonomy->get_db_identifier(), $data['taxonomy']);
}