Comments: Improve rel attribute usage in comments.

Internal links should be followed and it should be easier to modify other rel attributes on comments. This adds a helper function for determining if a URL is internal and also adds some new filters to make it easy to modify rel attributes in comments.

Props thomasplevy, desrosj, sabernhardt, benish74, samiamnot, galbaras, jorbin.

Fixes #53290, #56444.

Built from https://develop.svn.wordpress.org/trunk@55289


git-svn-id: http://core.svn.wordpress.org/trunk@54822 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Aaron Jorbin
2023-02-07 18:54:19 +00:00
parent ed511681a8
commit 6d179f0d90
4 changed files with 149 additions and 41 deletions

View File

@@ -2917,24 +2917,9 @@ function _make_url_clickable_cb( $matches ) {
return $matches[0];
}
if ( 'comment_text' === current_filter() ) {
$rel = 'nofollow ugc';
} else {
$rel = 'nofollow';
}
$rel_attr = _make_clickable_rel_attr( $url );
return $matches[1] . "<a href=\"$url\"$rel_attr>$url</a>" . $suffix;
/**
* Filters the rel value that is added to URL matches converted to links.
*
* @since 5.3.0
*
* @param string $rel The rel value.
* @param string $url The matched URL being converted to a link tag.
*/
$rel = apply_filters( 'make_clickable_rel', $rel, $url );
$rel = esc_attr( $rel );
return $matches[1] . "<a href=\"$url\" rel=\"$rel\">$url</a>" . $suffix;
}
/**
@@ -2965,17 +2950,8 @@ function _make_web_ftp_clickable_cb( $matches ) {
return $matches[0];
}
if ( 'comment_text' === current_filter() ) {
$rel = 'nofollow ugc';
} else {
$rel = 'nofollow';
}
/** This filter is documented in wp-includes/formatting.php */
$rel = apply_filters( 'make_clickable_rel', $rel, $dest );
$rel = esc_attr( $rel );
return $matches[1] . "<a href=\"$dest\" rel=\"$rel\">$dest</a>$ret";
$rel_attr = _make_clickable_rel_attr( $dest );
return $matches[1] . "<a href='{$dest}'{$rel_attr}>{$dest}</a>{$ret}";
}
/**
@@ -2994,6 +2970,48 @@ function _make_email_clickable_cb( $matches ) {
return $matches[1] . "<a href=\"mailto:$email\">$email</a>";
}
/**
* Helper function used to build the "rel" attribute for a URL when creating an anchor using make_clickable().
*
* @since 6.2.0
*
* @param string $url The URL.
* @return string The rel attribute for the anchor or an empty string if no rel attribute should be added.
*/
function _make_clickable_rel_attr( $url ) {
$rel_parts = array();
$scheme = strtolower( wp_parse_url( $url, PHP_URL_SCHEME ) );
$nofollow_schemes = array_intersect( wp_allowed_protocols(), array( 'https', 'http' ) );
// Apply "nofollow" to external links with qualifying URL schemes (mailto:, tel:, etc... shouldn't be followed).
if ( ! wp_is_internal_link( $url ) && in_array( $scheme, $nofollow_schemes, true ) ) {
$rel_parts[] = 'nofollow';
}
// Apply "ugc" when in comment context.
if ( 'comment_text' === current_filter() ) {
$rel_parts[] = 'ugc';
}
$rel = implode( ' ', $rel_parts );
/**
* Filters the rel value that is added to URL matches converted to links.
*
* @since 5.3.0
*
* @param string $rel The rel value.
* @param string $url The matched URL being converted to a link tag.
*/
$rel = apply_filters( 'make_clickable_rel', $rel, $url );
$rel_attr = $rel ? ' rel="' . esc_attr( $rel ) . '"' : '';
return $rel_attr;
}
/**
* Converts plaintext URI to HTML links.
*
@@ -3137,12 +3155,8 @@ function wp_rel_callback( $matches, $rel ) {
$text = $matches[1];
$atts = wp_kses_hair( $matches[1], wp_allowed_protocols() );
if ( ! empty( $atts['href'] ) ) {
if ( in_array( strtolower( wp_parse_url( $atts['href']['value'], PHP_URL_SCHEME ) ), array( 'http', 'https' ), true ) ) {
if ( strtolower( wp_parse_url( $atts['href']['value'], PHP_URL_HOST ) ) === strtolower( wp_parse_url( home_url(), PHP_URL_HOST ) ) ) {
return "<a $text>";
}
}
if ( ! empty( $atts['href'] ) && wp_is_internal_link( $atts['href']['value'] ) ) {
$rel = trim( str_replace( 'nofollow', '', $rel ) );
}
if ( ! empty( $atts['rel'] ) ) {
@@ -3162,7 +3176,10 @@ function wp_rel_callback( $matches, $rel ) {
}
$text = trim( $html );
}
return "<a $text rel=\"" . esc_attr( $rel ) . '">';
$rel_attr = $rel ? ' rel="' . esc_attr( $rel ) . '"' : '';
return "<a {$text}{$rel_attr}>";
}
/**