Make WordPress Core

Changeset 30141

Timestamp:
11/01/2014 02:57:31 AM (10 years ago)
Author:
boonebgorges
Message:

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.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/ajax-actions.php

    r30055 r30141  
    789789    $level = 0;
    790790    if ( is_taxonomy_hierarchical($taxonomy) ) {
    791         $level = count( get_ancestors( $tag->term_id, $taxonomy ) );
     791        $level = count( get_ancestors( $tag->term_id, $taxonomy ) );
    792792        ob_start();
    793793        $wp_list_table->single_row( $tag, $level );
  • trunk/src/wp-includes/taxonomy.php

    r30122 r30141  
    40554055        if ( $t->rewrite['hierarchical'] ) {
    40564056            $hierarchical_slugs = array();
    4057             $ancestors = get_ancestors($term->term_id, $taxonomy);
     4057            $ancestors = get_ancestors();
    40584058            foreach ( (array)$ancestors as $ancestor ) {
    40594059                $ancestor_term = get_term($ancestor, $taxonomy);
     
    42804280 * Get an array of ancestor IDs for a given object.
    42814281 *
    4282  * @param int $object_id The ID of the object
    4283  * @param string $object_type The type of object for which we'll be retrieving ancestors.
    4284  * @return array of ancestors from lowest to highest in the hierarchy.
    4285  */
    4286 function get_ancestors($object_id = 0, $object_type = '') {
     4282 * @since 3.1.0
     4283 * @since 4.1.0 Introduced the 'resource_type' parameter.
     4284 *
     4285 * @param int    $object_id     The ID of the object.
     4286 * @param string $object_type   The type of object for which we'll be retrieving ancestors.
     4287 *                              Accepts a post type or a taxonomy name.
     4288 * @param string $resource_type Optional. Type of resource $object_type is. Accepts 'post_type' or 'taxonomy'.
     4289 * @return array An array of ancestors from lowest to highest in the hierarchy.
     4290 */
     4291function get_ancestors( $object_id = 0, $object_type = '', $resource_type = '' ) {
    42874292    $object_id = (int) $object_id;
    42884293
     
    42924297
    42934298        /** This filter is documented in wp-includes/taxonomy.php */
    4294         return apply_filters( 'get_ancestors', $ancestors, $object_id, $object_type );
    4295     }
    4296 
    4297     if ( is_taxonomy_hierarchical( $object_type ) ) {
     4299        return apply_filters( 'get_ancestors', $ancestors, $object_id, $object_type, $resource_type );
     4300    }
     4301
     4302    if ( ! $resource_type ) {
     4303        if ( is_taxonomy_hierarchical( $object_type ) ) {
     4304            $resource_type = 'taxonomy';
     4305        } else if ( post_type_exists( $object_type ) ) {
     4306            $resource_type = 'post_type';
     4307        }
     4308    }
     4309
     4310    if ( 'taxonomy' === $resource_type ) {
    42984311        $term = get_term($object_id, $object_type);
    42994312        while ( ! is_wp_error($term) && ! empty( $term->parent ) && ! in_array( $term->parent, $ancestors ) ) {
     
    43014314            $term = get_term($term->parent, $object_type);
    43024315        }
    4303     } elseif ( post_type_exists( $object_type ) ) {
     4316    } elseif ( ) {
    43044317        $ancestors = get_post_ancestors($object_id);
    43054318    }
     
    43104323     * @since 3.1.0
    43114324     *
    4312      * @param array  $ancestors   An array of object ancestors.
    4313      * @param int    $object_id   Object ID.
    4314      * @param string $object_type Type of object.
     4325     * @param array  $ancestors     An array of object ancestors.
     4326     * @param int    $object_id     Object ID.
     4327     * @param string $object_type   Type of object.
     4328     * @param string $resource_type Type of resource $object_type is.
    43154329     */
    43164330    return apply_filters( 'get_ancestors', $ancestors, $object_id, $object_type );
  • trunk/tests/phpunit/tests/taxonomy.php

    r29830 r30141  
    235235        $this->assertEquals( 0, wp_insert_category( $cat, false ) );
    236236    }
     237
     238
     239
     240
     241
     242
     243
     244
     245
     246
     247
     248
     249
     250
     251
     252
     253
     254
     255
     256
     257
     258
     259
     260
     261
     262
     263
     264
     265
     266
     267
     268
     269
     270
     271
     272
     273
     274
     275
     276
     277
     278
     279
     280
     281
     282
     283
     284
     285
     286
     287
     288
     289
     290
     291
     292
     293
     294
     295
     296
     297
     298
     299
     300
     301
     302
     303
     304
     305
     306
     307
     308
     309
     310
     311
     312
     313
     314
     315
     316
     317
     318
     319
     320
     321
     322
     323
     324
     325
     326
     327
     328
     329
     330
     331
     332
     333
     334
     335
    237336}
Note: See TracChangeset for help on using the changeset viewer.