Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 24 additions & 23 deletions src/wp-includes/formatting.php
Original file line number Diff line number Diff line change
Expand Up @@ -5622,36 +5622,37 @@ function _links_add_base( $m ) {
*
* @since 2.7.0
*
* @global string $_links_add_target
*
* @param string $content String to search for links in.
* @param string $target The target to add to the links.
* @param string[] $tags An array of tags to apply to.
* @return string The processed content.
*/
function links_add_target( $content, $target = '_blank', $tags = array( 'a' ) ) {
global $_links_add_target;
$_links_add_target = $target;
$tags = implode( '|', (array) $tags );
return preg_replace_callback( "!<($tags)((\s[^>]*)?)>!i", '_links_add_target', $content );
}
/*
* Special-case finding a single tag, as all existing Core and plugin directory
* calls only attempt to add target attributes to a single kind of tag.
*/
if ( 1 === count( $tags ) ) {
$tag = strtoupper( $tags[0] );
$processor = new WP_HTML_Tag_Processor( $content );
while ( $processor->next_tag( $tag ) ) {
$processor->set_attribute( 'target', $target );
}
} else {
$tag_set = array();
foreach ( $tags as $tag ) {
$tag_set[ strtoupper( $tag ) ] = true;
}

/**
* Callback to add a target attribute to all links in passed content.
*
* @since 2.7.0
* @access private
*
* @global string $_links_add_target
*
* @param string $m The matched link.
* @return string The processed link.
*/
function _links_add_target( $m ) {
global $_links_add_target;
$tag = $m[1];
$link = preg_replace( '|( target=([\'"])(.*?)\2)|i', '', $m[2] );
return '<' . $tag . $link . ' target="' . esc_attr( $_links_add_target ) . '">';
$processor = new WP_HTML_Tag_Processor( $content );
while ( $processor->next_tag() ) {
if ( isset( $tag_set[ $processor->get_tag() ] ) ) {
$processor->set_attribute( 'target', $target );
}
}
}

return $processor->get_updated_html();
}

/**
Expand Down
5 changes: 0 additions & 5 deletions tests/phpstan/baselines/argument.type.neon
Original file line number Diff line number Diff line change
Expand Up @@ -908,11 +908,6 @@ parameters:
identifier: argument.type
count: 1
path: ../../../src/wp-includes/formatting.php
-
message: '#^Parameter \#2 \$callback of function preg_replace_callback expects callable\(array\<string\>\)\: string, ''_links_add_target'' given\.$#'
identifier: argument.type
count: 1
path: ../../../src/wp-includes/formatting.php
-
message: '#^Parameter \#1 \$prefix of function uniqid expects string, int\<0, max\> given\.$#'
identifier: argument.type
Expand Down
6 changes: 3 additions & 3 deletions tests/phpunit/tests/formatting/linksAddTarget.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ class Tests_Formatting_LinksAddTarget extends WP_UnitTestCase {
*/
public function test_links_add_target( $content, $target, $tags, $expected ) {
if ( is_null( $target ) ) {
$this->assertSame( $expected, links_add_target( $content ) );
$this->assertEqualHTML( $expected, links_add_target( $content ) );
} elseif ( is_null( $tags ) ) {
$this->assertSame( $expected, links_add_target( $content, $target ) );
$this->assertEqualHTML( $expected, links_add_target( $content, $target ) );
} else {
$this->assertSame( $expected, links_add_target( $content, $target, $tags ) );
$this->assertEqualHTML( $expected, links_add_target( $content, $target, $tags ) );
}
}

Expand Down
Loading