Conveção de nomes de classes.

This commit is contained in:
weryques 2017-11-13 14:05:11 -02:00
parent fb6e5fb11e
commit 3612d37c81
19 changed files with 1951 additions and 0 deletions

109
src/class-tainacan.php Normal file
View File

@ -0,0 +1,109 @@
<?php
/*
Plugin Name: Tainacan
Plugin URI:
Description: Lorem Ipsum
Author: MediaLab UFG
Version: 10.9.8.7.6.5.4
*/
const ENTITIES_DIR = __DIR__ . '/classes/entities/';
const FIELD_TYPES_DIR = __DIR__ . '/classes/field-types/';
const REPOSITORIES_DIR = __DIR__ . '/classes/repositories/';
const TRAITS_DIR = __DIR__ . '/classes/traits/';
const CLASSES_DIR = __DIR__ . '/classes/';
const DIRS = [
CLASSES_DIR,
ENTITIES_DIR,
FIELD_TYPES_DIR,
REPOSITORIES_DIR,
TRAITS_DIR,
];
spl_autoload_register('tainacan_autoload');
function tainacan_autoload($class_name){
foreach(DIRS as $dir){
$file = $dir . 'class-'. strtolower(str_replace('_', '-' , $class_name)) . '.php';
if(file_exists($file)){
include($file);
}
}
}
global $Tainacan_Collections;
$Tainacan_Collections = new Tainacan_Collections();
global $Tainacan_Item_Metadata;
$Tainacan_Item_Metadata = new Tainacan_Item_Metadata();
global $Tainacan_Metadatas;
$Tainacan_Metadatas = new Tainacan_Metadatas();
global $Tainacan_Taxonomies;
$Tainacan_Taxonomies = new Tainacan_Taxonomies();
global $Tainacan_Items;
$Tainacan_Items = new Tainacan_Items();
global $Tainacan_Terms;
$Tainacan_Terms = new Tainacan_Terms();
/**
*
*
* nos loops instancia a Classes
*
* as classes no plural em repositories (talvez troccar esse nome pra não confundir)
* lidam com registro de post type, incialiação
* e tem o metodo find() pra busca, q usa o WP_Query, mas itera e substitui por objetos
* certos, talvez não precise instanciar na mão
* Nessas classes tb vão ter metodos, ativos se quisermos ver a interface dev padrao do WP
* q vai criar os metaboxes
* e tb os pre_get_posts...
*
*
*
* as classe em entities mapeiam suas propriedades para o esquema do WP, e não tem nenhuma lõgica,
* são objetos com propriedades, collection pode acessar seus metadados. item pode
* aessar sua coleção e metdados
* talvez ter um getter que tenta passar a propriedade buscada pra dentro da propriedade o objeto wp,
* usando o mapeamento ao contrãrio. assim um tema padrão não quebra
*
*
* Repository (não confundir) tem as opções gerais do repo, como o slug padrão das coisas (colecoes, item...)
*
* Vai no banco:
* Collections**
* Metadata
* Taxonomies
* Items**
* Filters
*
* ** Items e Collections vão aparecer na hierarquia de templates e podem ter loops
*
* $collections ou $items registra os post types das coleções?
*
* db_identifier das coleções não pode mudar, mesmo q mude nome e slug
*
* essas classes tem q ter um esquema de validação, (filtro, unicidade)
*
* $Collections->add(), find(), get()
*
* $collection->getItems(), getItem(), addItem(), deleteItem()
*
* metadados registrado via codigo deinem ibase_add_user
* colecoes registradas via cõdigo passam o objeto inteiro e marcamos de algum jeito q não são editaveis
* (source)
*
*
*/
function tnc_enable_dev_wp_interface() {
return defined('TNC_ENABLE_DEV_WP_INTERFACE') && true === TNC_ENABLE_DEV_WP_INTERFACE ? true : false;
}

View File

@ -0,0 +1,76 @@
<?php
class Tainacan_Entity {
var $repository;
var $errors = [];
function get_mapped_property($prop) {
global ${$this->repository};
$map = ${$this->repository}->map;
if (isset($this->$prop) && !empty($this->$prop))
return $this->$prop;
if (!array_key_exists($prop, $map))
return null;
$mapped = $map[$prop]['map'];
if ( $mapped == 'meta') {
$return = get_post_meta($this->WP_Post->ID, $prop, true);
}elseif ( $mapped == 'meta_multi') {
$return = get_post_meta($this->WP_Post->ID, $prop, false);
}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;
} elseif ( isset( $this->WP_Term )) {
$return = isset($this->WP_Term->$mapped) ? $this->WP_Term->$mapped : null;
}
if (empty($return) && isset($map[$prop]['default']) && !empty($map[$prop]['default']))
$return = $map[$prop]['default'];
return $return;
}
function set_mapped_property($prop, $value) {
$this->$prop = $value;
}
function save() {
// validate
global ${$this->repository};
return ${$this->repository}->insert($this);
}
function validate() {
return true;
}
function get_errors() {
return $this->errors;
}
function add_error($type, $message) {
$this->errors[] = [$type => $message];
}
function reset_errors() {
$this->errors = [];
}
}

View File

@ -0,0 +1,17 @@
<?php
class Tainacan_Repository {
function find_by($prop, $value) {
$map = $this->map;
if (!key_exists($prop, $map))
return null;
}
}

View File

@ -0,0 +1,135 @@
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
class Tainacan_Collection extends Tainacan_Entity {
function __construct($which = 0) {
$this->repository = 'Tainacan_Collections';
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();
}
}
function register_post_type() {
$cpt_labels = array(
'name' => 'Item',
'singular_name' => 'Item',
'add_new' => 'Adicionar Novo',
'add_new_item' =>'Adicionar Item',
'edit_item' => 'Editar',
'new_item' => 'Novo Item',
'view_item' => 'Visualizar',
'search_items' => 'Pesquisar',
'not_found' => 'Nenhum Item encontrado',
'not_found_in_trash' => 'Nenhum Item encontrado na lixeira',
'parent_item_colon' => 'Item acima:',
'menu_name' => $this->get_name()
);
$cpt_slug = $this->get_db_identifier();
$args = array(
'labels' => $cpt_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' => [
'slug' => $this->get_slug()
],
'capability_type' => 'post',
);
if (post_type_exists($this->get_db_identifier()))
unregister_post_type($this->get_db_identifier());
register_post_type($cpt_slug, $args);
}
// Getters
//
function get_id() {
return $this->get_mapped_property('ID');
}
function get_name() {
return $this->get_mapped_property('name');
}
function get_slug() {
return $this->get_mapped_property('slug');
}
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_itens_per_page() {
return $this->get_mapped_property('itens_per_page');
}
// special Getters
//
function get_db_identifier() {
return $this->get_id() ? 'tnc_col_' . $this->get_id() : false;
}
// metadata
function get_metadata() {
$Tainacan_Metadatas = new Tainacan_Metadatas();
return $Tainacan_Metadatas->get_metadata_by_collection($this);
}
// Setters
//
function set_name($value) {
return $this->set_mapped_property('name', $value);
}
function set_slug($value) {
return $this->set_mapped_property('slug', $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_itens_per_page($value) {
return $this->set_mapped_property('itens_per_page', $value);
}
}

View File

@ -0,0 +1,144 @@
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
class Tainacan_Item_Metadata_Entity extends Tainacan_Entity {
function __construct(Tainacan_Item $item, Tainacan_Metadata $metadata) {
$this->repository = 'Tainacan_Item_Metadata';
$this->set_item($item);
$this->set_metadata($metadata);
}
function set_item(Tainacan_Item $item) {
$this->item = $item;
}
function set_value($value) {
$this->value = $value;
}
function set_metadata(Tainacan_Metadata $metadata) {
$this->metadata = $metadata;
}
function get_item() {
return $this->item;
}
function get_metadata() {
return $this->metadata;
}
function get_value() {
if (isset($this->value))
return $this->value;
$Tainacan_Item_Metadata = new Tainacan_Item_Metadata();
return $Tainacan_Item_Metadata->get_item_metadata_value($this);
}
//////////////////
function is_multiple() {
return $this->get_metadata()->is_multiple();
}
function is_collection_key() {
return $this->get_metadata()->is_collection_key();
}
function is_required() {
return $this->get_metadata()->is_required();
}
function validate() {
$value = $this->get_value();
$metadata = $this->get_metadata();
$item = $this->get_item();
if (empty($value) && $this->is_required()) {
$this->add_error('required', $metadata->get_name() . ' is required');
return false;
}
if ($this->is_multiple()) {
if (is_array($value)) {
// if its required, at least one must be filled
$one_filled = false;
$valid = true;
foreach($value as $val) {
if (!empty($val))
$one_filled = true;
// TODO: call fieldtype validation
// if (invalid) $valid = false;
}
if ($this->is_required() && !$one_filled) {
$this->add_error('required', $metadata->get_name() . ' is required');
return false;
}
if (!$valid) {
$this->add_error('invalid', $metadata->get_name() . ' is invalid');
return false;
}
$this->reset_errors();
return true;
} else {
$this->add_error('invalid', $metadata->get_name() . ' is invalid');
return false;
}
} else {
if ($this->is_collection_key()) {
$Tainacan_Items = new Tainacan_Items();
$test = $Tainacan_Items->query([
'collections' => $item->get_collection(),
'metadata' => [
[
'key' => $this->metadata->get_id(),
'value' => $value
]
]
]);
if (!empty($test)) {
$this->add_error('key_exists', $metadata->get_name() . ' is a collection key and there is another item with the same value');
return false;
}
}
// TODO: call fieldType validation
//
$this->reset_errors();
return true;
}
}
}

View File

@ -0,0 +1,103 @@
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
class Tainacan_Item extends Tainacan_Entity {
use Tainacan_Entity_Collection_Relation;
function __construct($which = 0) {
$this->repository = 'Tainacan_Items';
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');
}
// 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);
}
// Metadata
function get_metadata() {
if (isset($this->metadata))
return $this->metadata;
$collection = $this->get_collection();
$return = [];
if ($collection) {
$metaList = $collection->get_metadata();
foreach ($metaList as $meta) {
$return[$meta->get_id()] = new Tainacan_Item_Metadata_Entity($this, $meta);
}
}
return $return;
}
function add_metadata(Tainacan_Metadata $new_metadata, $value) {
//TODO Multiple metadata must receive an array as value
$item_metadata = new Tainacan_Item_Metadata_Entity($this, $new_metadata);
$item_metadata->set_value($value);
$current_meta = $this->get_metadata();
$current_meta[$new_metadata->get_id()] = $item_metadata;
$this->set_metadata($current_meta);
}
function set_metadata(Array $metadata) {
$this->metadata = $metadata;
}
}

View File

@ -0,0 +1,170 @@
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
class Tainacan_Metadata extends Tainacan_Entity {
use Tainacan_Entity_Collection_Relation;
function __construct( $which = 0 ) {
$this->repository = 'Tainacan_Metadatas';
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_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_required(){
return $this->get_mapped_property('required');
}
function get_multiple(){
return $this->get_mapped_property('multiple');
}
function get_cardinality(){
return $this->get_mapped_property('cardinality');
}
function get_collection_key(){
return $this->get_mapped_property('collection_key');
}
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');
}
}
// 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_required( $value ){
return $this->set_mapped_property('required', $value);
}
function set_multiple( $value ){
return $this->set_mapped_property('multiple', $value);
}
function set_cardinality( $value ){
return $this->set_mapped_property('cardinality', $value);
}
function set_collection_key( $value ){
return $this->set_mapped_property('collection_key', $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) ) ;
}
// helpers
function is_multiple() {
return $this->get_multiple() === 'yes';
}
function is_collection_key() {
return $this->get_collection_key() === 'yes';
}
function is_required() {
return $this->get_required() === 'yes';
}
}

View File

@ -0,0 +1,131 @@
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
class Tainacan_Taxonomy extends Tainacan_Entity {
use Tainacan_Entity_Collections_Relation;
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();
}
}
function register_taxonomy() {
$labels = array(
'name' => $this->get_name(),
'singular_name' => __( 'Taxonomy','textdomain' ),
'search_items' => __( 'Search taxonomies', 'textdomain' ),
'all_items' => __( 'All taxonomies', 'textdomain' ),
'parent_item' => __( 'Parent taxonomy', 'textdomain' ),
'parent_item_colon' => __( 'Parent taxonomy:', 'textdomain' ),
'edit_item' => __( 'Edit taxonomy', 'textdomain' ),
'update_item' => __( 'Update taxonomy', 'textdomain' ),
'add_new_item' => __( 'Add New taxonomy', 'textdomain' ),
'new_item_name' => __( 'New Genre taxonomy', 'textdomain' ),
'menu_name' => __( 'Genre', 'textdomain' ),
);
$args = array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => tnc_enable_dev_wp_interface(),
'show_admin_column' => tnc_enable_dev_wp_interface(),
'rewrite' => [
'slug' => $this->get_slug()
],
);
$tax_cpts = [];
if (is_array($this->get_collections()))
foreach ($this->get_collections() as $tax_col)
$tax_cpts[] = $tax_col->get_db_identifier();
if (taxonomy_exists($this->get_db_identifier()))
unregister_taxonomy($this->get_db_identifier());
register_taxonomy(
$this->get_db_identifier(),
$tax_cpts,
$args
);
return true;
}
// Getters
function get_id() {
return $this->get_mapped_property('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_allow_insert() {
return ( boolean ) $this->get_mapped_property('allow_insert');
}
function get_slug() {
return $this->get_mapped_property('slug');
}
// special Getters
//
function get_db_identifier() {
return $this->get_id() ? 'tnc_tax_' . $this->get_id() : false;
}
// 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_slug($value) {
return $this->set_mapped_property('slug', $value);
}
function set_description($value) {
return $this->set_mapped_property('description', $value);
}
function set_allow_insert($value) {
return $this->set_mapped_property('allow_insert', $value);
}
}

View File

@ -0,0 +1,78 @@
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
class Tainacan_Term extends Tainacan_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

@ -0,0 +1,20 @@
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Class TainacanFieldType
*/
abstract class Tainacan_Field_Type {
abstract function render( $metadata );
function validate($value) {
return true;
}
function get_validation_errors() {
return [];
}
}

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

@ -0,0 +1,147 @@
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
class Tainacan_Collections {
const POST_TYPE = 'tainacan-collections';
var $map = [
'ID' => [
'map' => 'ID',
'validation' => ''
],
'name' => [
'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' => ''
],
];
function __construct() {
add_action('init', array(&$this, 'register_post_type'));
}
function register_post_type() {
$labels = array(
'name' => 'Collections',
'singular_name' => 'Collections',
'add_new' => 'Adicionar Novo',
'add_new_item' =>'Adicionar Collections',
'edit_item' => 'Editar',
'new_item' => 'Novo Collections',
'view_item' => 'Visualizar',
'search_items' => 'Pesquisar',
'not_found' => 'Nenhum ticket encontrado',
'not_found_in_trash' => 'Nenhum Collections encontrado na lixeira',
'parent_item_colon' => 'Collections acima:',
'menu_name' => 'Collections'
);
$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 insert(Tainacan_Collection $collection) {
// First iterate through the native post properties
$map = $this->map;
foreach ($map as $prop => $mapped) {
if ($mapped['map'] != 'meta' && $mapped['map'] != 'meta_multi') {
$collection->WP_Post->{$mapped['map']} = $collection->get_mapped_property($prop);
}
}
// save post and geet its ID
$collection->WP_Post->post_type = self::POST_TYPE;
$collection->WP_Post->post_status = 'publish';
// TODO verificar se salvou mesmo
$id = wp_insert_post($collection->WP_Post);
// reset object
$collection->WP_Post = get_post($id);
// Now run through properties stored as postmeta
foreach ($map as $prop => $mapped) {
if ($mapped['map'] == 'meta') {
update_post_meta($id, $prop, $collection->get_mapped_property($prop));
} elseif ($mapped['map'] == 'meta_multi') {
$values = $collection->get_mapped_property($prop);
delete_post_meta($id, $prop);
if (is_array($values))
foreach ($values as $value)
add_post_meta($id, $prop, $value);
}
}
$collection->register_post_type();
// return a brand new object
return new Tainacan_Collection($collection->WP_Post);
}
function get_collections($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_Collection($post);
}
// TODO: Pegar coleções registradas via código
return $return;
}
function get_collection_by_id($id) {
return new Tainacan_Collection($id);
}
}

View File

@ -0,0 +1,58 @@
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
class Tainacan_Item_Metadata {
function get_item_metadata_by_item(Tainacan_Item $item) {
global $Tainacan_Items, $Tainacan_Metadatas;
$collection = $item->get_collection();
if (!$collection instanceof Tainacan_Collection)
return [];
$meta_list = $Tainacan_Metadatas->get_metadata_by_collection($collection);
$return = [];
if (is_array($meta_list)) {
foreach ($meta_list as $meta) {
$return = new Tainacan_Item_Metadata_Entity($item, $meta);
}
}
return $return;
}
function insert(Tainacan_Item_Metadata_Entity $item_metadata) {
$unique = ! $item_metadata->is_multiple();
if ($unique) {
update_post_meta($item_metadata->item->get_id(), $item_metadata->metadata->get_id(), $item_metadata->get_value());
} else {
delete_post_meta($item_metadata->item->get_id(), $item_metadata->metadata->get_id());
if (is_array($item_metadata->get_value()))
foreach ($item_metadata->get_value() as $value)
add_post_meta($item_metadata->item->get_id(), $item_metadata->metadata->get_id(), $value);
}
// return a brand new object
return new Tainacan_Item_Metadata_Entity($item_metadata->get_item(), $item_metadata->get_metadata());
}
function get_item_metadata_value(Tainacan_Item_Metadata_Entity $item_metadata) {
$unique = ! $item_metadata->is_multiple();
return get_post_meta($item_metadata->item->get_id(), $item_metadata->metadata->get_id(), $unique);
}
}

View File

@ -0,0 +1,218 @@
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
class Tainacan_Items {
var $map = [
'ID' => [
'map' => 'ID',
'validation' => ''
],
'title' => [
'map' => 'post_title',
'validation' => ''
],
'description' => [
'map' => 'post_content',
'validation' => ''
],
'collection_id' => [
'map' => 'meta',
'validation' => ''
],
//'collection' => 'relation...',
// metadata .. metadata...
];
function __construct() {
add_action('init', array(&$this, 'register_post_types'));
}
function register_post_types() {
global $Tainacan_Collections, $Tainacan_Taxonomies;
$collections = $Tainacan_Collections->get_collections();
$taxonomies = $Tainacan_Taxonomies->get_taxonomies();
if (!is_array($collections))
return;
// register collections post type and associate taxonomies
foreach ($collections as $collection) {
$collection->register_post_type();
}
// register taxonomies
foreach ($taxonomies as $taxonomy) {
$taxonomy->register_taxonomy();
}
}
function insert(Tainacan_Item $item) {
$map = $this->map;
// get collection to determine post type
$collection = $item->get_collection();
if (!$collection)
return false;
$cpt = $collection->get_db_identifier();
// iterate through the native post properties
foreach ($map as $prop => $mapped) {
if ($mapped['map'] != 'meta' && $mapped['map'] != 'meta_multi') {
$item->WP_Post->{$mapped['map']} = $item->get_mapped_property($prop);
}
}
// save post and geet its ID
$item->WP_Post->post_type = $cpt;
$item->WP_Post->post_status = 'publish';
$id = wp_insert_post($item->WP_Post);
$item->WP_Post = get_post($id);
// Now run through properties stored as postmeta
foreach ($map as $prop => $mapped) {
if ($mapped['map'] == 'meta') {
update_post_meta($id, $prop, $item->get_mapped_property($prop));
} elseif ($mapped['map'] == 'meta_multi') {
$values = $item->get_mapped_property($prop);
delete_post_meta($id, $prop);
if (is_array($values))
foreach ($values as $value)
add_post_meta($id, $prop, $value);
}
}
// save metadata
$metadata = $item->get_metadata();
global $Tainacan_Item_Metadata;
foreach ($metadata as $meta) {
$Tainacan_Item_Metadata->insert($meta);
}
// return a brand new object
return new Tainacan_Item($item->WP_Post);
}
// collections id or array of ids; collection object or array of objects
function get_items($args = array(), $collections = []) {
global $Tainacan_Collections;
if (empty($collections)) {
$collections = $Tainacan_Collections->get_collections();
}
if (is_numeric($collections))
$collections = $Tainacan_Collections->get_collection_by_id($collection);
if ($collections instanceof Tainacan_Collection) {
$cpt = $collections->get_db_identifier();
} elseif (is_array($collections)) {
$cpt = [];
foreach ($collections as $collection) {
if (is_numeric($collection))
$collection = $Tainacan_Collections->get_collection_by_id($collection);
if ($collection instanceof Tainacan_Collection)
$cpt[] = $collection->get_db_identifier();
}
} else {
return [];
}
if (empty($cpt))
return [];
$args = array_merge([
'post_type' => $cpt,
'posts_per_page' => -1,
'post_status' => 'publish',
], $args);
$posts = get_posts($args);
$return = [];
foreach ($posts as $post) {
$return[] = new Tainacan_Item($post);
}
return $return;
}
// same as WP_Query with few paramaters more:
// collections ID or array of IDs, object or array of objects
// metadata - array of metadata in meta_query format
// other item properties, present in the "map"
function query($args) {
$map = $this->map;
$wp_query_exceptions = [
'ID' => 'p',
'post_title' => 'title'
];
$meta_query = [];
foreach ($map as $prop => $mapped) {
if (array_key_exists($prop, $args)) {
$prop_value = $args[$prop];
unset($args[$prop]);
if ( $mapped['map'] == 'meta' || $mapped['map'] == 'meta_multi' ) {
$meta_query[] = [
'key' => $prop,
'value' => $prop_value
];
} else {
$prop_search_name = array_key_exists($mapped['map'], $wp_query_exceptions) ? $wp_query_exceptions[$mapped['map']] : $mapped['map'];
$args[$prop_search_name] = $prop_value;
}
}
}
if ( isset($args['metadata']) && is_array($args['metadata']) && !empty($args['metadata'])) {
$meta_query = array_merge($args['metadata'],$meta_query);
}
$args['meta_query'] = $meta_query;
unset($args['metadata']);
$collections = !empty($args['collections']) ? $args['collections'] : [];
unset($args['collections']);
return $this->get_items($args, $collections);
### TODO I think its better if we return a WP_Query object. easier for loop and debugging
}
function get_item_by_id($id) {
return new Tainacan_Item($id);
}
}

View File

@ -0,0 +1,195 @@
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Class Tainacan_Metadatas
*/
class Tainacan_Metadatas {
const POST_TYPE = 'tainacan-metadata';
var $map = [
'ID' => [
'map' => 'ID',
'validation' => ''
],
'name' => [
'map' => 'post_title',
'validation' => ''
],
'order' => [
'map' => 'menu_order',
'validation' => ''
],
'parent' => [
'map' => 'parent',
'validation' => ''
],
'description' => [
'map' => 'post_content',
'validation' => ''
],
'type' => [
'map' => 'meta',
'validation' => ''
],
'required' => [
'map' => 'meta',
'validation' => '', // yes or no
'default' => 'no'
],
'collection_key' => [
'map' => 'meta',
'validation' => '', // yes or no. it cant be key if its multiple
'default' => 'no'
],
'multiple' => [
'map' => 'meta',
'validation' => '', // yes or no. It cant be multiple if its collection_key
'default' => 'no'
],
'cardinality' => [
'map' => 'meta',
'validation' => '',
'default' => 1
],
'privacy' => [
'map' => 'meta',
'validation' => ''
],
'mask' => [
'map' => 'meta',
'validation' => ''
],
'default_value' => [
'map' => 'meta',
'validation' => ''
],
'option' => [
'map' => 'meta',
'validation' => ''
],
'collection_id' => [
'map' => 'meta',
'validation' => ''
],
];
function __construct() {
add_action('init', array(&$this, 'register_post_type'));
}
function register_post_type() {
$labels = array(
'name' => 'Metadata',
'singular_name' => 'Metadata',
'add_new' => 'Adicionar Metadata',
'add_new_item' =>'Adicionar Metadata',
'edit_item' => 'Editar',
'new_item' => 'Novo Metadata',
'view_item' => 'Visualizar',
'search_items' => 'Pesquisar',
'not_found' => 'Nenhum ticket encontrado',
'not_found_in_trash' => 'Nenhum Collections encontrado na lixeira',
'parent_item_colon' => 'Metadata acima:',
'menu_name' => 'Metadata'
);
$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);
}
/**
* @param Tainacan_Metadata $metadata
* @return int
*/
function insert( Tainacan_Metadata $metadata ) {
// First iterate through the native post properties
$map = $this->map;
foreach ($map as $prop => $mapped) {
if ($mapped['map'] != 'meta' && $mapped['map'] != 'meta_multi') {
$metadata->WP_Post->{$mapped['map']} = $metadata->get_mapped_property($prop);
}
}
// save post and get its ID
$metadata->WP_Post->post_type = self::POST_TYPE;
$metadata->WP_Post->post_status = 'publish';
$id = wp_insert_post($metadata->WP_Post);
$metadata->WP_Post = get_post($id);
// Now run through properties stored as postmeta
foreach ($map as $prop => $mapped) {
if ($mapped['map'] == 'meta') {
update_post_meta($id, $prop, $metadata->get_mapped_property($prop));
} elseif ($mapped['map'] == 'meta_multi') {
$values = $metadata->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_Metadata($metadata->WP_Post);
}
/**
* @param ( Tainacan_Collection ) $collection_id
* @param array $args
* @return array
*/
function get_metadata_by_collection( $collection, $args = array()) {
// TODO: get metadata from parent collections
$collection_id = ( is_object( $collection ) ) ? $collection->get_id() : $collection;
$args = array_merge([
'post_type' => self::POST_TYPE,
'posts_per_page' => -1,
'post_status' => 'publish',
'meta_key' => 'collection_id',
'meta_value' => $collection_id
], $args);
$posts = get_posts($args);
$return = [];
foreach ($posts as $post) {
$return[] = new Tainacan_Metadata($post);
}
return $return;
}
/**
* @param int $id
* @return Tainacan_Metadata
*/
function get_metadata_by_id($id) {
return new Tainacan_Metadata($id);
}
}

View File

@ -0,0 +1,173 @@
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Class Tainacan_Taxonomies
*/
class Tainacan_Taxonomies {
const POST_TYPE = 'tainacan-taxonomies';
var $map = [
'ID' => [
'map' => 'ID',
'validation' => ''
],
'name' => [
'map' => 'post_title',
'validation' => ''
],
'parent' => [
'map' => 'parent',
'validation' => ''
],
'description' => [
'map' => 'post_content',
'validation' => ''
],
'slug' => [
'map' => 'post_name',
'validation' => ''
],
'allow_insert' => [
'map' => 'meta',
'validation' => ''
],
'collections_ids' => [
'map' => 'meta_multi',
'validation' => ''
],
];
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' => 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 get_taxonomies($args = []){
$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_Taxonomy($post);
}
// TODO: Pegar taxonomias registradas via código
return $return;
}
/**
* @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['map'] != 'meta' && $mapped['map'] != 'meta_multi') {
$taxonomy->WP_Post->{$mapped['map']} = $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);
$taxonomy->WP_Post = get_post($id);
// Now run through properties stored as postmeta
foreach ($map as $prop => $mapped) {
if ($mapped['map'] == 'meta') {
update_post_meta($id, $prop, $taxonomy->get_mapped_property($prop));
} elseif ($mapped['map'] == 'meta_multi') {
$values = $taxonomy->get_mapped_property($prop);
delete_post_meta($id, $prop);
if (is_array($values))
foreach ($values as $value)
add_post_meta($id, $prop, $value);
}
}
$taxonomy->register_taxonomy();
// return a brand new object
return new Tainacan_Taxonomy($taxonomy->WP_Post);
}
function registerTainacanTaxonomy( $taxonomy_name ){
$labels = array(
'name' => __( 'Taxonomies', 'textdomain' ),
'singular_name' => __( 'Taxonomy','textdomain' ),
'search_items' => __( 'Search taxonomies', 'textdomain' ),
'all_items' => __( 'All taxonomies', 'textdomain' ),
'parent_item' => __( 'Parent taxonomy', 'textdomain' ),
'parent_item_colon' => __( 'Parent taxonomy:', 'textdomain' ),
'edit_item' => __( 'Edit taxonomy', 'textdomain' ),
'update_item' => __( 'Update taxonomy', 'textdomain' ),
'add_new_item' => __( 'Add New taxonomy', 'textdomain' ),
'new_item_name' => __( 'New Genre taxonomy', 'textdomain' ),
'menu_name' => __( 'Genre', 'textdomain' ),
);
$args = array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => tnc_enable_dev_wp_interface(),
'show_admin_column' => tnc_enable_dev_wp_interface(),
);
register_taxonomy( $taxonomy_name, array( ), $args );
}
function get_taxonomy_by_id($id) {
return new Tainacan_Taxonomy($id);
}
}

View File

@ -0,0 +1,75 @@
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Class Tainacan_Terms
*/
class Tainacan_Terms {
var $map = [
'term_id' => [
'map' => 'term_id',
'validation' => ''
],
'name' => [
'map' => 'name',
'validation' => ''
],
'parent' => [
'map' => 'parent',
'validation' => ''
],
'description' => [
'map' => 'description',
'validation' => ''
],
'taxonomy' => [
'map' => 'taxonomy',
'validation' => ''
],
'user' => [
'map' => 'termmeta',
'validation' => ''
],
];
function insert( Tainacan_Term $term ){
// First iterate through the native post properties
$map = $this->map;
foreach ($map as $prop => $mapped) {
if ($mapped['map'] != 'termmeta') {
$term->WP_Term->{$mapped['map']} = $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['map'] == '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;
}
}

View File

@ -0,0 +1,37 @@
<?php
// used by Item, Event, Field
trait Tainacan_Entity_Collection_Relation {
function get_collection_id() {
return $this->get_mapped_property('collection_id');
}
function get_collection() {
if (isset($this->collection) && $this->collection instanceof Tainacan_Collection)
return $this->collection;
if (is_numeric($this->get_collection_id())) {
global $Tainacan_Collections;
$this->collection = $Tainacan_Collections->get_collection_by_id($this->get_collection_id());
return $this->collection;
}
return null;
}
function set_collection_id($value) {
$this->collection = null;
return $this->set_mapped_property('collection_id', $value);
}
function set_collection(Tainacan_Collection $collection) {
$this->collection = $collection;
$this->set_collection_id($collection->get_id());
}
}

View File

@ -0,0 +1,46 @@
<?php
// used by Taxonomy
trait Tainacan_Entity_Collections_Relation {
function get_collections_ids() {
return $this->get_mapped_property('collections_ids');
}
function get_collections() {
if (isset($this->collection) && !empty($this->collection) && is_array($this->collection))
return $this->collection;
if (is_array($this->get_collections_ids()) && !empty(is_array($this->get_collections_ids()))) {
global $Tainacan_Collections;
$collections = [];
foreach ($this->get_collections_ids() as $col_id) {
$collections[] = $Tainacan_Collections->get_collection_by_id($col_id);
}
return $collections;
}
return null;
}
function set_collections_ids(Array $value) {
return $this->set_mapped_property('collection_id', $value);
$this->collections = null;
}
function set_collections(Array $collections) {
$collections_ids = [];
$this->collections = $collections;
foreach ($collections as $collection)
$collections_ids[] = $collection->get_id();
$this->set_collections_ids($collections_ids);
}
}