From 4270cd0a63bbb70df9f22d97a1823a695d4d7526 Mon Sep 17 00:00:00 2001 From: weryques Date: Mon, 20 Nov 2017 15:24:07 -0200 Subject: [PATCH] Now Tainacan logs implements Repository --- src/classes/entities/class-tainacan-log.php | 2 + .../repositories/class-tainacan-logs.php | 139 +++++++++--------- tests/test-logs.php | 2 +- 3 files changed, 73 insertions(+), 70 deletions(-) diff --git a/src/classes/entities/class-tainacan-log.php b/src/classes/entities/class-tainacan-log.php index 130a5ff50..0499df61b 100644 --- a/src/classes/entities/class-tainacan-log.php +++ b/src/classes/entities/class-tainacan-log.php @@ -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) { diff --git a/src/classes/repositories/class-tainacan-logs.php b/src/classes/repositories/class-tainacan-logs.php index 22d9fa74d..f66704865 100644 --- a/src/classes/repositories/class-tainacan-logs.php +++ b/src/classes/repositories/class-tainacan-logs.php @@ -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){ + + } } \ No newline at end of file diff --git a/tests/test-logs.php b/tests/test-logs.php index 01f58f7f8..ec65c904a 100644 --- a/tests/test-logs.php +++ b/tests/test-logs.php @@ -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() );