organizando metodos de relações entre classes
This commit is contained in:
parent
d69aad435b
commit
15f6821293
|
@ -7,7 +7,7 @@ if ( ! defined( 'ABSPATH' ) ) {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class TainacanCollection extends Entity {
|
class TainacanCollection extends Entity {
|
||||||
|
|
||||||
|
|
||||||
function __construct($which = 0) {
|
function __construct($which = 0) {
|
||||||
|
@ -27,6 +27,51 @@ class TainacanCollection extends Entity {
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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
|
// Getters
|
||||||
//
|
//
|
||||||
|
@ -36,6 +81,9 @@ class TainacanCollection extends Entity {
|
||||||
function get_name() {
|
function get_name() {
|
||||||
return $this->get_mapped_property('name');
|
return $this->get_mapped_property('name');
|
||||||
}
|
}
|
||||||
|
function get_slug() {
|
||||||
|
return $this->get_mapped_property('slug');
|
||||||
|
}
|
||||||
function get_order() {
|
function get_order() {
|
||||||
return $this->get_mapped_property('order');
|
return $this->get_mapped_property('order');
|
||||||
}
|
}
|
||||||
|
@ -53,8 +101,7 @@ class TainacanCollection extends Entity {
|
||||||
//
|
//
|
||||||
|
|
||||||
function get_db_identifier() {
|
function get_db_identifier() {
|
||||||
global $TainacanCollections;
|
return $this->get_id() ? 'tnc_col_' . $this->get_id() : false;
|
||||||
return $TainacanCollections->get_collection_db_identifier($this->get_id());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -64,6 +111,9 @@ class TainacanCollection extends Entity {
|
||||||
function set_name($value) {
|
function set_name($value) {
|
||||||
return $this->set_mapped_property('name', $value);
|
return $this->set_mapped_property('name', $value);
|
||||||
}
|
}
|
||||||
|
function set_slug($value) {
|
||||||
|
return $this->set_mapped_property('slug', $value);
|
||||||
|
}
|
||||||
function set_order($value) {
|
function set_order($value) {
|
||||||
return $this->set_mapped_property('order', $value);
|
return $this->set_mapped_property('order', $value);
|
||||||
}
|
}
|
||||||
|
@ -78,4 +128,19 @@ class TainacanCollection extends Entity {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Relation methods
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
function get_metadata( $collection ){
|
||||||
|
global $Tainacan_Metadatas;
|
||||||
|
return $Tainacan_Metadatas->get_collection_metadata( $collection );
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
|
@ -9,6 +9,7 @@ if ( ! defined( 'ABSPATH' ) ) {
|
||||||
|
|
||||||
class TainacanItem extends Entity {
|
class TainacanItem extends Entity {
|
||||||
|
|
||||||
|
use EntityCollectionRelation;
|
||||||
|
|
||||||
function __construct($which = 0) {
|
function __construct($which = 0) {
|
||||||
|
|
||||||
|
@ -45,26 +46,7 @@ class TainacanItem extends Entity {
|
||||||
function get_description() {
|
function get_description() {
|
||||||
return $this->get_mapped_property('description');
|
return $this->get_mapped_property('description');
|
||||||
}
|
}
|
||||||
function get_collection_id() {
|
|
||||||
return $this->get_mapped_property('collection_id');
|
|
||||||
}
|
|
||||||
|
|
||||||
// sepecial Getters
|
|
||||||
function get_collection() {
|
|
||||||
if (isset($this->collection) && $this->collection instanceof TainacanCollection)
|
|
||||||
return $this->collection;
|
|
||||||
|
|
||||||
if (is_numeric($this->get_collection_id())) {
|
|
||||||
$collection = get_post($this->get_collection_id());
|
|
||||||
if ($collection instanceof WP_Post) {
|
|
||||||
$this->collection = new TainacanCollection($collection);
|
|
||||||
return $this->collection;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
function get_metadata(){
|
function get_metadata(){
|
||||||
global $TainacanItems;
|
global $TainacanItems;
|
||||||
|
@ -91,16 +73,11 @@ class TainacanItem extends Entity {
|
||||||
function set_description($value) {
|
function set_description($value) {
|
||||||
return $this->set_mapped_property('description', $value);
|
return $this->set_mapped_property('description', $value);
|
||||||
}
|
}
|
||||||
function set_collection_id($value) {
|
|
||||||
return $this->set_mapped_property('collection_id', $value);
|
|
||||||
}
|
|
||||||
|
|
||||||
// sepecial Setters
|
// sepecial Setters
|
||||||
|
|
||||||
function set_collection(TainacanCollection $collection) {
|
|
||||||
$this->collection = $collection;
|
|
||||||
$this->set_collection_id($collection->get_id());
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
function set_metadata( $metadata ){
|
function set_metadata( $metadata ){
|
||||||
|
|
|
@ -5,6 +5,8 @@ if ( ! defined( 'ABSPATH' ) ) {
|
||||||
|
|
||||||
class Tainacan_Metadata extends Entity {
|
class Tainacan_Metadata extends Entity {
|
||||||
|
|
||||||
|
use EntityCollectionRelation;
|
||||||
|
|
||||||
function __construct( $which = 0 ) {
|
function __construct( $which = 0 ) {
|
||||||
|
|
||||||
$this->repository = 'Tainacan_Metadatas';
|
$this->repository = 'Tainacan_Metadatas';
|
||||||
|
@ -76,12 +78,6 @@ class Tainacan_Metadata extends Entity {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function get_collection() {
|
|
||||||
return new TainacanCollection( $this->get_mapped_property('collection') );
|
|
||||||
}
|
|
||||||
|
|
||||||
// Setters
|
// Setters
|
||||||
|
|
||||||
|
|
||||||
|
@ -132,12 +128,5 @@ class Tainacan_Metadata extends Entity {
|
||||||
return $this->set_mapped_property('option', serialize($value) ) ;
|
return $this->set_mapped_property('option', serialize($value) ) ;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @param TainacanCollection
|
|
||||||
*/
|
|
||||||
function set_collection($value) {
|
|
||||||
$ID = ($value instanceof TainacanCollection ) ? $value->get_id() : $value;
|
|
||||||
return $this->set_mapped_property('collection', $ID);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
|
@ -5,6 +5,8 @@ if ( ! defined( 'ABSPATH' ) ) {
|
||||||
|
|
||||||
class Tainacan_Taxonomy extends Entity {
|
class Tainacan_Taxonomy extends Entity {
|
||||||
|
|
||||||
|
use EntityCollectionsRelation;
|
||||||
|
|
||||||
function __construct( $which = 0 ) {
|
function __construct( $which = 0 ) {
|
||||||
|
|
||||||
$this->repository = 'Tainacan_Taxonomies';
|
$this->repository = 'Tainacan_Taxonomies';
|
||||||
|
@ -22,6 +24,49 @@ class Tainacan_Taxonomy extends Entity {
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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
|
// Getters
|
||||||
|
|
||||||
|
@ -47,17 +92,18 @@ class Tainacan_Taxonomy extends Entity {
|
||||||
return ( boolean ) $this->get_mapped_property('allow_insert');
|
return ( boolean ) $this->get_mapped_property('allow_insert');
|
||||||
}
|
}
|
||||||
|
|
||||||
function get_taxonomy_name() {
|
function get_slug() {
|
||||||
return $this->get_mapped_property('taxonomy_name');
|
return $this->get_mapped_property('slug');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// special Getters
|
||||||
|
//
|
||||||
// TODO: Uma taxonomia pode estar vinculada a mais de uma coleção
|
|
||||||
function get_collection() {
|
function get_db_identifier() {
|
||||||
return new TainacanCollection( $this->get_mapped_property('collection') );
|
return $this->get_id() ? 'tnc_tax_' . $this->get_id() : false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Setters
|
// Setters
|
||||||
|
|
||||||
|
|
||||||
|
@ -68,6 +114,10 @@ class Tainacan_Taxonomy extends Entity {
|
||||||
function set_parent($value) {
|
function set_parent($value) {
|
||||||
return $this->set_mapped_property('parent', $value);
|
return $this->set_mapped_property('parent', $value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function set_slug($value) {
|
||||||
|
return $this->set_mapped_property('slug', $value);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
function set_description($value) {
|
function set_description($value) {
|
||||||
|
@ -78,13 +128,4 @@ class Tainacan_Taxonomy extends Entity {
|
||||||
return $this->set_mapped_property('allow_insert', $value);
|
return $this->set_mapped_property('allow_insert', $value);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @param TainacanCollection
|
|
||||||
*
|
|
||||||
* TODO: Uma taxonomia pode estar vinculada a mais de uma coleção
|
|
||||||
*/
|
|
||||||
function set_collection($value) {
|
|
||||||
$ID = ($value instanceof TainacanCollection ) ? $value->get_id() : $value;
|
|
||||||
return $this->set_mapped_property('collection', $ID);
|
|
||||||
}
|
|
||||||
}
|
}
|
|
@ -22,6 +22,8 @@ class Entity {
|
||||||
|
|
||||||
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);
|
||||||
|
}elseif ( $mapped == 'meta_multi') {
|
||||||
|
return get_post_meta($this->WP_Post->ID, $prop, false);
|
||||||
}elseif ( $mapped == 'termmeta' ){
|
}elseif ( $mapped == 'termmeta' ){
|
||||||
return get_term_meta($this->WP_Term->term_id, $prop, true);
|
return get_term_meta($this->WP_Term->term_id, $prop, true);
|
||||||
}elseif ( isset( $this->WP_Post )) {
|
}elseif ( isset( $this->WP_Post )) {
|
||||||
|
@ -39,6 +41,17 @@ class Entity {
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function save() {
|
||||||
|
|
||||||
|
// validate
|
||||||
|
|
||||||
|
global ${$this->repository};
|
||||||
|
|
||||||
|
return ${$this->repository}->insert($this);
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
|
@ -8,7 +8,6 @@ if ( ! defined( 'ABSPATH' ) ) {
|
||||||
class TainacanCollections {
|
class TainacanCollections {
|
||||||
|
|
||||||
const POST_TYPE = 'tainacan-collections';
|
const POST_TYPE = 'tainacan-collections';
|
||||||
const DB_IDENTIFIER_META = '_db_identifier';
|
|
||||||
|
|
||||||
var $map = [
|
var $map = [
|
||||||
'ID' => 'ID',
|
'ID' => 'ID',
|
||||||
|
@ -16,6 +15,7 @@ class TainacanCollections {
|
||||||
'order' => 'menu_order',
|
'order' => 'menu_order',
|
||||||
'parent' => 'parent',
|
'parent' => 'parent',
|
||||||
'description' => 'post_content',
|
'description' => 'post_content',
|
||||||
|
'slug' => 'post_name',
|
||||||
'itens_per_page' => 'meta'
|
'itens_per_page' => 'meta'
|
||||||
];
|
];
|
||||||
|
|
||||||
|
@ -71,24 +71,27 @@ class TainacanCollections {
|
||||||
// save post and geet its ID
|
// save post and geet its ID
|
||||||
$collection->WP_Post->post_type = self::POST_TYPE;
|
$collection->WP_Post->post_type = self::POST_TYPE;
|
||||||
$collection->WP_Post->post_status = 'publish';
|
$collection->WP_Post->post_status = 'publish';
|
||||||
|
|
||||||
|
// TODO verificar se salvou mesmo
|
||||||
$id = wp_insert_post($collection->WP_Post);
|
$id = wp_insert_post($collection->WP_Post);
|
||||||
|
|
||||||
|
// reset object
|
||||||
|
$collection->WP_Post = get_post($id);
|
||||||
|
|
||||||
// Now run through properties stored as postmeta
|
// Now run through properties stored as postmeta
|
||||||
foreach ($map as $prop => $mapped) {
|
foreach ($map as $prop => $mapped) {
|
||||||
if ($mapped == 'meta') {
|
if ($mapped == 'meta') {
|
||||||
update_post_meta($id, $prop, $collection->get_mapped_property($prop));
|
update_post_meta($id, $prop, $collection->get_mapped_property($prop));
|
||||||
|
} elseif ($mapped == '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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// DB Identifier
|
$collection->register_post_type();
|
||||||
// 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;
|
return $id;
|
||||||
}
|
}
|
||||||
|
@ -114,26 +117,10 @@ class TainacanCollections {
|
||||||
return $return;
|
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) {
|
function get_collection_by_id($id) {
|
||||||
return new TainacanCollection($id);
|
return new TainacanCollection($id);
|
||||||
}
|
}
|
||||||
|
|
||||||
function get_metadata( $collection ){
|
|
||||||
global $Tainacan_Metadatas;
|
|
||||||
return $Tainacan_Metadatas->get_collection_metadata( $collection );
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -26,21 +26,6 @@ class TainacanItems {
|
||||||
$collections = $TainacanCollections->get_collections();
|
$collections = $TainacanCollections->get_collections();
|
||||||
$taxonomies = $Tainacan_Taxonomies->get_taxonomies();
|
$taxonomies = $Tainacan_Taxonomies->get_taxonomies();
|
||||||
|
|
||||||
$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' => 'Item'
|
|
||||||
);
|
|
||||||
|
|
||||||
if (!is_array($collections))
|
if (!is_array($collections))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
@ -51,59 +36,14 @@ class TainacanItems {
|
||||||
// register collections post type and associate taxonomies
|
// register collections post type and associate taxonomies
|
||||||
foreach ($collections as $collection) {
|
foreach ($collections as $collection) {
|
||||||
|
|
||||||
$cpt_labels['menu_name'] = $collection->get_name();
|
$collection->register_post_type();
|
||||||
$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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// register taxonomies
|
// register taxonomies
|
||||||
foreach ($taxonomies as $taxonomy) {
|
foreach ($taxonomies as $taxonomy) {
|
||||||
$labels = array(
|
$taxonomy->register_taxonomy();
|
||||||
'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(),
|
|
||||||
);
|
|
||||||
|
|
||||||
// TODO Uma taxonomia pode estar vinculada a mais de uma coleção
|
|
||||||
register_taxonomy(
|
|
||||||
$Tainacan_Taxonomies->get_taxonomy_db_identifier($taxonomy),
|
|
||||||
array( $TainacanCollections->get_collection_db_identifier($taxonomy->get_collection()->get_id()) ), $args
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -134,11 +74,18 @@ class TainacanItems {
|
||||||
// save post and geet its ID
|
// save post and geet its ID
|
||||||
$item->WP_Post->post_type = $cpt;
|
$item->WP_Post->post_type = $cpt;
|
||||||
$id = wp_insert_post($item->WP_Post);
|
$id = wp_insert_post($item->WP_Post);
|
||||||
|
$item->WP_Post = get_post($id);
|
||||||
|
|
||||||
// Now run through properties stored as postmeta
|
// Now run through properties stored as postmeta
|
||||||
foreach ($map as $prop => $mapped) {
|
foreach ($map as $prop => $mapped) {
|
||||||
if ($mapped == 'meta') {
|
if ($mapped == 'meta') {
|
||||||
update_post_meta($id, $prop, $item->get_mapped_property($prop));
|
update_post_meta($id, $prop, $item->get_mapped_property($prop));
|
||||||
|
} elseif ($mapped == '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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -9,7 +9,6 @@ if ( ! defined( 'ABSPATH' ) ) {
|
||||||
class Tainacan_Metadatas {
|
class Tainacan_Metadatas {
|
||||||
|
|
||||||
const POST_TYPE = 'tainacan-metadata';
|
const POST_TYPE = 'tainacan-metadata';
|
||||||
const DB_IDENTIFIER_META = '_db_identifier';
|
|
||||||
|
|
||||||
var $map = [
|
var $map = [
|
||||||
'ID' => 'ID',
|
'ID' => 'ID',
|
||||||
|
@ -24,7 +23,7 @@ class Tainacan_Metadatas {
|
||||||
'mask' => 'meta',
|
'mask' => 'meta',
|
||||||
'default_value' => 'meta',
|
'default_value' => 'meta',
|
||||||
'option' => 'meta',
|
'option' => 'meta',
|
||||||
'collection' => 'meta',
|
'collection_id' => 'meta',
|
||||||
];
|
];
|
||||||
|
|
||||||
function __construct() {
|
function __construct() {
|
||||||
|
@ -85,24 +84,21 @@ class Tainacan_Metadatas {
|
||||||
$metadata->WP_Post->post_type = self::POST_TYPE;
|
$metadata->WP_Post->post_type = self::POST_TYPE;
|
||||||
$metadata->WP_Post->post_status = 'publish';
|
$metadata->WP_Post->post_status = 'publish';
|
||||||
$id = wp_insert_post($metadata->WP_Post);
|
$id = wp_insert_post($metadata->WP_Post);
|
||||||
|
$metadata->WP_Post = get_post($id);
|
||||||
|
|
||||||
// Now run through properties stored as postmeta
|
// Now run through properties stored as postmeta
|
||||||
foreach ($map as $prop => $mapped) {
|
foreach ($map as $prop => $mapped) {
|
||||||
if ($mapped == 'meta') {
|
if ($mapped == 'meta') {
|
||||||
update_post_meta($id, $prop, $metadata->get_mapped_property($prop));
|
update_post_meta($id, $prop, $metadata->get_mapped_property($prop));
|
||||||
|
} elseif ($mapped == '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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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;
|
return $id;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -111,7 +107,7 @@ class Tainacan_Metadatas {
|
||||||
* @param array $args
|
* @param array $args
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
function get_collection_metadata( $collection, $args = array()) {
|
function get_metadata_by_collection( $collection, $args = array()) {
|
||||||
$collection_id = ( is_object( $collection ) ) ? $collection->get_id() : $collection;
|
$collection_id = ( is_object( $collection ) ) ? $collection->get_id() : $collection;
|
||||||
|
|
||||||
$args = array_merge([
|
$args = array_merge([
|
||||||
|
@ -133,17 +129,7 @@ class Tainacan_Metadatas {
|
||||||
return $return;
|
return $return;
|
||||||
}
|
}
|
||||||
|
|
||||||
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 int $id
|
* @param int $id
|
||||||
* @return Tainacan_Metadata
|
* @return Tainacan_Metadata
|
||||||
|
|
|
@ -9,16 +9,15 @@ if ( ! defined( 'ABSPATH' ) ) {
|
||||||
class Tainacan_Taxonomies {
|
class Tainacan_Taxonomies {
|
||||||
|
|
||||||
const POST_TYPE = 'tainacan-taxonomies';
|
const POST_TYPE = 'tainacan-taxonomies';
|
||||||
const DB_IDENTIFIER_META = '_db_identifier';
|
|
||||||
|
|
||||||
var $map = [
|
var $map = [
|
||||||
'ID' => 'ID',
|
'ID' => 'ID',
|
||||||
'name' => 'post_title',
|
'name' => 'post_title',
|
||||||
'parent' => 'parent',
|
'parent' => 'parent',
|
||||||
'description' => 'post_content',
|
'description' => 'post_content',
|
||||||
'taxonomy_name' => 'post_name',
|
'slug' => 'post_name',
|
||||||
'allow_insert' => 'meta',
|
'allow_insert' => 'meta',
|
||||||
'collection' => 'meta',
|
'collections_ids' => 'meta_multi',
|
||||||
];
|
];
|
||||||
|
|
||||||
|
|
||||||
|
@ -52,9 +51,9 @@ class Tainacan_Taxonomies {
|
||||||
'show_in_menu' => tnc_enable_dev_wp_interface(),
|
'show_in_menu' => tnc_enable_dev_wp_interface(),
|
||||||
//'menu_position' => 5,
|
//'menu_position' => 5,
|
||||||
//'show_in_nav_menus' => false,
|
//'show_in_nav_menus' => false,
|
||||||
'publicly_queryable' => true,
|
'publicly_queryable' => false,
|
||||||
'exclude_from_search' => true,
|
'exclude_from_search' => true,
|
||||||
'has_archive' => true,
|
'has_archive' => false,
|
||||||
'query_var' => true,
|
'query_var' => true,
|
||||||
'can_export' => true,
|
'can_export' => true,
|
||||||
'rewrite' => true,
|
'rewrite' => true,
|
||||||
|
@ -83,30 +82,6 @@ class Tainacan_Taxonomies {
|
||||||
return $return;
|
return $return;
|
||||||
}
|
}
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
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
|
||||||
|
@ -125,25 +100,23 @@ class Tainacan_Taxonomies {
|
||||||
$taxonomy->WP_Post->post_type = self::POST_TYPE;
|
$taxonomy->WP_Post->post_type = self::POST_TYPE;
|
||||||
$taxonomy->WP_Post->post_status = 'publish';
|
$taxonomy->WP_Post->post_status = 'publish';
|
||||||
$id = wp_insert_post($taxonomy->WP_Post);
|
$id = wp_insert_post($taxonomy->WP_Post);
|
||||||
|
$taxonomy->WP_Post = get_post($id);
|
||||||
|
|
||||||
// Now run through properties stored as postmeta
|
// Now run through properties stored as postmeta
|
||||||
foreach ($map as $prop => $mapped) {
|
foreach ($map as $prop => $mapped) {
|
||||||
if ($mapped == 'meta') {
|
if ($mapped == 'meta') {
|
||||||
update_post_meta($id, $prop, $taxonomy->get_mapped_property($prop));
|
update_post_meta($id, $prop, $taxonomy->get_mapped_property($prop));
|
||||||
|
} elseif ($mapped == '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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// DB Identifier
|
$taxonomy->register_taxonomy();
|
||||||
// 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);
|
|
||||||
$this->registerTainacanTaxonomy( $p->post_name );
|
|
||||||
}
|
|
||||||
|
|
||||||
return $id;
|
return $id;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,36 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
// used by Item, Event, Field
|
||||||
|
|
||||||
|
trait EntityCollectionRelation {
|
||||||
|
|
||||||
|
function get_collection_id() {
|
||||||
|
return $this->get_mapped_property('collection_id');
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
function get_collection() {
|
||||||
|
if (isset($this->collection) && $this->collection instanceof TainacanCollection)
|
||||||
|
return $this->collection;
|
||||||
|
|
||||||
|
if (is_numeric($this->get_collection_id())) {
|
||||||
|
global $TainacanCollections;
|
||||||
|
$this->collection = $TainacanCollections->get_collection_by_id($this->get_collection_id());
|
||||||
|
return $this->collection;
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
function set_collection_id($value) {
|
||||||
|
return $this->set_mapped_property('collection_id', $value);
|
||||||
|
$this->collection = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
function set_collection(TainacanCollection $collection) {
|
||||||
|
$this->collection = $collection;
|
||||||
|
$this->set_collection_id($collection->get_id());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,46 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
// used by Taxonomy
|
||||||
|
|
||||||
|
trait EntityCollectionsRelation {
|
||||||
|
|
||||||
|
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 $TainacanCollections;
|
||||||
|
$collections = [];
|
||||||
|
foreach ($this->get_collections_ids() as $col_id) {
|
||||||
|
$collections[] = $TainacanCollections->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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -7,6 +7,8 @@ Author: MediaLab UFG
|
||||||
Version: 10.9.8.7.6.5.4
|
Version: 10.9.8.7.6.5.4
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
include('classes/Traits/EntityCollectionRelation.php');
|
||||||
|
include('classes/Traits/EntityCollectionsRelation.php');
|
||||||
include('classes/Repositories/Collections.php');
|
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');
|
||||||
|
@ -21,6 +23,7 @@ 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');
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
*
|
*
|
||||||
|
|
|
@ -25,7 +25,7 @@ class TestMetadata extends WP_UnitTestCase {
|
||||||
//setando os valores na classe do metadado
|
//setando os valores na classe do metadado
|
||||||
$metadata->set_name('metadado');
|
$metadata->set_name('metadado');
|
||||||
$metadata->set_description('descricao');
|
$metadata->set_description('descricao');
|
||||||
$metadata->set_collection( new TainacanCollection( $id ) );
|
$metadata->set_collection_id( $id );
|
||||||
|
|
||||||
//inserindo o metadado
|
//inserindo o metadado
|
||||||
$metadata_id = $Tainacan_Metadatas->insert($metadata);
|
$metadata_id = $Tainacan_Metadatas->insert($metadata);
|
||||||
|
@ -53,7 +53,7 @@ class TestMetadata extends WP_UnitTestCase {
|
||||||
|
|
||||||
//setando os valores na classe do metadado
|
//setando os valores na classe do metadado
|
||||||
$metadata->set_name('metadado');
|
$metadata->set_name('metadado');
|
||||||
$metadata->set_collection( new TainacanCollection( $id ) );
|
$metadata->set_collection_id( $id );
|
||||||
$metadata->set_type( $type );
|
$metadata->set_type( $type );
|
||||||
|
|
||||||
//inserindo o metadado
|
//inserindo o metadado
|
||||||
|
|
|
@ -33,7 +33,7 @@ class TestTaxonomies extends WP_UnitTestCase {
|
||||||
$this->assertEquals( $test->get_name(), 'genero' );
|
$this->assertEquals( $test->get_name(), 'genero' );
|
||||||
$this->assertEquals( $test->get_description(), 'tipos de musica' );
|
$this->assertEquals( $test->get_description(), 'tipos de musica' );
|
||||||
$this->assertEquals( $test->get_allow_insert(), true );
|
$this->assertEquals( $test->get_allow_insert(), true );
|
||||||
$this->assertEquals( taxonomy_exists( $test->get_taxonomy_name() ) , true );
|
$this->assertEquals( taxonomy_exists( $test->get_db_identifier() ) , true );
|
||||||
}
|
}
|
||||||
|
|
||||||
function test_add_term_taxonomy(){
|
function test_add_term_taxonomy(){
|
||||||
|
@ -51,13 +51,13 @@ class TestTaxonomies extends WP_UnitTestCase {
|
||||||
$taxonomy_test = $Tainacan_Taxonomies->get_taxonomy_by_id($taxonomy_id);
|
$taxonomy_test = $Tainacan_Taxonomies->get_taxonomy_by_id($taxonomy_id);
|
||||||
|
|
||||||
//insere um termo na taxonomia
|
//insere um termo na taxonomia
|
||||||
$term->set_taxonomy( $taxonomy_test->get_taxonomy_name() );
|
$term->set_taxonomy( $taxonomy_test->get_db_identifier() );
|
||||||
$term->set_name('Rock');
|
$term->set_name('Rock');
|
||||||
$term->set_user(56);
|
$term->set_user(56);
|
||||||
$term_id = $Tainacan_Terms->insert( $term ) ;
|
$term_id = $Tainacan_Terms->insert( $term ) ;
|
||||||
|
|
||||||
//retorna um objeto da classe Tainacan_Term
|
//retorna um objeto da classe Tainacan_Term
|
||||||
$test = $Tainacan_Terms->get_term_by('id', $term_id, $taxonomy_test->get_taxonomy_name());
|
$test = $Tainacan_Terms->get_term_by('id', $term_id, $taxonomy_test->get_db_identifier());
|
||||||
|
|
||||||
$this->assertEquals( $test->get_name(), 'Rock' );
|
$this->assertEquals( $test->get_name(), 'Rock' );
|
||||||
$this->assertEquals( $test->get_user(), 56 );
|
$this->assertEquals( $test->get_user(), 56 );
|
||||||
|
|
Loading…
Reference in New Issue