Ticket #21760: 21760.14.diff
File 21760.14.diff, 10.0 KB (added by , 6 years ago) |
---|
-
src/wp-includes/taxonomy.php
diff --git src/wp-includes/taxonomy.php src/wp-includes/taxonomy.php index 1216d36..f407621 100644
function get_term($term, $taxonomy, $output = OBJECT, $filter = 'raw') { 1305 1305 return $error; 1306 1306 } 1307 1307 1308 $incrementor = wp_cache_get( 'last_changed', 'terms' );1309 1308 if ( is_object($term) && empty($term->filter) ) { 1310 wp_cache_add( $term->term_id, $term, $taxonomy . ':terms:' . $incrementor ); 1311 wp_cache_add( $term->slug, $term->term_id, $taxonomy . ':slugs:' . $incrementor ); 1312 wp_cache_add( $term->name, $term->term_id, $taxonomy . ':names:' . $incrementor ); 1309 wp_cache_add( $term->term_id, $term, $taxonomy ); 1313 1310 $_term = $term; 1314 1311 } else { 1315 1312 if ( is_object($term) ) 1316 1313 $term = $term->term_id; 1317 1314 if ( !$term = (int) $term ) 1318 1315 return null; 1319 if ( ! $_term = wp_cache_get( $term, $taxonomy . ':terms:' . $incrementor) ) {1316 if ( ! $_term = wp_cache_get( $term, $taxonomy ) ) { 1320 1317 $_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) ); 1321 1318 if ( ! $_term ) 1322 1319 return null; 1323 wp_cache_add( $term, $_term, $taxonomy . ':terms:' . $incrementor ); 1324 wp_cache_add( $_term->slug, $term, $taxonomy . ':slugs:' . $incrementor ); 1325 wp_cache_add( $_term->name, $term, $taxonomy . ':names:' . $incrementor ); 1320 wp_cache_add( $term, $_term, $taxonomy ); 1326 1321 } 1327 1322 } 1328 1323 … … function get_term_by($field, $value, $taxonomy, $output = OBJECT, $filter = 'raw 1393 1388 if ( ! taxonomy_exists($taxonomy) ) 1394 1389 return false; 1395 1390 1396 $cache = false;1397 $incrementor = wp_cache_get( 'last_changed', 'terms' );1398 1391 if ( 'slug' == $field ) { 1399 1392 $field = 't.slug'; 1400 1393 $value = sanitize_title($value); 1401 1394 if ( empty($value) ) 1402 1395 return false; 1403 1404 $term_id = wp_cache_get( $value, $taxonomy . ':slugs:' . $incrementor );1405 if ( $term_id ) {1406 $value = $term_id;1407 $cache = true;1408 }1409 1396 } else if ( 'name' == $field ) { 1410 1397 // Assume already escaped 1411 1398 $value = wp_unslash($value); 1412 1399 $field = 't.name'; 1413 $term_id = wp_cache_get( $value, $taxonomy . ':names:' . $incrementor );1414 if ( $term_id ) {1415 $value = $term_id;1416 $cache = true;1417 }1418 1400 } else if ( 'term_taxonomy_id' == $field ) { 1419 1401 $value = (int) $value; 1420 1402 $field = 'tt.term_taxonomy_id'; 1421 1403 } else { 1422 $cache = true; 1423 } 1424 1425 if ( $cache ) { 1426 $term = get_term( (int) $value, $taxonomy, $output, $filter); 1427 if ( is_wp_error( $term ) ) { 1404 $term = get_term( (int) $value, $taxonomy, $output, $filter ); 1405 if ( is_wp_error( $term ) ) 1428 1406 $term = false; 1429 } 1430 } else { 1431 $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 $field = %s LIMIT 1", $taxonomy, $value) ); 1407 return $term; 1432 1408 } 1433 1409 1434 if ( !$term ) 1410 $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 $field = %s LIMIT 1", $taxonomy, $value ) ); 1411 if ( ! $term ) 1435 1412 return false; 1436 1413 1414 wp_cache_add( $term->term_id, $term, $taxonomy ); 1415 1437 1416 /** This filter is documented in wp-includes/taxonomy.php */ 1438 1417 $term = apply_filters( 'get_term', $term, $taxonomy ); 1439 1418 … … function get_term_by($field, $value, $taxonomy, $output = OBJECT, $filter = 'raw 1442 1421 1443 1422 $term = sanitize_term($term, $taxonomy, $filter); 1444 1423 1445 wp_cache_add( $term->term_id, $term, $taxonomy . ':terms:' . $incrementor );1446 wp_cache_add( $term->slug, $term->term_id, $taxonomy . ':slugs:' . $incrementor );1447 wp_cache_add( $term->name, $term->term_id, $taxonomy . ':names:' . $incrementor );1448 1449 1424 if ( $output == OBJECT ) { 1450 1425 return $term; 1451 1426 } elseif ( $output == ARRAY_A ) { … … function clean_object_term_cache($object_ids, $object_type) { 3649 3624 * @param bool $clean_taxonomy Whether to clean taxonomy wide caches (true), or just individual term object caches (false). Default is true. 3650 3625 */ 3651 3626 function clean_term_cache($ids, $taxonomy = '', $clean_taxonomy = true) { 3652 global $_wp_suspend_cache_invalidation, $wpdb; 3653 3654 if ( ! empty( $_wp_suspend_cache_invalidation ) ) { 3655 return; 3656 } 3627 global $wpdb; 3657 3628 3658 3629 if ( !is_array($ids) ) 3659 3630 $ids = array($ids); … … function update_object_term_cache($object_ids, $object_type) { 3792 3763 * @param string $taxonomy Optional. Update Term to this taxonomy in cache 3793 3764 */ 3794 3765 function update_term_cache($terms, $taxonomy = '') { 3795 global $_wp_suspend_cache_invalidation;3796 3797 if ( ! empty( $_wp_suspend_cache_invalidation ) ) {3798 return;3799 }3800 3801 3766 foreach ( (array) $terms as $term ) { 3802 3767 $term_taxonomy = $taxonomy; 3803 3768 if ( empty($term_taxonomy) ) 3804 3769 $term_taxonomy = $term->taxonomy; 3805 3770 3806 $incrementor = wp_cache_set( 'last_changed', microtime(), 'terms' ); 3807 3808 wp_cache_add( $term->term_id, $term, $term_taxonomy . ':terms:' . $incrementor ); 3809 wp_cache_add( $term->slug, $term->term_id, $taxonomy . ':slugs:' . $incrementor ); 3810 wp_cache_add( $term->name, $term->term_id, $taxonomy . ':names:' . $incrementor ); 3771 wp_cache_add( $term->term_id, $term, $term_taxonomy ); 3811 3772 } 3812 3773 } 3813 3774 -
tests/phpunit/tests/term/cache.php
diff --git tests/phpunit/tests/term/cache.php tests/phpunit/tests/term/cache.php index 9e922f3..d424353 100644
class Tests_Term_Cache extends WP_UnitTestCase { 93 93 94 94 _unregister_taxonomy( $tax ); 95 95 } 96 97 /**98 * @ticket 2176099 */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( $term, wp_cache_get( $term_id, 'post_tag:terms:' . wp_cache_get( 'last_changed', 'terms' ) ) );112 113 $this->assertEquals( get_term( $term_id, 'post_tag' ), $term );114 $this->assertEquals( $initial, $wpdb->num_queries );115 }116 117 /**118 * @ticket 21760119 */120 function test_get_term_by_slug_cache_update() {121 global $wpdb;122 $term_id = $this->factory->term->create( array( 'slug' => 'burrito', 'taxonomy' => 'post_tag' ) );123 124 $queries = $wpdb->num_queries;125 get_term_by( 'slug', 'burrito', 'post_tag' );126 $initial = $queries + 1;127 $this->assertEquals( $initial, $wpdb->num_queries );128 $term = get_term_by( 'slug', 'burrito', 'post_tag' );129 $this->assertEquals( $initial, $wpdb->num_queries );130 131 $this->assertEquals( $term, wp_cache_get( $term_id, 'post_tag:terms:' . wp_cache_get( 'last_changed', 'terms' ) ) );132 133 wp_update_term( $term_id, 'post_tag', array( 'name' => 'Taco' ) );134 $this->assertNotEquals( $term, get_term( $term_id, 'post_tag' ) );135 $after_queries = $wpdb->num_queries;136 get_term_by( 'slug', 'burrito', 'post_tag' );137 $this->assertEquals( $after_queries, $wpdb->num_queries );138 }139 140 /**141 * @ticket 21760142 */143 function test_get_term_by_name_cache() {144 global $wpdb;145 $term_id = $this->factory->term->create( array( 'name' => 'burrito', 'taxonomy' => 'post_tag' ) );146 147 $queries = $wpdb->num_queries;148 get_term_by( 'name', 'burrito', 'post_tag' );149 $initial = $queries + 1;150 $this->assertEquals( $initial, $wpdb->num_queries );151 $term = get_term_by( 'name', 'burrito', 'post_tag' );152 $this->assertEquals( $initial, $wpdb->num_queries );153 154 $this->assertEquals( get_term( $term_id, 'post_tag' ), $term );155 $this->assertEquals( $initial, $wpdb->num_queries );156 }157 158 /**159 * @ticket 21760160 */161 function test_get_term_by_name_cache_update() {162 global $wpdb;163 $term_id = $this->factory->term->create( array( 'name' => 'burrito', 'taxonomy' => 'post_tag' ) );164 165 $queries = $wpdb->num_queries;166 get_term_by( 'name', 'burrito', 'post_tag' );167 $initial = $queries + 1;168 $this->assertEquals( $initial, $wpdb->num_queries );169 $term = get_term_by( 'name', 'burrito', 'post_tag' );170 $this->assertEquals( $initial, $wpdb->num_queries );171 172 wp_update_term( $term_id, 'post_tag', array( 'slug' => 'Taco' ) );173 $this->assertNotEquals( $term, get_term( $term_id, 'post_tag' ) );174 $after_queries = $wpdb->num_queries;175 get_term_by( 'name', 'burrito', 'post_tag' );176 $this->assertEquals( $after_queries, $wpdb->num_queries );177 }178 179 /**180 * @ticket 21760181 */182 function test_invalidating_term_caches_should_fail_when_invalidation_is_suspended() {183 $slug = 'taco';184 $name = 'Taco';185 $taxonomy = 'post_tag';186 $cache_key_slug = $slug;187 $cache_key_name = $name;188 189 $term_id = $this->factory->term->create( array( 'slug' => $slug, 'name' => $name, 'taxonomy' => $taxonomy ) );190 191 $last_changed = wp_cache_get( 'last_changed', 'terms' );192 193 $term = get_term_by( 'slug', $slug, $taxonomy );194 195 // Verify the term is cached by ID, slug and name196 $this->assertEquals( $term, wp_cache_get( $term_id, $taxonomy . ':terms:' . wp_cache_get( 'last_changed', 'terms' ) ) );197 $this->assertSame( $term_id, wp_cache_get( $cache_key_slug, $taxonomy . ':slugs:' . wp_cache_get( 'last_changed', 'terms' ) ) );198 $this->assertSame( $term_id, wp_cache_get( $cache_key_name, $taxonomy . ':names:' . wp_cache_get( 'last_changed', 'terms' ) ) );199 200 $suspend = wp_suspend_cache_invalidation();201 clean_term_cache( $term_id, $taxonomy );202 203 // Verify that the cached value still matches the correct value204 $this->assertEquals( $term, wp_cache_get( $term_id, $taxonomy . ':terms:' . wp_cache_get( 'last_changed', 'terms' ) ) );205 $this->assertSame( $term_id, wp_cache_get( $cache_key_slug, $taxonomy . ':slugs:' . wp_cache_get( 'last_changed', 'terms' ) ) );206 $this->assertSame( $term_id, wp_cache_get( $cache_key_name, $taxonomy . ':names:' . wp_cache_get( 'last_changed', 'terms' ) ) );207 208 // Verify that last changed has not been updated as part of an invalidation routine209 $this->assertSame( $last_changed, wp_cache_get( 'last_changed', 'terms' ) );210 211 // Clean up.212 wp_suspend_cache_invalidation( $suspend );213 }214 96 }