WordPress.org

Make WordPress Core

Ticket #30415: 30415.3.diff

File 30415.3.diff, 4.4 KB (added by hlashbrooke, 7 years ago)

Refactoring solution

  • wp-includes/category-template.php

     
    3636 * @param bool $link Optional, default is false. Whether to format with link.
    3737 * @param string $separator Optional, default is '/'. How to separate categories.
    3838 * @param bool $nicename Optional, default is false. Whether to use nice name for display.
    39  * @param array $visited Optional. Already linked to categories to prevent duplicates.
    40  * @return string|WP_Error A list of category parents on success, WP_Error on failure.
     39 * @return string A list of category parents on success, empty string on failure.
    4140 */
    42 function get_category_parents( $id, $link = false, $separator = '/', $nicename = false, $visited = array() ) {
    43         $chain = '';
    44         $parent = get_term( $id, 'category' );
    45         if ( is_wp_error( $parent ) )
    46                 return $parent;
    47 
    48         if ( $nicename )
    49                 $name = $parent->slug;
    50         else
    51                 $name = $parent->name;
    52 
    53         if ( $parent->parent && ( $parent->parent != $parent->term_id ) && !in_array( $parent->parent, $visited ) ) {
    54                 $visited[] = $parent->parent;
    55                 $chain .= get_category_parents( $parent->parent, $link, $separator, $nicename, $visited );
    56         }
    57 
    58         if ( $link )
    59                 $chain .= '<a href="' . esc_url( get_category_link( $parent->term_id ) ) . '">'.$name.'</a>' . $separator;
    60         else
    61                 $chain .= $name.$separator;
    62         return $chain;
     41function get_category_parents( $id, $link = false, $separator = '/', $nicename = false ) {
     42        return get_term_parents_html( $id, 'category', $link, $separator, $nicename );
    6343}
    6444
    6545/**
  • wp-includes/taxonomy.php

     
    14961496}
    14971497
    14981498/**
     1499 * Return a hierarchical array of a given term's parents.
     1500 *
     1501 * @since 4.1.0
     1502 *
     1503 * @param string $term_id ID of Term to get parents.
     1504 * @param string $taxonomy Taxonomy name.
     1505 * @param boolean $reverse Optional, default to false. Whether to return terms array in reverse order.
     1506 * @return array|WP_Error List of Term IDs. WP_Error returned if $taxonomy does not exist or $taxonomy is not hierarchical.
     1507 */
     1508function get_term_parents( $term_id, $taxonomy, $reverse = false ) {
     1509
     1510        if ( ! taxonomy_exists( $taxonomy ) ) {
     1511                return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy' ) );
     1512        }
     1513
     1514        if( ! is_taxonomy_hierarchical( $taxonomy ) ) {
     1515                return new WP_Error( 'taxonomy_not_hierarchical', sprintf( __( 'The "%s" taxonomy is not hierarchical.' ), $taxonomy ) );
     1516        }
     1517
     1518        $term_id = intval( $term_id );
     1519
     1520        $term = get_term( $term_id, $taxonomy );
     1521
     1522        if ( is_wp_error( $term ) ) {
     1523                return $term;
     1524        }
     1525
     1526        $parents = array();
     1527        while( ! is_wp_error( $term ) ) {
     1528                if ( $term->parent && ( $term->parent != $term->term_id ) ) {
     1529                        $parents[] = $term->parent;
     1530                        $term = get_term( $term->parent, $taxonomy );
     1531                } else {
     1532                        break;
     1533                }
     1534        }
     1535
     1536        if( $reverse ) {
     1537                $parents = array_reverse( $parents );
     1538        }
     1539
     1540        return $parents;
     1541}
     1542
     1543/**
     1544 * Return term parents with a given separator.
     1545 *
     1546 * @since 4.1.0
     1547 *
     1548 * @param string $term_id ID of Term to get parents.
     1549 * @param string $taxonomy Optional, default is 'category. Taxonomy name.
     1550 * @param bool $link Optional, default is false. Whether to format with link.
     1551 * @param string $separator Optional, default is '/'. How to separate categories.
     1552 * @param bool $nicename Optional, default is false. Whether to use nice name for display.
     1553 * @return string A list of term parents on success, empty string on failure.
     1554 */
     1555function get_term_parents_html( $term_id, $taxonomy = 'category', $link = false, $separator = '/', $nicename = false ) {
     1556
     1557        $parents = (array) get_term_parents( $term_id, $taxonomy, true );
     1558
     1559        $chain = '';
     1560
     1561        if( 0 == count( $parents ) ) {
     1562                return $chain;
     1563        }
     1564
     1565        foreach( $parents as $parent ) {
     1566
     1567                $term = get_term( $parent, $taxonomy );
     1568
     1569                if ( is_wp_error( $term ) ) {
     1570                        continue;
     1571                }
     1572
     1573                if ( $nicename ) {
     1574                        $name = $term->slug;
     1575                } else {
     1576                        $name = $term->name;
     1577                }
     1578
     1579                if( $chain ) {
     1580                        $chain .= $separator;
     1581                }
     1582
     1583                if ( $link ) {
     1584                        $chain .= '<a href="' . esc_url( get_term_link( $term->term_id, $taxonomy ) ) . '">' . $name . '</a>';
     1585                } else {
     1586                        $chain .= $name;
     1587                }
     1588
     1589        }
     1590
     1591        return $chain;
     1592}
     1593
     1594/**
    14991595 * Get sanitized Term field.
    15001596 *
    15011597 * Does checks for $term, based on the $taxonomy. The function is for contextual