Skip to content

Commit 7447c82

Browse files
committed
Taxonomy: Bypass clearing taxonomy hierarchy for non-hierarchical taxonomies.
Modifies `clean_taxonomy_cache()` to bypass attempting to delete the `{$taxonomy}_children` option for non-hierarchical taxonomies, such as tags. This removes a needless database query within `delete_option()` to determine if the `{$taxonomy}_children` option is set. It isn't. Props: chouby, mukesh27, rollybueno, sabernhardt, shailu25, spacedmonkey, westonruter. Fixes #64090. git-svn-id: https://develop.svn.wordpress.org/trunk@60933 602fd350-edb4-49c9-b593-d223f7449a82
1 parent ce984ff commit 7447c82

File tree

2 files changed

+78
-2
lines changed

2 files changed

+78
-2
lines changed

src/wp-includes/taxonomy.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3732,8 +3732,10 @@ function clean_taxonomy_cache( $taxonomy ) {
37323732
wp_cache_set_terms_last_changed();
37333733

37343734
// Regenerate cached hierarchy.
3735-
delete_option( "{$taxonomy}_children" );
3736-
_get_term_hierarchy( $taxonomy );
3735+
if ( is_taxonomy_hierarchical( $taxonomy ) ) {
3736+
delete_option( "{$taxonomy}_children" );
3737+
_get_term_hierarchy( $taxonomy );
3738+
}
37373739

37383740
/**
37393741
* Fires after a taxonomy's caches have been cleaned.
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
/**
4+
* @group taxonomy
5+
*
6+
* @covers ::clean_taxonomy_cache
7+
*/
8+
class Tests_Taxonomy_CleanTaxonomyCache extends WP_UnitTestCase {
9+
/**
10+
* Ensure clearing the cache of a non-hierarchical taxonomy doesn't attempt to delete the hierarchy cache.
11+
*
12+
* @ticket 64090
13+
*/
14+
public function test_hierarchy_cache_not_cleared_for_non_hierarchical_taxonomy() {
15+
// Prime the tag cache by inserting terms.
16+
wp_insert_term( 'foo', 'post_tag' );
17+
18+
/*
19+
* Determine if delete_option( "{$taxonomy}_children" ) is called.
20+
*
21+
* None of the actions in delete_option() or _get_term_hierarchy() fire for
22+
* non-hierarchical taxonomies, so the query filter needs to be used to determine
23+
* if an attempt to delete the option was made.
24+
*/
25+
$delete_post_tag_children_called = false;
26+
add_filter(
27+
'query',
28+
static function ( $query ) use ( &$delete_post_tag_children_called ) {
29+
global $wpdb;
30+
if ( "SELECT autoload FROM $wpdb->options WHERE option_name = 'post_tag_children'" === $query ) {
31+
$delete_post_tag_children_called = true;
32+
}
33+
return $query;
34+
}
35+
);
36+
37+
clean_taxonomy_cache( 'post_tag' );
38+
39+
$this->assertFalse( $delete_post_tag_children_called, 'An attempt to clear the post_tag_children option should not be made.' );
40+
}
41+
42+
/**
43+
* Ensure clearing the cache of a hierarchical taxonomy does attempt to delete the hierarchy cache.
44+
*
45+
* @ticket 64090
46+
*/
47+
public function test_hierarchy_cache_cleared_for_hierarchical_taxonomy() {
48+
// Prime the category cache by inserting terms.
49+
wp_insert_term( 'foo', 'category' );
50+
51+
/*
52+
* Determine if delete_option( "{$taxonomy}_children" ) is called.
53+
*
54+
* None of the actions in delete_option() or _get_term_hierarchy() fire for
55+
* non-hierarchical taxonomies, so the query filter needs to be used to determine
56+
* if an attempt to delete the option was made.
57+
*/
58+
$delete_category_children_called = false;
59+
add_filter(
60+
'query',
61+
static function ( $query ) use ( &$delete_category_children_called ) {
62+
global $wpdb;
63+
if ( "SELECT autoload FROM $wpdb->options WHERE option_name = 'category_children'" === $query ) {
64+
$delete_category_children_called = true;
65+
}
66+
return $query;
67+
}
68+
);
69+
70+
clean_taxonomy_cache( 'category' );
71+
72+
$this->assertTrue( $delete_category_children_called, 'An attempt to clear the category_children option should be made.' );
73+
}
74+
}

0 commit comments

Comments
 (0)