WordPress.org

Make WordPress Core

Ticket #20635: 20635.3.diff

File 20635.3.diff, 2.6 KB (added by boonebgorges, 5 years ago)
  • src/wp-includes/taxonomy.php

    diff --git src/wp-includes/taxonomy.php src/wp-includes/taxonomy.php
    index a95c7a3..8c39b3e 100644
    function _get_term_children($term_id, $terms, $taxonomy) { 
    38843884 *
    38853885 * @param array $terms List of Term IDs
    38863886 * @param string $taxonomy Term Context
     3887 * @param array $ancestors List of ancestor terms already referenced. Passed by reference.
    38873888 * @return null Will break from function if conditions are not met.
    38883889 */
    3889 function _pad_term_counts(&$terms, $taxonomy) {
     3890function _pad_term_counts( &$terms, $taxonomy ) {
    38903891        global $wpdb;
    38913892
    38923893        // This function only works for hierarchical taxonomies like post categories.
    function _pad_term_counts(&$terms, $taxonomy) { 
    39173918        }
    39183919
    39193920        // Touch every ancestor's lookup row for each post in each term
     3921        $ancestors = array();
    39203922        foreach ( $term_ids as $term_id ) {
     3923                $ancestors[] = $term_id;
    39213924                $child = $term_id;
    39223925                while ( !empty( $terms_by_id[$child] ) && $parent = $terms_by_id[$child]->parent ) {
     3926                        if ( in_array( $parent, $ancestors ) ) {
     3927                                break;
     3928                        }
     3929
    39233930                        if ( !empty( $term_items[$term_id] ) )
    39243931                                foreach ( $term_items[$term_id] as $item_id => $touches ) {
    39253932                                        $term_items[$parent][$item_id] = isset($term_items[$parent][$item_id]) ? ++$term_items[$parent][$item_id]: 1;
  • tests/phpunit/tests/term/getTerms.php

    diff --git tests/phpunit/tests/term/getTerms.php tests/phpunit/tests/term/getTerms.php
    index 8e0abce..7b04ac8 100644
    class Tests_Term_getTerms extends WP_UnitTestCase { 
    10391039                $this->assertEqualSets( $expected, $actual );
    10401040        }
    10411041
     1042        /*
     1043         * @ticket 20635
     1044         */
     1045        public function test_pad_counts_should_not_recurse_infinitely_when_term_hierarchy_has_a_loop() {
     1046                remove_filter( 'wp_update_term_parent', 'wp_check_term_hierarchy_for_loops', 10 );
     1047
     1048                $c1 = $this->factory->category->create();
     1049                $c2 = $this->factory->category->create( array( 'parent' => $c1 ) );
     1050                $c3 = $this->factory->category->create( array( 'parent' => $c2 ) );
     1051                wp_update_term( $c1, 'category', array( 'parent' => $c3 ) );
     1052
     1053                add_filter( 'wp_update_term_parent', 'wp_check_term_hierarchy_for_loops', 10, 3 );
     1054
     1055                $posts = $this->factory->post->create_many( 3 );
     1056                wp_set_post_terms( $posts[0], $c1, 'category' );
     1057                wp_set_post_terms( $posts[1], $c2, 'category' );
     1058                wp_set_post_terms( $posts[2], $c3, 'category' );
     1059
     1060                $terms = get_terms( 'category', array(
     1061                        'pad_counts' => true,
     1062                ) );
     1063
     1064                $this->assertEqualSets( array( $c1, $c2, $c3 ), wp_list_pluck( $terms, 'term_id' ) );
     1065        }
     1066
    10421067        protected function create_hierarchical_terms_and_posts() {
    10431068                $terms = array();
    10441069