Improve handling the existing rel attribute in wp_rel_nofollow_callback().

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

git-svn-id: https://develop.svn.wordpress.org/branches/4.4@46001 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Sergey Biryukov 2019-09-04 17:56:42 +00:00
parent 5182bb6ff0
commit fcffda2869
2 changed files with 13 additions and 3 deletions

View File

@ -2344,10 +2344,10 @@ function wp_rel_nofollow( $text ) {
*/
function wp_rel_nofollow_callback( $matches ) {
$text = $matches[1];
$atts = shortcode_parse_atts( $matches[1] );
$atts = wp_kses_hair( $matches[1], wp_allowed_protocols() );
$rel = 'nofollow';
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 ) ) {
$parts[] = 'nofollow';
}
@ -2356,7 +2356,11 @@ function wp_rel_nofollow_callback( $matches ) {
$html = '';
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 );
}

View File

@ -22,4 +22,10 @@ class Tests_Rel_No_Follow extends WP_UnitTestCase {
$expected = '<p>This is some cool <a href=\"/\" rel=\"weird nofollow\">Code</a></p>';
$this->assertEquals( $expected, wp_rel_nofollow( $content ) );
}
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 ) );
}
}