Comments: in `wp_rel_nofollow_callback()`, account for the fact that a link might already have a `rel` attribute. Currently, if a link already has a `rel`, it will result it duplicate attributes on the element with conflicting values.

Adds unit tests.

Props junsuijin, wonderboymusic.
Fixes #9959.


git-svn-id: https://develop.svn.wordpress.org/trunk@34277 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Scott Taylor 2015-09-18 04:35:37 +00:00
parent 50419ff08d
commit d5e5ca8e93
2 changed files with 35 additions and 2 deletions

View File

@ -2259,8 +2259,23 @@ function wp_rel_nofollow( $text ) {
*/
function wp_rel_nofollow_callback( $matches ) {
$text = $matches[1];
$text = str_replace(array(' rel="nofollow"', " rel='nofollow'"), '', $text);
return "<a $text rel=\"nofollow\">";
$atts = shortcode_parse_atts( $matches[1] );
$rel = 'nofollow';
if ( ! empty( $atts['rel'] ) ) {
$parts = array_map( 'trim', explode( ' ', $atts['rel'] ) );
if ( false === array_search( 'nofollow', $parts ) ) {
$parts[] = 'nofollow';
}
$rel = implode( ' ', $parts );
unset( $atts['rel'] );
$html = '';
foreach ( $atts as $name => $value ) {
$html .= "{$name}=\"$value\" ";
}
$text = trim( $html );
}
return "<a $text rel=\"$rel\">";
}
/**

View File

@ -0,0 +1,18 @@
<?php
/**
* @ticket 9959
*/
class Tests_Rel_No_Follow extends WP_UnitTestCase {
public function test_add_no_follow() {
$content = '<p>This is some cool <a href="/">Code</a></p>';
$expected = '<p>This is some cool <a href=\"/\" rel=\"nofollow\">Code</a></p>';
$this->assertEquals( $expected, wp_rel_nofollow( $content ) );
}
public function test_convert_no_follow() {
$content = '<p>This is some cool <a href="/" rel="weird">Code</a></p>';
$expected = '<p>This is some cool <a href=\"/\" rel=\"weird nofollow\">Code</a></p>';
$this->assertEquals( $expected, wp_rel_nofollow( $content ) );
}
}