Ticket #21760: 21760.9.diff
File 21760.9.diff, 7.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 * Helper function to retrieve an incrementer identified by $group 4603 * 4604 * @since 4.0.0 4605 * 4606 * @param string $group The cache group for the incrementer. 4607 * @param bool $force Whether or not to generate a new incrementor. 4608 * @return int The timestamp representing 'last_changed'. 4609 */ 4610 function wp_get_last_changed( $group, $force = false ) { 4611 $last_changed = wp_cache_get( 'last_changed', $group ); 4612 if ( ! $last_changed || true === $force ) { 4613 $last_changed = microtime(); 4614 wp_cache_set( 'last_changed', $last_changed, $group ); 4615 } 4616 return $last_changed; 4617 } 4618 No newline at end of file -
src/wp-includes/taxonomy.php
962 962 return $error; 963 963 } 964 964 965 $group = $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, $group ); 969 wp_cache_add( "name:" . md5( $term->name ), $term->term_id, $group ); 967 970 $_term = $term; 968 971 } else { 969 972 if ( is_object($term) ) … … 974 977 $_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 978 if ( ! $_term ) 976 979 return null; 977 wp_cache_add($term, $_term, $taxonomy); 980 wp_cache_add( $term, $_term, $taxonomy ); 981 wp_cache_add( "slug:{$_term->slug}", $term, $group ); 982 wp_cache_add( "name:" . md5( $_term->name ), $term, $group ); 978 983 } 979 984 } 980 985 … … 1046 1051 if ( ! taxonomy_exists($taxonomy) ) 1047 1052 return false; 1048 1053 1054 $cache = false; 1055 $group = $taxonomy . ':' . wp_get_last_changed( 'terms' ); 1049 1056 if ( 'slug' == $field ) { 1050 1057 $field = 't.slug'; 1051 1058 $value = sanitize_title($value); 1052 1059 if ( empty($value) ) 1053 1060 return false; 1061 1062 $term_id = wp_cache_get( "slug:{$value}", $group ); 1063 if ( $term_id ) { 1064 $value = $term_id; 1065 $cache = true; 1066 } 1054 1067 } else if ( 'name' == $field ) { 1055 1068 // Assume already escaped 1056 1069 $value = wp_unslash($value); 1057 1070 $field = 't.name'; 1071 $term_id = wp_cache_get( "name:" . md5( $value ), $group ); 1072 if ( $term_id ) { 1073 $value = $term_id; 1074 $cache = true; 1075 } 1058 1076 } else if ( 'term_taxonomy_id' == $field ) { 1059 1077 $value = (int) $value; 1060 1078 $field = 'tt.term_taxonomy_id'; 1061 1079 } else { 1080 $cache = true; 1081 } 1082 1083 if ( $cache ) { 1062 1084 $term = get_term( (int) $value, $taxonomy, $output, $filter); 1063 1085 if ( is_wp_error( $term ) ) 1064 1086 $term = false; … … 1069 1091 if ( !$term ) 1070 1092 return false; 1071 1093 1072 wp_cache_add($term->term_id, $term, $taxonomy); 1094 wp_cache_add( $term->term_id, $term, $taxonomy ); 1095 wp_cache_add( "slug:{$term->slug}", $term->term_id, $group ); 1096 wp_cache_add( "name:" . md5( $term->name ), $term->term_id, $group ); 1073 1097 1074 1098 /** This filter is documented in wp-includes/taxonomy.php */ 1075 1099 $term = apply_filters( 'get_term', $term, $taxonomy ); … … 3407 3431 if ( empty($term_taxonomy) ) 3408 3432 $term_taxonomy = $term->taxonomy; 3409 3433 3410 wp_cache_add($term->term_id, $term, $term_taxonomy); 3434 wp_cache_add( $term->term_id, $term, $term_taxonomy ); 3435 $group = $term_taxonomy . ':' . wp_get_last_changed( 'terms', true ); 3436 wp_cache_add( "slug:{$term->slug}", $term->term_id, $group ); 3437 wp_cache_add( "name:" . md5( $term->name ), $term->term_id, $group ); 3411 3438 } 3412 3439 } 3413 3440 -
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 } 135 136 /** 137 * @ticket 21760 138 */ 139 function test_get_term_by_name_cache() { 140 global $wpdb; 141 $term_id = $this->factory->term->create( array( 'name' => 'burrito', 'taxonomy' => 'post_tag' ) ); 142 143 $queries = $wpdb->num_queries; 144 get_term_by( 'name', 'burrito', 'post_tag' ); 145 $initial = $queries + 1; 146 $this->assertEquals( $initial, $wpdb->num_queries ); 147 $term = get_term_by( 'name', 'burrito', 'post_tag' ); 148 $this->assertEquals( $initial, $wpdb->num_queries ); 149 150 $this->assertEquals( get_term( $term_id, 'post_tag' ), $term ); 151 $this->assertEquals( $initial, $wpdb->num_queries ); 152 } 153 154 /** 155 * @ticket 21760 156 */ 157 function test_get_term_by_name_cache_update() { 158 global $wpdb; 159 $term_id = $this->factory->term->create( array( 'name' => 'burrito', 'taxonomy' => 'post_tag' ) ); 160 161 $queries = $wpdb->num_queries; 162 get_term_by( 'name', 'burrito', 'post_tag' ); 163 $initial = $queries + 1; 164 $this->assertEquals( $initial, $wpdb->num_queries ); 165 $term = get_term_by( 'name', 'burrito', 'post_tag' ); 166 $this->assertEquals( $initial, $wpdb->num_queries ); 167 168 wp_update_term( $term_id, 'post_tag', array( 'slug' => 'Taco' ) ); 169 $this->assertNotEquals( $term, get_term( $term_id, 'post_tag' ) ); 170 $after_queries = $wpdb->num_queries; 171 get_term_by( 'name', 'burrito', 'post_tag' ); 172 $this->assertEquals( $after_queries, $wpdb->num_queries ); 173 } 96 174 } 175 No newline at end of file