prototipo da classe Collections e Items, com opcao para interface padrao WP

This commit is contained in:
Leo Germani 2017-11-03 18:23:41 -02:00
parent 007afeb2ea
commit 95d00ed9e1
8 changed files with 437 additions and 66 deletions

View File

@ -0,0 +1,85 @@
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
class TainacanCollection extends Entity {
function __construct($which = 0) {
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 map_properties() {
return [
'ID' => 'ID',
'name' => 'post_title',
'order' => 'menu_order',
'parent' => 'parent',
'description' => 'post_content',
'itens_per_page' => 'meta'
];
}
// 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_itens_per_page() {
return $this->get_mapped_property('itens_per_page');
}
// 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_itens_per_page($value) {
return $this->set_mapped_property('itens_per_page', $value);
}
}

View File

38
src/classes/Entity.php Normal file
View File

@ -0,0 +1,38 @@
<?php
class Entity {
function get_mapped_property($prop) {
if (isset($this->$prop) && !empty($this->$prop))
return $this->$prop;
$map = $this->map_properties();
if (!array_key_exists($prop, $map))
return null;
$mapped = $map[$prop];
if ($mapped == 'meta') {
return get_post_meta($this->WP_Post->ID, $prop, true);
} else {
return isset($this->WP_Post->$mapped) ? $this->WP_Post->$mapped : null;
}
}
function set_mapped_property($prop, $value) {
$this->$prop = $value;
}
}

View File

@ -0,0 +1,127 @@
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
class TainacanCollections {
const POST_TYPE = 'tainacan-collections';
const DB_IDENTIFIER_META = '_db_identifier';
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(TainacanCollection $collection) {
// First iterate through the native post properties
$map = $collection->map_properties();
foreach ($map as $prop => $mapped) {
if ($mapped != 'meta') {
$collection->WP_Post->$mapped = $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';
$id = wp_insert_post($collection->WP_Post);
// Now run through properties stored as postmeta
foreach ($map as $prop => $mapped) {
if ($mapped == 'meta') {
update_post_meta($id, $prop, $collection->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 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 TainacanCollection($post);
}
// TODO: Pegar coleções registradas via código
return $return;
}
function get_collection_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;
}
function get_collection_by_id($id) {
return new TainacanCollection($id);
}
}
global $TainacanCollections;
$TainacanCollections = new TainacanCollections();

View File

@ -0,0 +1,100 @@
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
class TainacanItems {
function __construct() {
add_action('init', array(&$this, 'register_post_types'));
}
function register_post_types() {
global $TainacanCollections;
$collections = $TainacanCollections->get_collections();
$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' => 'Item'
);
if (!is_array($collections))
return;
foreach ($collections as $collection) {
$labels['menu_name'] = $collection->get_name();
$cpt_slug = $TainacanCollections->get_collection_db_identifier($collection->get_id());
$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($cpt_slug, $args);
}
}
/*
function insert(TainacanItem $item) {
// First iterate through the native post properties
$map = $item->map_properties();
foreach ($map as $prop => $mapped) {
if ($mapped != 'meta') {
$item->WP_Post->$mapped = $item->get_mapped_property($prop);
}
}
// save post and geet its ID
$item->WP_Post->post_type = self::POST_TYPE;
$id = wp_insert_post($item->WP_Post);
// Now run through properties stored as postmeta
foreach ($map as $prop => $mapped) {
if ($mapped == 'meta') {
update_post_meta($id, $prop, $item->get_mapped_property($prop));
}
}
return $id;
}
*/
function getItemById($id) {
return new TainacanItem($id);
}
}
global $TainacanItems;
$TainacanItems = new TainacanItems();

View File

@ -7,64 +7,74 @@ Author: MediaLab UFG
Version: 10.9.8.7.6.5.4
*/
include('classes/Repositories/Collections.php');
include('classes/Repositories/Items.php');
include('classes/Entity.php');
include('classes/Entities/Collection.php');
class TainacanCollections {
/**
*
*
* 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)
*
*
*/
const POST_TYPE = 'tainacan-collections';
function __construct() {
add_action('init', array(&$this, 'register_post_types'));
}
function register_post_types() {
$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' => false,
'show_in_menu' => false,
//'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 add($title) {
$post = [
'post_title' => $title,
'post_type' => self::POST_TYPE
];
return wp_insert_post($post);
}
function getCollectionById($id) {
return get_post($id);
}
function tnc_enable_dev_wp_interface() {
return defined('TNC_ENABLE_DEV_WP_INTERFACE') && true === TNC_ENABLE_DEV_WP_INTERFACE ? true : false;
}
global $TainacanCollections;
$TainacanCollections = new TainacanCollections();

View File

@ -6,6 +6,7 @@
*/
$_tests_dir = getenv( 'WP_TESTS_DIR' );
$_tests_dir = '/home/leo/devel/rhs/tests/wordpress-tests-lib';
if ( ! $_tests_dir ) {
$_tests_dir = '/tmp/wordpress-tests-lib';
}

View File

@ -14,15 +14,25 @@ class TestCollections extends WP_UnitTestCase {
* A single example test.
*/
function test_add() {
// Replace this with some actual testing code.
global $TainacanCollections;
$testTitle = 'Teste';
$newId = $TainacanCollections->add($testTitle);
$check = $TainacanCollections->getCollectionById($newId);
$x = new TainacanCollection();
$x->set_name('teste');
$x->set_description('adasdasdsa');
$x->set_itens_per_page(23);
global $TainacanCollections;
$id = $TainacanCollections->insert($x);
//
$test = $TainacanCollections->get_collection_by_id($id);
$this->assertEquals($test->get_name(), 'teste');
$this->assertEquals($test->get_description(), 'adasdasdsa');
$this->assertEquals($test->get_itens_per_page(), 23);
$this->assertEquals($check->ID, $newId);
$this->assertEquals($check->post_title, $testTitle);
}
}