Skip to content

Commit db04e39

Browse files
Improve handling the existing rel attribute in wp_rel_nofollow_callback().
Merges [45990] to the 4.5 branch. Props xknown, sstoqnov. git-svn-id: https://develop.svn.wordpress.org/branches/4.5@45999 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 3796421 commit db04e39

2 files changed

Lines changed: 14 additions & 4 deletions

File tree

src/wp-includes/formatting.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2347,11 +2347,11 @@ function wp_rel_nofollow( $text ) {
23472347
*/
23482348
function wp_rel_nofollow_callback( $matches ) {
23492349
$text = $matches[1];
2350-
$atts = shortcode_parse_atts( $matches[1] );
2350+
$atts = wp_kses_hair( $matches[1], wp_allowed_protocols() );
23512351
$rel = 'nofollow';
23522352

23532353
if ( ! empty( $atts['href'] ) ) {
2354-
$href_parts = wp_parse_url( $atts['href'] );
2354+
$href_parts = wp_parse_url( $atts['href']['value'] );
23552355
$href_scheme = isset( $href_parts['scheme'] ) ? $href_parts['scheme'] : '';
23562356
$href_host = isset( $href_parts['host'] ) ? $href_parts['host'] : '';
23572357
$home_parts = wp_parse_url( home_url() );
@@ -2364,7 +2364,7 @@ function wp_rel_nofollow_callback( $matches ) {
23642364
}
23652365

23662366
if ( ! empty( $atts['rel'] ) ) {
2367-
$parts = array_map( 'trim', explode( ' ', $atts['rel'] ) );
2367+
$parts = array_map( 'trim', explode( ' ', $atts['rel']['value'] ) );
23682368
if ( false === array_search( 'nofollow', $parts ) ) {
23692369
$parts[] = 'nofollow';
23702370
}
@@ -2373,7 +2373,11 @@ function wp_rel_nofollow_callback( $matches ) {
23732373

23742374
$html = '';
23752375
foreach ( $atts as $name => $value ) {
2376-
$html .= "{$name}=\"" . esc_attr( $value ) . "\" ";
2376+
if ( isset( $value['vless'] ) && 'y' === $value['vless'] ) {
2377+
$html .= $name . ' ';
2378+
} else {
2379+
$html .= "{$name}=\"" . esc_attr( $value['value'] ) . '" ';
2380+
}
23772381
}
23782382
$text = trim( $html );
23792383
}

tests/phpunit/tests/formatting/WPRelNoFollow.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,10 @@ public function data_wp_rel_nofollow() {
7474
),
7575
);
7676
}
77+
78+
public function test_append_no_follow_with_valueless_attribute() {
79+
$content = '<p>This is some cool <a href="demo.com" download rel="hola">Code</a></p>';
80+
$expected = '<p>This is some cool <a href=\"demo.com\" download rel=\"hola nofollow\">Code</a></p>';
81+
$this->assertEquals( $expected, wp_rel_nofollow( $content ) );
82+
}
7783
}

0 commit comments

Comments
 (0)