From 0670cf7cc4b200f380e298fbc9ba5e22a1a9eb6f Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Fri, 8 Nov 2013 22:37:41 +0000 Subject: [PATCH] Don't convert URLs inside `
` and `` tags when
 parsing string using `make_clickable()`. Adds Unit Tests.

Props johnjamesjacoby, helen, nacin, adamsilverstein, sirbrillig.
Fixes #23756.



git-svn-id: https://develop.svn.wordpress.org/trunk@26052 602fd350-edb4-49c9-b593-d223f7449a82
---
 src/wp-includes/formatting.php                |  9 ++++-
 .../tests/formatting/MakeClickable.php        | 40 +++++++++++++++++++
 2 files changed, 48 insertions(+), 1 deletion(-)

diff --git a/src/wp-includes/formatting.php b/src/wp-includes/formatting.php
index 5d6813c27b..fc982cc410 100644
--- a/src/wp-includes/formatting.php
+++ b/src/wp-includes/formatting.php
@@ -1606,8 +1606,15 @@ function _make_email_clickable_cb($matches) {
 function make_clickable( $text ) {
 	$r = '';
 	$textarr = preg_split( '/(<[^<>]+>)/', $text, -1, PREG_SPLIT_DELIM_CAPTURE ); // split out HTML tags
+	$nested_code_pre = 0; // Keep track of how many levels link is nested inside 
 or 
 	foreach ( $textarr as $piece ) {
-		if ( empty( $piece ) || ( $piece[0] == '<' && ! preg_match('|^<\s*[\w]{1,20}+://|', $piece) ) ) {
+
+		if ( preg_match( '|^]|', $piece ) || preg_match( '|^]|', $piece ) )
+			$nested_code_pre++;
+		elseif ( ( '' === $piece || '
' === $piece ) && $nested_code_pre ) + $nested_code_pre--; + + if ( $nested_code_pre || empty( $piece ) || ( $piece[0] === '<' && ! preg_match( '|^<\s*[\w]{1,20}+://|', $piece ) ) ) { $r .= $piece; continue; } diff --git a/tests/phpunit/tests/formatting/MakeClickable.php b/tests/phpunit/tests/formatting/MakeClickable.php index 63ad5559a2..221635d72a 100644 --- a/tests/phpunit/tests/formatting/MakeClickable.php +++ b/tests/phpunit/tests/formatting/MakeClickable.php @@ -288,6 +288,46 @@ class Tests_Formatting_MakeClickable extends WP_UnitTestCase { } } + /** + * @ticket 23756 + */ + function test_no_links_inside_pre_or_code() { + $before = array( + '
http://wordpress.org
', + 'http://wordpress.org', + '
http://wordpress.org
', + 'http://wordpress.org', + 'http://wordpress.org', + 'http://wordpress.org', + 'URL before pre http://wordpress.org
http://wordpress.org
', + 'URL before code http://wordpress.orghttp://wordpress.org', + 'URL after pre
http://wordpress.org
http://wordpress.org', + 'URL after code http://wordpress.orghttp://wordpress.org', + 'URL before and after pre http://wordpress.org
http://wordpress.org
http://wordpress.org', + 'URL before and after code http://wordpress.orghttp://wordpress.orghttp://wordpress.org', + 'code inside pre
http://wordpress.org http://wordpress.org http://wordpress.org
', + ); + + $expected = array( + '
http://wordpress.org
', + 'http://wordpress.org', + '
http://wordpress.org
', + 'http://wordpress.org', + 'http://wordpress.org', + 'http://wordpress.org', + 'URL before pre http://wordpress.org
http://wordpress.org
', + 'URL before code http://wordpress.orghttp://wordpress.org', + 'URL after pre
http://wordpress.org
http://wordpress.org', + 'URL after code http://wordpress.orghttp://wordpress.org', + 'URL before and after pre http://wordpress.org
http://wordpress.org
http://wordpress.org', + 'URL before and after code http://wordpress.orghttp://wordpress.orghttp://wordpress.org', + 'code inside pre
http://wordpress.org http://wordpress.org http://wordpress.org
', + ); + + foreach ( $before as $key => $url ) + $this->assertEquals( $expected[ $key ], make_clickable( $url ) ); + } + /** * @ticket 16892 */