<?php
function wptexturize($text) {
+ global $wp_cockneyreplace;
$output = '';
// Capture tags and everything inside them
$textarr = preg_split("/(<.*>)/Us", $text, -1, PREG_SPLIT_DELIM_CAPTURE);
$curl = str_replace('...', '…', $curl);
$curl = str_replace('``', '“', $curl);
- // This is a hack, look at this more later. It works pretty well though.
- $cockney = array("'tain't","'twere","'twas","'tis","'twill","'til","'bout","'nuff","'round","'cause");
- $cockneyreplace = array("’tain’t","’twere","’twas","’tis","’twill","’til","’bout","’nuff","’round","’cause");
+ // if a plugin has provided an autocorrect array, use it
+ if ( isset($wp_cockneyreplace) ) {
+ $cockney = array_keys($wp_cockneyreplace);
+ $cockney_replace = array_values($wp_cockneyreplace);
+ } else {
+ $cockney = array("'tain't","'twere","'twas","'tis","'twill","'til","'bout","'nuff","'round","'cause");
+ $cockneyreplace = array("’tain’t","’twere","’twas","’tis","’twill","’til","’bout","’nuff","’round","’cause");
+ }
+
$curl = str_replace($cockney, $cockneyreplace, $curl);
$curl = preg_replace("/'s/", '’s', $curl);
$pee = preg_replace('!(</?(?:table|thead|tfoot|caption|colgroup|tbody|tr|td|th|div|dl|dd|dt|ul|ol|li|pre|select|form|blockquote|address|math|p|h[1-6])[^>]*>)\s*</p>!', "$1", $pee);
if ($br) $pee = preg_replace('|(?<!<br />)\s*\n|', "<br />\n", $pee); // optionally make line breaks
$pee = preg_replace('!(</?(?:table|thead|tfoot|caption|tbody|tr|td|th|div|dl|dd|dt|ul|ol|li|pre|select|form|blockquote|address|math|p|h[1-6])[^>]*>)\s*<br />!', "$1", $pee);
- $pee = preg_replace('!<br />(\s*</?(?:p|li|div|dl|dd|dt|th|pre|td|ul|ol)>)!', '$1', $pee);
+ $pee = preg_replace('!<br />(\s*</?(?:p|li|div|dl|dd|dt|th|pre|td|ul|ol)[^>]*>)!', '$1', $pee);
$pee = preg_replace('!(<pre.*?>)(.*?)</pre>!ise', " stripslashes('$1') . stripslashes(clean_pre('$2')) . '</pre>' ", $pee);
return $pee;
chr(197).chr(188) => 'z', chr(197).chr(189) => 'Z',
chr(197).chr(190) => 'z', chr(197).chr(191) => 's',
// Euro Sign
- chr(226).chr(130).chr(172) => 'E');
-
+ chr(226).chr(130).chr(172) => 'E',
+ // GBP (Pound) Sign
+ chr(194).chr(163) => '');
+
$string = strtr($string, $chars);
} else {
// Assume ISO-8859-1 if not UTF-8
/*
balanceTags
-
+
Balances Tags of string using a modified stack.
-
+
@param text Text to be balanced
+ @param force Forces balancing, ignoring the value of the option
@return Returns balanced text
@author Leonard Lin (leonard@acm.org)
@version v1.1
@date November 4, 2001
@license GPL v2.0
- @notes
- @changelog
+ @notes
+ @changelog
--- Modified by Scott Reilly (coffee2code) 02 Aug 2004
- 1.2 ***TODO*** Make better - change loop condition to $text
- 1.1 Fixed handling of append/stack pop order of end text
- Added Cleaning Hooks
- 1.0 First Version
+ 1.2 ***TODO*** Make better - change loop condition to $text
+ 1.1 Fixed handling of append/stack pop order of end text
+ Added Cleaning Hooks
+ 1.0 First Version
*/
-function balanceTags($text, $is_comment = 0) {
-
- if ( get_option('use_balanceTags') == 0)
+function balanceTags($text, $force = false) {
+
+ if ( !$force && get_option('use_balanceTags') == 0 )
return $text;
$tagstack = array(); $stacksize = 0; $tagqueue = ''; $newtext = '';
if ($regex[1][0] == "/") { // End Tag
$tag = strtolower(substr($regex[1],1));
// if too many closing tags
- if($stacksize <= 0) {
+ if($stacksize <= 0) {
$tag = '';
//or close to be safe $tag = '/' . $tag;
}
}
$newtext .= substr($text,0,$i) . $tag;
$text = substr($text,$i+$l);
- }
+ }
// Clear Tag Queue
$newtext .= $tagqueue;
}
function force_balance_tags($text) {
- return balanceTags($text, 0, true);
+ return balanceTags($text, true);
}
function format_to_edit($content, $richedit = false) {
function make_clickable($ret) {
$ret = ' ' . $ret;
- $ret = preg_replace("#(^|[\n ])([\w]+?://[\w\#$%&~/.\-;:=,?@\[\]+]*)#is", "$1<a href='$2' rel='nofollow'>$2</a>", $ret);
- $ret = preg_replace("#(^|[\n ])((www|ftp)\.[\w\#$%&~/.\-;:=,?@\[\]+]*)#is", "$1<a href='http://$2' rel='nofollow'>$2</a>", $ret);
- $ret = preg_replace("#(\s)([a-z0-9\-_.]+)@([^,< \n\r]+)#i", "$1<a href=\"mailto:$2@$3\">$2@$3</a>", $ret);
- $ret = substr($ret, 1);
+ // in testing, using arrays here was found to be faster
+ $ret = preg_replace(
+ array(
+ '#([\s>])([\w]+?://[\w\#$%&~/.\-;:=,?@\[\]+]*)#is',
+ '#([\s>])((www|ftp)\.[\w\#$%&~/.\-;:=,?@\[\]+]*)#is',
+ '#([\s>])([a-z0-9\-_.]+)@([^,< \n\r]+)#i'),
+ array(
+ '$1<a href="$2" rel="nofollow">$2</a>',
+ '$1<a href="http://$2" rel="nofollow">$2</a>',
+ '$1<a href="mailto:$2@$3">$2@$3</a>'),$ret);
+ // this one is not in an array because we need it to run last, for cleanup of accidental links within links
+ $ret = preg_replace("#(<a( [^>]+?>|>))<a [^>]+?>([^>]+?)</a></a>#i", "$1$3</a>", $ret);
$ret = trim($ret);
return $ret;
}
function wp_rel_nofollow( $text ) {
- $text = preg_replace('|<a (.+?)>|i', '<a $1 rel="nofollow">', $text);
+ $text = preg_replace('|<a (.+?)>|ie', "'<a ' . str_replace(' rel=\"nofollow\"','',stripslashes('$1')) . ' rel=\"nofollow\">'", $text);
return $text;
}
return apply_filters('richedit_pre', $output);
}
+function clean_url( $url, $protocols = null ) {
+ if ('' == $url) return $url;
+ $url = preg_replace('|[^a-z0-9-~+_.?#=!&;,/:%]|i', '', $url);
+ $strip = array('%0d', '%0a');
+ $url = str_replace($strip, '', $url);
+ $url = str_replace(';//', '://', $url);
+ // Append http unless a relative link starting with / or a php file.
+ if ( strpos($url, '://') === false &&
+ substr( $url, 0, 1 ) != '/' && !preg_match('/^[a-z0-9]+?\.php/i', $url) )
+ $url = 'http://' . $url;
+
+ $url = preg_replace('/&([^#])(?![a-z]{2,8};)/', '&$1', $url);
+ if ( !is_array($protocols) )
+ $protocols = array('http', 'https', 'ftp', 'ftps', 'mailto', 'news', 'irc', 'gopher', 'nntp', 'feed', 'telnet');
+ if ( wp_kses_bad_protocol( $url, $protocols ) != $url )
+ return '';
+ return $url;
+}
+
// Escape single quotes, specialchar double quotes, and fix line endings.
function js_escape($text) {
- $text = wp_specialchars($text, 'double');
- $text = str_replace(''', "'", $text);
- return preg_replace("/\r?\n/", "\\n", addslashes($text));
+ $safe_text = wp_specialchars($text, 'double');
+ $safe_text = preg_replace('/&#(x)?0*(?(1)27|39);?/i', "'", stripslashes($safe_text));
+ $safe_text = preg_replace("/\r?\n/", "\\n", addslashes($safe_text));
+ return apply_filters('js_escape', $safe_text, $text);
}
+
+// Escaping for HTML attributes
+function attribute_escape($text) {
+ $safe_text = wp_specialchars($text, true);
+ return apply_filters('attribute_escape', $safe_text, $text);
+}
+
?>