diff --git a/src/wp-includes/class-wp-term.php b/src/wp-includes/class-wp-term.php index a9a58da875e4a..476164e97979e 100644 --- a/src/wp-includes/class-wp-term.php +++ b/src/wp-includes/class-wp-term.php @@ -190,10 +190,12 @@ public static function get_instance( $term_id, $taxonomy = null ) { } } - $term_obj = new WP_Term( $_term ); - $term_obj->filter( $term_obj->filter ); + if ( empty( $_term->filter ) ) { + $_term = sanitize_term( $_term, $_term->taxonomy, 'raw' ); + wp_cache_replace( $term_id, $_term, 'terms' ); + } - return $term_obj; + return new WP_Term( $_term ); } /** @@ -215,9 +217,22 @@ public function __construct( $term ) { * @since 4.4.0 * * @param string $filter Filter context. Accepts 'edit', 'db', 'display', 'attribute', 'js', 'rss', or 'raw'. + * @return WP_Term Sanitized term object. */ public function filter( $filter ) { - sanitize_term( $this, $this->taxonomy, $filter ); + if ( $this->filter === $filter ) { + return $this; + } + + if ( 'raw' === $filter ) { + $raw = self::get_instance( $this->term_id, $this->taxonomy ); + if ( $raw instanceof WP_Term ) { + return $raw; + } + return $this; + } + + return sanitize_term( $this, $this->taxonomy, $filter ); } /** diff --git a/src/wp-includes/taxonomy.php b/src/wp-includes/taxonomy.php index 8a64e726d2fcd..a82b1e6bba858 100644 --- a/src/wp-includes/taxonomy.php +++ b/src/wp-includes/taxonomy.php @@ -1023,6 +1023,11 @@ function get_term( $term, $taxonomy = '', $output = OBJECT, $filter = 'raw' ) { return null; } + // Restore raw data before applying hooks so their changes are preserved. + if ( 'raw' === $filter && 'raw' !== $_term->filter ) { + $_term = $_term->filter( $filter ); + } + // Ensure for filters that this is not empty. $taxonomy = $_term->taxonomy; @@ -1067,7 +1072,7 @@ function get_term( $term, $taxonomy = '', $output = OBJECT, $filter = 'raw' ) { // Sanitize term, according to the specified filter. if ( $_term !== $old_term || $_term->filter !== $filter ) { - $_term->filter( $filter ); + $_term = sanitize_term( $_term, $_term->taxonomy, $filter ); } if ( ARRAY_A === $output ) { @@ -1807,6 +1812,16 @@ function sanitize_term( $term, $taxonomy, $context = 'display' ) { $do_object = is_object( $term ); + if ( 'raw' !== $context ) { + if ( $do_object ) { + if ( isset( $term->filter ) && $context === $term->filter ) { + return $term; + } + } elseif ( isset( $term['filter'] ) && $context === $term['filter'] ) { + return $term; + } + } + $term_id = $do_object ? ( $term->term_id ?? 0 ) : ( $term['term_id'] ?? 0 ); foreach ( (array) $fields as $field ) { @@ -2633,6 +2648,8 @@ function wp_insert_term( $term, $taxonomy, $args = array() ) { // Coerce null description to strings, to avoid database errors. $args['description'] = (string) $args['description']; + unset( $args['filter'] ); + $args = sanitize_term( $args, $taxonomy, 'db' ); // expected_slashed ($name) @@ -3424,6 +3441,8 @@ function wp_update_term( $term_id, $taxonomy, $args = array() ) { // Merge old and new args with new args overwriting old ones. $args = array_merge( $term, $args ); + unset( $args['filter'] ); + $defaults = array( 'alias_of' => '', 'description' => '', @@ -4064,8 +4083,8 @@ function update_object_term_cache( $object_ids, $object_type ) { * * @since 2.3.0 * - * @param WP_Term[] $terms Array of term objects to change. - * @param string $taxonomy Not used. + * @param WP_Term[]|stdClass[] $terms Array of term objects to change. + * @param string $taxonomy Not used. */ function update_term_cache( $terms, $taxonomy = '' ) { $data = array(); @@ -4073,6 +4092,11 @@ function update_term_cache( $terms, $taxonomy = '' ) { // Create a copy in case the array was passed by reference. $_term = clone $term; + // WP_Term's constructor can mark unnormalized database fields as raw. + if ( isset( $_term->filter ) && 'raw' === $_term->filter ) { + $_term = sanitize_term( $_term, $_term->taxonomy, 'raw' ); + } + // Object ID should not be cached. unset( $_term->object_id ); diff --git a/tests/phpstan/baselines/argument.type.neon b/tests/phpstan/baselines/argument.type.neon index dd184d5a12924..4cd9e775feb93 100644 --- a/tests/phpstan/baselines/argument.type.neon +++ b/tests/phpstan/baselines/argument.type.neon @@ -1183,11 +1183,6 @@ parameters: identifier: argument.type count: 1 path: ../../../src/wp-includes/taxonomy.php - - - message: '#^Parameter \#1 \$terms of function update_term_cache expects array\, list\\|null given\.$#' - identifier: argument.type - count: 1 - path: ../../../src/wp-includes/taxonomy.php - message: '#^Parameter \#1 \$terms of function wp_update_term_count expects array\|int, null given\.$#' identifier: argument.type diff --git a/tests/phpunit/tests/term/cache.php b/tests/phpunit/tests/term/cache.php index 89f7a68b3bd30..6d4072554e2fb 100644 --- a/tests/phpunit/tests/term/cache.php +++ b/tests/phpunit/tests/term/cache.php @@ -233,6 +233,67 @@ public function test_term_objects_should_not_be_modified_by_update_term_cache() } } + /** + * @ticket 50568 + */ + public function test_get_instance_should_prime_unfiltered_cached_term_as_raw() { + register_taxonomy( 'wptests_tax', 'post' ); + $term_id = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax' ) ); + $term = get_term( $term_id, 'wptests_tax' ); + + $this->assertInstanceOf( 'WP_Term', $term ); + + $term->filter = ''; + wp_cache_delete( $term_id, 'terms' ); + update_term_cache( array( $term ) ); + + $cached_term = wp_cache_get( $term_id, 'terms' ); + $this->assertSame( '', $cached_term->filter ); + + $found = WP_Term::get_instance( $term_id ); + + $this->assertInstanceOf( 'WP_Term', $found ); + $this->assertSame( 'raw', $found->filter ); + + $cached_term = wp_cache_get( $term_id, 'terms' ); + $this->assertSame( 'raw', $cached_term->filter ); + } + + /** + * @ticket 50568 + */ + public function test_update_term_cache_should_normalize_raw_integer_fields() { + $term_id = self::factory()->term->create( array( 'taxonomy' => 'category' ) ); + $term = new WP_Term( + (object) array( + 'term_id' => (string) $term_id, + 'taxonomy' => 'category', + 'term_taxonomy_id' => '123', + 'parent' => '0', + 'count' => '7', + 'term_group' => '0', + ) + ); + + wp_cache_delete( $term_id, 'terms' ); + update_term_cache( array( $term ) ); + + $cached_term = wp_cache_get( $term_id, 'terms' ); + $found = WP_Term::get_instance( $term_id ); + + foreach ( array( $cached_term, $found ) as $normalized_term ) { + $this->assertSame( $term_id, $normalized_term->term_id ); + $this->assertSame( 123, $normalized_term->term_taxonomy_id ); + $this->assertSame( 0, $normalized_term->parent ); + $this->assertSame( 7, $normalized_term->count ); + $this->assertSame( 0, $normalized_term->term_group ); + $this->assertSame( 'raw', $normalized_term->filter ); + } + + $this->assertSame( (string) $term_id, $term->term_id ); + $this->assertSame( '7', $term->count ); + } + /** * @ticket 21760 */ diff --git a/tests/phpunit/tests/term/getTerm.php b/tests/phpunit/tests/term/getTerm.php index 559f4fe393878..6a4dceddccbb0 100644 --- a/tests/phpunit/tests/term/getTerm.php +++ b/tests/phpunit/tests/term/getTerm.php @@ -180,6 +180,93 @@ public function test_numeric_properties_should_be_cast_to_ints() { } } + /** + * @ticket 50568 + */ + public function test_raw_filter_object_should_normalize_integer_fields() { + $term = new stdClass(); + $term->term_id = (string) self::$term->term_id; + $term->term_taxonomy_id = '123'; + $term->parent = '0'; + $term->count = '7'; + $term->term_group = '0'; + $term->filter = 'raw'; + $term->name = 'Test Term'; + $term->slug = 'test-term'; + $term->taxonomy = 'wptests_tax'; + + $found = get_term( $term, 'wptests_tax' ); + + $this->assertInstanceOf( 'WP_Term', $found ); + $this->assertSame( self::$term->term_id, $found->term_id ); + $this->assertSame( 123, $found->term_taxonomy_id ); + $this->assertSame( 0, $found->parent ); + $this->assertSame( 7, $found->count ); + $this->assertSame( 0, $found->term_group ); + $this->assertSame( 'raw', $found->filter ); + } + + /** + * @ticket 50568 + */ + public function test_display_filtered_term_should_be_returned_raw_when_raw_filter_is_requested() { + $display_term = get_term( self::$term->term_id, 'wptests_tax', OBJECT, 'display' ); + + $this->assertInstanceOf( 'WP_Term', $display_term ); + $this->assertSame( 'display', $display_term->filter ); + + $raw_term = get_term( $display_term, 'wptests_tax', OBJECT, 'raw' ); + + $this->assertInstanceOf( 'WP_Term', $raw_term ); + $this->assertSame( 'raw', $raw_term->filter ); + } + + /** + * @ticket 50568 + */ + public function test_raw_filter_should_preserve_hook_changes() { + foreach ( array( 'get_term', 'get_wptests_tax' ) as $hook ) { + $display_term = get_term( self::$term->term_id, 'wptests_tax', OBJECT, 'display' ); + $callback = static function ( $term ) { + $term->name = 'Filtered name'; + $term->extra_data = 'Hook data'; + return $term; + }; + + add_filter( $hook, $callback ); + $count = did_filter( $hook ); + $raw_term = get_term( $display_term, 'wptests_tax', OBJECT, 'raw' ); + remove_filter( $hook, $callback ); + + $this->assertSame( 'raw', $raw_term->filter ); + $this->assertSame( self::$term->description, $raw_term->description ); + $this->assertSame( 'Filtered name', $raw_term->name ); + $this->assertSame( 'Hook data', $raw_term->extra_data ); + $this->assertSame( $count + 1, did_filter( $hook ) ); + } + } + + /** + * @ticket 50568 + */ + public function test_raw_filter_should_preserve_a_term_replaced_by_a_hook() { + $callback = static function ( $term ) { + $term = clone $term; + $term->name = 'Filtered name'; + $term->extra_data = 'Hook data'; + $term->filter = 'display'; + return $term; + }; + + add_filter( 'get_term', $callback ); + $term = get_term( self::$term->term_id, 'wptests_tax' ); + remove_filter( 'get_term', $callback ); + + $this->assertSame( 'raw', $term->filter ); + $this->assertSame( 'Filtered name', $term->name ); + $this->assertSame( 'Hook data', $term->extra_data ); + } + /** * @ticket 34332 */ diff --git a/tests/phpunit/tests/term/sanitizeTerm.php b/tests/phpunit/tests/term/sanitizeTerm.php index 40762c37c104e..4cfefd597e7a3 100644 --- a/tests/phpunit/tests/term/sanitizeTerm.php +++ b/tests/phpunit/tests/term/sanitizeTerm.php @@ -119,4 +119,43 @@ public static function data_sanitize_term(): array { ), ); } + + /** + * @ticket 50568 + */ + public function test_sanitize_term_should_return_early_when_object_already_filtered_for_context() { + $term = new stdClass(); + $term->term_id = 1; + $term->name = 'Test'; + $term->slug = 'test'; + $term->taxonomy = 'category'; + $term->description = ''; + $term->filter = 'edit'; + + $count = did_filter( 'edit_term_name' ); + + $result = sanitize_term( $term, 'category', 'edit' ); + + $this->assertSame( $term, $result ); + $this->assertSame( $count, did_filter( 'edit_term_name' ), 'edit_term_name filter should not have fired.' ); + } + + /** + * @ticket 50568 + */ + public function test_sanitize_term_should_return_early_when_array_already_filtered_for_context() { + $term = array( + 'term_id' => 1, + 'name' => 'Test', + 'slug' => 'test', + 'filter' => 'display', + ); + + $count = did_filter( 'term_name' ); + + $result = sanitize_term( $term, 'category', 'display' ); + + $this->assertSame( $term, $result ); + $this->assertSame( $count, did_filter( 'term_name' ), 'term_name filter should not have fired.' ); + } } diff --git a/tests/phpunit/tests/term/wpInsertTerm.php b/tests/phpunit/tests/term/wpInsertTerm.php index 24a67e0fe5313..e52d112a631b1 100644 --- a/tests/phpunit/tests/term/wpInsertTerm.php +++ b/tests/phpunit/tests/term/wpInsertTerm.php @@ -56,6 +56,29 @@ public function test_wp_insert_term_taxonomy_does_not_exist() { $this->assertSame( 'invalid_taxonomy', $found->get_error_code() ); } + /** + * @ticket 50568 + */ + public function test_wp_insert_term_should_ignore_supplied_filter_context() { + $result = wp_insert_term( + 'Test term', + 'category', + array( + 'filter' => 'db', + 'slug' => 'Test Slug', + 'description' => 'Allowed', + ) + ); + + $this->assertNotWPError( $result ); + + $term = get_term( $result['term_id'], 'category' ); + + $this->assertSame( 'Test term', $term->name ); + $this->assertSame( 'test-slug', $term->slug ); + $this->assertSame( 'Allowed', $term->description ); + } + public function test_wp_insert_term_pre_insert_term_filter_returns_wp_error() { add_filter( 'pre_insert_term', array( $this, '_pre_insert_term_callback' ) ); $found = wp_insert_term( 'foo', 'post_tag' ); diff --git a/tests/phpunit/tests/term/wpTerm.php b/tests/phpunit/tests/term/wpTerm.php index 0eda866356514..e98fbba899664 100644 --- a/tests/phpunit/tests/term/wpTerm.php +++ b/tests/phpunit/tests/term/wpTerm.php @@ -5,16 +5,19 @@ */ class Tests_Term_WpTerm extends WP_UnitTestCase { protected static int $term_id; + protected static $shared_terms = array(); public function set_up() { parent::set_up(); register_taxonomy( 'wptests_tax', 'post' ); + register_taxonomy( 'wptests_tax_2', 'post' ); } public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) { global $wpdb; register_taxonomy( 'wptests_tax', 'post' ); + register_taxonomy( 'wptests_tax_2', 'post' ); // Ensure that there is a term with ID 1. if ( ! get_term( 1 ) ) { @@ -36,7 +39,44 @@ public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) { clean_term_cache( 1, 'wptests_tax' ); } - self::$term_id = $factory->term->create( array( 'taxonomy' => 'wptests_tax' ) ); + self::$term_id = $factory->term->create( array( 'taxonomy' => 'wptests_tax' ) ); + self::$shared_terms = self::generate_shared_terms(); + } + + /** + * Utility function for generating two shared terms, in the 'wptests_tax' and 'wptests_tax_2' taxonomies. + * + * @return array Array of term_id/old_term_id/term_taxonomy_id triplets. + */ + protected static function generate_shared_terms() { + global $wpdb; + + $term_1 = wp_insert_term( 'Foo', 'wptests_tax' ); + $term_2 = wp_insert_term( 'Foo', 'wptests_tax_2' ); + + // Manually modify because shared terms shouldn't naturally occur. + $wpdb->update( + $wpdb->term_taxonomy, + array( 'term_id' => $term_1['term_id'] ), + array( 'term_taxonomy_id' => $term_2['term_taxonomy_id'] ), + array( '%d' ), + array( '%d' ) + ); + + clean_term_cache( $term_1['term_id'] ); + + return array( + array( + 'term_id' => $term_1['term_id'], + 'old_term_id' => $term_1['term_id'], + 'term_taxonomy_id' => $term_1['term_taxonomy_id'], + ), + array( + 'term_id' => $term_1['term_id'], + 'old_term_id' => $term_2['term_id'], + 'term_taxonomy_id' => $term_2['term_taxonomy_id'], + ), + ); } /** @@ -90,6 +130,52 @@ public function test_get_instance_should_respect_taxonomy_when_term_id_is_found_ $this->assertFalse( $found ); } + /** + * @ticket 50568 + */ + public function test_filter_should_return_same_instance_when_context_matches() { + $term = WP_Term::get_instance( self::$term_id ); + + $this->assertSame( 'raw', $term->filter ); + $this->assertSame( $term, $term->filter( 'raw' ) ); + } + + /** + * @ticket 50568 + */ + public function test_filter_raw_should_return_raw_instance_from_display_filtered_term() { + $term = WP_Term::get_instance( self::$term_id ); + $display_term = $term->filter( 'display' ); + + $this->assertSame( 'display', $display_term->filter ); + + $raw_term = $display_term->filter( 'raw' ); + + $this->assertInstanceOf( 'WP_Term', $raw_term ); + $this->assertSame( 'raw', $raw_term->filter ); + $this->assertNotSame( $display_term, $raw_term ); + } + + /** + * @ticket 50568 + */ + public function test_filter_raw_should_use_taxonomy_to_disambiguate_shared_terms() { + $terms = self::$shared_terms; + + $display_term = get_term( $terms[0]['term_id'], 'wptests_tax', OBJECT, 'display' ); + + $this->assertInstanceOf( 'WP_Term', $display_term ); + $this->assertSame( 'display', $display_term->filter ); + $this->assertSame( 'wptests_tax', $display_term->taxonomy ); + + $raw_term = $display_term->filter( 'raw' ); + + $this->assertInstanceOf( 'WP_Term', $raw_term ); + $this->assertSame( 'raw', $raw_term->filter ); + $this->assertSame( 'wptests_tax', $raw_term->taxonomy ); + $this->assertSame( $terms[0]['term_taxonomy_id'], $raw_term->term_taxonomy_id ); + } + /** * Tests that a cached value which cannot be used as a term is treated as a cache miss. * diff --git a/tests/phpunit/tests/term/wpUpdateTerm.php b/tests/phpunit/tests/term/wpUpdateTerm.php index eff2dfbbe6852..7a5bec75af9e3 100644 --- a/tests/phpunit/tests/term/wpUpdateTerm.php +++ b/tests/phpunit/tests/term/wpUpdateTerm.php @@ -4,6 +4,31 @@ * @group taxonomy */ class Tests_Term_WpUpdateTerm extends WP_UnitTestCase { + /** + * @ticket 50568 + */ + public function test_wp_update_term_should_ignore_supplied_filter_context() { + $term_id = self::factory()->term->create( array( 'taxonomy' => 'category' ) ); + $result = wp_update_term( + $term_id, + 'category', + array( + 'filter' => 'db', + 'name' => 'Updated term', + 'slug' => 'Updated Slug', + 'description' => 'Allowed', + ) + ); + + $this->assertNotWPError( $result ); + + $term = get_term( $term_id, 'category' ); + + $this->assertSame( 'Updated term', $term->name ); + $this->assertSame( 'updated-slug', $term->slug ); + $this->assertSame( 'Allowed', $term->description ); + } + public function test_wp_update_term_taxonomy_does_not_exist() { $found = wp_update_term( 1, 'bar' );