From 5866165adf17d1b537c93a2a8a64c04d45401c70 Mon Sep 17 00:00:00 2001 From: Jonny Harris Date: Fri, 29 Apr 2022 12:34:35 +0000 Subject: [PATCH] =?UTF-8?q?Taxonomy:=20Increase=20cache=20hits=20in=C2=A0`?= =?UTF-8?q?WP=5FTerm=5FQuery`=20when=20using=20include=20and=20exclude=20p?= =?UTF-8?q?arameters.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Ensure that empty values of include and exclude passed to the parameters of `WP_Term_Query`, reused existing caches by resetting values to an empty array. Props Spacedmonkey, peterwilsoncc, hellofromtonya. Follow-up to [52970]. See #55352. git-svn-id: https://develop.svn.wordpress.org/trunk@53309 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/class-wp-term-query.php | 8 +++ tests/phpunit/tests/term/getTerms.php | 70 +++++++++++++++++++++++++ 2 files changed, 78 insertions(+) diff --git a/src/wp-includes/class-wp-term-query.php b/src/wp-includes/class-wp-term-query.php index 45d45927a0..8ff0028ecf 100644 --- a/src/wp-includes/class-wp-term-query.php +++ b/src/wp-includes/class-wp-term-query.php @@ -457,6 +457,14 @@ class WP_Term_Query { "tt.taxonomy IN ('" . implode( "', '", array_map( 'esc_sql', $taxonomies ) ) . "')"; } + if ( empty( $args['exclude'] ) ) { + $args['exclude'] = array(); + } + + if ( empty( $args['include'] ) ) { + $args['include'] = array(); + } + $exclude = $args['exclude']; $exclude_tree = $args['exclude_tree']; $include = $args['include']; diff --git a/tests/phpunit/tests/term/getTerms.php b/tests/phpunit/tests/term/getTerms.php index b1901886a8..41f6758045 100644 --- a/tests/phpunit/tests/term/getTerms.php +++ b/tests/phpunit/tests/term/getTerms.php @@ -3146,6 +3146,76 @@ class Tests_Term_getTerms extends WP_UnitTestCase { 'term_taxonomy_id' => array(), ), ), + 'array exclude vs no exclude' => array( + array( + 'taxonomy' => self::$taxonomy, + 'fields' => 'ids', + ), + array( + 'taxonomy' => self::$taxonomy, + 'fields' => 'all', + 'exclude' => array(), + ), + ), + 'array exclude vs zero exclude' => array( + array( + 'taxonomy' => self::$taxonomy, + 'fields' => 'ids', + 'exclude' => 0, + ), + array( + 'taxonomy' => self::$taxonomy, + 'fields' => 'all', + 'exclude' => array(), + ), + ), + 'array exclude vs string exclude' => array( + array( + 'taxonomy' => self::$taxonomy, + 'fields' => 'ids', + 'exclude' => '', + ), + array( + 'taxonomy' => self::$taxonomy, + 'fields' => 'all', + 'exclude' => array(), + ), + ), + 'array include vs no include' => array( + array( + 'taxonomy' => self::$taxonomy, + 'fields' => 'ids', + ), + array( + 'taxonomy' => self::$taxonomy, + 'fields' => 'all', + 'include' => array(), + ), + ), + 'array include vs zero include' => array( + array( + 'taxonomy' => self::$taxonomy, + 'fields' => 'ids', + 'include' => 0, + ), + array( + 'taxonomy' => self::$taxonomy, + 'fields' => 'all', + 'include' => array(), + ), + ), + 'array include vs string include' => array( + array( + 'taxonomy' => self::$taxonomy, + 'fields' => 'ids', + 'include' => '', + ), + array( + 'taxonomy' => self::$taxonomy, + 'fields' => 'all', + 'include' => array(), + ), + ), 'array 1 slug vs string slug' => array( array( 'taxonomy' => self::$taxonomy,