Make WordPress Core

Changeset 27102


Ignore:
Timestamp:
02/06/2014 01:58:01 AM (10 years ago)
Author:
wonderboymusic
Message:

Regenerate the term hierarchy cache ({taxonomy}_children) when it is out of sync with the passed taxonomy's last_changed value.

Introduces taxonomy_hierarchy_is_fresh(), which is only called in _get_term_hierarchy(). The taxonomy's last_changed value is checked against the value of wp_cache_get( 'hierarchy_last_changed', $taxonomy ).

Adds a unit test - Tests_Term:test_hierachy_invalidation().

See [27101], which makes this type of cache invalidation possible.
Fixes #14485.

Location:
trunk
Files:
2 edited

Legend:

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

    r27101 r27102  
    29492949    if ( !is_taxonomy_hierarchical($taxonomy) )
    29502950        return array();
    2951     $children = get_option("{$taxonomy}_children");
     2951    $children = false;
     2952    if ( taxonomy_hierarchy_is_fresh( $taxonomy ) ) {
     2953        $children = get_option("{$taxonomy}_children");
     2954    }
    29522955
    29532956    if ( is_array($children) )
     
    35223525 * Determine if a post's cache for the passed taxonomy
    35233526 *  is in sync.
     3527 *
    35243528 * @since 3.9.0
    35253529 *
     
    35373541    return true;
    35383542}
     3543
     3544/**
     3545 * Determine if a hierarchy's cache for the passed taxonomy
     3546 *  is in sync.
     3547 *
     3548 * @since 3.9.0
     3549 *
     3550 * @param int $id
     3551 * @param string $taxonomy
     3552 * @return boolean
     3553 */
     3554function taxonomy_hierarchy_is_fresh( $taxonomy ) {
     3555    $last_changed = get_taxonomy_last_changed( $taxonomy );
     3556    $hierarchy_last_changed = wp_cache_get( 'hierarchy_last_changed', $taxonomy );
     3557    if ( ! $hierarchy_last_changed || $last_changed !== $hierarchy_last_changed ) {
     3558        wp_cache_set( 'hierarchy_last_changed', $last_changed, $taxonomy );
     3559        return false;
     3560    }
     3561    return true;
     3562}
  • trunk/tests/phpunit/tests/term.php

    r27101 r27102  
    586586        $this->assertNotEquals( $last_changed2, $last_changed3 );
    587587    }
    588    
     588
    589589    /**
    590590     * @ticket 22526
     
    635635        $this->assertNotEquals( $term->name, reset( $cats2 )->name );
    636636    }
     637
     638    function test_hierachy_invalidation() {
     639        $tax = 'burrito';
     640        register_taxonomy( $tax, 'post', array( 'hierarchical' => true ) );
     641        $this->assertTrue( get_taxonomy( $tax )->hierarchical );
     642
     643        $step = 1;
     644        $parent_id = 0;
     645        $children = 0;
     646
     647        foreach ( range( 1, 99 ) as $i ) {
     648            switch ( $step ) {
     649            case 1:
     650                $parent = wp_insert_term( 'Parent' . $i, $tax );
     651                $parent_id = $parent['term_id'];
     652                break;
     653            case 2:
     654                $parent = wp_insert_term( 'Child' . $i, $tax, array( 'parent' => $parent_id ) );
     655                $parent_id = $parent['term_id'];
     656                $children++;
     657                break;
     658            case 3:
     659                wp_insert_term( 'Grandchild' . $i, $tax, array( 'parent' => $parent_id ) );
     660                $parent_id = 0;
     661                $children++;
     662                break;
     663            }
     664
     665            $terms = get_terms( $tax, array( 'hide_empty' => false ) );
     666            $this->assertEquals( $i, count( $terms ) );
     667            if ( 1 < $i ) {
     668                $hierarchy = _get_term_hierarchy( $tax );
     669                $this->assertNotEmpty( $hierarchy );
     670                $this->assertEquals( $children, count( $hierarchy, COUNT_RECURSIVE ) - count( $hierarchy ) );
     671            }
     672
     673            if ( $i % 3 === 0 ) {
     674                $step = 1;
     675            } else {
     676                $step++;
     677            }
     678        }
     679
     680        _unregister_taxonomy( $tax );
     681    }
    637682}
Note: See TracChangeset for help on using the changeset viewer.