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

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

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'));
@ -16,36 +14,36 @@ class Logs {
function get_map() { function get_map() {
return [ return [
'id' => [ 'id' => [
'map' => 'ID', 'map' => 'ID',
//'validation' => '' //'validation' => ''
], ],
'title' => [ 'title' => [
'map' => 'post_title', 'map' => 'post_title',
'validation' => '' 'validation' => ''
], ],
'order' => [ 'order' => [
'map' => 'menu_order', 'map' => 'menu_order',
'validation' => '' 'validation' => ''
], ],
'parent' => [ 'parent' => [
'map' => 'parent', 'map' => 'parent',
'validation' => '' 'validation' => ''
], ],
'description' => [ 'description' => [
'map' => 'post_content', 'map' => 'post_content',
'validation' => '' 'validation' => ''
], ],
'slug' => [ 'slug' => [
'map' => 'post_name', 'map' => 'post_name',
'validation' => '' 'validation' => ''
], ],
'itens_per_page' => [ 'itens_per_page' => [
'map' => 'meta', 'map' => 'meta',
'validation' => '' 'validation' => ''
], ],
'user_id' => [ 'user_id' => [
'map' => 'post_author', 'map' => 'post_author',
'validation' => '' 'validation' => ''
], ],
]; ];
@ -53,41 +51,41 @@ class Logs {
function register_post_type() { function register_post_type() {
$labels = array( $labels = array(
'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',
'search_items' => 'Pesquisar', 'search_items' => 'Pesquisar',
'not_found' => 'Nenhum log encontrado', 'not_found' => 'Nenhum log encontrado',
'not_found_in_trash' => 'Nenhum log encontrado na lixeira', 'not_found_in_trash' => 'Nenhum log encontrado na lixeira',
'parent_item_colon' => 'Log aterior:', 'parent_item_colon' => 'Log aterior:',
'menu_name' => 'Logs' 'menu_name' => 'Logs'
); );
$args = array( $args = array(
'labels' => $labels, 'labels' => $labels,
'hierarchical' => true, 'hierarchical' => true,
//'supports' => array('title'), //'supports' => array('title'),
//'taxonomies' => array(self::TAXONOMY), //'taxonomies' => array(self::TAXONOMY),
'public' => false, 'public' => false,
'show_ui' => tnc_enable_dev_wp_interface(), 'show_ui' => tnc_enable_dev_wp_interface(),
'show_in_menu' => tnc_enable_dev_wp_interface(), 'show_in_menu' => tnc_enable_dev_wp_interface(),
//'menu_position' => 5, //'menu_position' => 5,
//'show_in_nav_menus' => false, //'show_in_nav_menus' => false,
'publicly_queryable' => false, 'publicly_queryable' => false,
'exclude_from_search' => true, 'exclude_from_search' => true,
'has_archive' => false, 'has_archive' => false,
'query_var' => true, 'query_var' => true,
'can_export' => true, 'can_export' => true,
'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)){
$args = array_merge([ return new Entities\Log($object);
'post_type' => self::POST_TYPE, } else {
'posts_per_page' => -1, $args = array_merge([
'post_status' => 'publish', 'post_type' => Entities\Log::POST_TYPE,
], $args); 'posts_per_page' => -1,
'post_status' => 'publish',
$posts = get_posts($args); ], $object);
$return = []; $posts = get_posts($args);
foreach ($posts as $post) { $logs = [];
$return[] = new Entities\Log($post);
foreach ($posts as $post) {
$logs[] = new Entities\Log($post);
}
// TODO: Pegar coleções registradas via código
return $logs;
} }
// TODO: Pegar coleções registradas via código
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() );