From c4c54f035c9cb3aad5b9391973a5ce3357c0ffe7 Mon Sep 17 00:00:00 2001 From: spacedmonkey Date: Thu, 21 Sep 2023 16:36:20 +0000 Subject: [PATCH] Taxonomy: Stop double sanitization in get_term function. In the `get_term` function, the filter method is invoked on the `WP_Term` object, which subsequently triggers the execution of `sanitize_term`. The filter method is also executed within `WP_Term::get_instance`. A common scenario when calling the `get_term` function is to invoke the function with an integer ID for the term and a filter set to "raw." This results in a call to `WP_Term::get_instance`. However, since both `get_term` and `WP_Term::get_instance` invoke the filter method, it leads to double sanitization of the term. Considering that `get_term` may be called thousands of times on a page, especially when priming a large number of terms into memory, this redundancy can result in thousands of unnecessary calls to `sanitize_term`. Performing the same sanitization operation twice with the same parameters is wasteful and detrimental to performance. To address this issue, the code has been updated to execute the filter method only when the filter parameter does not match or when changes have been made to the term object within the get_term hook. This optimization ensures that the filter is applied selectively, mitigating performance concerns and avoiding unnecessary sanitization calls. Props spacedmonkey, flixos90, costdev, mukesh27, joemcgill, oglekler, peterwilsoncc. Fixes #58329. Built from https://develop.svn.wordpress.org/trunk@56650 git-svn-id: http://core.svn.wordpress.org/trunk@56162 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/taxonomy.php | 5 ++++- wp-includes/version.php | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/wp-includes/taxonomy.php b/wp-includes/taxonomy.php index b0d053f648..be1faaeba5 100644 --- a/wp-includes/taxonomy.php +++ b/wp-includes/taxonomy.php @@ -980,6 +980,7 @@ function get_term( $term, $taxonomy = '', $output = OBJECT, $filter = 'raw' ) { // Ensure for filters that this is not empty. $taxonomy = $_term->taxonomy; + $old_term = $_term; /** * Filters a taxonomy term object. * @@ -1019,7 +1020,9 @@ function get_term( $term, $taxonomy = '', $output = OBJECT, $filter = 'raw' ) { } // Sanitize term, according to the specified filter. - $_term->filter( $filter ); + if ( $_term !== $old_term || $_term->filter !== $filter ) { + $_term->filter( $filter ); + } if ( ARRAY_A === $output ) { return $_term->to_array(); diff --git a/wp-includes/version.php b/wp-includes/version.php index 1b7ea47ecb..0287fdfa95 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -16,7 +16,7 @@ * * @global string $wp_version */ -$wp_version = '6.4-alpha-56649'; +$wp_version = '6.4-alpha-56650'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.