mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-08-12 04:40:26 +00:00
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. git-svn-id: https://develop.svn.wordpress.org/trunk@56650 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
@@ -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();
|
||||
|
||||
@@ -308,4 +308,74 @@ class Tests_Term extends WP_UnitTestCase {
|
||||
$cat_id2 = self::factory()->category->create( array( 'parent' => $cat_id1 ) );
|
||||
$this->assertWPError( $cat_id2 );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 58329
|
||||
*
|
||||
* @covers ::get_term
|
||||
*
|
||||
*/
|
||||
public function test_get_term_sanitize_once() {
|
||||
$cat_id1 = self::factory()->category->create();
|
||||
$_term = get_term( $cat_id1, '', OBJECT, 'edit' );
|
||||
|
||||
$filter = new MockAction();
|
||||
add_filter( 'edit_term_slug', array( $filter, 'filter' ) );
|
||||
|
||||
$term = get_term( $_term, '', OBJECT, 'edit' );
|
||||
|
||||
$this->assertSame( 0, $filter->get_call_count(), 'The term was filtered more than once' );
|
||||
$this->assertSame( $_term, $term, 'Both terms should match' );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 58329
|
||||
*
|
||||
* @covers ::get_term
|
||||
*
|
||||
* @dataProvider data_get_term_filter
|
||||
*
|
||||
* @param string $filter How to sanitize term fields.
|
||||
*/
|
||||
public function test_get_term_should_set_term_filter_property_to_filter_argument( $filter ) {
|
||||
$cat_id1 = self::factory()->category->create();
|
||||
|
||||
$term = get_term( $cat_id1, '', OBJECT, $filter );
|
||||
|
||||
$this->assertSame( $filter, $term->filter, "The term's 'filter' property should be set to '$filter'." );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 58329
|
||||
*
|
||||
* @covers ::get_term
|
||||
*
|
||||
* @dataProvider data_get_term_filter
|
||||
*
|
||||
* @param string $filter How to sanitize term fields.
|
||||
*/
|
||||
public function test_get_term_filtered( $filter ) {
|
||||
$cat_id1 = self::factory()->category->create();
|
||||
$cat = self::factory()->category->create_and_get();
|
||||
add_filter(
|
||||
'get_term',
|
||||
static function () use ( $cat ) {
|
||||
return $cat;
|
||||
}
|
||||
);
|
||||
|
||||
$term = get_term( $cat_id1, '', OBJECT, $filter );
|
||||
|
||||
$this->assertSame( $filter, $term->filter, "The term's 'filter' property should be set to '$filter'." );
|
||||
$this->assertSame( $term, $cat, 'The returned term should match the filtered term' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Data provider.
|
||||
*
|
||||
* @return array[]
|
||||
*/
|
||||
public function data_get_term_filter() {
|
||||
return self::text_array_to_dataprovider( array( 'edit', 'db', 'display', 'attribute', 'js', 'rss', 'raw' ) );
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user