Skip to content

Commit

Permalink
Improve handling the existing rel attribute in `wp_rel_nofollow_cal…
Browse files Browse the repository at this point in the history
…lback()`.

Merges [45990] to the 5.0 branch.
Props xknown, sstoqnov.

git-svn-id: https://develop.svn.wordpress.org/branches/5.0@45993 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
SergeyBiryukov committed Sep 4, 2019
1 parent 8d7e1d1 commit 9ab36e0
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
14 changes: 9 additions & 5 deletions src/wp-includes/formatting.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -2747,19 +2747,19 @@ function wp_rel_nofollow( $text ) {
*/ */
function wp_rel_nofollow_callback( $matches ) { function wp_rel_nofollow_callback( $matches ) {
$text = $matches[1]; $text = $matches[1];
$atts = shortcode_parse_atts( $matches[1] ); $atts = wp_kses_hair( $matches[1], wp_allowed_protocols() );
$rel = 'nofollow'; $rel = 'nofollow';


if ( ! empty( $atts['href'] ) ) { if ( ! empty( $atts['href'] ) ) {
if ( in_array( strtolower( wp_parse_url( $atts['href'], PHP_URL_SCHEME ) ), array( 'http', 'https' ), true ) ) { if ( in_array( strtolower( wp_parse_url( $atts['href']['value'], PHP_URL_SCHEME ) ), array( 'http', 'https' ), true ) ) {
if ( strtolower( wp_parse_url( $atts['href'], PHP_URL_HOST ) ) === strtolower( wp_parse_url( home_url(), PHP_URL_HOST ) ) ) { if ( strtolower( wp_parse_url( $atts['href']['value'], PHP_URL_HOST ) ) === strtolower( wp_parse_url( home_url(), PHP_URL_HOST ) ) ) {
return "<a $text>"; return "<a $text>";
} }
} }
} }


if ( ! empty( $atts['rel'] ) ) { if ( ! empty( $atts['rel'] ) ) {
$parts = array_map( 'trim', explode( ' ', $atts['rel'] ) ); $parts = array_map( 'trim', explode( ' ', $atts['rel']['value'] ) );
if ( false === array_search( 'nofollow', $parts ) ) { if ( false === array_search( 'nofollow', $parts ) ) {
$parts[] = 'nofollow'; $parts[] = 'nofollow';
} }
Expand All @@ -2768,7 +2768,11 @@ function wp_rel_nofollow_callback( $matches ) {


$html = ''; $html = '';
foreach ( $atts as $name => $value ) { foreach ( $atts as $name => $value ) {
$html .= "{$name}=\"" . esc_attr( $value ) . "\" "; if ( isset( $value['vless'] ) && 'y' === $value['vless'] ) {
$html .= $name . ' ';
} else {
$html .= "{$name}=\"" . esc_attr( $value['value'] ) . '" ';
}
} }
$text = trim( $html ); $text = trim( $html );
} }
Expand Down
6 changes: 6 additions & 0 deletions tests/phpunit/tests/formatting/WPRelNoFollow.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -74,4 +74,10 @@ public function data_wp_rel_nofollow() {
), ),
); );
} }

public function test_append_no_follow_with_valueless_attribute() {
$content = '<p>This is some cool <a href="demo.com" download rel="hola">Code</a></p>';
$expected = '<p>This is some cool <a href=\"demo.com\" download rel=\"hola nofollow\">Code</a></p>';
$this->assertEquals( $expected, wp_rel_nofollow( $content ) );
}
} }

0 comments on commit 9ab36e0

Please sign in to comment.