Don't require a $taxonomy to be specified in get_term_field().

After [34997], the `$taxonomy` parameter of `get_term()` is optional. This
changeset brings `get_term_field()` in line with the new usage.

Adds unit tests for `get_term_field()`.

Props DrewAPicture.
See #34245.

git-svn-id: https://develop.svn.wordpress.org/trunk@35028 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Boone Gorges 2015-10-12 04:13:25 +00:00
parent ff65d77908
commit 8847a58443
2 changed files with 92 additions and 9 deletions

View File

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

View File

@ -0,0 +1,83 @@
<?php
/**
* @group taxonomy
*/
class Tests_Term_getTermField extends WP_UnitTestCase {
public $taxonomy = 'wptests_tax';
function setUp() {
parent::setUp();
register_taxonomy( $this->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() );
}
}