Merge branch 'feature/taxonomy-enable-posttypes' into develop

This commit is contained in:
leogermani 2019-01-21 15:25:03 -02:00
commit 244f933bcb
6 changed files with 109 additions and 8 deletions

View File

@ -213,6 +213,20 @@ class Admin {
$settings['form_hooks'] = Admin_Hooks::get_instance()->get_registered_hooks();
$wp_post_types = get_post_types(['show_ui' => true], 'objects');
if (isset($wp_post_types['attachment'])) {
unset($wp_post_types['attachment']);
}
$wp_post_types = array_map(function($i) {
return [
'slug' => $i->name,
'label' => $i->label
];
}, $wp_post_types);
$settings['wp_post_types'] = $wp_post_types;
return $settings;
}

View File

@ -112,6 +112,31 @@
:message="$i18n.getHelperMessage('taxonomies', 'allow_insert')"/>
</label>
</b-field>
<!-- Activate for other post types -->
<b-field
:addons="false"
:label="$i18n.getHelperTitle('taxonomies', 'enabled_post_types')"
:type="editFormErrors['enabled_post_types'] != undefined ? 'is-danger' : ''"
:message="editFormErrors['enabled_post_types'] != undefined ? editFormErrors['enabled_post_types'] : ''">
<help-button
:title="$i18n.getHelperTitle('taxonomies', 'enabled_post_types')"
:message="$i18n.getHelperMessage('taxonomies', 'enabled_post_types')"/>
<div
v-for="wpPostType in wpPostTypes"
:key="wpPostType.slug"
class="field">
<b-checkbox
:native-value="wpPostType.slug"
:true-value="wpPostType.slug"
false-value=""
v-model="form.enabledPostTypes"
name="enabled_post_types" >
{{ wpPostType.label }}
</b-checkbox>
</div>
</b-field>
<!-- Hook for extra Form options -->
<template
@ -180,7 +205,8 @@
status: String,
description: String,
slug: String,
allowInsert: String
allowInsert: String,
enabledPostTypes: Array
},
statusOptions: [{
value: 'publish',
@ -195,6 +221,7 @@
value: 'trash',
label: this.$i18n.get('trash')
}],
wpPostTypes: tainacan_plugin.wp_post_types,
editFormErrors: {},
formErrorMessage: '',
entityName: 'taxonomy'
@ -216,6 +243,8 @@
formNotSaved = true;
if (this.taxonomy.status != this.form.status)
formNotSaved = true;
if (this.taxonomy.enabled_post_types != this.form.enabledPostTypes)
formNotSaved = true;
if (formNotSaved) {
this.$modal.open({
@ -275,7 +304,8 @@
description: this.form.description,
slug: this.form.slug ? this.form.slug : '',
status: this.form.status,
allow_insert: this.form.allowInsert
allow_insert: this.form.allowInsert,
enabled_post_types: this.form.enabledPostTypes
};
this.fillExtraFormData(data);
this.updateTaxonomy(data)
@ -292,6 +322,7 @@
this.form.description = this.taxonomy.description;
this.form.status = this.taxonomy.status;
this.form.allowInsert = this.taxonomy.allow_insert;
this.form.enabledPostTypes = this.taxonomy.enabled_post_types;
this.isLoadingTaxonomy = false;
this.formErrorMessage = '';
@ -408,6 +439,7 @@
this.form.slug = this.taxonomy.slug;
this.form.status = this.taxonomy.status;
this.form.allowInsert = this.taxonomy.allow_insert;
this.form.enabledPostTypes = this.taxonomy.enabled_post_types;
this.isLoadingTaxonomy = false;
});
@ -417,4 +449,3 @@
</script>
<style>

View File

@ -71,8 +71,8 @@ class Taxonomy extends Entity {
$args = array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => tnc_enable_dev_wp_interface(),
'show_admin_column' => tnc_enable_dev_wp_interface(),
'show_ui' => true,
'show_admin_column' => false,
'rewrite' => [
'slug' => $this->get_slug()
],
@ -82,9 +82,12 @@ class Taxonomy extends Entity {
unregister_taxonomy($this->get_db_identifier());
}
$enabled_post_types = $this->get_enabled_post_types();
$enabled_post_types = sizeof($enabled_post_types) ? $enabled_post_types : null;
register_taxonomy(
$this->get_db_identifier(),
null,
$enabled_post_types,
$args
);
@ -129,6 +132,15 @@ class Taxonomy extends Entity {
return $this->get_mapped_property('slug');
}
/**
* Return the enabled post types
*
* @return array
*/
function get_enabled_post_types() {
return $this->get_mapped_property('enabled_post_types');
}
// special Getters
/**
@ -178,6 +190,15 @@ class Taxonomy extends Entity {
function set_allow_insert($value) {
$this->set_mapped_property('allow_insert', $value);
}
/**
* Sets enabled post types
*
* @param array $value array of post types slugs
*/
function set_enabled_post_types($value) {
$this->set_mapped_property('enabled_post_types', $value);
}
/**
* Validate Taxonomy

View File

@ -65,6 +65,15 @@ class Taxonomies extends Repository {
'validation' => v::stringType()->in( [ 'yes', 'no' ] ), // yes or no
'default' => 'yes'
],
'enabled_post_types' => [
'map' => 'meta_multi',
'title' => __( 'Enabled for post types', 'tainacan' ),
'type' => 'array/string',
'description' => __( 'Also enable this taxonomy for other WordPress post types', 'tainacan' ),
'on_error' => __( 'Error enabling this taxonomy for post types', 'tainacan' ),
'validation' => '',
'default' => []
],
'collections_ids' => [
'map' => 'meta_multi',
'title' => __( 'Collections', 'tainacan' ),

View File

@ -37,6 +37,8 @@ class Entity_Factory {
$Tainacan_Item_Metadata = \Tainacan\Repositories\Item_Metadata::get_instance();
try {
$type = trim($type);
if(empty($type)){
throw new \InvalidArgumentException('The type can\'t be empty');
} elseif(!strrchr($type, '_')){
@ -68,7 +70,7 @@ class Entity_Factory {
if (!empty($args) && $is_validated_and_in_db) {
foreach ($args as $attribute => $content) {
$set_ = 'set_' . $attribute;
$set_ = 'set_' . trim($attribute);
$this->entity->$set_( $content );
}
@ -80,7 +82,7 @@ class Entity_Factory {
} elseif (!empty($args) && !$is_validated_and_in_db){
foreach ($args as $attribute => $content) {
$set_ = 'set_' . $attribute;
$set_ = 'set_' . trim($attribute);
$this->entity->$set_( $content );
}

View File

@ -252,4 +252,28 @@ class Taxonomies extends TAINACAN_UnitTestCase {
}
/**
* @group enabled
*/
function test_enabled_post_types(){
$Tainacan_Taxonomies = \Tainacan\Repositories\Taxonomies::get_instance();
$Tainacan_Terms = \Tainacan\Repositories\Terms::get_instance();
$taxonomy = $this->tainacan_entity_factory->create_entity(
'taxonomy',
array(
'name' => 'genero',
'enabled_post_types' => ['post']
),
true
);
$taxonomy = $Tainacan_Taxonomies->insert($taxonomy);
$pto = get_object_taxonomies('post');
$pages = get_object_taxonomies('page');
$this->assertContains($taxonomy->get_db_identifier(), $pto);
$this->assertNotContains($taxonomy->get_db_identifier(), $pages);
}
}