save user_id and blog_id

This commit is contained in:
Jacson Passold 2017-11-20 23:31:46 -02:00
parent ad5ee31f5d
commit 5c76b0ccf7
3 changed files with 63 additions and 7 deletions

View File

@ -27,6 +27,10 @@ class Log extends \Tainacan\Entity {
$this->WP_Post = new \StdClass(); $this->WP_Post = new \StdClass();
} }
if( is_int($which) && $which == 0) {
$this->set_user_id();
$this->set_blog_id();
}
} }
/** /**
@ -74,6 +78,15 @@ class Log extends \Tainacan\Entity {
return $this->get_mapped_property('description'); return $this->get_mapped_property('description');
} }
/**
* Retorn the ID of blog
*
* @return integer
*/
function get_blog_id() {
return $this->get_mapped_property('blog_id');
}
/** /**
* Return User Id of who make the action * Return User Id of who make the action
* *
@ -84,9 +97,9 @@ class Log extends \Tainacan\Entity {
} }
/** /**
* Atribui titulo ao log * Set log tittle
* *
* @param [type] $value * @param string $value
* @return void * @return void
*/ */
function set_title($value) { function set_title($value) {
@ -122,4 +135,26 @@ class Log extends \Tainacan\Entity {
function set_description($value) { function set_description($value) {
$this->set_mapped_property('description', $value); $this->set_mapped_property('description', $value);
} }
/**
* user_id of log entry
*
* @param integer $value
* @return void
*/
protected function set_user_id($value = 0) {
if(0 == $value) $value = get_current_user_id();
$this->set_mapped_property('user_id', $value);
}
/**
* blog_id of log entry
*
* @param integer $value
* @return void
*/
protected function set_blog_id($value = 0) {
if(0 == $value) $value = get_current_blog_id();
$this->set_mapped_property('blog_id', $value);
}
} }

View File

@ -6,6 +6,12 @@ if ( ! defined( 'ABSPATH' ) ) {
exit; exit;
} }
/**
* Implement a Logs system
*
* @author medialab
*
*/
class Logs implements Repository { class Logs implements Repository {
function __construct() { function __construct() {
@ -46,6 +52,10 @@ class Logs implements Repository {
'map' => 'post_author', 'map' => 'post_author',
'validation' => '' 'validation' => ''
], ],
'blog_id' => [
'map' => 'meta',
'validation' => ''
],
]; ];
} }
@ -54,7 +64,7 @@ class Logs implements Repository {
'name' => 'logs', 'name' => 'logs',
'singular_name' => 'logs', 'singular_name' => 'logs',
'add_new' => 'Adicionar Novo', 'add_new' => 'Adicionar Novo',
'add_new_item' =>'Adicionar Log', 'add_new_item' => 'Adicionar Log',
'edit_item' => 'Editar', 'edit_item' => 'Editar',
'new_item' => 'Novo Log', 'new_item' => 'Novo Log',
'view_item' => 'Visualizar', 'view_item' => 'Visualizar',
@ -85,6 +95,14 @@ class Logs implements Repository {
register_post_type(Entities\Log::POST_TYPE, $args); register_post_type(Entities\Log::POST_TYPE, $args);
} }
/**
*
* {@inheritDoc}
* @see \Tainacan\Repositories\Repository::insert()
*
* @param \Tainacan\Entities\Log $log
*
*/
function insert($log) { function insert($log) {
// First iterate through the native post properties // First iterate through the native post properties
$map = $this->get_map(); $map = $this->get_map();
@ -100,10 +118,9 @@ class Logs implements Repository {
// TODO verificar se salvou mesmo // TODO verificar se salvou mesmo
$id = wp_insert_post($log->WP_Post); $id = wp_insert_post($log->WP_Post);
//$log->WP_Post->ID = $id;
$log->WP_Post = get_post($id); $log->WP_Post = get_post($id);
/* Now run through properties stored as postmeta TODO maybe a parent class function leave for future use /* Now run through properties stored as postmeta TODO maybe a parent class function leave for future use */
foreach ($map as $prop => $mapped) { foreach ($map as $prop => $mapped) {
if ($mapped['map'] == 'meta') { if ($mapped['map'] == 'meta') {
update_post_meta($id, $prop, $log->get_mapped_property($prop)); update_post_meta($id, $prop, $log->get_mapped_property($prop));
@ -114,7 +131,7 @@ class Logs implements Repository {
foreach ($values as $value) foreach ($values as $value)
add_post_meta($id, $prop, $value); add_post_meta($id, $prop, $value);
} }
}*/ }
// return a brand new object // return a brand new object
return new Entities\Log($log->WP_Post); return new Entities\Log($log->WP_Post);

View File

@ -25,6 +25,9 @@ class Logs extends \WP_UnitTestCase {
//setando os valores na classe do tainacan //setando os valores na classe do tainacan
$log->set_title('blame someone'); $log->set_title('blame someone');
$log->set_description('someone did that'); $log->set_description('someone did that');
$user_id = get_current_user_id();
$blog_id = get_current_blog_id();
//inserindo //inserindo
$log = $Tainacan_Logs->insert($log); $log = $Tainacan_Logs->insert($log);
@ -34,6 +37,7 @@ class Logs extends \WP_UnitTestCase {
$this->assertEquals( 'blame someone', $test->get_title() ); $this->assertEquals( 'blame someone', $test->get_title() );
$this->assertEquals( 'someone did that', $test->get_description() ); $this->assertEquals( 'someone did that', $test->get_description() );
$this->assertEquals( $user_id, $test->get_user_id() );
$this->assertEquals( $blog_id, $test->get_blog_id() );
} }
} }