Make WP update terms count for all items #318
This commit is contained in:
parent
678f69f26a
commit
56b9e76aa1
|
@ -22,28 +22,28 @@ class Taxonomy extends Entity {
|
|||
* @var string
|
||||
*/
|
||||
static $post_type = 'tainacan-taxonomy';
|
||||
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
* @see \Tainacan\Entities\Entity::capability_type
|
||||
* @var string
|
||||
*/
|
||||
protected static $capability_type = ['tainacan-taxonomy', 'tainacan-taxonomies'];
|
||||
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
* @see \Tainacan\Entities\Entity::repository
|
||||
* @var string
|
||||
*/
|
||||
protected $repository = 'Taxonomies';
|
||||
|
||||
|
||||
/**
|
||||
* Prefix used to create the db_identifier
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
static $db_identifier_prefix = 'tnc_tax_';
|
||||
|
||||
|
||||
public function __toString(){
|
||||
return apply_filters("tainacan-taxonomy-to-string", $this->get_name(), $this);
|
||||
}
|
||||
|
@ -67,7 +67,7 @@ class Taxonomy extends Entity {
|
|||
'new_item_name' => __( 'New Genre term', 'tainacan' ),
|
||||
'menu_name' => $this->get_name(),
|
||||
);
|
||||
|
||||
|
||||
$enabled_post_types = $this->get_enabled_post_types();
|
||||
$enabled_post_types = sizeof($enabled_post_types) ? $enabled_post_types : null;
|
||||
$show_ui = is_array($enabled_post_types) ? true : false;
|
||||
|
@ -80,21 +80,22 @@ class Taxonomy extends Entity {
|
|||
'show_admin_column' => false,
|
||||
'rewrite' => [
|
||||
'slug' => $this->get_slug()
|
||||
],
|
||||
],
|
||||
'update_count_callback' => '_update_generic_term_count'
|
||||
);
|
||||
|
||||
|
||||
if (taxonomy_exists($this->get_db_identifier())){
|
||||
unregister_taxonomy($this->get_db_identifier());
|
||||
}
|
||||
|
||||
|
||||
|
||||
register_taxonomy(
|
||||
$this->get_db_identifier(),
|
||||
$enabled_post_types,
|
||||
$args
|
||||
|
||||
|
||||
|
||||
register_taxonomy(
|
||||
$this->get_db_identifier(),
|
||||
$enabled_post_types,
|
||||
$args
|
||||
);
|
||||
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -135,7 +136,7 @@ class Taxonomy extends Entity {
|
|||
function get_slug() {
|
||||
return $this->get_mapped_property('slug');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Return the enabled post types
|
||||
*
|
||||
|
@ -144,7 +145,7 @@ class Taxonomy extends Entity {
|
|||
function get_enabled_post_types() {
|
||||
return $this->get_mapped_property('enabled_post_types');
|
||||
}
|
||||
|
||||
|
||||
// special Getters
|
||||
|
||||
/**
|
||||
|
@ -194,7 +195,7 @@ class Taxonomy extends Entity {
|
|||
function set_allow_insert($value) {
|
||||
$this->set_mapped_property('allow_insert', $value);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Sets enabled post types
|
||||
*
|
||||
|
@ -221,19 +222,19 @@ class Taxonomy extends Entity {
|
|||
return parent::validate();
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Check if a term already exists
|
||||
* Check if a term already exists
|
||||
*
|
||||
* @param string $term_name The term name
|
||||
* @param int|null $parent The ID of the parent term to look for children or null to look for terms in any hierarchical position. Default is null
|
||||
* @param bool $return_term wether to return the term object if it exists. default is to false
|
||||
*
|
||||
* @return bool|WP_Term return boolean indicating if term exists. If $return_term is true and term exists, return WP_Term object
|
||||
* @param string $term_name The term name
|
||||
* @param int|null $parent The ID of the parent term to look for children or null to look for terms in any hierarchical position. Default is null
|
||||
* @param bool $return_term wether to return the term object if it exists. default is to false
|
||||
*
|
||||
* @return bool|WP_Term return boolean indicating if term exists. If $return_term is true and term exists, return WP_Term object
|
||||
*/
|
||||
function term_exists($term_name, $parent = null, $return_term = false) {
|
||||
$repo = $this->get_repository();
|
||||
return $repo->term_exists($this, $term_name, $parent, $return_term);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -69,9 +69,9 @@ class Taxonomies extends TAINACAN_UnitTestCase {
|
|||
$this->assertEquals( $test->get_name(), 'Rock' );
|
||||
$this->assertEquals( $test->get_user(), 56 );
|
||||
}
|
||||
|
||||
|
||||
function test_terms_of_draft_taxonomy() {
|
||||
|
||||
|
||||
$taxonomy = $this->tainacan_entity_factory->create_entity(
|
||||
'taxonomy',
|
||||
array(
|
||||
|
@ -82,14 +82,14 @@ class Taxonomies extends TAINACAN_UnitTestCase {
|
|||
),
|
||||
true
|
||||
);
|
||||
|
||||
|
||||
$Tainacan_Taxonomies = \Tainacan\Repositories\Taxonomies::get_instance();
|
||||
$Tainacan_Terms = \Tainacan\Repositories\Terms::get_instance();
|
||||
|
||||
$terms = $Tainacan_Terms->fetch(['hide_empty' => false], $taxonomy->get_id());
|
||||
|
||||
|
||||
$this->assertEquals(0, sizeof($terms), 'new auto draft taxonomy should return 0 terms');
|
||||
|
||||
|
||||
$term = $this->tainacan_entity_factory->create_entity(
|
||||
'term',
|
||||
array(
|
||||
|
@ -98,17 +98,17 @@ class Taxonomies extends TAINACAN_UnitTestCase {
|
|||
),
|
||||
true
|
||||
);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
$terms = $Tainacan_Terms->fetch(['hide_empty' => false], $taxonomy->get_id());
|
||||
|
||||
|
||||
$this->assertEquals(1, sizeof($terms), 'you should be able to create a term even if the taxonomy is still auto-draft');
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
function test_term_exists() {
|
||||
|
||||
|
||||
$taxonomy = $this->tainacan_entity_factory->create_entity(
|
||||
'taxonomy',
|
||||
array(
|
||||
|
@ -119,10 +119,10 @@ class Taxonomies extends TAINACAN_UnitTestCase {
|
|||
),
|
||||
true
|
||||
);
|
||||
|
||||
|
||||
$Tainacan_Taxonomies = \Tainacan\Repositories\Taxonomies::get_instance();
|
||||
$Tainacan_Terms = \Tainacan\Repositories\Terms::get_instance();
|
||||
|
||||
|
||||
$term = $this->tainacan_entity_factory->create_entity(
|
||||
'term',
|
||||
array(
|
||||
|
@ -131,7 +131,7 @@ class Taxonomies extends TAINACAN_UnitTestCase {
|
|||
),
|
||||
true
|
||||
);
|
||||
|
||||
|
||||
$parent = $this->tainacan_entity_factory->create_entity(
|
||||
'term',
|
||||
array(
|
||||
|
@ -140,7 +140,7 @@ class Taxonomies extends TAINACAN_UnitTestCase {
|
|||
),
|
||||
true
|
||||
);
|
||||
|
||||
|
||||
$child = $this->tainacan_entity_factory->create_entity(
|
||||
'term',
|
||||
array(
|
||||
|
@ -150,14 +150,14 @@ class Taxonomies extends TAINACAN_UnitTestCase {
|
|||
),
|
||||
true
|
||||
);
|
||||
|
||||
|
||||
$this->assertFalse( $Tainacan_Terms->term_exists('Reggae', $taxonomy->get_db_identifier()) );
|
||||
$this->assertTrue( $Tainacan_Terms->term_exists('Rock', $taxonomy->get_db_identifier()) );
|
||||
|
||||
|
||||
//var_dump( $Tainacan_Terms->term_exists('Rock', $taxonomy->get_db_identifier(), 0, true) );
|
||||
|
||||
// test extreme case
|
||||
|
||||
|
||||
// test extreme case
|
||||
|
||||
$term_2 = $this->tainacan_entity_factory->create_entity(
|
||||
'term',
|
||||
array(
|
||||
|
@ -167,49 +167,49 @@ class Taxonomies extends TAINACAN_UnitTestCase {
|
|||
),
|
||||
true
|
||||
);
|
||||
|
||||
|
||||
$this->assertFalse( $Tainacan_Terms->term_exists('test 123', $taxonomy->get_db_identifier(), 0) ); // parent 0
|
||||
$this->assertTrue( $Tainacan_Terms->term_exists('test 123', $taxonomy->get_db_identifier(), $term->get_id()) ); // spaces in between
|
||||
|
||||
// testing passing taxonomy object
|
||||
|
||||
// testing passing taxonomy object
|
||||
$this->assertTrue( $Tainacan_Terms->term_exists('Rock', $taxonomy) );
|
||||
|
||||
|
||||
// testing passing taxonomy ID
|
||||
$this->assertTrue( $Tainacan_Terms->term_exists('Rock', $taxonomy->get_id()) );
|
||||
|
||||
|
||||
// testing via Taxonomy object
|
||||
$this->assertTrue( $taxonomy->term_exists('Rock') );
|
||||
|
||||
// testing retrieving the term
|
||||
|
||||
// testing retrieving the term
|
||||
$this->assertTrue( $taxonomy->term_exists('Rock', 0, true) instanceof \WP_Term );
|
||||
$this->assertEquals( $term->get_id(), $taxonomy->term_exists('Rock', 0, true)->term_taxonomy_id );
|
||||
|
||||
// test parent
|
||||
|
||||
// test parent
|
||||
$this->assertTrue( $Tainacan_Terms->term_exists('Child', $taxonomy->get_db_identifier()) ); // parent null
|
||||
$this->assertFalse( $Tainacan_Terms->term_exists('Child', $taxonomy->get_db_identifier(), 0) ); // parent 0
|
||||
$this->assertTrue( $Tainacan_Terms->term_exists('Child', $taxonomy->get_db_identifier(), $parent->get_id()) ); // parent
|
||||
|
||||
// test with ID
|
||||
$this->assertTrue( $Tainacan_Terms->term_exists('Child', $taxonomy->get_db_identifier(), $parent->get_id()) ); // parent
|
||||
|
||||
// test with ID
|
||||
$this->assertTrue( $Tainacan_Terms->term_exists($term->get_id(), $taxonomy->get_id()) );
|
||||
|
||||
// test get term
|
||||
|
||||
// test get term
|
||||
$test_term = $Tainacan_Terms->term_exists($term->get_id(), $taxonomy->get_id(), null, true);
|
||||
$this->assertEquals($term->get_id(), $test_term->term_id);
|
||||
|
||||
|
||||
$test_term = $Tainacan_Terms->term_exists('Rock', $taxonomy->get_id(), null, true);
|
||||
$this->assertEquals($term->get_id(), $test_term->term_id);
|
||||
|
||||
|
||||
$test_term = $Tainacan_Terms->term_exists('Parent', $taxonomy->get_id(), null, true);
|
||||
$this->assertEquals($parent->get_id(), $test_term->term_id);
|
||||
|
||||
|
||||
// test brackets
|
||||
$test_term = $Tainacan_Terms->term_exists('[Rock]', $taxonomy->get_id(), null, true);
|
||||
$this->assertFalse($test_term);
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
function test_term_validation() {
|
||||
|
||||
|
||||
$taxonomy = $this->tainacan_entity_factory->create_entity(
|
||||
'taxonomy',
|
||||
array(
|
||||
|
@ -220,10 +220,10 @@ class Taxonomies extends TAINACAN_UnitTestCase {
|
|||
),
|
||||
true
|
||||
);
|
||||
|
||||
|
||||
$Tainacan_Taxonomies = \Tainacan\Repositories\Taxonomies::get_instance();
|
||||
$Tainacan_Terms = \Tainacan\Repositories\Terms::get_instance();
|
||||
|
||||
|
||||
$term = $this->tainacan_entity_factory->create_entity(
|
||||
'term',
|
||||
array(
|
||||
|
@ -232,7 +232,7 @@ class Taxonomies extends TAINACAN_UnitTestCase {
|
|||
),
|
||||
true
|
||||
);
|
||||
|
||||
|
||||
$parent = $this->tainacan_entity_factory->create_entity(
|
||||
'term',
|
||||
array(
|
||||
|
@ -241,7 +241,7 @@ class Taxonomies extends TAINACAN_UnitTestCase {
|
|||
),
|
||||
true
|
||||
);
|
||||
|
||||
|
||||
$child = $this->tainacan_entity_factory->create_entity(
|
||||
'term',
|
||||
array(
|
||||
|
@ -251,27 +251,27 @@ class Taxonomies extends TAINACAN_UnitTestCase {
|
|||
),
|
||||
true
|
||||
);
|
||||
|
||||
|
||||
$newTerm = new \Tainacan\Entities\Term();
|
||||
$newTerm->set_name('Child');
|
||||
$newTerm->set_taxonomy($taxonomy->get_db_identifier());
|
||||
|
||||
|
||||
$this->assertTrue( $newTerm->validate() );
|
||||
|
||||
|
||||
$newTerm->set_parent($parent->get_id());
|
||||
|
||||
|
||||
$this->assertFalse( $newTerm->validate(), 'term should not validate because it has a duplicate in the same level' );
|
||||
|
||||
|
||||
$child->set_description('changed');
|
||||
|
||||
|
||||
$this->assertTrue( $child->validate(), 'child should validate');
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @group enabled
|
||||
* @group enabled
|
||||
*/
|
||||
function test_enabled_post_types(){
|
||||
$Tainacan_Taxonomies = \Tainacan\Repositories\Taxonomies::get_instance();
|
||||
|
@ -287,15 +287,15 @@ class Taxonomies extends TAINACAN_UnitTestCase {
|
|||
);
|
||||
|
||||
$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);
|
||||
}
|
||||
|
||||
|
||||
function test_brackets() {
|
||||
|
||||
|
||||
$taxonomy = $this->tainacan_entity_factory->create_entity(
|
||||
'taxonomy',
|
||||
array(
|
||||
|
@ -306,10 +306,10 @@ class Taxonomies extends TAINACAN_UnitTestCase {
|
|||
),
|
||||
true
|
||||
);
|
||||
|
||||
|
||||
$Tainacan_Taxonomies = \Tainacan\Repositories\Taxonomies::get_instance();
|
||||
$Tainacan_Terms = \Tainacan\Repositories\Terms::get_instance();
|
||||
|
||||
|
||||
$term = $this->tainacan_entity_factory->create_entity(
|
||||
'term',
|
||||
array(
|
||||
|
@ -318,7 +318,7 @@ class Taxonomies extends TAINACAN_UnitTestCase {
|
|||
),
|
||||
true
|
||||
);
|
||||
|
||||
|
||||
$term2 = $this->tainacan_entity_factory->create_entity(
|
||||
'term',
|
||||
array(
|
||||
|
@ -327,16 +327,16 @@ class Taxonomies extends TAINACAN_UnitTestCase {
|
|||
),
|
||||
true
|
||||
);
|
||||
|
||||
|
||||
$terms = $Tainacan_Terms->fetch(['hide_empty' => false], $taxonomy);
|
||||
$this->assertEquals(2, sizeof($terms));
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
function test_brackets_2() {
|
||||
|
||||
|
||||
$Tainacan_Item_Metadata = \Tainacan\Repositories\Item_Metadata::get_instance();
|
||||
|
||||
|
||||
$collection = $this->tainacan_entity_factory->create_entity(
|
||||
'collection',
|
||||
array(
|
||||
|
@ -345,7 +345,7 @@ class Taxonomies extends TAINACAN_UnitTestCase {
|
|||
),
|
||||
true
|
||||
);
|
||||
|
||||
|
||||
$taxonomy = $this->tainacan_entity_factory->create_entity(
|
||||
'taxonomy',
|
||||
array(
|
||||
|
@ -381,7 +381,7 @@ class Taxonomies extends TAINACAN_UnitTestCase {
|
|||
),
|
||||
true
|
||||
);
|
||||
|
||||
|
||||
$i2 = $this->tainacan_entity_factory->create_entity(
|
||||
'item',
|
||||
array(
|
||||
|
@ -391,44 +391,44 @@ class Taxonomies extends TAINACAN_UnitTestCase {
|
|||
),
|
||||
true
|
||||
);
|
||||
|
||||
|
||||
$itemMeta1 = new \Tainacan\Entities\Item_Metadata_Entity($i1, $metadatum);
|
||||
$itemMeta1->set_value('Rock');
|
||||
$itemMeta1->validate();
|
||||
$Tainacan_Item_Metadata->insert($itemMeta1);
|
||||
|
||||
|
||||
//$this->assertNotFalse(term_exists( 'Rock', $taxonomy->get_db_identifier() ));
|
||||
// term_exists() is not to be trusted
|
||||
//$this->assertFalse(term_exists( '[Rock]', $taxonomy->get_db_identifier() ));
|
||||
|
||||
|
||||
$itemMeta2 = new \Tainacan\Entities\Item_Metadata_Entity($i2, $metadatum);
|
||||
$itemMeta2->set_value('[Rock]');
|
||||
$itemMeta2->validate();
|
||||
$Tainacan_Item_Metadata->insert($itemMeta2);
|
||||
|
||||
|
||||
$itemMeta1_check = new \Tainacan\Entities\Item_Metadata_Entity($i1, $metadatum);
|
||||
$this->assertEquals('Rock', $itemMeta1_check->get_value()->get_name());
|
||||
|
||||
|
||||
$itemMeta2_check = new \Tainacan\Entities\Item_Metadata_Entity($i2, $metadatum);
|
||||
$this->assertEquals('[Rock]', $itemMeta2_check->get_value()->get_name());
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
function test_metadata_taxonomy_term_count() {
|
||||
$Tainacan_Metadata = \Tainacan\Repositories\Metadata::get_instance();
|
||||
$Tainacan_Item_Metadata = \Tainacan\Repositories\Item_Metadata::get_instance();
|
||||
|
||||
$collection_1 = $this->tainacan_entity_factory->create_entity(
|
||||
'collection',
|
||||
array( 'name' => 'test-1' ),
|
||||
array( 'name' => 'test-1', 'status' => 'publish', ),
|
||||
true
|
||||
);
|
||||
|
||||
$collection_2 = $this->tainacan_entity_factory->create_entity(
|
||||
'collection',
|
||||
array( 'name' => 'test-2' ),
|
||||
array( 'name' => 'test-2', 'status' => 'publish', ),
|
||||
true
|
||||
);
|
||||
|
||||
|
@ -469,7 +469,7 @@ class Taxonomies extends TAINACAN_UnitTestCase {
|
|||
),
|
||||
true
|
||||
);
|
||||
|
||||
|
||||
$metadatum_1 = $this->tainacan_entity_factory->create_entity(
|
||||
'metadatum',
|
||||
array(
|
||||
|
@ -523,7 +523,8 @@ class Taxonomies extends TAINACAN_UnitTestCase {
|
|||
array(
|
||||
'title' => 'item teste',
|
||||
'description' => 'adasdasdsa',
|
||||
'collection' => $collection_1
|
||||
'collection' => $collection_1,
|
||||
'status' => 'publish'
|
||||
),
|
||||
true
|
||||
);
|
||||
|
@ -531,7 +532,7 @@ class Taxonomies extends TAINACAN_UnitTestCase {
|
|||
$itemMeta1->set_value('term');
|
||||
$itemMeta1->validate();
|
||||
$Tainacan_Item_Metadata->insert($itemMeta1);
|
||||
|
||||
|
||||
$itemMeta1_repo = new \Tainacan\Entities\Item_Metadata_Entity($i1, $metadatum_repository);
|
||||
$itemMeta1_repo->set_value('term_repository');
|
||||
$itemMeta1_repo->validate();
|
||||
|
@ -543,7 +544,8 @@ class Taxonomies extends TAINACAN_UnitTestCase {
|
|||
array(
|
||||
'title' => 'item teste',
|
||||
'description' => 'adasdasdsa',
|
||||
'collection' => $collection_2
|
||||
'collection' => $collection_2,
|
||||
'status' => 'private'
|
||||
),
|
||||
true
|
||||
);
|
||||
|
@ -564,12 +566,12 @@ class Taxonomies extends TAINACAN_UnitTestCase {
|
|||
$this->assertEquals(1, count($terms));
|
||||
|
||||
wp_update_term_count($t1->get_term_id(), $tax->get_db_identifier());
|
||||
wp_update_term_count($t2->get_term_id(), $tax_repository->get_db_identifier());
|
||||
wp_update_term_count($t2->get_term_id(), $tax_repository->get_db_identifier());
|
||||
|
||||
$term = get_term($t1->get_term_id());
|
||||
$term_repo = get_term($t2->get_term_id());
|
||||
$tax_used = get_object_taxonomies( [$collection_1->get_db_identifier(), $collection_2->get_db_identifier()]);
|
||||
|
||||
|
||||
$tax = get_taxonomy($tax->get_db_identifier());
|
||||
$tax_repository = get_taxonomy($tax_repository->get_db_identifier());
|
||||
|
||||
|
@ -583,4 +585,4 @@ class Taxonomies extends TAINACAN_UnitTestCase {
|
|||
$this->assertEquals(2, $term_repo->count);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue