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/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&cat=$term_id" );
     953            $link = home_url( "?feed=$feed&cat=$term->term_id" );
    948954        } elseif ( 'post_tag' === $taxonomy ) {
    949955            $link = home_url( "?feed=$feed&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
Note: See TracChangeset for help on using the changeset viewer.