Make WordPress Core


Ignore:
Timestamp:
11/16/2021 02:55:04 PM (3 years ago)
Author:
hellofromTonya
Message:

Taxonomy: Allow get_*_*_link() and edit_term_link() functions to accept a term ID, WP_Term, or term object.

get_term() accepts a term ID, instance of WP_Term, or an object (i.e. stdClass as a result of a db query). Functions that use get_term() also now allow for the same data types.

Why? For consistency, removing extra processing code in consuming functions, and performance.

Functions changed in this commit are:

  • get_category_feed_link()
  • get_term_feed_link()
  • get_tag_feed_link()
  • get_edit_tag_link()
  • get_edit_term_link()
  • edit_term_link()

For each of consumer of these functions, changes to pass the object instead of the term ID.

Includes unit/integration tests for test coverage of these changes.

Follow-up to [6365], [9136], [9340], [14711], [15792], [15800], [18827], [32606], [36646], [37252].

Props davidbinda, johnbillion, peterwilsoncc, hellofromTonya, sergeybiryukov, mista-flo, hareesh-pillai, audrasjb, jeffpaul, chaion07.
Fixes #50225.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/term/getTermLink.php

    r51568 r52180  
    33/**
    44 * @group taxonomy
     5 * @covers ::get_term_link
    56 */
    67class Tests_Term_GetTermLink extends WP_UnitTestCase {
    78
     9    public static $terms;
     10
     11    public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
     12        self::register_custom_taxonomy();
     13
     14        $taxonomies = array( 'category', 'post_tag', 'wptests_tax' );
     15        foreach ( $taxonomies as $taxonomy ) {
     16            self::$terms[ $taxonomy ] = $factory->term->create_and_get( array( 'taxonomy' => $taxonomy ) );
     17        }
     18    }
     19
    820    public function set_up() {
    921        parent::set_up();
    10 
    11         register_taxonomy( 'wptests_tax', 'post' );
     22        self::register_custom_taxonomy();
    1223    }
    1324
     
    208219        $this->assertStringContainsString( '/foo/term2/', $actual );
    209220    }
     221
     222    /**
     223     * @dataProvider data_get_term_link
     224     *
     225     * @ticket 50225
     226     *
     227     * @param string $taxonomy Taxonomy been tested (used for index of term keys).
     228     * @param bool   $use_id   When true, pass term ID. Else, pass term object.
     229     */
     230    public function test_get_term_link_filter_is_object_by_term_id( $taxonomy, $use_id ) {
     231        $term = $this->get_term( $taxonomy, $use_id );
     232
     233        add_filter(
     234            'term_link',
     235            function( $location, $term ) {
     236                $this->assertInstanceOf( 'WP_Term', $term );
     237            },
     238            10,
     239            2
     240        );
     241
     242        get_term_link( $term, $taxonomy );
     243    }
     244
     245    /**
     246     * @dataProvider data_get_term_link
     247     *
     248     * @ticket 50225
     249     *
     250     * @param string $taxonomy Taxonomy been tested (used for index of term keys).
     251     * @param bool   $use_id   When true, pass term ID. Else, pass term object.
     252     */
     253    public function test_get_term_link_filter_is_object_by_term_object( $taxonomy, $use_id ) {
     254        $term = $this->get_term( $taxonomy, $use_id );
     255
     256        add_filter(
     257            'term_link',
     258            function( $location, $term ) {
     259                $this->assertInstanceOf( 'WP_Term', $term );
     260            },
     261            10,
     262            2
     263        );
     264
     265        get_term_link( get_term( $term, $taxonomy ), $taxonomy );
     266    }
     267
     268    /**
     269     * Data provider.
     270     *
     271     * @return array
     272     */
     273    public function data_get_term_link() {
     274        return array(
     275            'category passing term_id'          => array(
     276                'taxonomy' => 'category',
     277                'use_id'   => false,
     278            ),
     279            'category passing term object'      => array(
     280                'taxonomy' => 'category',
     281                'use_id'   => true,
     282            ),
     283            'post_tag passing term_id'          => array(
     284                'taxonomy' => 'post_tag',
     285                'use_id'   => false,
     286            ),
     287            'post_tag passing term object'      => array(
     288                'taxonomy' => 'post_tag',
     289                'use_id'   => true,
     290            ),
     291            'a custom taxonomy passing term_id' => array(
     292                'taxonomy' => 'wptests_tax',
     293                'use_id'   => false,
     294            ),
     295            'a custom taxonomy passing term_id' => array(
     296                'taxonomy' => 'wptests_tax',
     297                'use_id'   => true,
     298                'expected' => 'term.php?taxonomy=custom_taxonomy&tag_ID=%ID%&post_type=post',
     299            ),
     300        );
     301    }
     302
     303    /**
     304     * Helper to register a custom taxonomy for use in tests.
     305     *
     306     * @since 5.9.0
     307     */
     308    private static function register_custom_taxonomy() {
     309        register_taxonomy( 'wptests_tax', 'post' );
     310    }
     311
     312    /**
     313     * Helper to get the term for the given taxonomy.
     314     *
     315     * @since 5.9.0
     316     *
     317     * @param string $taxonomy Taxonomy been tested (used for index of term keys).
     318     * @param bool   $use_id   When true, pass term ID. Else, pass term object.
     319     * @return WP_Term|int If $use_id is true, term ID is returned; else instance of WP_Term.
     320     */
     321    private function get_term( $taxonomy, $use_id ) {
     322        $term = self::$terms[ $taxonomy ];
     323        if ( $use_id ) {
     324            $term = $term->term_id;
     325        }
     326
     327        return $term;
     328    }
    210329}
Note: See TracChangeset for help on using the changeset viewer.