começando taxonomias

This commit is contained in:
Eduardo humberto 2017-11-09 13:51:16 -02:00
parent e73c59ae71
commit 7bc5ef0dd1
8 changed files with 354 additions and 20 deletions

View File

@ -0,0 +1,12 @@
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Class TainacanFieldType
*/
abstract class Tainacan_Field_Type extends Entity {
abstract function render( $metadata );
}

View File

@ -23,21 +23,121 @@ class Tainacan_Metadata extends Entity {
}
/**
* @param $attribute
* @return
*/
function getter( $attribute ){
return $this->get_mapped_property( $attribute );
// Getters
function get_id() {
return $this->get_mapped_property('ID');
}
function get_name() {
return $this->get_mapped_property('name');
}
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');
}
function get_cardinality(){
return $this->get_mapped_property('cardinality');
}
function get_mask(){
return $this->get_mapped_property('mask');
}
function get_privacy(){
return $this->get_mapped_property('privacy');
}
function get_default_value(){
return $this->get_mapped_property('default_value');
}
function get_type( $output = 'object' ){
if( $output === 'object'){
return unserialize( $this->get_mapped_property('option') );
}else{
return $this->get_mapped_property('type');
}
}
function get_collection() {
return new TainacanCollection( $this->get_mapped_property('collection') );
}
// Setters
function set_name($value) {
return $this->set_mapped_property('name', $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);
}
function set_cardinality( $value ){
return $this->set_mapped_property('cardinality', $value);
}
function set_mask( $value ){
return $this->set_mapped_property('mask', $value);
}
function set_privacy( $value ){
return $this->set_mapped_property('privacy', $value);
}
function set_default_value( $value ){
return $this->set_mapped_property('default_property', $value);
}
function set_type($value){
if( is_object( $value ) && is_subclass_of( $value, 'Tainacan_Field_Type' ) ){
$this->set_option( $value );
return $this->set_mapped_property('type', get_class( $value ) ) ;
}
return null;
}
function set_option($value){
return $this->set_mapped_property('option', serialize($value) ) ;
}
/**
*
* @param $attribute
* @param $value
* @param TainacanCollection
*/
function setter( $attribute, $value ){
return $this->set_mapped_property( $attribute, $value );
function set_collection($value) {
$ID = ($value instanceof TainacanCollection ) ? $value->get_id() : $value;
return $this->set_mapped_property('collection', $ID);
}
}

View File

@ -0,0 +1,25 @@
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
class Tainacan_Taxonomy extends Entity {
function __construct( $which = 0 ) {
$this->repository = 'Tainacan_Taxonomies';
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();
}
}
}

View File

@ -0,0 +1,19 @@
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Class TainacanFieldType
*/
class Tainacan_Text_Field_Type extends Tainacan_Field_Type {
/**
* @param $metadata
* @return string
*/
function render( $metadata ){
return '<tainacan-text name="'.$metadata->get_name().'"></tainacan-text>';
}
}

View File

@ -19,6 +19,11 @@ class Tainacan_Metadatas {
'description' => 'post_content',
'type' => 'meta',
'required' => 'meta',
'cardinality' => 'meta',
'privacy' => 'meta',
'mask' => 'meta',
'default_value' => 'meta',
'option' => 'meta',
'collection' => 'meta',
];
@ -65,7 +70,7 @@ class Tainacan_Metadatas {
/**
* @param Tainacan_Metadata $metadata
* @return int O id do metadado criado
* @return int
*/
function insert( Tainacan_Metadata $metadata ) {
// First iterate through the native post properties
@ -102,11 +107,12 @@ class Tainacan_Metadatas {
}
/**
* @param int $collection_id
* @param ( TainacanCollection ) $collection_id
* @param array $args
* @return array
*/
function get_collection_metadata($collection_id,$args = array()) {
function get_collection_metadata( $collection, $args = array()) {
$collection_id = ( is_object( $collection ) ) ? $collection->get_id() : $collection;
$args = array_merge([
'post_type' => self::POST_TYPE,

View File

@ -0,0 +1,141 @@
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Class Tainacan_Metadatas
*/
class Tainacan_Taxonomies {
const POST_TYPE = 'tainacan-taxonomies';
const DB_IDENTIFIER_META = '_db_identifier';
var $map = [
'ID' => 'ID',
'name' => 'post_title',
'parent' => 'parent',
'description' => 'post_content',
'allow_insert' => 'meta',
'collection' => 'meta',
];
function __construct() {
add_action('init', array(&$this, 'register_post_type'));
}
function register_post_type() {
$labels = array(
'name' => 'Taxonomy',
'singular_name' => 'Taxonomy',
'add_new' => 'Adicionar Taxonomy',
'add_new_item' =>'Adicionar Taxonomy',
'edit_item' => 'Editar',
'new_item' => 'Novo Taxonomy',
'view_item' => 'Visualizar',
'search_items' => 'Pesquisar',
'not_found' => 'Nenhum ticket encontrado',
'not_found_in_trash' => 'Nenhum Taxonomy encontrado na lixeira',
'parent_item_colon' => 'Taxonomy acima:',
'menu_name' => 'Taxonomy'
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
//'supports' => array('title'),
//'taxonomies' => array(self::TAXONOMY),
'public' => true,
'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' => true,
'exclude_from_search' => true,
'has_archive' => true,
'query_var' => true,
'can_export' => true,
'rewrite' => true,
'capability_type' => 'post',
);
register_post_type(self::POST_TYPE, $args);
}
function get_taxonomy_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
* @return int
*/
function insert( Tainacan_Taxonomy $taxonomy ) {
// First iterate through the native post properties
$map = $this->map;
foreach ($map as $prop => $mapped) {
if ($mapped != 'meta') {
$taxonomy->WP_Post->$mapped = $taxonomy->get_mapped_property($prop);
}
}
// save post and get its ID
$taxonomy->WP_Post->post_type = self::POST_TYPE;
$taxonomy->WP_Post->post_status = 'publish';
$id = wp_insert_post($taxonomy->WP_Post);
// Now run through properties stored as postmeta
foreach ($map as $prop => $mapped) {
if ($mapped == 'meta') {
update_post_meta($id, $prop, $taxonomy->get_mapped_property($prop));
}
}
// DB Identifier
// the first time a collection is saved, save the slug as a db Identifier,
// This will be the slug of the post type that will be created for this collection
// Later, if the slug is changed (and thus the URL of this collection) the db Identifier
// does not change and we dont lose all the items
if (!get_post_meta($id, self::DB_IDENTIFIER_META, true)) {
$p = get_post($id);
add_post_meta($id, self::DB_IDENTIFIER_META, $p->post_name);
}
return $id;
}
function registerTainacanTaxonomy( $taxonomy_name ){
$labels = array(
'name' => __( 'Genres', 'taxonomy general name', 'textdomain' ),
'singular_name' => __( 'Genre', 'taxonomy singular name', 'textdomain' ),
'search_items' => __( 'Search Genres', 'textdomain' ),
'all_items' => __( 'All Genres', 'textdomain' ),
'parent_item' => __( 'Parent Genre', 'textdomain' ),
'parent_item_colon' => __( 'Parent Genre:', 'textdomain' ),
'edit_item' => __( 'Edit Genre', 'textdomain' ),
'update_item' => __( 'Update Genre', 'textdomain' ),
'add_new_item' => __( 'Add New Genre', 'textdomain' ),
'new_item_name' => __( 'New Genre Name', 'textdomain' ),
'menu_name' => __( 'Genre', 'textdomain' ),
);
$args = array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'genre' ),
);
register_taxonomy( $taxonomy_name, array( 'book' ), $args );
}
}

View File

@ -14,6 +14,8 @@ include('classes/Entity.php');
include('classes/Entities/Collection.php');
include('classes/Entities/Item.php');
include('classes/Entities/Metadata.php');
include('classes/Entities/FieldType.php');
include('classes/Entities/TextFieldType.php');
/**
*

View File

@ -23,18 +23,47 @@ class TestMetadata extends WP_UnitTestCase {
$id = $TainacanCollections->insert($collection);
//setando os valores na classe do metadado
$metadata->setter('name','metadado');
$metadata->setter('description','descricao');
$metadata->setter('collection',$id);
$metadata->set_name('metadado');
$metadata->set_description('descricao');
$metadata->set_collection( new TainacanCollection( $id ) );
//inserindo o metadado
$metadata_id = $Tainacan_Metadatas->insert($metadata);
$test = $Tainacan_Metadatas->get_metadata_by_id($metadata_id);
$this->assertEquals($test->getter('name'), 'metadado');
$this->assertEquals($test->getter('description'), 'descricao');
$this->assertEquals($test->getter('collection'), $id);
$this->assertEquals($test->get_name(), 'metadado');
$this->assertEquals($test->get_description(), 'descricao');
$this->assertEquals($test->get_collection()->get_id(), $id);
}
/**
* Teste da insercao de um metadado simples com o tipo
*/
function teste_add_type(){
global $TainacanCollections, $Tainacan_Metadatas;
$collection = new TainacanCollection();
$metadata = new Tainacan_Metadata();
$type = new Tainacan_Text_Field_Type();
$collection->set_name('teste');
$id = $TainacanCollections->insert($collection);
//setando os valores na classe do metadado
$metadata->set_name('metadado');
$metadata->set_collection( new TainacanCollection( $id ) );
$metadata->set_type( $type );
//inserindo o metadado
$metadata_id = $Tainacan_Metadatas->insert($metadata);
$test = $Tainacan_Metadatas->get_metadata_by_id($metadata_id);
$this->assertEquals($test->get_name(), 'metadado');
$this->assertEquals($test->get_collection()->get_id(), $id);
$this->assertEquals($test->get_type('name'), 'Tainacan_Text_Field_Type');
$this->assertEquals($test->get_type(), $type);
}
}