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.
Built from https://develop.svn.wordpress.org/trunk@52180


git-svn-id: http://core.svn.wordpress.org/trunk@51772 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
hellofromTonya 2021-11-16 14:57:02 +00:00
parent 873c2deba4
commit c5518c2f11
5 changed files with 55 additions and 46 deletions

View File

@ -397,7 +397,7 @@ class WP_Terms_List_Table extends WP_List_Table {
$uri = wp_doing_ajax() ? wp_get_referer() : $_SERVER['REQUEST_URI']; $uri = wp_doing_ajax() ? wp_get_referer() : $_SERVER['REQUEST_URI'];
$edit_link = get_edit_term_link( $tag->term_id, $taxonomy, $this->screen->post_type ); $edit_link = get_edit_term_link( $tag, $taxonomy, $this->screen->post_type );
if ( $edit_link ) { if ( $edit_link ) {
$edit_link = add_query_arg( $edit_link = add_query_arg(
@ -466,7 +466,7 @@ class WP_Terms_List_Table extends WP_List_Table {
$edit_link = add_query_arg( $edit_link = add_query_arg(
'wp_http_referer', 'wp_http_referer',
urlencode( wp_unslash( $uri ) ), urlencode( wp_unslash( $uri ) ),
get_edit_term_link( $tag->term_id, $taxonomy, $this->screen->post_type ) get_edit_term_link( $tag, $taxonomy, $this->screen->post_type )
); );
$actions = array(); $actions = array();

View File

@ -737,9 +737,9 @@ function wp_tag_cloud( $args = '' ) {
foreach ( $tags as $key => $tag ) { foreach ( $tags as $key => $tag ) {
if ( 'edit' === $args['link'] ) { if ( 'edit' === $args['link'] ) {
$link = get_edit_term_link( $tag->term_id, $tag->taxonomy, $args['post_type'] ); $link = get_edit_term_link( $tag, $tag->taxonomy, $args['post_type'] );
} else { } else {
$link = get_term_link( (int) $tag->term_id, $tag->taxonomy ); $link = get_term_link( $tag, $tag->taxonomy );
} }
if ( is_wp_error( $link ) ) { if ( is_wp_error( $link ) ) {

View File

@ -164,7 +164,7 @@ class Walker_Category extends Walker {
$link .= '('; $link .= '(';
} }
$link .= '<a href="' . esc_url( get_term_feed_link( $category->term_id, $category->taxonomy, $args['feed_type'] ) ) . '"'; $link .= '<a href="' . esc_url( get_term_feed_link( $category, $category->taxonomy, $args['feed_type'] ) ) . '"';
if ( empty( $args['feed'] ) ) { if ( empty( $args['feed'] ) ) {
/* translators: %s: Category name. */ /* translators: %s: Category name. */

View File

@ -904,13 +904,13 @@ function get_author_feed_link( $author_id, $feed = '' ) {
* *
* @since 2.5.0 * @since 2.5.0
* *
* @param int $cat_id Category ID. * @param int|WP_Term|object $cat The ID or term object whose feed link will be retrieved.
* @param string $feed Optional. Feed type. Possible values include 'rss2', 'atom'. * @param string $feed Optional. Feed type. Possible values include 'rss2', 'atom'.
* Default is the value of get_default_feed(). * Default is the value of get_default_feed().
* @return string Link to the feed for the category specified by $cat_id. * @return string Link to the feed for the category specified by $cat_id.
*/ */
function get_category_feed_link( $cat_id, $feed = '' ) { function get_category_feed_link( $cat, $feed = '' ) {
return get_term_feed_link( $cat_id, 'category', $feed ); return get_term_feed_link( $cat, 'category', $feed );
} }
/** /**
@ -921,16 +921,22 @@ function get_category_feed_link( $cat_id, $feed = '' ) {
* *
* @since 3.0.0 * @since 3.0.0
* *
* @param int $term_id Term ID. * @param int|WP_Term|object $term The ID or term object whose feed link will be retrieved.
* @param string $taxonomy Optional. Taxonomy of `$term_id`. Default 'category'. * @param string $taxonomy Optional. Taxonomy of `$term_id`.
* @param string $feed Optional. Feed type. Possible values include 'rss2', 'atom'. * Defaults to 'category' if term ID or non WP_Term object is passed.
* Default is the value of get_default_feed(). * @param string $feed Optional. Feed type. Possible values include 'rss2', 'atom'.
* Default is the value of get_default_feed().
* @return string|false Link to the feed for the term specified by $term_id and $taxonomy. * @return string|false Link to the feed for the term specified by $term_id and $taxonomy.
*/ */
function get_term_feed_link( $term_id, $taxonomy = 'category', $feed = '' ) { function get_term_feed_link( $term, $taxonomy = '', $feed = '' ) {
$term_id = (int) $term_id; if ( ! is_object( $term ) ) {
$term = (int) $term;
$taxonomy = 'category';
} elseif ( ! $term instanceof WP_Term ) {
$taxonomy = $term->taxonomy;
}
$term = get_term( $term_id, $taxonomy ); $term = get_term( $term, $taxonomy );
if ( empty( $term ) || is_wp_error( $term ) ) { if ( empty( $term ) || is_wp_error( $term ) ) {
return false; return false;
@ -944,7 +950,7 @@ function get_term_feed_link( $term_id, $taxonomy = 'category', $feed = '' ) {
if ( ! $permalink_structure ) { if ( ! $permalink_structure ) {
if ( 'category' === $taxonomy ) { if ( 'category' === $taxonomy ) {
$link = home_url( "?feed=$feed&amp;cat=$term_id" ); $link = home_url( "?feed=$feed&amp;cat=$term->term_id" );
} elseif ( 'post_tag' === $taxonomy ) { } elseif ( 'post_tag' === $taxonomy ) {
$link = home_url( "?feed=$feed&amp;tag=$term->slug" ); $link = home_url( "?feed=$feed&amp;tag=$term->slug" );
} else { } else {
@ -952,7 +958,7 @@ function get_term_feed_link( $term_id, $taxonomy = 'category', $feed = '' ) {
$link = home_url( "?feed=$feed&amp;$t->query_var=$term->slug" ); $link = home_url( "?feed=$feed&amp;$t->query_var=$term->slug" );
} }
} else { } else {
$link = get_term_link( $term_id, $term->taxonomy ); $link = get_term_link( $term, $term->taxonomy );
if ( get_default_feed() == $feed ) { if ( get_default_feed() == $feed ) {
$feed_link = 'feed'; $feed_link = 'feed';
} else { } else {
@ -1003,13 +1009,13 @@ function get_term_feed_link( $term_id, $taxonomy = 'category', $feed = '' ) {
* *
* @since 2.3.0 * @since 2.3.0
* *
* @param int $tag_id Tag ID. * @param int|WP_Term|object $tag The ID or term object whose feed link will be retrieved.
* @param string $feed Optional. Feed type. Possible values include 'rss2', 'atom'. * @param string $feed Optional. Feed type. Possible values include 'rss2', 'atom'.
* Default is the value of get_default_feed(). * Default is the value of get_default_feed().
* @return string The feed permalink for the given tag. * @return string The feed permalink for the given tag.
*/ */
function get_tag_feed_link( $tag_id, $feed = '' ) { function get_tag_feed_link( $tag, $feed = '' ) {
return get_term_feed_link( $tag_id, 'post_tag', $feed ); return get_term_feed_link( $tag, 'post_tag', $feed );
} }
/** /**
@ -1017,11 +1023,11 @@ function get_tag_feed_link( $tag_id, $feed = '' ) {
* *
* @since 2.7.0 * @since 2.7.0
* *
* @param int $tag_id Tag ID. * @param int|WP_Term|object $tag The ID or term object whose edit link will be retrieved.
* @param string $taxonomy Optional. Taxonomy slug. Default 'post_tag'. * @param string $taxonomy Optional. Taxonomy slug. Default 'post_tag'.
* @return string The edit tag link URL for the given tag. * @return string The edit tag link URL for the given tag.
*/ */
function get_edit_tag_link( $tag_id, $taxonomy = 'post_tag' ) { function get_edit_tag_link( $tag, $taxonomy = 'post_tag' ) {
/** /**
* Filters the edit link for a tag (or term in another taxonomy). * Filters the edit link for a tag (or term in another taxonomy).
* *
@ -1029,7 +1035,7 @@ function get_edit_tag_link( $tag_id, $taxonomy = 'post_tag' ) {
* *
* @param string $link The term edit link. * @param string $link The term edit link.
*/ */
return apply_filters( 'get_edit_tag_link', get_edit_term_link( $tag_id, $taxonomy ) ); return apply_filters( 'get_edit_tag_link', get_edit_term_link( $tag, $taxonomy ) );
} }
/** /**
@ -1062,28 +1068,29 @@ function edit_tag_link( $link = '', $before = '', $after = '', $tag = null ) {
* @since 3.1.0 * @since 3.1.0
* @since 4.5.0 The `$taxonomy` parameter was made optional. * @since 4.5.0 The `$taxonomy` parameter was made optional.
* *
* @param int $term_id Term ID. * @param int|WP_Term|object $term The ID or term object whose edit link will be retrieved.
* @param string $taxonomy Optional. Taxonomy. Defaults to the taxonomy of the term identified * @param string $taxonomy Optional. Taxonomy. Defaults to the taxonomy of the term identified
* by `$term_id`. * by `$term`.
* @param string $object_type Optional. The object type. Used to highlight the proper post type * @param string $object_type Optional. The object type. Used to highlight the proper post type
* menu on the linked page. Defaults to the first object_type associated * menu on the linked page. Defaults to the first object_type associated
* with the taxonomy. * with the taxonomy.
* @return string|null The edit term link URL for the given term, or null on failure. * @return string|null The edit term link URL for the given term, or null on failure.
*/ */
function get_edit_term_link( $term_id, $taxonomy = '', $object_type = '' ) { function get_edit_term_link( $term, $taxonomy = '', $object_type = '' ) {
$term = get_term( $term_id, $taxonomy ); $term = get_term( $term, $taxonomy );
if ( ! $term || is_wp_error( $term ) ) { if ( ! $term || is_wp_error( $term ) ) {
return; return;
} }
$tax = get_taxonomy( $term->taxonomy ); $tax = get_taxonomy( $term->taxonomy );
if ( ! $tax || ! current_user_can( 'edit_term', $term->term_id ) ) { $term_id = $term->term_id;
if ( ! $tax || ! current_user_can( 'edit_term', $term_id ) ) {
return; return;
} }
$args = array( $args = array(
'taxonomy' => $taxonomy, 'taxonomy' => $taxonomy,
'tag_ID' => $term->term_id, 'tag_ID' => $term_id,
); );
if ( $object_type ) { if ( $object_type ) {
@ -1116,16 +1123,18 @@ function get_edit_term_link( $term_id, $taxonomy = '', $object_type = '' ) {
* *
* @since 3.1.0 * @since 3.1.0
* *
* @param string $link Optional. Anchor text. If empty, default is 'Edit This'. Default empty. * @param string $link Optional. Anchor text. If empty, default is 'Edit This'. Default empty.
* @param string $before Optional. Display before edit link. Default empty. * @param string $before Optional. Display before edit link. Default empty.
* @param string $after Optional. Display after edit link. Default empty. * @param string $after Optional. Display after edit link. Default empty.
* @param WP_Term $term Optional. Term object. If null, the queried object will be inspected. Default null. * @param int|WP_Term|null $term Optional. Term ID or object. If null, the queried object will be inspected. Default null.
* @param bool $echo Optional. Whether or not to echo the return. Default true. * @param bool $echo Optional. Whether or not to echo the return. Default true.
* @return string|void HTML content. * @return string|void HTML content.
*/ */
function edit_term_link( $link = '', $before = '', $after = '', $term = null, $echo = true ) { function edit_term_link( $link = '', $before = '', $after = '', $term = null, $echo = true ) {
if ( is_null( $term ) ) { if ( is_null( $term ) ) {
$term = get_queried_object(); $term = get_queried_object();
} else {
$term = get_term( $term );
} }
if ( ! $term ) { if ( ! $term ) {

View File

@ -16,7 +16,7 @@
* *
* @global string $wp_version * @global string $wp_version
*/ */
$wp_version = '5.9-alpha-52179'; $wp_version = '5.9-alpha-52180';
/** /**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.