Allow resource_type to be specified in get_ancestors()
.
Being explicit about resource type (taxonomy vs post_type) allows for the proper resolution of conflicts when a taxonomy and post_type share a slug. Props filosofo. Fixes #15029. git-svn-id: https://develop.svn.wordpress.org/trunk@30141 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
8dbaaf9927
commit
d6b7b7aa41
@ -788,7 +788,7 @@ function wp_ajax_add_tag() {
|
|||||||
|
|
||||||
$level = 0;
|
$level = 0;
|
||||||
if ( is_taxonomy_hierarchical($taxonomy) ) {
|
if ( is_taxonomy_hierarchical($taxonomy) ) {
|
||||||
$level = count( get_ancestors( $tag->term_id, $taxonomy ) );
|
$level = count( get_ancestors( $tag->term_id, $taxonomy, 'taxonomy' ) );
|
||||||
ob_start();
|
ob_start();
|
||||||
$wp_list_table->single_row( $tag, $level );
|
$wp_list_table->single_row( $tag, $level );
|
||||||
$noparents = ob_get_clean();
|
$noparents = ob_get_clean();
|
||||||
|
@ -4054,7 +4054,7 @@ function get_term_link( $term, $taxonomy = '') {
|
|||||||
} else {
|
} else {
|
||||||
if ( $t->rewrite['hierarchical'] ) {
|
if ( $t->rewrite['hierarchical'] ) {
|
||||||
$hierarchical_slugs = array();
|
$hierarchical_slugs = array();
|
||||||
$ancestors = get_ancestors($term->term_id, $taxonomy);
|
$ancestors = get_ancestors( $term->term_id, $taxonomy, 'taxonomy' );
|
||||||
foreach ( (array)$ancestors as $ancestor ) {
|
foreach ( (array)$ancestors as $ancestor ) {
|
||||||
$ancestor_term = get_term($ancestor, $taxonomy);
|
$ancestor_term = get_term($ancestor, $taxonomy);
|
||||||
$hierarchical_slugs[] = $ancestor_term->slug;
|
$hierarchical_slugs[] = $ancestor_term->slug;
|
||||||
@ -4279,11 +4279,16 @@ function is_object_in_taxonomy($object_type, $taxonomy) {
|
|||||||
/**
|
/**
|
||||||
* Get an array of ancestor IDs for a given object.
|
* Get an array of ancestor IDs for a given object.
|
||||||
*
|
*
|
||||||
* @param int $object_id The ID of the object
|
* @since 3.1.0
|
||||||
* @param string $object_type The type of object for which we'll be retrieving ancestors.
|
* @since 4.1.0 Introduced the 'resource_type' parameter.
|
||||||
* @return array of ancestors from lowest to highest in the hierarchy.
|
*
|
||||||
|
* @param int $object_id The ID of the object.
|
||||||
|
* @param string $object_type The type of object for which we'll be retrieving ancestors.
|
||||||
|
* Accepts a post type or a taxonomy name.
|
||||||
|
* @param string $resource_type Optional. Type of resource $object_type is. Accepts 'post_type' or 'taxonomy'.
|
||||||
|
* @return array An array of ancestors from lowest to highest in the hierarchy.
|
||||||
*/
|
*/
|
||||||
function get_ancestors($object_id = 0, $object_type = '') {
|
function get_ancestors( $object_id = 0, $object_type = '', $resource_type = '' ) {
|
||||||
$object_id = (int) $object_id;
|
$object_id = (int) $object_id;
|
||||||
|
|
||||||
$ancestors = array();
|
$ancestors = array();
|
||||||
@ -4291,16 +4296,24 @@ function get_ancestors($object_id = 0, $object_type = '') {
|
|||||||
if ( empty( $object_id ) ) {
|
if ( empty( $object_id ) ) {
|
||||||
|
|
||||||
/** This filter is documented in wp-includes/taxonomy.php */
|
/** This filter is documented in wp-includes/taxonomy.php */
|
||||||
return apply_filters( 'get_ancestors', $ancestors, $object_id, $object_type );
|
return apply_filters( 'get_ancestors', $ancestors, $object_id, $object_type, $resource_type );
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( is_taxonomy_hierarchical( $object_type ) ) {
|
if ( ! $resource_type ) {
|
||||||
|
if ( is_taxonomy_hierarchical( $object_type ) ) {
|
||||||
|
$resource_type = 'taxonomy';
|
||||||
|
} else if ( post_type_exists( $object_type ) ) {
|
||||||
|
$resource_type = 'post_type';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( 'taxonomy' === $resource_type ) {
|
||||||
$term = get_term($object_id, $object_type);
|
$term = get_term($object_id, $object_type);
|
||||||
while ( ! is_wp_error($term) && ! empty( $term->parent ) && ! in_array( $term->parent, $ancestors ) ) {
|
while ( ! is_wp_error($term) && ! empty( $term->parent ) && ! in_array( $term->parent, $ancestors ) ) {
|
||||||
$ancestors[] = (int) $term->parent;
|
$ancestors[] = (int) $term->parent;
|
||||||
$term = get_term($term->parent, $object_type);
|
$term = get_term($term->parent, $object_type);
|
||||||
}
|
}
|
||||||
} elseif ( post_type_exists( $object_type ) ) {
|
} elseif ( 'post_type' === $resource_type ) {
|
||||||
$ancestors = get_post_ancestors($object_id);
|
$ancestors = get_post_ancestors($object_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -4309,9 +4322,10 @@ function get_ancestors($object_id = 0, $object_type = '') {
|
|||||||
*
|
*
|
||||||
* @since 3.1.0
|
* @since 3.1.0
|
||||||
*
|
*
|
||||||
* @param array $ancestors An array of object ancestors.
|
* @param array $ancestors An array of object ancestors.
|
||||||
* @param int $object_id Object ID.
|
* @param int $object_id Object ID.
|
||||||
* @param string $object_type Type of object.
|
* @param string $object_type Type of object.
|
||||||
|
* @param string $resource_type Type of resource $object_type is.
|
||||||
*/
|
*/
|
||||||
return apply_filters( 'get_ancestors', $ancestors, $object_id, $object_type );
|
return apply_filters( 'get_ancestors', $ancestors, $object_id, $object_type );
|
||||||
}
|
}
|
||||||
|
@ -234,4 +234,103 @@ class Tests_Taxonomy extends WP_UnitTestCase {
|
|||||||
);
|
);
|
||||||
$this->assertEquals( 0, wp_insert_category( $cat, false ) );
|
$this->assertEquals( 0, wp_insert_category( $cat, false ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function test_get_ancestors_taxonomy_non_hierarchical() {
|
||||||
|
register_taxonomy( 'wptests_tax', 'post' );
|
||||||
|
$t = $this->factory->term->create( array(
|
||||||
|
'taxonomy' => 'wptests_tax',
|
||||||
|
) );
|
||||||
|
|
||||||
|
$this->assertSame( array(), get_ancestors( $t, 'wptests_tax' ) );
|
||||||
|
_unregister_taxonomy( 'wptests_tax' );
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_get_ancestors_taxonomy() {
|
||||||
|
register_taxonomy( 'wptests_tax', 'post', array(
|
||||||
|
'hierarchical' => true,
|
||||||
|
) );
|
||||||
|
$t1 = $this->factory->term->create( array(
|
||||||
|
'taxonomy' => 'wptests_tax',
|
||||||
|
) );
|
||||||
|
$t2 = $this->factory->term->create( array(
|
||||||
|
'taxonomy' => 'wptests_tax',
|
||||||
|
'parent' => $t1,
|
||||||
|
) );
|
||||||
|
$t3 = $this->factory->term->create( array(
|
||||||
|
'taxonomy' => 'wptests_tax',
|
||||||
|
'parent' => $t2,
|
||||||
|
) );
|
||||||
|
$t4 = $this->factory->term->create( array(
|
||||||
|
'taxonomy' => 'wptests_tax',
|
||||||
|
'parent' => $t1,
|
||||||
|
) );
|
||||||
|
|
||||||
|
$this->assertEqualSets( array( $t2, $t1 ), get_ancestors( $t3, 'wptests_tax' ) );
|
||||||
|
_unregister_taxonomy( 'wptests_tax' );
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_get_ancestors_post_type_non_hierarchical() {
|
||||||
|
register_post_type( 'wptests_pt' );
|
||||||
|
$p = $this->factory->post->create( array(
|
||||||
|
'taxonomy' => 'wptests_pt',
|
||||||
|
) );
|
||||||
|
|
||||||
|
$this->assertEqualSets( array(), get_ancestors( $p, 'wptests_tax' ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_get_ancestors_post_type() {
|
||||||
|
register_post_type( 'wptests_pt', array(
|
||||||
|
'hierarchical' => true,
|
||||||
|
) );
|
||||||
|
$p1 = $this->factory->post->create( array(
|
||||||
|
'post_type' => 'wptests_pt',
|
||||||
|
) );
|
||||||
|
$p2 = $this->factory->post->create( array(
|
||||||
|
'post_type' => 'wptests_pt',
|
||||||
|
'post_parent' => $p1,
|
||||||
|
) );
|
||||||
|
$p3 = $this->factory->post->create( array(
|
||||||
|
'post_type' => 'wptests_pt',
|
||||||
|
'post_parent' => $p2,
|
||||||
|
) );
|
||||||
|
$p4 = $this->factory->post->create( array(
|
||||||
|
'post_type' => 'wptests_pt',
|
||||||
|
'post_parent' => $p1,
|
||||||
|
) );
|
||||||
|
|
||||||
|
$this->assertEqualSets( array( $p2, $p1 ), get_ancestors( $p3, 'wptests_pt' ) );
|
||||||
|
_unregister_post_type( 'wptests_pt' );
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ticket 15029
|
||||||
|
*/
|
||||||
|
public function test_get_ancestors_taxonomy_post_type_conflict_resource_type_taxonomy() {
|
||||||
|
register_post_type( 'wptests_conflict', array(
|
||||||
|
'hierarchical' => true,
|
||||||
|
) );
|
||||||
|
$p1 = $this->factory->post->create( array(
|
||||||
|
'post_type' => 'wptests_conflict',
|
||||||
|
) );
|
||||||
|
$p2 = $this->factory->post->create( array(
|
||||||
|
'post_type' => 'wptests_conflict',
|
||||||
|
'post_parent' => $p1,
|
||||||
|
) );
|
||||||
|
|
||||||
|
register_taxonomy( 'wptests_conflict', 'post', array(
|
||||||
|
'hierarchical' => true,
|
||||||
|
) );
|
||||||
|
$t1 = $this->factory->term->create( array(
|
||||||
|
'taxonomy' => 'wptests_conflict',
|
||||||
|
) );
|
||||||
|
$t2 = $this->factory->term->create( array(
|
||||||
|
'taxonomy' => 'wptests_conflict',
|
||||||
|
'parent' => $t1,
|
||||||
|
) );
|
||||||
|
|
||||||
|
$this->assertEqualSets( array( $p1 ), get_ancestors( $p2, 'wptests_conflict', 'post_type' ) );
|
||||||
|
$this->assertEqualSets( array( $t1 ), get_ancestors( $t2, 'wptests_conflict', 'taxonomy' ) );
|
||||||
|
$this->assertEqualSets( array( $t1 ), get_ancestors( $t2, 'wptests_conflict' ) );
|
||||||
|
_unregister_post_type( 'wptests_pt' );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user