criando a entidade Term

This commit is contained in:
Eduardo humberto 2017-11-10 14:39:27 -02:00
parent 7e2dd2c199
commit 5b354a9139
6 changed files with 172 additions and 24 deletions

View File

@ -0,0 +1,78 @@
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
class Tainacan_Term extends Entity {
function __construct($which = 0, $taxonomy = '' ) {
$this->repository = 'Tainacan_Terms';
$this->set_taxonomy( $taxonomy );
if ( is_numeric( $which ) && $which > 0) {
$post = get_term_by('id', $which, $taxonomy);
if ( $post instanceof WP_Term) {
$this->WP_Term = get_term_by('id', $which, $taxonomy);
}
} elseif ( $which instanceof WP_Term ) {
$this->WP_Term = $which;
} else {
$this->WP_Term = new StdClass();
}
}
// Getters
function get_id() {
return $this->get_mapped_property('term_id');
}
function get_name() {
return $this->get_mapped_property('name');
}
function get_parent() {
return $this->get_mapped_property('parent');
}
function get_description() {
return $this->get_mapped_property('description');
}
function get_user() {
return $this->get_mapped_property('user');
}
function get_taxonomy() {
return $this->get_mapped_property('taxonomy');
}
// Setters
function set_name($value) {
return $this->set_mapped_property('name', $value);
}
function set_parent($value) {
return $this->set_mapped_property('parent', $value);
}
function set_description($value) {
return $this->set_mapped_property('description', $value);
}
function set_user($value) {
return $this->set_mapped_property('user', $value);
}
function set_taxonomy($value) {
return $this->set_mapped_property('taxonomy', $value);
}
}

View File

@ -20,11 +20,14 @@ class Entity {
$mapped = $map[$prop]; $mapped = $map[$prop];
if ($mapped == 'meta') { if ( $mapped == 'meta') {
return get_post_meta($this->WP_Post->ID, $prop, true); return get_post_meta($this->WP_Post->ID, $prop, true);
} else { }elseif ( $mapped == 'termmeta' ){
return get_term_meta($this->WP_Term->term_id, $prop, true);
}elseif ( isset( $this->WP_Post )) {
return isset($this->WP_Post->$mapped) ? $this->WP_Post->$mapped : null; return isset($this->WP_Post->$mapped) ? $this->WP_Post->$mapped : null;
} else{
return isset($this->WP_Term->$mapped) ? $this->WP_Term->$mapped : null;
} }
} }

View File

@ -4,7 +4,7 @@ if ( ! defined( 'ABSPATH' ) ) {
} }
/** /**
* Class Tainacan_Metadatas * Class Tainacan_Taxonomies
*/ */
class Tainacan_Taxonomies { class Tainacan_Taxonomies {
@ -75,6 +75,19 @@ class Tainacan_Taxonomies {
return $meta; return $meta;
} }
function get_metadata_db_identifier($id) {
$meta = get_post_meta($id, self::DB_IDENTIFIER_META, true);
if (!$meta) {
$p = get_post($id);
add_post_meta($id, self::DB_IDENTIFIER_META, $p->post_name);
return $p->post_name;
}
return $meta;
}
/** /**
* @param Tainacan_Taxonomy $metadata * @param Tainacan_Taxonomy $metadata
* @return int * @return int
@ -142,19 +155,6 @@ class Tainacan_Taxonomies {
function get_taxonomy_by_id($id) { function get_taxonomy_by_id($id) {
return new Tainacan_Taxonomy($id); return new Tainacan_Taxonomy($id);
} }
function insert_term( $name, $taxonomy, $args = array() ){
$term = wp_insert_term( $name, $taxonomy, $args );
return $term['term_id'];
}
function get_terms( $taxonomies, $args ){
return get_terms( $taxonomies, $args );
}
function get_term_by($field,$value,$taxonomy){
return get_term_by($field,$value,$taxonomy);
}
} }
global $Tainacan_Taxonomies; global $Tainacan_Taxonomies;

View File

@ -0,0 +1,60 @@
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Class Tainacan_Terms
*/
class Tainacan_Terms {
var $map = [
'term_id' => 'term_id',
'name' => 'name',
'parent' => 'parent',
'description' => 'description',
'taxonomy' => 'taxonomy',
'user' => 'termmeta',
];
function insert( Tainacan_Term $term ){
// First iterate through the native post properties
$map = $this->map;
foreach ($map as $prop => $mapped) {
if ($mapped != 'termmeta') {
$term->WP_Term->$mapped = $term->get_mapped_property($prop);
}
}
// save post and get its ID
$term_inserted = wp_insert_term( $term->WP_Term->name, $term->WP_Term->taxonomy, [
'parent' => ( isset( $term->WP_Term->parent ) ) ? $term->WP_Term->parent : 0,
'description' => ( isset( $term->WP_Term->description ) ) ? $term->WP_Term->description : '',
]);
// Now run through properties stored as postmeta
foreach ($map as $prop => $mapped) {
if ($mapped == 'termmeta') {
update_term_meta($term_inserted['term_id'], $prop, $term->get_mapped_property($prop));
}
}
return $term_inserted['term_id'];
}
function get_terms( $taxonomies, $args ){
return get_terms( $taxonomies, $args );
}
function get_term_by($field,$value,$taxonomy){
$wp_term = get_term_by($field,$value,$taxonomy);
$tainacan_term = new Tainacan_Term( $wp_term );
$tainacan_term->set_user( get_term_meta($tainacan_term->get_id() , 'user', true ) );
return $tainacan_term;
}
}
global $Tainacan_Terms;
$Tainacan_Terms = new Tainacan_Terms();

View File

@ -11,11 +11,13 @@ include('classes/Repositories/Collections.php');
include('classes/Repositories/Items.php'); include('classes/Repositories/Items.php');
include('classes/Repositories/Metatadas.php'); include('classes/Repositories/Metatadas.php');
include('classes/Repositories/Taxonomies.php'); include('classes/Repositories/Taxonomies.php');
include('classes/Repositories/Terms.php');
include('classes/Entity.php'); include('classes/Entity.php');
include('classes/Entities/Collection.php'); include('classes/Entities/Collection.php');
include('classes/Entities/Item.php'); include('classes/Entities/Item.php');
include('classes/Entities/Metadata.php'); include('classes/Entities/Metadata.php');
include('classes/Entities/Taxonomy.php'); include('classes/Entities/Taxonomy.php');
include('classes/Entities/Term.php');
include('classes/FieldTypes/FieldType.php'); include('classes/FieldTypes/FieldType.php');
include('classes/FieldTypes/TextFieldType.php'); include('classes/FieldTypes/TextFieldType.php');

View File

@ -37,10 +37,11 @@ class TestTaxonomies extends WP_UnitTestCase {
} }
function test_add_term_taxonomy(){ function test_add_term_taxonomy(){
global $Tainacan_Taxonomies; global $Tainacan_Taxonomies,$Tainacan_Terms;
$taxonomy = new Tainacan_Taxonomy(); $taxonomy = new Tainacan_Taxonomy();
$term = new Tainacan_Term();
//setando os valores na classe do tainacan //setando os valores na classe de taxonomia
$taxonomy->set_name('genero'); $taxonomy->set_name('genero');
//insere a taxonomia //insere a taxonomia
@ -49,12 +50,16 @@ class TestTaxonomies extends WP_UnitTestCase {
//retorna a taxonomia //retorna a taxonomia
$taxonomy_test = $Tainacan_Taxonomies->get_taxonomy_by_id($taxonomy_id); $taxonomy_test = $Tainacan_Taxonomies->get_taxonomy_by_id($taxonomy_id);
//insere um termo na taxonmia //insere um termo na taxonomia
$term_id = $Tainacan_Taxonomies->insert_term('Rock', $taxonomy_test->get_taxonomy_name()); $term->set_taxonomy( $taxonomy_test->get_taxonomy_name() );
$term->set_name('Rock');
$term->set_user(56);
$term_id = $Tainacan_Terms->insert( $term ) ;
//retorna o termo //retorna um objeto da classe Tainacan_Term
$term = $Tainacan_Taxonomies->get_term_by('id', $term_id, $taxonomy_test->get_taxonomy_name()); $test = $Tainacan_Terms->get_term_by('id', $term_id, $taxonomy_test->get_taxonomy_name());
$this->assertEquals( $term->name, 'Rock' ); $this->assertEquals( $test->get_name(), 'Rock' );
$this->assertEquals( $test->get_user(), 56 );
} }
} }