WordPress.org

Make WordPress Core

Ticket #45352: 45352.2.diff

File 45352.2.diff, 2.1 KB (added by meatman89fs, 2 years ago)
  • src/wp-includes/formatting.php

    diff --git a/src/wp-includes/formatting.php b/src/wp-includes/formatting.php
    index a0438e9c53..5bd10e1855 100644
    a b function wp_targeted_link_rel( $text ) { 
    30543054function wp_targeted_link_rel_callback( $matches ) {
    30553055        $link_html = $matches[1];
    30563056        $rel_match = array();
    3057 
     3057        $rel = 'noopener noreferrer';
     3058       
    30583059        /**
    30593060         * Filters the rel values that are added to links with `target` attribute.
    30603061         *
    function wp_targeted_link_rel_callback( $matches ) { 
    30633064         * @param string The rel values.
    30643065         * @param string $link_html The matched content of the link tag including all HTML attributes.
    30653066         */
    3066         $rel = apply_filters( 'wp_targeted_link_rel', 'noopener noreferrer', $link_html );
     3067        $targeted_link_rel = apply_filters( 'wp_targeted_link_rel', $rel, $link_html );
     3068        if( $targeted_link_rel ){
     3069                $rel = $targeted_link_rel;
     3070        }
    30673071
    30683072        // Value with delimiters, spaces around are optional.
    30693073        $attr_regex = '|rel\s*=\s*?(\\\\{0,1}["\'])(.*?)\\1|i';
  • tests/phpunit/tests/formatting/WPTargetedLinkRel.php

    diff --git a/tests/phpunit/tests/formatting/WPTargetedLinkRel.php b/tests/phpunit/tests/formatting/WPTargetedLinkRel.php
    index 49c843ec2c..832b82bef7 100644
    a b class Tests_Targeted_Link_Rel extends WP_UnitTestCase { 
    7171                $expected = '<p>Links: <a href="/" target="_blank" rel="noopener noreferrer">Change me</a> <a href="/">Do not change me</a></p>';
    7272                $this->assertEquals( $expected, wp_targeted_link_rel( $content ) );
    7373        }
     74       
     75        public function test_filter_non_empty_value() {
     76                add_filter('wp_targeted_link_rel', array( $this, 'filter_empty_targeted_link_rel' ) );
     77                $content  = '<p>Links: <a href="/" target="_blank">No rel</a></p>';
     78                $expected = '<p>Links: <a href="/" target="_blank" rel="noopener noreferrer">No rel</a></p>';
     79                $this->assertEquals( $expected, wp_targeted_link_rel( $content ) );
     80                remove_filter( 'wp_targeted_link_rel', array( $this, 'filter_empty_targeted_link_rel' ) );
     81        }
     82       
     83        public function filter_empty_targeted_link_rel( $value ) {
     84                return '';
     85        }
     86       
    7487}