From 60aa1d30628821e8c60c2e2a98df7d761b53ceaf Mon Sep 17 00:00:00 2001 From: westi Date: Wed, 15 Dec 2010 13:43:30 +0000 Subject: [PATCH] Ultimate make_clickable. Fixes #14993 props filosofo git-svn-id: http://svn.automattic.com/wordpress/trunk@16948 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/formatting.php | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/wp-includes/formatting.php b/wp-includes/formatting.php index f3b0bc332..82ce377a4 100644 --- a/wp-includes/formatting.php +++ b/wp-includes/formatting.php @@ -1312,12 +1312,19 @@ function antispambot($emailaddy, $mailto=0) { */ function _make_url_clickable_cb($matches) { $url = $matches[2]; + $suffix = ''; + + /** Include parentheses in the URL only if paired **/ + while ( substr_count( $url, '(' ) < substr_count( $url, ')' ) ) { + $suffix = strrchr( $url, ')' ) . $suffix; + $url = substr( $url, 0, strrpos( $url, ')' ) ); + } $url = esc_url($url); if ( empty($url) ) return $matches[0]; - return $matches[1] . "$url"; + return $matches[1] . "$url" . $suffix; } /** @@ -1379,7 +1386,7 @@ function _make_email_clickable_cb($matches) { function make_clickable($ret) { $ret = ' ' . $ret; // in testing, using arrays here was found to be faster - $ret = preg_replace_callback('#(?<=[\s>])(\()?([\w]+?://(?:[\w\\x80-\\xff\#$%&~/=?@\[\](!+-]|[.,;:](?![\s<]|(\))?([\s]|$))|(?(1)\)(?![\s<.,;:]|$)|\)))+)#is', '_make_url_clickable_cb', $ret); + $ret = preg_replace_callback('#(?])(\()?([\w]+?://(?:[\w\\x80-\\xff\#%~/?@\[\]-]|[\'*(+.,;:!=&$](?![\b\)]|(\))?([\s]|$))|(?(1)\)(?![\s<.,;:]|$)|\)))+)#is', '_make_url_clickable_cb', $ret); $ret = preg_replace_callback('#([\s>])((www|ftp)\.[\w\\x80-\\xff\#$%&~/.\-;:=,?@\[\]+]+)#is', '_make_web_ftp_clickable_cb', $ret); $ret = preg_replace_callback('#([\s>])([.0-9a-z_+-]+)@(([0-9a-z-]+\.)+[0-9a-z]{2,})#i', '_make_email_clickable_cb', $ret); // this one is not in an array because we need it to run last, for cleanup of accidental links within links