This commit is contained in:
weryques 2017-11-24 17:07:28 -02:00
commit 8a84bcc4df
4 changed files with 19 additions and 47 deletions

View File

@ -6,7 +6,7 @@ use Tainacan\Entities;
defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
class Items extends Repository {
protected $entities_type = '\Tainacan\Entities\Item';
public function get_map() {
return [
'id' => [
@ -53,47 +53,7 @@ class Items extends Repository {
}
public function insert($item) {
$map = $this->get_map();
// get collection to determine post type
$collection = $item->get_collection();
if (!$collection){
return false;
}
$cpt = $collection->get_db_identifier();
// iterate through the native post properties
foreach ($map as $prop => $mapped) {
if ($mapped['map'] != 'meta' && $mapped['map'] != 'meta_multi') {
$item->WP_Post->{$mapped['map']} = $item->get_mapped_property($prop);
}
}
// save post and geet its ID
$item->WP_Post->post_type = $cpt;
$item->WP_Post->post_status = 'publish';
$id = wp_insert_post($item->WP_Post);
$item->WP_Post = get_post($id);
// Now run through properties stored as postmeta
foreach ($map as $prop => $mapped) {
if ($mapped['map'] == 'meta') {
update_post_meta($id, $prop, $item->get_mapped_property($prop));
} elseif ($mapped['map'] == 'meta_multi') {
$values = $item->get_mapped_property($prop);
delete_post_meta($id, $prop);
if (is_array($values)){
foreach ($values as $value){
add_post_meta($id, $prop, $value);
}
}
}
}
$new_item = parent::insert($item);
// save metadata
$metadata = $item->get_metadata();
@ -103,8 +63,7 @@ class Items extends Repository {
$Tainacan_Item_Metadata->insert($meta);
}
// return a brand new object
return new Entities\Item($item->WP_Post);
return $new_item;
}
public function fetch($args = [], $object = []){

View File

@ -36,8 +36,19 @@ abstract class Repository {
}
}
// save post and geet its ID
$obj->WP_Post->post_type = $obj::get_post_type();
// If implement trait \Tainacan\Traits\Entity_Collection_Relation get its collection relation, else get post type from entity
if ( method_exists($obj, 'get_collection') ) {
$collection = $obj->get_collection();
if (!$collection){
return false;
}
$cpt = $collection->get_db_identifier();
$obj->WP_Post->post_type = $cpt;
}
else {
$obj->WP_Post->post_type = $obj::get_post_type();
}
$obj->WP_Post->post_status = 'publish';
// TODO verificar se salvou mesmo

View File

@ -88,7 +88,9 @@ class Collections extends \WP_UnitTestCase {
$this->assertTrue($x->validate());
$this->assertTrue(empty($x->get_errors()));
global $Tainacan_Collections;
$this->assertTrue(has_action('init', array($Tainacan_Collections, 'register_post_type')) !== false, 'Collections Init is not registred!');
}
}

View File

@ -51,7 +51,7 @@ class Items extends \WP_UnitTestCase {
$this->assertTrue( isset($meta_test[$metadata->get_id()]) );
$this->assertTrue( $meta_test[$metadata->get_id()] instanceof \Tainacan\Entities\Item_Metadata_Entity );
$this->assertEquals( $meta_test[$metadata->get_id()]->get_value(), 'value_1');
$this->assertEquals( 'value_1', $meta_test[$metadata->get_id()]->get_value());
$i = new \Tainacan\Entities\Item();
$i->set_title('apple');