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
This commit is contained in:
spacedmonkey 2023-09-21 16:36:20 +00:00
parent 6bf6244dc7
commit c4c54f035c
2 changed files with 5 additions and 2 deletions

View File

@ -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();

View File

@ -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.