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
*/
class Log extends \Tainacan\Entity {
const POST_TYPE = 'tainacan-logs';
function __construct($which = 0) {

View File

@ -6,9 +6,7 @@ if ( ! defined( 'ABSPATH' ) ) {
exit;
}
class Logs {
const POST_TYPE = 'tainacan-logs';
class Logs implements Repository {
function __construct() {
add_action('init', array(&$this, 'register_post_type'));
@ -16,36 +14,36 @@ class Logs {
function get_map() {
return [
'id' => [
'map' => 'ID',
'id' => [
'map' => 'ID',
//'validation' => ''
],
'title' => [
'map' => 'post_title',
'title' => [
'map' => 'post_title',
'validation' => ''
],
'order' => [
'map' => 'menu_order',
'order' => [
'map' => 'menu_order',
'validation' => ''
],
'parent' => [
'map' => 'parent',
'parent' => [
'map' => 'parent',
'validation' => ''
],
'description' => [
'map' => 'post_content',
'description' => [
'map' => 'post_content',
'validation' => ''
],
'slug' => [
'map' => 'post_name',
'slug' => [
'map' => 'post_name',
'validation' => ''
],
'itens_per_page' => [
'map' => 'meta',
'map' => 'meta',
'validation' => ''
],
'user_id' => [
'map' => 'post_author',
'user_id' => [
'map' => 'post_author',
'validation' => ''
],
];
@ -53,41 +51,41 @@ class Logs {
function register_post_type() {
$labels = array(
'name' => 'logs',
'singular_name' => 'logs',
'add_new' => 'Adicionar Novo',
'add_new_item' =>'Adicionar Log',
'edit_item' => 'Editar',
'new_item' => 'Novo Log',
'view_item' => 'Visualizar',
'search_items' => 'Pesquisar',
'not_found' => 'Nenhum log encontrado',
'name' => 'logs',
'singular_name' => 'logs',
'add_new' => 'Adicionar Novo',
'add_new_item' =>'Adicionar Log',
'edit_item' => 'Editar',
'new_item' => 'Novo Log',
'view_item' => 'Visualizar',
'search_items' => 'Pesquisar',
'not_found' => 'Nenhum log encontrado',
'not_found_in_trash' => 'Nenhum log encontrado na lixeira',
'parent_item_colon' => 'Log aterior:',
'menu_name' => 'Logs'
'parent_item_colon' => 'Log aterior:',
'menu_name' => 'Logs'
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
//'supports' => array('title'),
//'taxonomies' => array(self::TAXONOMY),
'public' => false,
'show_ui' => tnc_enable_dev_wp_interface(),
'show_in_menu' => tnc_enable_dev_wp_interface(),
//'menu_position' => 5,
'labels' => $labels,
'hierarchical' => true,
//'supports' => array('title'),
//'taxonomies' => array(self::TAXONOMY),
'public' => false,
'show_ui' => tnc_enable_dev_wp_interface(),
'show_in_menu' => tnc_enable_dev_wp_interface(),
//'menu_position' => 5,
//'show_in_nav_menus' => false,
'publicly_queryable' => false,
'publicly_queryable' => false,
'exclude_from_search' => true,
'has_archive' => false,
'query_var' => true,
'can_export' => true,
'rewrite' => true,
'capability_type' => 'post',
'has_archive' => false,
'query_var' => true,
'can_export' => true,
'rewrite' => true,
'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
$map = $this->get_map();
foreach ($map as $prop => $mapped) {
@ -97,7 +95,7 @@ class Logs {
}
// 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';
// TODO verificar se salvou mesmo
@ -122,30 +120,33 @@ class Logs {
return new Entities\Log($log->WP_Post);
}
function get_logs($args = array()) {
$args = array_merge([
'post_type' => self::POST_TYPE,
'posts_per_page' => -1,
'post_status' => 'publish',
], $args);
$posts = get_posts($args);
$return = [];
foreach ($posts as $post) {
$return[] = new Entities\Log($post);
public function fetch($object = []){
if(is_numeric($object)){
return new Entities\Log($object);
} else {
$args = array_merge([
'post_type' => Entities\Log::POST_TYPE,
'posts_per_page' => -1,
'post_status' => 'publish',
], $object);
$posts = get_posts($args);
$logs = [];
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);
//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( 'someone did that', $test->get_description() );