diff --git a/src/wp-includes/taxonomy-functions.php b/src/wp-includes/taxonomy-functions.php index 02e0499e84..d61e726d37 100644 --- a/src/wp-includes/taxonomy-functions.php +++ b/src/wp-includes/taxonomy-functions.php @@ -920,20 +920,20 @@ function get_term_children( $term_id, $taxonomy ) { /** * Get sanitized Term field. * - * Does checks for $term, based on the $taxonomy. The function is for contextual - * reasons and for simplicity of usage. See sanitize_term_field() for more - * information. + * The function is for contextual reasons and for simplicity of usage. * * @since 2.3.0 + * @since 4.4.0 The `$taxonomy` parameter was made optional. `$term` can also now accept a WP_Term object. * - * @param string $field Term field to fetch. - * @param int $term Term ID. - * @param string $taxonomy Taxonomy Name. + * @see sanitize_term_field() + * + * @param string $field Term field to fetch. + * @param int|WP_Term $term Term ID or object. + * @param string $taxonomy Optional. Taxonomy Name. Default empty. * @param string $context Optional, default is display. Look at sanitize_term_field() for available options. * @return string|int|null|WP_Error Will return an empty string if $term is not an object or if $field is not set in $term. */ -function get_term_field( $field, $term, $taxonomy, $context = 'display' ) { - $term = (int) $term; +function get_term_field( $field, $term, $taxonomy = '', $context = 'display' ) { $term = get_term( $term, $taxonomy ); if ( is_wp_error($term) ) return $term; @@ -944,7 +944,7 @@ function get_term_field( $field, $term, $taxonomy, $context = 'display' ) { if ( !isset($term->$field) ) return ''; - return sanitize_term_field($field, $term->$field, $term->term_id, $taxonomy, $context); + return sanitize_term_field( $field, $term->$field, $term->term_id, $term->taxonomy, $context ); } /** diff --git a/tests/phpunit/tests/term/getTermField.php b/tests/phpunit/tests/term/getTermField.php new file mode 100644 index 0000000000..9b7d55aa1e --- /dev/null +++ b/tests/phpunit/tests/term/getTermField.php @@ -0,0 +1,83 @@ +taxonomy, 'post' ); + } + + /** + * @ticket 34245 + */ + public function test_get_term_field_should_not_return_error_for_empty_taxonomy() { + $term = $this->factory->term->create_and_get( array( 'taxonomy' => $this->taxonomy ) ); + + $found = get_term_field( 'taxonomy', $term->term_id, '' ); + $this->assertNotWPError( $found ); + $this->assertSame( $this->taxonomy, $found ); + } + + /** + * @ticket 34245 + */ + public function test_get_term_field_supplying_a_taxonomy() { + $term = $this->factory->term->create_and_get( array( 'taxonomy' => $this->taxonomy ) ); + + $found = get_term_field( 'taxonomy', $term->term_id, $term->taxonomy ); + $this->assertSame( $this->taxonomy, $found ); + } + + /** + * @ticket 34245 + */ + public function test_get_term_field_supplying_no_taxonomy() { + $term = $this->factory->term->create_and_get( array( 'taxonomy' => $this->taxonomy ) ); + + $found = get_term_field( 'taxonomy', $term->term_id ); + $this->assertSame( $this->taxonomy, $found ); + } + + /** + * @ticket 34245 + */ + public function test_get_term_field_should_accept_a_WP_Term_object_or_a_term_id() { + $term = $this->factory->term->create_and_get( array( 'taxonomy' => $this->taxonomy ) ); + + $this->assertInstanceOf( 'WP_Term', $term ); + $this->assertSame( $term->term_id, get_term_field( 'term_id', $term ) ); + $this->assertSame( $term->term_id, get_term_field( 'term_id', $term->term_id ) ); + } + + /** + * @ticket 34245 + */ + public function test_get_term_field_invalid_taxonomy_should_return_WP_Error() { + $term = $this->factory->term->create_and_get( array( 'taxonomy' => $this->taxonomy ) ); + + $found = get_term_field( 'taxonomy', $term, 'foo-taxonomy' ); + $this->assertWPError( $found ); + $this->assertSame( 'invalid_taxonomy', $found->get_error_code() ); + } + + /** + * @ticket 34245 + */ + public function test_get_term_field_invalid_term_should_return_WP_Error() { + $found = get_term_field( 'taxonomy', 0, $this->taxonomy ); + + $this->assertWPError( $found ); + $this->assertSame( 'invalid_term', $found->get_error_code() ); + + $_found = get_term_field( 'taxonomy', 0 ); + + $this->assertWPError( $_found ); + $this->assertSame( 'invalid_term', $_found->get_error_code() ); + } +}