Make WordPress Core

Changeset 52180


Ignore:
Timestamp:
11/16/2021 02:55:04 PM (5 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.

Location:
trunk
Files:
2 added
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/class-wp-terms-list-table.php

    r51896 r52180  
    398398                $uri = wp_doing_ajax() ? wp_get_referer() : $_SERVER['REQUEST_URI'];
    399399
    400                 $edit_link = get_edit_term_link( $tag->term_id, $taxonomy, $this->screen->post_type );
     400                $edit_link = get_edit_term_link( $tag, $taxonomy, $this->screen->post_type );
    401401
    402402                if ( $edit_link ) {
    … …  
    467467                        'wp_http_referer',
    468468                        urlencode( wp_unslash( $uri ) ),
    469                         get_edit_term_link( $tag->term_id, $taxonomy, $this->screen->post_type )
     469                        get_edit_term_link( $tag, $taxonomy, $this->screen->post_type )
    470470                );
    471471
  • trunk/src/wp-includes/category-template.php

    r51837 r52180  
    738738        foreach ( $tags as $key => $tag ) {
    739739                if ( 'edit' === $args['link'] ) {
    740                         $link = get_edit_term_link( $tag->term_id, $tag->taxonomy, $args['post_type'] );
     740                        $link = get_edit_term_link( $tag, $tag->taxonomy, $args['post_type'] );
    741741                } else {
    742                         $link = get_term_link( (int) $tag->term_id, $tag->taxonomy );
     742                        $link = get_term_link( $tag, $tag->taxonomy );
    743743                }
    744744
  • trunk/src/wp-includes/class-walker-category.php

    r51780 r52180  
    165165                        }
    166166
    167                         $link .= '<a href="' . esc_url( get_term_feed_link( $category->term_id, $category->taxonomy, $args['feed_type'] ) ) . '"';
     167                        $link .= '<a href="' . esc_url( get_term_feed_link( $category, $category->taxonomy, $args['feed_type'] ) ) . '"';
    168168
    169169                        if ( empty( $args['feed'] ) ) {
  • trunk/src/wp-includes/link-template.php

    r52111 r52180  
    905905 * @since 2.5.0
    906906 *
    907  * @param int    $cat_id Category ID.
    908  * @param string $feed  Optional. Feed type. Possible values include 'rss2', 'atom'.
    909  *                       Default is the value of get_default_feed().
     907 * @param int|WP_Term|object $cat  The ID or term object whose feed link will be retrieved.
     908 * @param string             $feed Optional. Feed type. Possible values include 'rss2', 'atom'.
     909 *                                 Default is the value of get_default_feed().
    910910 * @return string Link to the feed for the category specified by $cat_id.
    911911 */
    912 function get_category_feed_link( $cat_id, $feed = '' ) {
    913         return get_term_feed_link( $cat_id, 'category', $feed );
     912function get_category_feed_link( $cat, $feed = '' ) {
     913        return get_term_feed_link( $cat, 'category', $feed );
    914914}
    915915
    … …  
    922922 * @since 3.0.0
    923923 *
    924  * @param int    $term_id  Term ID.
    925  * @param string $taxonomy Optional. Taxonomy of `$term_id`. Default 'category'.
    926  * @param string $feed     Optional. Feed type. Possible values include 'rss2', 'atom'.
    927  *                         Default is the value of get_default_feed().
     924 * @param int|WP_Term|object $term     The ID or term object whose feed link will be retrieved.
     925 * @param string             $taxonomy Optional. Taxonomy of `$term_id`.
     926 *                                     Defaults to 'category' if term ID or non WP_Term object is passed.
     927 * @param string             $feed     Optional. Feed type. Possible values include 'rss2', 'atom'.
     928 *                                     Default is the value of get_default_feed().
    928929 * @return string|false Link to the feed for the term specified by $term_id and $taxonomy.
    929930 */
    930 function get_term_feed_link( $term_id, $taxonomy = 'category', $feed = '' ) {
    931         $term_id = (int) $term_id;
    932 
    933         $term = get_term( $term_id, $taxonomy );
     931function get_term_feed_link( $term, $taxonomy = '', $feed = '' ) {
     932        if ( ! is_object( $term ) ) {
     933                $term     = (int) $term;
     934                $taxonomy = 'category';
     935        } elseif ( ! $term instanceof WP_Term ) {
     936                $taxonomy = $term->taxonomy;
     937        }
     938
     939        $term = get_term( $term, $taxonomy );
    934940
    935941        if ( empty( $term ) || is_wp_error( $term ) ) {
    … …  
    945951        if ( ! $permalink_structure ) {
    946952                if ( 'category' === $taxonomy ) {
    947                         $link = home_url( "?feed=$feed&amp;cat=$term_id" );
     953                        $link = home_url( "?feed=$feed&amp;cat=$term->term_id" );
    948954                } elseif ( 'post_tag' === $taxonomy ) {
    949955                        $link = home_url( "?feed=$feed&amp;tag=$term->slug" );
    … …  
    953959                }
    954960        } else {
    955                 $link = get_term_link( $term_id, $term->taxonomy );
     961                $link = get_term_link( $term, $term->taxonomy );
    956962                if ( get_default_feed() == $feed ) {
    957963                        $feed_link = 'feed';
    … …  
    10041010 * @since 2.3.0
    10051011 *
    1006  * @param int    $tag_id Tag ID.
    1007  * @param string $feed  Optional. Feed type. Possible values include 'rss2', 'atom'.
    1008  *                       Default is the value of get_default_feed().
    1009  * @return string The feed permalink for the given tag.
    1010  */
    1011 function get_tag_feed_link( $tag_id, $feed = '' ) {
    1012         return get_term_feed_link( $tag_id, 'post_tag', $feed );
     1012 * @param int|WP_Term|object $tag  The ID or term object whose feed link will be retrieved.
     1013 * @param string             $feed Optional. Feed type. Possible values include 'rss2', 'atom'.
     1014 *                                 Default is the value of get_default_feed().
     1015 * @return string                  The feed permalink for the given tag.
     1016 */
     1017function get_tag_feed_link( $tag, $feed = '' ) {
     1018        return get_term_feed_link( $tag, 'post_tag', $feed );
    10131019}
    10141020
    … …  
    10181024 * @since 2.7.0
    10191025 *
    1020  * @param int    $tag_id   Tag ID.
    1021  * @param string $taxonomy Optional. Taxonomy slug. Default 'post_tag'.
     1026 * @param int|WP_Term|object $tag      The ID or term object whose edit link will be retrieved.
     1027 * @param string             $taxonomy Optional. Taxonomy slug. Default 'post_tag'.
    10221028 * @return string The edit tag link URL for the given tag.
    10231029 */
    1024 function get_edit_tag_link( $tag_id, $taxonomy = 'post_tag' ) {
     1030function get_edit_tag_link( $tag, $taxonomy = 'post_tag' ) {
    10251031        /**
    10261032         * Filters the edit link for a tag (or term in another taxonomy).
    … …  
    10301036         * @param string $link The term edit link.
    10311037         */
    1032         return apply_filters( 'get_edit_tag_link', get_edit_term_link( $tag_id, $taxonomy ) );
     1038        return apply_filters( 'get_edit_tag_link', get_edit_term_link( $tag, $taxonomy ) );
    10331039}
    10341040
    … …  
    10631069 * @since 4.5.0 The `$taxonomy` parameter was made optional.
    10641070 *
    1065  * @param int    $term_id     Term ID.
    1066  * @param string $taxonomy    Optional. Taxonomy. Defaults to the taxonomy of the term identified
    1067  *                            by `$term_id`.
    1068  * @param string $object_type Optional. The object type. Used to highlight the proper post type
    1069  *                            menu on the linked page. Defaults to the first object_type associated
    1070  *                            with the taxonomy.
     1071 * @param int|WP_Term|object $term        The ID or term object whose edit link will be retrieved.
     1072 * @param string             $taxonomy    Optional. Taxonomy. Defaults to the taxonomy of the term identified
     1073 *                                        by `$term`.
     1074 * @param string             $object_type Optional. The object type. Used to highlight the proper post type
     1075 *                                        menu on the linked page. Defaults to the first object_type associated
     1076 *                                        with the taxonomy.
    10711077 * @return string|null The edit term link URL for the given term, or null on failure.
    10721078 */
    1073 function get_edit_term_link( $term_id, $taxonomy = '', $object_type = '' ) {
    1074         $term = get_term( $term_id, $taxonomy );
     1079function get_edit_term_link( $term, $taxonomy = '', $object_type = '' ) {
     1080        $term = get_term( $term, $taxonomy );
    10751081        if ( ! $term || is_wp_error( $term ) ) {
    10761082                return;
    10771083        }
    10781084
    1079         $tax = get_taxonomy( $term->taxonomy );
    1080         if ( ! $tax || ! current_user_can( 'edit_term', $term->term_id ) ) {
     1085        $tax     = get_taxonomy( $term->taxonomy );
     1086        $term_id = $term->term_id;
     1087        if ( ! $tax || ! current_user_can( 'edit_term', $term_id ) ) {
    10811088                return;
    10821089        }
    … …  
    10841091        $args = array(
    10851092                'taxonomy' => $taxonomy,
    1086                 'tag_ID'   => $term->term_id,
     1093                'tag_ID'   => $term_id,
    10871094        );
    10881095
    … …  
    11171124 * @since 3.1.0
    11181125 *
    1119  * @param string  $link   Optional. Anchor text. If empty, default is 'Edit This'. Default empty.
    1120  * @param string  $before Optional. Display before edit link. Default empty.
    1121  * @param string  $after  Optional. Display after edit link. Default empty.
    1122  * @param WP_Term $term   Optional. Term object. If null, the queried object will be inspected. Default null.
    1123  * @param bool    $echo   Optional. Whether or not to echo the return. Default true.
     1126 * @param string           $link   Optional. Anchor text. If empty, default is 'Edit This'. Default empty.
     1127 * @param string           $before Optional. Display before edit link. Default empty.
     1128 * @param string           $after  Optional. Display after edit link. Default empty.
     1129 * @param int|WP_Term|null $term   Optional. Term ID or object. If null, the queried object will be inspected. Default null.
     1130 * @param bool             $echo   Optional. Whether or not to echo the return. Default true.
    11241131 * @return string|void HTML content.
    11251132 */
    … …  
    11271134        if ( is_null( $term ) ) {
    11281135                $term = get_queried_object();
     1136        } else {
     1137                $term = get_term( $term );
    11291138        }
    11301139
  • 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.