Ticket #21760: 21760.5.diff
File 21760.5.diff, 5.2 KB (added by , 7 years ago) |
---|
-
src/wp-includes/taxonomy.php
963 963 } 964 964 965 965 if ( is_object($term) && empty($term->filter) ) { 966 wp_cache_add($term->term_id, $term, $taxonomy); 966 wp_cache_add( $term->term_id, $term, $taxonomy ); 967 wp_cache_add( "slug:{$term->slug}", $term->term_id, $taxonomy ); 967 968 $_term = $term; 968 969 } else { 969 970 if ( is_object($term) ) … … 974 975 $_term = $wpdb->get_row( $wpdb->prepare( "SELECT t.*, tt.* FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id WHERE tt.taxonomy = %s AND t.term_id = %d LIMIT 1", $taxonomy, $term) ); 975 976 if ( ! $_term ) 976 977 return null; 977 wp_cache_add($term, $_term, $taxonomy); 978 wp_cache_add( $term, $_term, $taxonomy ); 979 wp_cache_add( "slug:{$_term->slug}", $term, $taxonomy ); 978 980 } 979 981 } 980 982 … … 1046 1048 if ( ! taxonomy_exists($taxonomy) ) 1047 1049 return false; 1048 1050 1051 $cache = false; 1049 1052 if ( 'slug' == $field ) { 1050 1053 $field = 't.slug'; 1051 1054 $value = sanitize_title($value); 1052 1055 if ( empty($value) ) 1053 1056 return false; 1057 $term_id = wp_cache_get( "slug:{$value}", $taxonomy ); 1058 if ( $term_id ) { 1059 $value = $term_id; 1060 $cache = true; 1061 } 1054 1062 } else if ( 'name' == $field ) { 1055 1063 // Assume already escaped 1056 1064 $value = wp_unslash($value); … … 1059 1067 $value = (int) $value; 1060 1068 $field = 'tt.term_taxonomy_id'; 1061 1069 } else { 1070 $cache = true; 1071 } 1072 1073 if ( $cache ) { 1062 1074 $term = get_term( (int) $value, $taxonomy, $output, $filter); 1063 1075 if ( is_wp_error( $term ) ) 1064 1076 $term = false; … … 1069 1081 if ( !$term ) 1070 1082 return false; 1071 1083 1072 wp_cache_add($term->term_id, $term, $taxonomy); 1084 wp_cache_add( $term->term_id, $term, $taxonomy ); 1085 wp_cache_add( "slug:{$term->slug}", $term->term_id, $taxonomy ); 1073 1086 1074 1087 /** This filter is documented in wp-includes/taxonomy.php */ 1075 1088 $term = apply_filters( 'get_term', $term, $taxonomy ); … … 3270 3283 if ( empty($taxonomy) ) { 3271 3284 $tt_ids = array_map('intval', $ids); 3272 3285 $tt_ids = implode(', ', $tt_ids); 3273 $terms = $wpdb->get_results( "SELECT term_id, taxonomy FROM $wpdb->term_taxonomy WHERE term_taxonomy_id IN ($tt_ids)");3286 $terms = $wpdb->get_results( "SELECT t.slug, tt.term_id, tt.taxonomy FROM $wpdb->terms t INNER JOIN $wpdb->term_taxonomy tt WHERE tt.term_taxonomy_id IN ($tt_ids)" ); 3274 3287 $ids = array(); 3275 3288 foreach ( (array) $terms as $term ) { 3276 3289 $taxonomies[] = $term->taxonomy; 3277 3290 $ids[] = $term->term_id; 3278 wp_cache_delete($term->term_id, $term->taxonomy); 3291 wp_cache_delete( $term->term_id, $term->taxonomy ); 3292 wp_cache_delete( "slug:{$term->slug}", $term->taxonomy ); 3279 3293 } 3280 3294 $taxonomies = array_unique($taxonomies); 3281 3295 } else { … … 3282 3296 $taxonomies = array($taxonomy); 3283 3297 foreach ( $taxonomies as $taxonomy ) { 3284 3298 foreach ( $ids as $id ) { 3285 wp_cache_delete( $id, $taxonomy);3299 wp_cache_delete( $id, $taxonomy ); 3286 3300 } 3301 3302 $term_ids = implode( ', ', array_map( 'intval', $ids ) ); 3303 $slugs = $wpdb->get_col( "SELECT t.slug FROM $wpdb->terms t INNER JOIN $wpdb->term_taxonomy tt ON t.term_id = tt.term_id WHERE tt.term_id IN ($term_ids)" ); 3304 foreach ( $slugs as $slug ) { 3305 wp_cache_delete( "slug:{$slug}", $taxonomy ); 3306 } 3287 3307 } 3288 3308 } 3289 3309 … … 3407 3427 if ( empty($term_taxonomy) ) 3408 3428 $term_taxonomy = $term->taxonomy; 3409 3429 3410 wp_cache_add($term->term_id, $term, $term_taxonomy); 3430 wp_cache_add( $term->term_id, $term, $term_taxonomy ); 3431 wp_cache_add( "slug:{$term->slug}", $term->term_id, $term_taxonomy ); 3411 3432 } 3412 3433 } 3413 3434 -
tests/phpunit/tests/term/cache.php
93 93 94 94 _unregister_taxonomy( $tax ); 95 95 } 96 97 /** 98 * @ticket 21760 99 */ 100 function test_get_term_by_slug_cache() { 101 global $wpdb; 102 $term_id = $this->factory->term->create( array( 'slug' => 'burrito', 'taxonomy' => 'post_tag' ) ); 103 104 $queries = $wpdb->num_queries; 105 get_term_by( 'slug', 'burrito', 'post_tag' ); 106 $initial = $queries + 1; 107 $this->assertEquals( $initial, $wpdb->num_queries ); 108 $term = get_term_by( 'slug', 'burrito', 'post_tag' ); 109 $this->assertEquals( $initial, $wpdb->num_queries ); 110 111 $this->assertEquals( get_term( $term_id, 'post_tag' ), $term ); 112 $this->assertEquals( $initial, $wpdb->num_queries ); 113 } 114 115 /** 116 * @ticket 21760 117 */ 118 function test_get_term_by_slug_cache_update() { 119 global $wpdb; 120 $term_id = $this->factory->term->create( array( 'slug' => 'burrito', 'taxonomy' => 'post_tag' ) ); 121 122 $queries = $wpdb->num_queries; 123 get_term_by( 'slug', 'burrito', 'post_tag' ); 124 $initial = $queries + 1; 125 $this->assertEquals( $initial, $wpdb->num_queries ); 126 $term = get_term_by( 'slug', 'burrito', 'post_tag' ); 127 $this->assertEquals( $initial, $wpdb->num_queries ); 128 129 wp_update_term( $term_id, 'post_tag', array( 'name' => 'Taco' ) ); 130 $this->assertNotEquals( $term, get_term( $term_id, 'post_tag' ) ); 131 $after_queries = $wpdb->num_queries; 132 get_term_by( 'slug', 'burrito', 'post_tag' ); 133 $this->assertEquals( $after_queries, $wpdb->num_queries ); 134 } 96 135 } 136 No newline at end of file