Make WordPress Core

Ticket #34245: 34245.2.diff

File 34245.2.diff, 3.7 KB (added by DrewAPicture, 10 years ago)

$term should accept an ID or an object

  • src/wp-includes/taxonomy-functions.php

     
    920920/**
    921921 * Get sanitized Term field.
    922922 *
    923  * Does checks for $term, based on the $taxonomy. The function is for contextual
    924  * reasons and for simplicity of usage. See sanitize_term_field() for more
    925  * information.
     923 * The function is for contextual reasons and for simplicity of usage.
    926924 *
    927925 * @since 2.3.0
     926 * @since 4.4.0 The `$taxonomy` parameter was made optional. `$term` can also now accept a WP_Term object.
    928927 *
     928 * @see sanitize_term_field()
     929 *
    929930 * @param string $field    Term field to fetch.
    930  * @param int    $term     Term ID.
    931  * @param string $taxonomy Taxonomy Name.
     931 * @param int|WP_Term $term     Term ID or object.
     932 * @param string $taxonomy Optional. Taxonomy Name. Default empty.
    932933 * @param string $context  Optional, default is display. Look at sanitize_term_field() for available options.
    933934 * @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.
    934935 */
    935 function get_term_field( $field, $term, $taxonomy, $context = 'display' ) {
    936         $term = (int) $term;
     936function get_term_field( $field, $term, $taxonomy = '', $context = 'display' ) {
    937937        $term = get_term( $term, $taxonomy );
    938938        if ( is_wp_error($term) )
    939939                return $term;
     
    944944        if ( !isset($term->$field) )
    945945                return '';
    946946
    947         return sanitize_term_field($field, $term->$field, $term->term_id, $taxonomy, $context);
     947        return sanitize_term_field($field, $term->$field, $term->term_id, $term->taxonomy, $context);
    948948}
    949949
    950950/**
  • tests/phpunit/tests/term/getTermField.php

     
     1<?php
     2
     3/**
     4 * @group taxonomy
     5 */
     6class Tests_Term_getTermField extends WP_UnitTestCase {
     7
     8        public $taxonomy = 'wptests_tax';
     9
     10        function setUp() {
     11                parent::setUp();
     12
     13                register_taxonomy( $this->taxonomy, 'post' );
     14        }
     15
     16        /**
     17         * @ticket 34245
     18         */
     19        public function test_get_term_field_should_return_error_for_empty_term() {
     20                $found = get_term_field( 'term_id', 0, $this->taxonomy );
     21                $this->assertWPError( $found );
     22                $this->assertSame( 'invalid_term', $found->get_error_code() );
     23        }
     24
     25        /**
     26         * @ticket 34245
     27         */
     28        public function test_get_term_field_should_not_return_error_for_empty_taxonomy() {
     29                $term = $this->factory->term->create_and_get( array( 'taxonomy' => $this->taxonomy ) );
     30
     31                $found = get_term_field( 'taxonomy', $term->term_id, '' );
     32                $this->assertNotWPError( $found );
     33        }
     34
     35        /**
     36         * @ticket 34245
     37         */
     38        public function test_get_term_field_supplying_a_taxonomy() {
     39                $term = $this->factory->term->create_and_get( array( 'taxonomy' => $this->taxonomy ) );
     40
     41                $this->assertSame( $this->taxonomy, get_term_field( 'taxonomy', $term->term_id, $term->taxonomy ) );
     42        }
     43
     44        /**
     45         * @ticket 34245
     46         */
     47        public function test_get_term_field_supplying_no_taxonomy() {
     48                $term = $this->factory->term->create_and_get( array( 'taxonomy' => $this->taxonomy ) );
     49
     50                $this->assertSame( $this->taxonomy, get_term_field( 'taxonomy', $term->term_id ) );
     51        }
     52
     53        /**
     54         * @ticket 34245
     55         */
     56        public function test_get_term_field_should_accept_a_WP_Term_object_or_a_term_id() {
     57                $term = $this->factory->term->create_and_get( array( 'taxonomy' => $this->taxonomy ) );
     58
     59                $this->assertInstanceOf( 'WP_Term', $term );
     60                $this->assertSame( $term->term_id, get_term_field( 'term_id', $term ) );
     61                $this->assertSame( $term->term_id, get_term_field( 'term_id', $term->term_id ) );
     62        }
     63}