From fc8d09e2a61744377d8d26ae43c79a8c20911647 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Wed, 6 Nov 2013 23:40:46 +0000 Subject: [PATCH] A negative term parent value should be sanitized to 0, not 1. Fix a regression in sanitize_term_field() caused by [26010]. props mattheu for initial patch. fixes #25852. git-svn-id: https://develop.svn.wordpress.org/trunk@26028 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/taxonomy.php | 7 +++++-- tests/phpunit/tests/term.php | 15 +++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/taxonomy.php b/src/wp-includes/taxonomy.php index 12c2a6b99c..06a9126616 100644 --- a/src/wp-includes/taxonomy.php +++ b/src/wp-includes/taxonomy.php @@ -1710,8 +1710,11 @@ function sanitize_term($term, $taxonomy, $context = 'display') { */ function sanitize_term_field($field, $value, $term_id, $taxonomy, $context) { $int_fields = array( 'parent', 'term_id', 'count', 'term_group', 'term_taxonomy_id', 'object_id' ); - if ( in_array( $field, $int_fields ) ) - $value = absint( $value ); + if ( in_array( $field, $int_fields ) ) { + $value = (int) $value; + if ( $value < 0 ) + $value = 0; + } if ( 'raw' == $context ) return $value; diff --git a/tests/phpunit/tests/term.php b/tests/phpunit/tests/term.php index 8749f86095..a2eb148b30 100644 --- a/tests/phpunit/tests/term.php +++ b/tests/phpunit/tests/term.php @@ -433,6 +433,9 @@ class Tests_Term extends WP_UnitTestCase { unset( $GLOBALS['wp_taxonomies'][ $random_tax ] ); } + /** + * @ticket 17646 + */ function test_get_object_terms_types() { $post_id = $this->factory->post->create(); $term = wp_insert_term( 'one', $this->taxonomy ); @@ -447,6 +450,18 @@ class Tests_Term extends WP_UnitTestCase { $this->assertInternalType( 'int', $term, 'term' ); } + /** + * @ticket 25852 + */ + function test_sanitize_term_field() { + $term = wp_insert_term( 'foo', $this->taxonomy ); + + $this->assertEquals( 0, sanitize_term_field( 'parent', 0, $term['term_id'], $this->taxonomy, 'raw' ) ); + $this->assertEquals( 1, sanitize_term_field( 'parent', 1, $term['term_id'], $this->taxonomy, 'raw' ) ); + $this->assertEquals( 0, sanitize_term_field( 'parent', -1, $term['term_id'], $this->taxonomy, 'raw' ) ); + $this->assertEquals( 0, sanitize_term_field( 'parent', '', $term['term_id'], $this->taxonomy, 'raw' ) ); + } + private function assertPostHasTerms( $post_id, $expected_term_ids, $taxonomy ) { $assigned_term_ids = wp_get_object_terms( $post_id, $taxonomy, array( 'fields' => 'ids'