Ticket #7267: prevent-category-looping-r8296.patch

File prevent-category-looping-r8296.patch, 1.8 KB (added by tellyworth, 5 years ago)
  • wp-includes/category-template.php

     
    11<?php 
    22 
    3 function get_category_children($id, $before = '/', $after = '') { 
     3function get_category_children($id, $before = '/', $after = '', $visited=array()) { 
    44        if ( 0 == $id ) 
    55                return ''; 
    66 
     
    1414                $category = get_category($cat_id); 
    1515                if ( is_wp_error( $category ) ) 
    1616                        return $category; 
    17                 if ( $category->parent == $id ) { 
     17                if ( $category->parent == $id && !in_array($category->term_id, $visited) ) { 
     18                        $visited[] = $category->term_id; 
    1819                        $chain .= $before.$category->term_id.$after; 
    1920                        $chain .= get_category_children($category->term_id, $before, $after); 
    2021                } 
     
    4445        return apply_filters('category_link', $catlink, $category_id); 
    4546} 
    4647 
    47 function get_category_parents($id, $link = FALSE, $separator = '/', $nicename = FALSE){ 
     48function get_category_parents($id, $link = FALSE, $separator = '/', $nicename = FALSE, $visited = array()){ 
    4849        $chain = ''; 
    4950        $parent = &get_category($id); 
    5051        if ( is_wp_error( $parent ) ) 
     
    5556        else 
    5657                $name = $parent->cat_name; 
    5758 
    58         if ( $parent->parent && ($parent->parent != $parent->term_id) ) 
    59                 $chain .= get_category_parents($parent->parent, $link, $separator, $nicename); 
     59        if ( $parent->parent && ($parent->parent != $parent->term_id) && !in_array($parent->parent, $visited) ) { 
     60                $visited[] = $parent->parent; 
     61                $chain .= get_category_parents($parent->parent, $link, $separator, $nicename, $visited); 
     62        } 
    6063 
    6164        if ( $link ) 
    6265                $chain .= '<a href="' . get_category_link($parent->term_id) . '" title="' . sprintf(__("View all posts in %s"), $parent->cat_name) . '">'.$name.'</a>' . $separator;