Make WordPress Core


Ignore:
Timestamp:
09/21/2023 04:34:59 PM (18 months ago)
Author:
spacedmonkey
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/taxonomy.php

    r56642 r56650  
    981981    $taxonomy = $_term->taxonomy;
    982982
     983    $old_term = $_term;
    983984    /**
    984985     * Filters a taxonomy term object.
     
    10201021
    10211022    // Sanitize term, according to the specified filter.
    1022     $_term->filter( $filter );
     1023    if ( $_term !== $old_term || $_term->filter !== $filter ) {
     1024        $_term->filter( $filter );
     1025    }
    10231026
    10241027    if ( ARRAY_A === $output ) {
Note: See TracChangeset for help on using the changeset viewer.