disallow new terms on meta when tax changes #366

This commit is contained in:
leogermani 2020-02-20 14:57:18 -03:00
parent cb4d464768
commit 29977826d9
2 changed files with 104 additions and 5 deletions

View File

@ -41,6 +41,7 @@ class Metadata extends Repository {
add_filter( 'pre_delete_post', array( &$this, 'force_delete_core_metadata' ), 10, 3 ); add_filter( 'pre_delete_post', array( &$this, 'force_delete_core_metadata' ), 10, 3 );
add_action('tainacan-insert-tainacan-taxonomy', [$this, 'hook_taxonomies_saved_as_private']); add_action('tainacan-insert-tainacan-taxonomy', [$this, 'hook_taxonomies_saved_as_private']);
add_action('tainacan-insert-tainacan-taxonomy', [$this, 'hook_taxonomies_saved_not_allow_insert_new_terms']);
} }
@ -1081,7 +1082,7 @@ class Metadata extends Repository {
FROM FROM
$wpdb->terms t INNER JOIN $wpdb->term_taxonomy tt ON t.term_id = tt.term_id $wpdb->terms t INNER JOIN $wpdb->term_taxonomy tt ON t.term_id = tt.term_id
LEFT JOIN ( LEFT JOIN (
SELECT DISTINCT term_taxonomy_id FROM $wpdb->term_relationships SELECT DISTINCT term_taxonomy_id FROM $wpdb->term_relationships
INNER JOIN ($items_query) as posts ON $wpdb->term_relationships.object_id = posts.ID INNER JOIN ($items_query) as posts ON $wpdb->term_relationships.object_id = posts.ID
) as tr ON tt.term_taxonomy_id = tr.term_taxonomy_id ) as tr ON tt.term_taxonomy_id = tr.term_taxonomy_id
WHERE tt.taxonomy = %s ORDER BY t.name ASC", $taxonomy_slug WHERE tt.taxonomy = %s ORDER BY t.name ASC", $taxonomy_slug
@ -1496,13 +1497,13 @@ class Metadata extends Repository {
$taxonomy_id = $taxonomy->get_id(); $taxonomy_id = $taxonomy->get_id();
$args = [ $args = [
'metadata_type' => 'Tainacan\Metadata_Types\Taxonomy', 'metadata_type' => 'Tainacan\Metadata_Types\Taxonomy',
'meta_query' => [ 'meta_query' => [
[ [
'key' => '_option_taxonomy_id', 'key' => '_option_taxonomy_id',
'value' => $taxonomy_id 'value' => $taxonomy_id
] ]
], ],
'post_status' => $stati 'post_status' => $stati
]; ];
$metadata = $this->fetch($args, 'OBJECT'); $metadata = $this->fetch($args, 'OBJECT');
@ -1521,4 +1522,51 @@ class Metadata extends Repository {
} }
} }
/**
* When a taxonomy is saved disabling allow_insert. All related metadata should set allow_new_terms to 'no'
*
* @param \Tainacan\Entities\Taxonomy $taxonomy
* @return void
*/
public function hook_taxonomies_saved_not_allow_insert_new_terms($taxonomy) {
if ( $taxonomy instanceof Entities\Taxonomy && 'no' === $taxonomy->get_allow_insert() ) {
$taxonomy_id = $taxonomy->get_id();
$args = [
'metadata_type' => 'Tainacan\Metadata_Types\Taxonomy',
'meta_query' => [
[
'key' => '_option_taxonomy_id',
'value' => $taxonomy_id
],
[
'key' => '_option_allow_new_terms',
'value' => 'yes'
]
],
'post_status' => 'any',
];
$metadata = $this->fetch($args);
var_dump($metadata->request);
$metadata = $this->fetch($args, 'OBJECT');
foreach ($metadata as $meta) {
$options = $meta->get_metadata_type_options();
$options['allow_new_terms'] = 'no';
$meta->set_metadata_type_options( $options );
if ( $meta->validate() ) {
$this->insert($meta);
}
}
}
}
} }

View File

@ -746,6 +746,57 @@ class TaxonomyMetadatumTypes extends TAINACAN_UnitTestCase {
$this->assertTrue( $taxCheck instanceof \Tainacan\Entities\Taxonomy ); $this->assertTrue( $taxCheck instanceof \Tainacan\Entities\Taxonomy );
$this->assertEquals($tax->get_id(), $taxCheck->get_id()); $this->assertEquals($tax->get_id(), $taxCheck->get_id());
} }
function test_set_metadata_to_not_allow_new_terms_after_taxonomy_is_set_to_it() {
$Tainacan_Metadata = \Tainacan\Repositories\Metadata::get_instance();
$Tainacan_Taxonomies = \Tainacan\Repositories\Taxonomies::get_instance();
$Tainacan_ItemMetadata = \Tainacan\Repositories\Item_Metadata::get_instance();
$collection = $this->tainacan_entity_factory->create_entity(
'collection',
array(
'name' => 'test',
'status' => 'publish'
),
true
);
$tax = $this->tainacan_entity_factory->create_entity(
'taxonomy',
array(
'name' => 'tax_test',
'status' => 'publish'
),
true
);
$metadatum = $this->tainacan_entity_factory->create_entity(
'metadatum',
array(
'name' => 'meta',
'description' => 'description',
'collection' => $collection,
'metadata_type' => 'Tainacan\Metadata_Types\Taxonomy',
'status' => 'publish',
'metadata_type_options' => [
'taxonomy_id' => $tax->get_id(),
'allow_new_terms' => 'yes'
]
),
true
);
$tax->set_allow_insert('no');
$tax->validate();
$Tainacan_Taxonomies->insert($tax);
$checkMeta = $Tainacan_Metadata->fetch( $metadatum->get_id() );
$this->assertEquals('no', $checkMeta->get_metadata_type_options()['allow_new_terms']);
}
} }