Make WordPress Core

Changeset 39549


Ignore:
Timestamp:
12/09/2016 04:09:31 PM (8 years ago)
Author:
boonebgorges
Message:

Taxonomy: Introduce get_term_parents_list().

This new function is a taxonomy-agnostic version of get_category_parents().

Props keesiemeijer, SergeyBiryukov, rafaehlers.
Fixes #17069.

Location:
trunk
Files:
1 added
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/category-template.php

    r39530 r39549  
    3333 *
    3434 * @since 1.2.0
     35 * @since 4.8.0 The `$visited` parameter was deprecated and renamed to `$deprecated`.
    3536 *
    3637 * @param int $id Category ID.
     
    3839 * @param string $separator Optional, default is '/'. How to separate categories.
    3940 * @param bool $nicename Optional, default is false. Whether to use nice name for display.
    40  * @param array $visited Optional. Already linked to categories to prevent duplicates.
     41 * @param array $deprecated Not used.
    4142 * @return string|WP_Error A list of category parents on success, WP_Error on failure.
    4243 */
    43 function get_category_parents( $id, $link = false, $separator = '/', $nicename = false, $visited = array() ) {
    44     $chain = '';
    45     $parent = get_term( $id, 'category' );
    46     if ( is_wp_error( $parent ) )
    47         return $parent;
    48 
    49     if ( $nicename )
    50         $name = $parent->slug;
    51     else
    52         $name = $parent->name;
    53 
    54     if ( $parent->parent && ( $parent->parent != $parent->term_id ) && !in_array( $parent->parent, $visited ) ) {
    55         $visited[] = $parent->parent;
    56         $chain .= get_category_parents( $parent->parent, $link, $separator, $nicename, $visited );
    57     }
    58 
    59     if ( $link )
    60         $chain .= '<a href="' . esc_url( get_category_link( $parent->term_id ) ) . '">'.$name.'</a>' . $separator;
    61     else
    62         $chain .= $name.$separator;
    63     return $chain;
     44function get_category_parents( $id, $link = false, $separator = '/', $nicename = false, $deprecated = array() ) {
     45
     46    if ( ! empty( $deprecated ) ) {
     47        _deprecated_argument( __FUNCTION__, '4.8.0' );
     48    }
     49
     50    $format = $nicename ? 'slug' : 'name';
     51
     52    $args = array(
     53        'separator' => $separator,
     54        'link'      => $link,
     55        'format'    => $format,
     56    );
     57
     58    return get_term_parents_list( $id, 'category', $args );
    6459}
    6560
     
    12341229
    12351230/**
     1231 * Retrieve term parents with separator.
     1232 *
     1233 * @since 4.8.0
     1234 *
     1235 * @param int     $term_id  Term ID.
     1236 * @param string  $taxonomy Taxonomy name.
     1237 * @param string|array $args {
     1238 *     Array of optional arguments.
     1239 *
     1240 *     @type string $format    Use term names or slugs for display. Accepts 'name' or 'slug'.
     1241 *                             Default 'name'.
     1242 *     @type string $separator Separator for between the terms. Default '/'.
     1243 *     @type bool   $link      Whether to format as a link. Default true.
     1244 *     @type bool   $inclusive Include the term to get the parents for. Default true.
     1245 * }
     1246 * @return string|WP_Error A list of term parents on success, WP_Error or empty string on failure.
     1247 */
     1248function get_term_parents_list( $term_id, $taxonomy, $args = array() ) {
     1249    $list = '';
     1250    $term = get_term( $term_id, $taxonomy );
     1251
     1252    if ( is_wp_error( $term ) ) {
     1253        return $term;
     1254    }
     1255
     1256    if ( ! $term ) {
     1257        return $list;
     1258    }
     1259
     1260    $term_id = $term->term_id;
     1261
     1262    $defaults = array(
     1263        'format'    => 'name',
     1264        'separator' => '/',
     1265        'link'      => true,
     1266        'inclusive' => true,
     1267    );
     1268
     1269    $args = wp_parse_args( $args, $defaults );
     1270
     1271    foreach ( array( 'link', 'inclusive' ) as $bool ) {
     1272        $args[ $bool ] = wp_validate_boolean( $args[ $bool ] );
     1273    }
     1274
     1275    $parents = get_ancestors( $term_id, $taxonomy, 'taxonomy' );
     1276
     1277    if ( $args['inclusive'] ) {
     1278        array_unshift( $parents, $term_id );
     1279    }
     1280
     1281    foreach ( array_reverse( $parents ) as $term_id ) {
     1282        $parent = get_term( $term_id, $taxonomy );
     1283        $name   = ( 'slug' === $args['format'] ) ? $parent->slug : $parent->name;
     1284
     1285        if ( $args['link'] ) {
     1286            $list .= '<a href="' . esc_url( get_category_link( $parent->term_id ) ) . '">' . $name . '</a>' . $args['separator'];
     1287        } else {
     1288            $list .= $name . $args['separator'];
     1289        }
     1290    }
     1291
     1292    return $list;
     1293}
     1294
     1295/**
    12361296 * Display the terms in a list.
    12371297 *
  • trunk/tests/phpunit/tests/category/getCategoryParents.php

    r36778 r39549  
    5151    }
    5252
    53     public function test_visited_should_also_exclude_children_of_visited_categories() {
    54         $c3 = self::factory()->category->create_and_get( array(
    55             'parent' => $this->c2->term_id,
    56         ) );
    57         $c4 = self::factory()->category->create_and_get( array(
    58             'parent' => $c3->term_id,
    59         ) );
     53    public function test_deprecated_argument_visited() {
     54        $this->setExpectedDeprecated( 'get_category_parents' );
     55        $found = get_category_parents( $this->c2->term_id, false, '/', false, array( $this->c1->term_id ) );
     56    }
    6057
    61         $expected = $this->c1->name . '/'. $this->c2->name . '/';
    62         $found = get_category_parents( $this->c2->term_id, false, '/', false, array( $c3->term_id ) );
     58    public function test_category_without_parents() {
     59        $expected = $this->c1->name . '/';
     60        $found = get_category_parents( $this->c1->term_id );
    6361        $this->assertSame( $expected, $found );
    6462    }
Note: See TracChangeset for help on using the changeset viewer.