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.
Built from https://develop.svn.wordpress.org/trunk@30141


git-svn-id: http://core.svn.wordpress.org/trunk@30141 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Boone Gorges 2014-11-01 02:58:23 +00:00
parent c27548265e
commit 96b42c2fdc
3 changed files with 27 additions and 13 deletions

View File

@ -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();

View File

@ -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 );
} }

View File

@ -4,7 +4,7 @@
* *
* @global string $wp_version * @global string $wp_version
*/ */
$wp_version = '4.1-alpha-30140'; $wp_version = '4.1-alpha-30141';
/** /**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.