Make WordPress Core

Changeset 46564


Ignore:
Timestamp:
10/21/2019 08:39:16 PM (5 years ago)
Author:
SergeyBiryukov
Message:

Comments: Add rel="nofollow ugc" attribute when converting plain URLs to <a> tags in comments via make_clickable().

Introduce make_clickable_rel filter for the rel value that is added to URL matches converted to links.

This is a follow-up to [46349], which added the rel="nofollow ugc" attribute to existing <a> tags in comments via wp_rel_ugc().

UGC stands for User Generated Content, and the ugc attribute value is recommended for links within user generated content, such as comments and forum posts.

See https://webmasters.googleblog.com/2019/09/evolving-nofollow-new-ways-to-identify.html.

Props blogginglife, SergeyBiryukov.
Reviewed by desrosj, audrasjb.
Fixes #48022.

Location:
trunk
Files:
2 edited

Legend:

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

    r46470 r46564  
    28362836    }
    28372837
    2838     return $matches[1] . "<a href=\"$url\" rel=\"nofollow\">$url</a>" . $suffix;
     2838    if ( 'comment_text' === current_filter() ) {
     2839        $rel = 'nofollow ugc';
     2840    } else {
     2841        $rel = 'nofollow';
     2842    }
     2843
     2844    /**
     2845     * Filters the rel value that is added to URL matches converted to links.
     2846     *
     2847     * @since 5.3.0
     2848     *
     2849     * @param string $rel The rel value.
     2850     * @param string $url The matched URL being converted to a link tag.
     2851     */
     2852    $rel = apply_filters( 'make_clickable_rel', $rel, $url );
     2853    $rel = esc_attr( $rel );
     2854
     2855    return $matches[1] . "<a href=\"$url\" rel=\"$rel\">$url</a>" . $suffix;
    28392856}
    28402857
     
    28662883    }
    28672884
    2868     return $matches[1] . "<a href=\"$dest\" rel=\"nofollow\">$dest</a>$ret";
     2885    if ( 'comment_text' === current_filter() ) {
     2886        $rel = 'nofollow ugc';
     2887    } else {
     2888        $rel = 'nofollow';
     2889    }
     2890
     2891    /** This filter is documented in wp-includes/formatting.php */
     2892    $rel = apply_filters( 'make_clickable_rel', $rel, $dest );
     2893    $rel = esc_attr( $rel );
     2894
     2895    return $matches[1] . "<a href=\"$dest\" rel=\"$rel\">$dest</a>$ret";
    28692896}
    28702897
     
    31493176     * @since 5.1.0
    31503177     *
    3151      * @param string The rel values.
     3178     * @param string $rel       The rel values.
    31523179     * @param string $link_html The matched content of the link tag including all HTML attributes.
    31533180     */
  • trunk/tests/phpunit/tests/formatting/MakeClickable.php

    r42343 r46564  
    376376
    377377    /**
     378     * @ticket 30162
    378379     * @dataProvider data_script_and_style_tags
    379      * @ticket 30162
    380380     */
    381381    public function test_dont_link_script_and_style_tags( $tag ) {
     
    400400    }
    401401
     402    /**
     403     * @ticket 48022
     404     * @dataProvider data_add_rel_ugc_in_comments
     405     */
     406    public function test_add_rel_ugc_in_comments( $content, $expected ) {
     407        $comment_id = self::factory()->comment->create(
     408            array(
     409                'comment_content' => $content,
     410            )
     411        );
     412
     413        ob_start();
     414        comment_text( $comment_id );
     415        $comment_text = ob_get_clean();
     416
     417        $this->assertContains( $expected, make_clickable( $comment_text ) );
     418    }
     419
     420    public function data_add_rel_ugc_in_comments() {
     421        return array(
     422            array(
     423                'http://wordpress.org',
     424                '<a href="http://wordpress.org" rel="nofollow ugc">http://wordpress.org</a>',
     425            ),
     426            array(
     427                'www.wordpress.org',
     428                '<p><a href="http://www.wordpress.org" rel="nofollow ugc">http://www.wordpress.org</a>',
     429            ),
     430        );
     431    }
     432
    402433}
Note: See TracChangeset for help on using the changeset viewer.