Now Tainacan logs implements Repository

This commit is contained in:
weryques 2017-11-20 15:24:07 -02:00
parent 6a4e2a024c
commit 4270cd0a63
3 changed files with 73 additions and 70 deletions

View File

@ -9,6 +9,8 @@ defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
*/ */
class Log extends \Tainacan\Entity { class Log extends \Tainacan\Entity {
const POST_TYPE = 'tainacan-logs';
function __construct($which = 0) { function __construct($which = 0) {
$this->repository = 'Tainacan_Logs'; $this->repository = 'Tainacan_Logs';

View File

@ -6,9 +6,7 @@ if ( ! defined( 'ABSPATH' ) ) {
exit; exit;
} }
class Logs { class Logs implements Repository {
const POST_TYPE = 'tainacan-logs';
function __construct() { function __construct() {
add_action('init', array(&$this, 'register_post_type')); add_action('init', array(&$this, 'register_post_type'));
@ -84,10 +82,10 @@ class Logs {
'rewrite' => true, 'rewrite' => true,
'capability_type' => 'post', 'capability_type' => 'post',
); );
register_post_type(self::POST_TYPE, $args); register_post_type(Entities\Log::POST_TYPE, $args);
} }
function insert(Entities\Log $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();
foreach ($map as $prop => $mapped) { foreach ($map as $prop => $mapped) {
@ -97,7 +95,7 @@ class Logs {
} }
// save post and geet its ID // save post and geet its ID
$log->WP_Post->post_type = self::POST_TYPE; $log->WP_Post->post_type = Entities\Log::POST_TYPE;
$log->WP_Post->post_status = 'publish'; $log->WP_Post->post_status = 'publish';
// TODO verificar se salvou mesmo // TODO verificar se salvou mesmo
@ -122,30 +120,33 @@ class Logs {
return new Entities\Log($log->WP_Post); return new Entities\Log($log->WP_Post);
} }
function get_logs($args = array()) { public function fetch($object = []){
if(is_numeric($object)){
return new Entities\Log($object);
} else {
$args = array_merge([ $args = array_merge([
'post_type' => self::POST_TYPE, 'post_type' => Entities\Log::POST_TYPE,
'posts_per_page' => -1, 'posts_per_page' => -1,
'post_status' => 'publish', 'post_status' => 'publish',
], $args); ], $object);
$posts = get_posts($args); $posts = get_posts($args);
$return = []; $logs = [];
foreach ($posts as $post) { foreach ($posts as $post) {
$return[] = new Entities\Log($post); $logs[] = new Entities\Log($post);
} }
// TODO: Pegar coleções registradas via código // TODO: Pegar coleções registradas via código
return $logs;
return $return; }
}
function get_log_by_id($id) {
return new Entities\Log($id);
} }
public function delete($object){
} }
public function update($object){
}
}

View File

@ -30,7 +30,7 @@ class Logs extends \WP_UnitTestCase {
$log = $Tainacan_Logs->insert($log); $log = $Tainacan_Logs->insert($log);
//retorna a taxonomia //retorna a taxonomia
$test = $Tainacan_Logs->get_log_by_id($log->get_id()); $test = $Tainacan_Logs->fetch($log->get_id());
$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() );