From 96b42c2fdc49d26978479af53182b1a5af68665a Mon Sep 17 00:00:00 2001 From: Boone Gorges Date: Sat, 1 Nov 2014 02:58:23 +0000 Subject: [PATCH] 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 --- wp-admin/includes/ajax-actions.php | 2 +- wp-includes/taxonomy.php | 36 +++++++++++++++++++++--------- wp-includes/version.php | 2 +- 3 files changed, 27 insertions(+), 13 deletions(-) diff --git a/wp-admin/includes/ajax-actions.php b/wp-admin/includes/ajax-actions.php index 30dffbc31b..c9b4563bc4 100644 --- a/wp-admin/includes/ajax-actions.php +++ b/wp-admin/includes/ajax-actions.php @@ -788,7 +788,7 @@ function wp_ajax_add_tag() { $level = 0; if ( is_taxonomy_hierarchical($taxonomy) ) { - $level = count( get_ancestors( $tag->term_id, $taxonomy ) ); + $level = count( get_ancestors( $tag->term_id, $taxonomy, 'taxonomy' ) ); ob_start(); $wp_list_table->single_row( $tag, $level ); $noparents = ob_get_clean(); diff --git a/wp-includes/taxonomy.php b/wp-includes/taxonomy.php index 4ca5369f06..44307fae60 100644 --- a/wp-includes/taxonomy.php +++ b/wp-includes/taxonomy.php @@ -4054,7 +4054,7 @@ function get_term_link( $term, $taxonomy = '') { } else { if ( $t->rewrite['hierarchical'] ) { $hierarchical_slugs = array(); - $ancestors = get_ancestors($term->term_id, $taxonomy); + $ancestors = get_ancestors( $term->term_id, $taxonomy, 'taxonomy' ); foreach ( (array)$ancestors as $ancestor ) { $ancestor_term = get_term($ancestor, $taxonomy); $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. * - * @param int $object_id The ID of the object - * @param string $object_type The type of object for which we'll be retrieving ancestors. - * @return array of ancestors from lowest to highest in the hierarchy. + * @since 3.1.0 + * @since 4.1.0 Introduced the 'resource_type' parameter. + * + * @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; $ancestors = array(); @@ -4291,16 +4296,24 @@ function get_ancestors($object_id = 0, $object_type = '') { if ( empty( $object_id ) ) { /** 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); while ( ! is_wp_error($term) && ! empty( $term->parent ) && ! in_array( $term->parent, $ancestors ) ) { $ancestors[] = (int) $term->parent; $term = get_term($term->parent, $object_type); } - } elseif ( post_type_exists( $object_type ) ) { + } elseif ( 'post_type' === $resource_type ) { $ancestors = get_post_ancestors($object_id); } @@ -4309,9 +4322,10 @@ function get_ancestors($object_id = 0, $object_type = '') { * * @since 3.1.0 * - * @param array $ancestors An array of object ancestors. - * @param int $object_id Object ID. - * @param string $object_type Type of object. + * @param array $ancestors An array of object ancestors. + * @param int $object_id Object ID. + * @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 ); } diff --git a/wp-includes/version.php b/wp-includes/version.php index 41a643475a..82d6ca045f 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -4,7 +4,7 @@ * * @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.