Work in progress enable post types for taxonomies. tests failing

This commit is contained in:
leogermani 2019-01-18 19:49:59 -02:00
parent 073ae100e5
commit c355f8ad95
5 changed files with 103 additions and 4 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,29 @@
:message="$i18n.getHelperMessage('taxonomies', 'allow_insert')"/>
</label>
</b-field>
<!-- Activate for other post types -->
<b-field
:addons="false"
:label="$i18n.getHelperTitle('taxonomies', 'post_types_enabled')"
:type="editFormErrors['post_types_enabled'] != undefined ? 'is-danger' : ''"
:message="editFormErrors['post_types_enabled'] != undefined ? editFormErrors['post_types_enabled'] : ''">
<b-checkbox
v-for="wpPostType in wpPostTypes"
:key="wpPostType.slug"
native-value="wpPostType.slug"
true-value="wpPostType.slug"
false-value=""
v-model="form.enabledPostTypes"
name="enabled_post_types" >
{{ wpPostType.label }}
</b-checkbox>
<help-button
:title="$i18n.getHelperTitle('taxonomies', 'post_types_enabled')"
:message="$i18n.getHelperMessage('taxonomies', 'post_types_enabled')"/>
</b-field>
<!-- Hook for extra Form options -->
<template
@ -180,7 +203,8 @@
status: String,
description: String,
slug: String,
allowInsert: String
allowInsert: String,
enabledPostTypes: Array
},
statusOptions: [{
value: 'publish',
@ -195,6 +219,7 @@
value: 'trash',
label: this.$i18n.get('trash')
}],
wpPostTypes: tainacan_plugin.wp_post_types,
editFormErrors: {},
formErrorMessage: '',
entityName: 'taxonomy'
@ -216,6 +241,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 +302,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 +320,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 +437,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 +447,3 @@
</script>
<style>

View File

@ -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' => '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

@ -252,4 +252,30 @@ 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->set('enabled_post_types', ['post']);
$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);
}
}