begin log entity and save post type object

This commit is contained in:
Jacson Passold 2017-11-13 21:23:13 -02:00
parent a39273a432
commit ddc207ab8d
4 changed files with 263 additions and 0 deletions

View File

@ -53,6 +53,9 @@ $Tainacan_Items = new Tainacan_Items();
global $Tainacan_Terms;
$Tainacan_Terms = new Tainacan_Terms();
global $Tainacan_Logs;
$Tainacan_Logs = new Tainacan_Logs();
/**
*
*

View File

@ -0,0 +1,76 @@
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
class Tainacan_Log extends Tainacan_Entity {
function __construct($which = 0) {
$this->repository = 'Tainacan_Logs';
if (is_numeric($which) && $which > 0) {
$post = get_post($which);
if ($post instanceof WP_Post) {
$this->WP_Post = get_post($which);
}
} elseif ($which instanceof WP_Post) {
$this->WP_Post = $which;
} else {
$this->WP_Post = new StdClass();
}
}
// Getters
//
function get_id() {
return $this->get_mapped_property('ID');
}
function get_title() {
return $this->get_mapped_property('title');
}
function get_order() {
return $this->get_mapped_property('order');
}
function get_parent() {
return $this->get_mapped_property('parent');
}
function get_description() {
return $this->get_mapped_property('description');
}
/**
* Return User Id of who make the action
* @return int User Id of logged action
*/
function get_user_id() {
return $this->get_mapped_property('user_id');
}
// Setters
//
function set_title($value) {
return $this->set_mapped_property('title', $value);
}
function set_order($value) {
return $this->set_mapped_property('order', $value);
}
function set_parent($value) {
return $this->set_mapped_property('parent', $value);
}
function set_description($value) {
return $this->set_mapped_property('description', $value);
}
}

View File

@ -0,0 +1,148 @@
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
class Tainacan_Logs extends Tainacan_Repository {
const POST_TYPE = 'tainacan-logs';
var $map = [
'ID' => [
'map' => 'ID',
'validation' => ''
],
'title' => [
'map' => 'post_title',
'validation' => ''
],
'order' => [
'map' => 'menu_order',
'validation' => ''
],
'parent' => [
'map' => 'parent',
'validation' => ''
],
'description' => [
'map' => 'post_content',
'validation' => ''
],
'slug' => [
'map' => 'post_name',
'validation' => ''
],
'itens_per_page' => [
'map' => 'meta',
'validation' => ''
],
'user_id' => [
'map' => 'post_author',
'validation' => ''
],
];
function __construct() {
add_action('init', array(&$this, 'register_post_type'));
}
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',
'not_found_in_trash' => 'Nenhum log encontrado na lixeira',
'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,
//'show_in_nav_menus' => false,
'publicly_queryable' => false,
'exclude_from_search' => true,
'has_archive' => false,
'query_var' => true,
'can_export' => true,
'rewrite' => true,
'capability_type' => 'post',
);
register_post_type(self::POST_TYPE, $args);
}
function insert(Tainacan_Log $log) {
// First iterate through the native post properties
$map = $this->map;
foreach ($map as $prop => $mapped) {
if ($mapped['map'] != 'meta' && $mapped['map'] != 'meta_multi') {
$log->WP_Post->{$mapped['map']} = $log->get_mapped_property($prop);
}
}
// save post and geet its ID
$log->WP_Post->post_type = self::POST_TYPE;
$log->WP_Post->post_status = 'publish';
// TODO verificar se salvou mesmo
$id = wp_insert_post($log->WP_Post);
//$log->WP_Post->ID = $id;
$log->WP_Post = get_post($id);
/* Now run through properties stored as postmeta TODO maybe a parent class function leave for future use
foreach ($map as $prop => $mapped) {
if ($mapped['map'] == 'meta') {
update_post_meta($id, $prop, $log->get_mapped_property($prop));
} elseif ($mapped['map'] == 'meta_multi') {
$values = $log->get_mapped_property($prop);
delete_post_meta($id, $prop);
if (is_array($values))
foreach ($values as $value)
add_post_meta($id, $prop, $value);
}
}*/
// return a brand new object
return new Tainacan_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 Tainacan_Log($post);
}
// TODO: Pegar coleções registradas via código
return $return;
}
function get_log_by_id($id) {
return new Tainacan_Log($id);
}
}

36
tests/test-logs.php Normal file
View File

@ -0,0 +1,36 @@
<?php
/**
* Class TestCollections
*
* @package Test_Tainacan
*/
/**
* Sample test case.
*/
class Test_Logs extends WP_UnitTestCase {
/**
* Teste da insercao de um log simples apenas se criar o dado bruto
*/
function test_add() {
global $Tainacan_Logs;
$log = new Tainacan_Log();
//setando os valores na classe do tainacan
$log->set_title('blame someone');
$log->set_description('someone did that');
//inserindo
$log = $Tainacan_Logs->insert($log);
//retorna a taxonomia
$test = $Tainacan_Logs->get_log_by_id($log->get_id());
$this->assertEquals( 'blame someone', $test->get_title() );
$this->assertEquals( 'someone did that', $test->get_description() );
}
}