Ticket #21760: 21760.6.diff
File 21760.6.diff, 5.0 KB (added by , 7 years ago) |
---|
-
src/wp-includes/functions.php
3502 3502 * Determine if the scheme of the given URL is https. 3503 3503 * 3504 3504 * @since 4.0.0 3505 * 3505 * 3506 3506 * @param string $url The URL 3507 3507 * @return boolean True if the given URL uses https, false if not (or if the URL is not valid). 3508 3508 */ … … 4597 4597 4598 4598 return (bool) $var; 4599 4599 } 4600 4601 /** 4602 * 4603 * @param string $bucket 4604 * @return int 4605 */ 4606 function wp_get_last_changed( $bucket ) { 4607 $last_changed = wp_cache_get( 'last_changed', $bucket ); 4608 if ( ! $last_changed ) { 4609 $last_changed = microtime(); 4610 wp_cache_set( 'last_changed', $last_changed, $bucket ); 4611 } 4612 return $last_changed; 4613 } 4614 No newline at end of file -
src/wp-includes/taxonomy.php
962 962 return $error; 963 963 } 964 964 965 $bucket = $taxonomy . ':' . wp_get_last_changed( 'terms' ); 965 966 if ( is_object($term) && empty($term->filter) ) { 966 wp_cache_add($term->term_id, $term, $taxonomy); 967 wp_cache_add( $term->term_id, $term, $taxonomy ); 968 wp_cache_add( "slug:{$term->slug}", $term->term_id, $bucket ); 967 969 $_term = $term; 968 970 } else { 969 971 if ( is_object($term) ) … … 974 976 $_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 977 if ( ! $_term ) 976 978 return null; 977 wp_cache_add($term, $_term, $taxonomy); 979 wp_cache_add( $term, $_term, $taxonomy ); 980 wp_cache_add( "slug:{$_term->slug}", $term, $bucket ); 978 981 } 979 982 } 980 983 … … 1046 1049 if ( ! taxonomy_exists($taxonomy) ) 1047 1050 return false; 1048 1051 1052 $cache = false; 1049 1053 if ( 'slug' == $field ) { 1050 1054 $field = 't.slug'; 1051 1055 $value = sanitize_title($value); 1052 1056 if ( empty($value) ) 1053 1057 return false; 1058 $bucket = $taxonomy . ':' . wp_get_last_changed( 'terms' ); 1059 $term_id = wp_cache_get( "slug:{$value}", $bucket ); 1060 if ( $term_id ) { 1061 $value = $term_id; 1062 $cache = true; 1063 } 1054 1064 } else if ( 'name' == $field ) { 1055 1065 // Assume already escaped 1056 1066 $value = wp_unslash($value); … … 1059 1069 $value = (int) $value; 1060 1070 $field = 'tt.term_taxonomy_id'; 1061 1071 } else { 1072 $cache = true; 1073 } 1074 1075 if ( $cache ) { 1062 1076 $term = get_term( (int) $value, $taxonomy, $output, $filter); 1063 1077 if ( is_wp_error( $term ) ) 1064 1078 $term = false; … … 1069 1083 if ( !$term ) 1070 1084 return false; 1071 1085 1072 wp_cache_add($term->term_id, $term, $taxonomy); 1086 wp_cache_add( $term->term_id, $term, $taxonomy ); 1087 $bucket = $taxonomy . ':' . wp_get_last_changed( 'terms' ); 1088 wp_cache_add( "slug:{$term->slug}", $term->term_id, $bucket ); 1073 1089 1074 1090 /** This filter is documented in wp-includes/taxonomy.php */ 1075 1091 $term = apply_filters( 'get_term', $term, $taxonomy ); … … 3407 3423 if ( empty($term_taxonomy) ) 3408 3424 $term_taxonomy = $term->taxonomy; 3409 3425 3410 wp_cache_add($term->term_id, $term, $term_taxonomy); 3426 wp_cache_add( $term->term_id, $term, $term_taxonomy ); 3427 $bucket = $term_taxonomy . ':' . wp_get_last_changed( 'terms' ); 3428 wp_cache_add( "slug:{$term->slug}", $term->term_id, $bucket ); 3411 3429 } 3412 3430 } 3413 3431 -
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