Make WordPress Core


Ignore:
Timestamp:
02/07/2023 06:52:24 PM (16 months ago)
Author:
jorbin
Message:

Comments: Improve rel attribute usage in comments.

Internal links should be followed and it should be easier to modify other rel attributes on comments. This adds a helper function for determining if a URL is internal and also adds some new filters to make it easy to modify rel attributes in comments.

Props thomasplevy, desrosj, sabernhardt, benish74, samiamnot, galbaras, jorbin.

Fixes #53290, #56444.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/comment-template.php

    r55287 r55289  
    219219 */
    220220function get_comment_author_link( $comment_ID = 0 ) {
    221     $comment = get_comment( $comment_ID );
    222     $url     = get_comment_author_url( $comment );
    223     $author  = get_comment_author( $comment );
     221    $comment    = get_comment( $comment_ID );
     222    $comment_ID = ! empty( $comment->comment_ID ) ? $comment->comment_ID : (string) $comment_ID;
     223    $url        = get_comment_author_url( $comment );
     224    $author     = get_comment_author( $comment );
    224225
    225226    if ( empty( $url ) || 'http://' === $url ) {
    226227        $return = $author;
    227228    } else {
    228         $return = "<a href='$url' rel='external nofollow ugc' class='url'>$author</a>";
     229        $rel_parts = array( 'ugc' );
     230        if ( ! wp_is_internal_link( $url ) ) {
     231            $rel_parts = array_merge(
     232                $rel_parts,
     233                array( 'external', 'nofollow' )
     234            );
     235        }
     236
     237        /**
     238         * Filters the rel attributes of the comment author's link.
     239         *
     240         * @since 6.2.0
     241         *
     242         * @param string[]   $rel_parts An array of strings representing the rel
     243         *                              tags which will be joined into the anchor's
     244         *                              rel attribute.
     245         * @param WP_Comment $comment   The comment object
     246         */
     247        $rel_parts = apply_filters( 'comment_author_link_rel', $rel_parts, $comment );
     248
     249        $rel = implode( ' ', $rel_parts );
     250        $rel = esc_attr( $rel );
     251        // empty space before rel necessary for later sprintf.
     252        $rel = ! empty( $rel ) ? sprintf( ' rel="%s"', $rel ) : '';
     253
     254        $return = sprintf(
     255            '<a href="%1$s" class="url"%2$s>%3$s</a>',
     256            $url,
     257            $rel,
     258            $author
     259        );
    229260    }
    230261
     
    240271     * @param string $comment_ID The comment ID as a numeric string.
    241272     */
    242     return apply_filters( 'get_comment_author_link', $return, $author, $comment->comment_ID );
     273    return apply_filters( 'get_comment_author_link', $return, $author, $comment_ID );
    243274}
    244275
Note: See TracChangeset for help on using the changeset viewer.