Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions src/wp-includes/class-wp-term.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}

/**
Expand All @@ -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 );
}

/**
Expand Down
30 changes: 27 additions & 3 deletions src/wp-includes/taxonomy.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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 ) {
Expand Down Expand Up @@ -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 ) {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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' => '',
Expand Down Expand Up @@ -4064,15 +4083,20 @@ 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();
foreach ( (array) $terms as $term ) {
// 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 );

Expand Down
5 changes: 0 additions & 5 deletions tests/phpstan/baselines/argument.type.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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\<WP_Term\>, list\<stdClass\>\|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
Expand Down
61 changes: 61 additions & 0 deletions tests/phpunit/tests/term/cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
87 changes: 87 additions & 0 deletions tests/phpunit/tests/term/getTerm.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
39 changes: 39 additions & 0 deletions tests/phpunit/tests/term/sanitizeTerm.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.' );
}
}
23 changes: 23 additions & 0 deletions tests/phpunit/tests/term/wpInsertTerm.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
'<b>Test term</b>',
'category',
array(
'filter' => 'db',
'slug' => 'Test Slug',
'description' => '<script></script><strong>Allowed</strong>',
)
);

$this->assertNotWPError( $result );

$term = get_term( $result['term_id'], 'category' );

$this->assertSame( 'Test term', $term->name );
$this->assertSame( 'test-slug', $term->slug );
$this->assertSame( '<strong>Allowed</strong>', $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' );
Expand Down
Loading
Loading