diff --git a/src/wp-includes/taxonomy.php b/src/wp-includes/taxonomy.php index b0d053f648..be1faaeba5 100644 --- a/src/wp-includes/taxonomy.php +++ b/src/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/tests/phpunit/tests/term.php b/tests/phpunit/tests/term.php index 09235e25b9..2c1e58d2ec 100644 --- a/tests/phpunit/tests/term.php +++ b/tests/phpunit/tests/term.php @@ -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' ) ); + } }