Make WordPress Core

Ticket #34245: 34245.4.diff

File 34245.4.diff, 4.3 KB (added by DrewAPicture, 10 years ago)
  • 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_not_return_error_for_empty_taxonomy() {
     20                $term = $this->factory->term->create_and_get( array( 'taxonomy' => $this->taxonomy ) );
     21
     22                $found = get_term_field( 'taxonomy', $term->term_id, '' );
     23                $this->assertNotWPError( $found );
     24                $this->assertSame( $this->taxonomy, $found );
     25        }
     26
     27        /**
     28         * @ticket 34245
     29         */
     30        public function test_get_term_field_supplying_a_taxonomy() {
     31                $term = $this->factory->term->create_and_get( array( 'taxonomy' => $this->taxonomy ) );
     32
     33                $found = get_term_field( 'taxonomy', $term->term_id, $term->taxonomy );
     34                $this->assertSame( $this->taxonomy, $found );
     35        }
     36
     37        /**
     38         * @ticket 34245
     39         */
     40        public function test_get_term_field_supplying_no_taxonomy() {
     41                $term = $this->factory->term->create_and_get( array( 'taxonomy' => $this->taxonomy ) );
     42
     43                $found = get_term_field( 'taxonomy', $term->term_id );
     44                $this->assertSame( $this->taxonomy, $found );
     45        }
     46
     47        /**
     48         * @ticket 34245
     49         */
     50        public function test_get_term_field_should_accept_a_WP_Term_object_or_a_term_id() {
     51                $term = $this->factory->term->create_and_get( array( 'taxonomy' => $this->taxonomy ) );
     52
     53                $this->assertInstanceOf( 'WP_Term', $term );
     54                $this->assertSame( $term->term_id, get_term_field( 'term_id', $term ) );
     55                $this->assertSame( $term->term_id, get_term_field( 'term_id', $term->term_id ) );
     56        }
     57
     58        /**
     59         * @ticket 34245
     60         */
     61        public function test_get_term_field_invalid_taxonomy_should_return_WP_Error() {
     62                $term = $this->factory->term->create_and_get( array( 'taxonomy' => $this->taxonomy ) );
     63
     64                $found = get_term_field( 'taxonomy', $term, 'foo-taxonomy' );
     65                $this->assertWPError( $found );
     66                $this->assertSame( 'invalid_taxonomy', $found->get_error_code() );
     67        }
     68
     69        /**
     70         * @ticket 34245
     71         */
     72        public function test_get_term_field_invalid_term_should_return_WP_Error() {
     73                $found = get_term_field( 'taxonomy', 0, $this->taxonomy );
     74
     75                $this->assertWPError( $found );
     76                $this->assertSame( 'invalid_term', $found->get_error_code() );
     77
     78                $_found = get_term_field( 'taxonomy', 0 );
     79
     80                $this->assertWPError( $_found );
     81                $this->assertSame( 'invalid_term', $_found->get_error_code() );
     82        }
     83}