WordPress.org

Make WordPress Core

Ticket #14485: garyc40-14485-rev4.patch

File garyc40-14485-rev4.patch, 3.5 KB (added by garyc40, 7 years ago)

removed $last_changed, added "cache_keys", properly invalidated get_terms cache

  • wp-includes/taxonomy.php

     
    11501150        // $args can be whatever, only use the args defined in defaults to compute the key
    11511151        $filter_key = ( has_filter('list_terms_exclusions') ) ? serialize($GLOBALS['wp_filter']['list_terms_exclusions']) : '';
    11521152        $key = md5( serialize( compact(array_keys($defaults)) ) . serialize( $taxonomies ) . $filter_key );
    1153         $last_changed = wp_cache_get('last_changed', 'terms');
    1154         if ( !$last_changed ) {
    1155                 $last_changed = time();
    1156                 wp_cache_set('last_changed', $last_changed, 'terms');
     1153        $cache_key = "get_terms:$key";
     1154       
     1155        // save cache_key so that it could be invalidate later
     1156        $cache_keys = wp_cache_get( 'get_terms:cache_keys', 'terms' );
     1157        if ( ! $cache_keys ) {
     1158                $cache_keys = array();
    11571159        }
    1158         $cache_key = "get_terms:$key:$last_changed";
     1160        if ( ! in_array( $cache_keys, $cache_keys ) ) {
     1161                $cache_keys[] = $cache_key;
     1162        }
     1163        wp_cache_set( 'get_terms:cache_keys', $cache_keys, 'terms' );
     1164
    11591165        $cache = wp_cache_get( $cache_key, 'terms' );
    11601166        if ( false !== $cache ) {
    11611167                $cache = apply_filters('get_terms', $cache, $taxonomies, $args);
     
    12971303
    12981304        if ( 'count' == $fields ) {
    12991305                $term_count = $wpdb->get_var($query);
     1306                wp_cache_add( $cache_key, $term_count, 'terms' );
    13001307                return $term_count;
    13011308        }
    13021309
     
    17071714        if ( !$wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_taxonomy WHERE term_id = %d", $term) ) )
    17081715                $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->terms WHERE term_id = %d", $term) );
    17091716
    1710         clean_term_cache($term, $taxonomy);
     1717        clean_term_cache($term, $taxonomy, true, true);
    17111718
    17121719        do_action('delete_term', $term, $tt_id, $taxonomy);
    17131720        do_action("delete_$taxonomy", $term, $tt_id);
     
    20012008
    20022009        $term_id = apply_filters('term_id_filter', $term_id, $tt_id);
    20032010
    2004         clean_term_cache($term_id, $taxonomy);
     2011        $force_clean_taxonomy = ( $parent ) ? true : false;
    20052012
     2013        clean_term_cache($term_id, $taxonomy, true, $force_clean_taxonomy);
     2014
    20062015        do_action("created_term", $term_id, $tt_id, $taxonomy);
    20072016        do_action("created_$taxonomy", $term_id, $tt_id);
    20082017
     
    24402449 * @param int|array $ids Single or list of Term IDs
    24412450 * @param string $taxonomy Can be empty and will assume tt_ids, else will use for context.
    24422451 * @param bool $clean_taxonomy Whether to clean taxonomy wide caches (true), or just individual term object caches (false). Default is true.
     2452 * @param bool $force_clean_taxonomy Whether to force clean taxonomy wide caches (true), or skip if it's already done (false). Default is false
     2453 *
    24432454 */
    2444 function clean_term_cache($ids, $taxonomy = '', $clean_taxonomy = true) {
     2455function clean_term_cache($ids, $taxonomy = '', $clean_taxonomy = true, $force_clean_taxonomy = false) {
    24452456        global $wpdb;
    24462457        static $cleaned = array();
    24472458
     
    24712482        }
    24722483
    24732484        foreach ( $taxonomies as $taxonomy ) {
    2474                 if ( isset($cleaned[$taxonomy]) )
     2485                if ( isset($cleaned[$taxonomy]) && ! $force_clean_taxonomy )
    24752486                        continue;
    24762487                $cleaned[$taxonomy] = true;
    24772488
     
    24862497                do_action('clean_term_cache', $ids, $taxonomy);
    24872498        }
    24882499
    2489         wp_cache_set('last_changed', time(), 'terms');
     2500        // invalidate all get_terms cache values
     2501        $cache_keys = wp_cache_get( 'get_terms:cache_keys', 'terms' );
     2502        if ( ! empty( $cache_keys ) ) {
     2503                foreach ( $cache_keys as $key ) {
     2504                        wp_cache_delete( $key, 'terms' );
     2505                }
     2506                wp_cache_delete( 'get_terms:cache_keys', 'terms' );
     2507        }
    24902508}