branch | wordpress |
changeset 132 | 4d4862461b8d |
parent 109 | 03b0d1493584 |
131:a4642baaf829 | 132:4d4862461b8d |
---|---|
1 <?php |
1 <?php |
2 /** |
2 /** |
3 * Main Wordpress Formatting API. |
3 * Main WordPress Formatting API. |
4 * |
4 * |
5 * Handles many functions for formatting output. |
5 * Handles many functions for formatting output. |
6 * |
6 * |
7 * @package WordPress |
7 * @package WordPress |
8 **/ |
8 **/ |
26 * @param string $text The text to be formatted |
26 * @param string $text The text to be formatted |
27 * @return string The string replaced with html entities |
27 * @return string The string replaced with html entities |
28 */ |
28 */ |
29 function wptexturize($text) { |
29 function wptexturize($text) { |
30 global $wp_cockneyreplace; |
30 global $wp_cockneyreplace; |
31 static $static_setup = false, $opening_quote, $closing_quote, $default_no_texturize_tags, $default_no_texturize_shortcodes, $static_characters, $static_replacements, $dynamic_characters, $dynamic_replacements; |
|
31 $output = ''; |
32 $output = ''; |
32 $curl = ''; |
33 $curl = ''; |
33 $textarr = preg_split('/(<.*>|\[.*\])/Us', $text, -1, PREG_SPLIT_DELIM_CAPTURE); |
34 $textarr = preg_split('/(<.*>|\[.*\])/Us', $text, -1, PREG_SPLIT_DELIM_CAPTURE); |
34 $stop = count($textarr); |
35 $stop = count($textarr); |
35 |
36 |
36 /* translators: opening curly quote */ |
37 // No need to setup these variables more than once |
37 $opening_quote = _x('“', 'opening curly quote'); |
38 if (!$static_setup) { |
38 /* translators: closing curly quote */ |
39 /* translators: opening curly quote */ |
39 $closing_quote = _x('”', 'closing curly quote'); |
40 $opening_quote = _x('“', 'opening curly quote'); |
40 |
41 /* translators: closing curly quote */ |
41 $no_texturize_tags = apply_filters('no_texturize_tags', array('pre', 'code', 'kbd', 'style', 'script', 'tt')); |
42 $closing_quote = _x('”', 'closing curly quote'); |
42 $no_texturize_shortcodes = apply_filters('no_texturize_shortcodes', array('code')); |
43 |
44 $default_no_texturize_tags = array('pre', 'code', 'kbd', 'style', 'script', 'tt'); |
|
45 $default_no_texturize_shortcodes = array('code'); |
|
46 |
|
47 // if a plugin has provided an autocorrect array, use it |
|
48 if ( isset($wp_cockneyreplace) ) { |
|
49 $cockney = array_keys($wp_cockneyreplace); |
|
50 $cockneyreplace = array_values($wp_cockneyreplace); |
|
51 } else { |
|
52 $cockney = array("'tain't","'twere","'twas","'tis","'twill","'til","'bout","'nuff","'round","'cause"); |
|
53 $cockneyreplace = array("’tain’t","’twere","’twas","’tis","’twill","’til","’bout","’nuff","’round","’cause"); |
|
54 } |
|
55 |
|
56 $static_characters = array_merge(array('---', ' -- ', '--', ' - ', 'xn–', '...', '``', '\'s', '\'\'', ' (tm)'), $cockney); |
|
57 $static_replacements = array_merge(array('—', ' — ', '–', ' – ', 'xn--', '…', $opening_quote, '’s', $closing_quote, ' ™'), $cockneyreplace); |
|
58 |
|
59 $dynamic_characters = array('/\'(\d\d(?:’|\')?s)/', '/(\s|\A|[([{<]|")\'/', '/(\d+)"/', '/(\d+)\'/', '/(\S)\'([^\'\s])/', '/(\s|\A|[([{<])"(?!\s)/', '/"(\s|\S|\Z)/', '/\'([\s.]|\Z)/', '/(\d+)x(\d+)/'); |
|
60 $dynamic_replacements = array('’$1','$1‘', '$1″', '$1′', '$1’$2', '$1' . $opening_quote . '$2', $closing_quote . '$1', '’$1', '$1×$2'); |
|
61 |
|
62 $static_setup = true; |
|
63 } |
|
64 |
|
65 // Transform into regexp sub-expression used in _wptexturize_pushpop_element |
|
66 // Must do this everytime in case plugins use these filters in a context sensitive manner |
|
67 $no_texturize_tags = '(' . implode('|', apply_filters('no_texturize_tags', $default_no_texturize_tags) ) . ')'; |
|
68 $no_texturize_shortcodes = '(' . implode('|', apply_filters('no_texturize_shortcodes', $default_no_texturize_shortcodes) ) . ')'; |
|
69 |
|
43 $no_texturize_tags_stack = array(); |
70 $no_texturize_tags_stack = array(); |
44 $no_texturize_shortcodes_stack = array(); |
71 $no_texturize_shortcodes_stack = array(); |
45 |
72 |
46 // if a plugin has provided an autocorrect array, use it |
|
47 if ( isset($wp_cockneyreplace) ) { |
|
48 $cockney = array_keys($wp_cockneyreplace); |
|
49 $cockneyreplace = array_values($wp_cockneyreplace); |
|
50 } else { |
|
51 $cockney = array("'tain't","'twere","'twas","'tis","'twill","'til","'bout","'nuff","'round","'cause"); |
|
52 $cockneyreplace = array("’tain’t","’twere","’twas","’tis","’twill","’til","’bout","’nuff","’round","’cause"); |
|
53 } |
|
54 |
|
55 $static_characters = array_merge(array('---', ' -- ', '--', ' - ', 'xn–', '...', '``', '\'s', '\'\'', ' (tm)'), $cockney); |
|
56 $static_replacements = array_merge(array('—', ' — ', '–', ' – ', 'xn--', '…', $opening_quote, '’s', $closing_quote, ' ™'), $cockneyreplace); |
|
57 |
|
58 $dynamic_characters = array('/\'(\d\d(?:’|\')?s)/', '/(\s|\A|")\'/', '/(\d+)"/', '/(\d+)\'/', '/(\S)\'([^\'\s])/', '/(\s|\A)"(?!\s)/', '/"(\s|\S|\Z)/', '/\'([\s.]|\Z)/', '/(\d+)x(\d+)/'); |
|
59 $dynamic_replacements = array('’$1','$1‘', '$1″', '$1′', '$1’$2', '$1' . $opening_quote . '$2', $closing_quote . '$1', '’$1', '$1×$2'); |
|
60 |
|
61 for ( $i = 0; $i < $stop; $i++ ) { |
73 for ( $i = 0; $i < $stop; $i++ ) { |
62 $curl = $textarr[$i]; |
74 $curl = $textarr[$i]; |
63 |
75 |
64 if ( !empty($curl) && '<' != $curl{0} && '[' != $curl{0} |
76 if ( !empty($curl) && '<' != $curl{0} && '[' != $curl{0} |
65 && empty($no_texturize_shortcodes_stack) && empty($no_texturize_tags_stack)) { // If it's not a tag |
77 && empty($no_texturize_shortcodes_stack) && empty($no_texturize_tags_stack)) { |
78 // This is not a tag, nor is the texturization disabled |
|
66 // static strings |
79 // static strings |
67 $curl = str_replace($static_characters, $static_replacements, $curl); |
80 $curl = str_replace($static_characters, $static_replacements, $curl); |
68 // regular expressions |
81 // regular expressions |
69 $curl = preg_replace($dynamic_characters, $dynamic_replacements, $curl); |
82 $curl = preg_replace($dynamic_characters, $dynamic_replacements, $curl); |
70 } else { |
83 } elseif (!empty($curl)) { |
71 wptexturize_pushpop_element($curl, $no_texturize_tags_stack, $no_texturize_tags, '<', '>'); |
84 /* |
72 wptexturize_pushpop_element($curl, $no_texturize_shortcodes_stack, $no_texturize_shortcodes, '[', ']'); |
85 * Only call _wptexturize_pushpop_element if first char is correct |
86 * tag opening |
|
87 */ |
|
88 if ('<' == $curl{0}) |
|
89 _wptexturize_pushpop_element($curl, $no_texturize_tags_stack, $no_texturize_tags, '<', '>'); |
|
90 elseif ('[' == $curl{0}) |
|
91 _wptexturize_pushpop_element($curl, $no_texturize_shortcodes_stack, $no_texturize_shortcodes, '[', ']'); |
|
73 } |
92 } |
74 |
93 |
75 $curl = preg_replace('/&([^#])(?![a-zA-Z1-4]{1,8};)/', '&$1', $curl); |
94 $curl = preg_replace('/&([^#])(?![a-zA-Z1-4]{1,8};)/', '&$1', $curl); |
76 $output .= $curl; |
95 $output .= $curl; |
77 } |
96 } |
78 |
97 |
79 return $output; |
98 return $output; |
80 } |
99 } |
81 |
100 |
82 function wptexturize_pushpop_element($text, &$stack, $disabled_elements, $opening = '<', $closing = '>') { |
101 /** |
83 $o = preg_quote($opening, '/'); |
102 * Search for disabled element tags. Push element to stack on tag open and pop |
84 $c = preg_quote($closing, '/'); |
103 * on tag close. Assumes first character of $text is tag opening. |
85 foreach($disabled_elements as $element) { |
104 * |
86 if (preg_match('/^'.$o.$element.'\b/', $text)) array_push($stack, $element); |
105 * @access private |
87 if (preg_match('/^'.$o.'\/'.$element.$c.'/', $text)) { |
106 * @since 2.9.0 |
107 * |
|
108 * @param string $text Text to check. First character is assumed to be $opening |
|
109 * @param array $stack Array used as stack of opened tag elements |
|
110 * @param string $disabled_elements Tags to match against formatted as regexp sub-expression |
|
111 * @param string $opening Tag opening character, assumed to be 1 character long |
|
112 * @param string $opening Tag closing character |
|
113 * @return object |
|
114 */ |
|
115 function _wptexturize_pushpop_element($text, &$stack, $disabled_elements, $opening = '<', $closing = '>') { |
|
116 // Check if it is a closing tag -- otherwise assume opening tag |
|
117 if (strncmp($opening . '/', $text, 2)) { |
|
118 // Opening? Check $text+1 against disabled elements |
|
119 if (preg_match('/^' . $disabled_elements . '\b/', substr($text, 1), $matches)) { |
|
120 /* |
|
121 * This disables texturize until we find a closing tag of our type |
|
122 * (e.g. <pre>) even if there was invalid nesting before that |
|
123 * |
|
124 * Example: in the case <pre>sadsadasd</code>"baba"</pre> |
|
125 * "baba" won't be texturize |
|
126 */ |
|
127 |
|
128 array_push($stack, $matches[1]); |
|
129 } |
|
130 } else { |
|
131 // Closing? Check $text+2 against disabled elements |
|
132 $c = preg_quote($closing, '/'); |
|
133 if (preg_match('/^' . $disabled_elements . $c . '/', substr($text, 2), $matches)) { |
|
88 $last = array_pop($stack); |
134 $last = array_pop($stack); |
89 // disable texturize until we find a closing tag of our type (e.g. <pre>) |
135 |
90 // even if there was invalid nesting before that |
136 // Make sure it matches the opening tag |
91 // Example: in the case <pre>sadsadasd</code>"baba"</pre> "baba" won't be texturized |
137 if ($last != $matches[1]) |
92 if ($last != $element) array_push($stack, $last); |
138 array_push($stack, $last); |
93 } |
139 } |
94 } |
140 } |
95 } |
141 } |
96 |
142 |
97 /** |
143 /** |
131 * @param string $pee The text which has to be formatted. |
177 * @param string $pee The text which has to be formatted. |
132 * @param int|bool $br Optional. If set, this will convert all remaining line-breaks after paragraphing. Default true. |
178 * @param int|bool $br Optional. If set, this will convert all remaining line-breaks after paragraphing. Default true. |
133 * @return string Text which has been converted into correct paragraph tags. |
179 * @return string Text which has been converted into correct paragraph tags. |
134 */ |
180 */ |
135 function wpautop($pee, $br = 1) { |
181 function wpautop($pee, $br = 1) { |
182 |
|
136 if ( trim($pee) === '' ) |
183 if ( trim($pee) === '' ) |
137 return ''; |
184 return ''; |
138 $pee = $pee . "\n"; // just to make things a little easier, pad the end |
185 $pee = $pee . "\n"; // just to make things a little easier, pad the end |
139 $pee = preg_replace('|<br />\s*<br />|', "\n\n", $pee); |
186 $pee = preg_replace('|<br />\s*<br />|', "\n\n", $pee); |
140 // Space things out a little |
187 // Space things out a little |
141 $allblocks = '(?:table|thead|tfoot|caption|col|colgroup|tbody|tr|td|th|div|dl|dd|dt|ul|ol|li|pre|select|form|map|area|blockquote|address|math|style|input|p|h[1-6]|hr)'; |
188 $allblocks = '(?:table|thead|tfoot|caption|col|colgroup|tbody|tr|td|th|div|dl|dd|dt|ul|ol|li|pre|select|form|map|area|blockquote|address|math|style|input|p|h[1-6]|hr|fieldset|legend)'; |
142 $pee = preg_replace('!(<' . $allblocks . '[^>]*>)!', "\n$1", $pee); |
189 $pee = preg_replace('!(<' . $allblocks . '[^>]*>)!', "\n$1", $pee); |
143 $pee = preg_replace('!(</' . $allblocks . '>)!', "$1\n\n", $pee); |
190 $pee = preg_replace('!(</' . $allblocks . '>)!', "$1\n\n", $pee); |
144 $pee = str_replace(array("\r\n", "\r"), "\n", $pee); // cross-platform newlines |
191 $pee = str_replace(array("\r\n", "\r"), "\n", $pee); // cross-platform newlines |
145 if ( strpos($pee, '<object') !== false ) { |
192 if ( strpos($pee, '<object') !== false ) { |
146 $pee = preg_replace('|\s*<param([^>]*)>\s*|', "<param$1>", $pee); // no pee inside object/embed |
193 $pee = preg_replace('|\s*<param([^>]*)>\s*|', "<param$1>", $pee); // no pee inside object/embed |
168 $pee = preg_replace('!(</?' . $allblocks . '[^>]*>)\s*<br />!', "$1", $pee); |
215 $pee = preg_replace('!(</?' . $allblocks . '[^>]*>)\s*<br />!', "$1", $pee); |
169 $pee = preg_replace('!<br />(\s*</?(?:p|li|div|dl|dd|dt|th|pre|td|ul|ol)[^>]*>)!', '$1', $pee); |
216 $pee = preg_replace('!<br />(\s*</?(?:p|li|div|dl|dd|dt|th|pre|td|ul|ol)[^>]*>)!', '$1', $pee); |
170 if (strpos($pee, '<pre') !== false) |
217 if (strpos($pee, '<pre') !== false) |
171 $pee = preg_replace_callback('!(<pre[^>]*>)(.*?)</pre>!is', 'clean_pre', $pee ); |
218 $pee = preg_replace_callback('!(<pre[^>]*>)(.*?)</pre>!is', 'clean_pre', $pee ); |
172 $pee = preg_replace( "|\n</p>$|", '</p>', $pee ); |
219 $pee = preg_replace( "|\n</p>$|", '</p>', $pee ); |
173 $pee = preg_replace('/<p>\s*?(' . get_shortcode_regex() . ')\s*<\/p>/s', '$1', $pee); // don't auto-p wrap shortcodes that stand alone |
220 |
221 return $pee; |
|
222 } |
|
223 |
|
224 /** |
|
225 * Don't auto-p wrap shortcodes that stand alone |
|
226 * |
|
227 * Ensures that shortcodes are not wrapped in <<p>>...<</p>>. |
|
228 * |
|
229 * @since 2.9.0 |
|
230 * |
|
231 * @param string $pee The content. |
|
232 * @return string The filtered content. |
|
233 */ |
|
234 function shortcode_unautop($pee) { |
|
235 global $shortcode_tags; |
|
236 |
|
237 if ( !empty($shortcode_tags) && is_array($shortcode_tags) ) { |
|
238 $tagnames = array_keys($shortcode_tags); |
|
239 $tagregexp = join( '|', array_map('preg_quote', $tagnames) ); |
|
240 $pee = preg_replace('/<p>\\s*?(\\[(' . $tagregexp . ')\\b.*?\\/?\\](?:.+?\\[\\/\\2\\])?)\\s*<\\/p>/s', '$1', $pee); |
|
241 } |
|
174 |
242 |
175 return $pee; |
243 return $pee; |
176 } |
244 } |
177 |
245 |
178 /** |
246 /** |
659 * @param bool $strict If set limits $username to specific characters. Default false. |
727 * @param bool $strict If set limits $username to specific characters. Default false. |
660 * @return string The sanitized username, after passing through filters. |
728 * @return string The sanitized username, after passing through filters. |
661 */ |
729 */ |
662 function sanitize_user( $username, $strict = false ) { |
730 function sanitize_user( $username, $strict = false ) { |
663 $raw_username = $username; |
731 $raw_username = $username; |
664 $username = strip_tags($username); |
732 $username = wp_strip_all_tags($username); |
665 // Kill octets |
733 // Kill octets |
666 $username = preg_replace('|%([a-fA-F0-9][a-fA-F0-9])|', '', $username); |
734 $username = preg_replace('|%([a-fA-F0-9][a-fA-F0-9])|', '', $username); |
667 $username = preg_replace('/&.+?;/', '', $username); // Kill entities |
735 $username = preg_replace('/&.+?;/', '', $username); // Kill entities |
668 |
736 |
669 // If strict, reduce to ASCII for max portability. |
737 // If strict, reduce to ASCII for max portability. |
1141 |
1209 |
1142 if (get_magic_quotes_gpc()) { |
1210 if (get_magic_quotes_gpc()) { |
1143 $gpc = stripslashes($gpc); |
1211 $gpc = stripslashes($gpc); |
1144 } |
1212 } |
1145 |
1213 |
1146 return $wpdb->escape($gpc); |
1214 return esc_sql($gpc); |
1147 } |
1215 } |
1148 |
1216 |
1149 /** |
1217 /** |
1150 * Navigates through an array and removes slashes from the values. |
1218 * Navigates through an array and removes slashes from the values. |
1151 * |
1219 * |
1216 * @param array $matches Single Regex Match. |
1284 * @param array $matches Single Regex Match. |
1217 * @return string HTML A element with URI address. |
1285 * @return string HTML A element with URI address. |
1218 */ |
1286 */ |
1219 function _make_url_clickable_cb($matches) { |
1287 function _make_url_clickable_cb($matches) { |
1220 $url = $matches[2]; |
1288 $url = $matches[2]; |
1289 |
|
1221 $url = esc_url($url); |
1290 $url = esc_url($url); |
1222 if ( empty($url) ) |
1291 if ( empty($url) ) |
1223 return $matches[0]; |
1292 return $matches[0]; |
1293 |
|
1224 return $matches[1] . "<a href=\"$url\" rel=\"nofollow\">$url</a>"; |
1294 return $matches[1] . "<a href=\"$url\" rel=\"nofollow\">$url</a>"; |
1225 } |
1295 } |
1226 |
1296 |
1227 /** |
1297 /** |
1228 * Callback to convert URL match to HTML A element. |
1298 * Callback to convert URL match to HTML A element. |
1241 $dest = $matches[2]; |
1311 $dest = $matches[2]; |
1242 $dest = 'http://' . $dest; |
1312 $dest = 'http://' . $dest; |
1243 $dest = esc_url($dest); |
1313 $dest = esc_url($dest); |
1244 if ( empty($dest) ) |
1314 if ( empty($dest) ) |
1245 return $matches[0]; |
1315 return $matches[0]; |
1246 // removed trailing [,;:] from URL |
1316 |
1247 if ( in_array(substr($dest, -1), array('.', ',', ';', ':')) === true ) { |
1317 // removed trailing [.,;:)] from URL |
1318 if ( in_array( substr($dest, -1), array('.', ',', ';', ':', ')') ) === true ) { |
|
1248 $ret = substr($dest, -1); |
1319 $ret = substr($dest, -1); |
1249 $dest = substr($dest, 0, strlen($dest)-1); |
1320 $dest = substr($dest, 0, strlen($dest)-1); |
1250 } |
1321 } |
1251 return $matches[1] . "<a href=\"$dest\" rel=\"nofollow\">$dest</a>" . $ret; |
1322 return $matches[1] . "<a href=\"$dest\" rel=\"nofollow\">$dest</a>$ret"; |
1252 } |
1323 } |
1253 |
1324 |
1254 /** |
1325 /** |
1255 * Callback to convert email address match to HTML A element. |
1326 * Callback to convert email address match to HTML A element. |
1256 * |
1327 * |
1280 * @return string Content with converted URIs. |
1351 * @return string Content with converted URIs. |
1281 */ |
1352 */ |
1282 function make_clickable($ret) { |
1353 function make_clickable($ret) { |
1283 $ret = ' ' . $ret; |
1354 $ret = ' ' . $ret; |
1284 // in testing, using arrays here was found to be faster |
1355 // in testing, using arrays here was found to be faster |
1285 $ret = preg_replace_callback('#(?<=[\s>])(\()?([\w]+?://(?:[\w\\x80-\\xff\#$%&~/\-=?@\[\](+]|[.,;:](?![\s<])|(?(1)\)(?![\s<])|\)))+)#is', '_make_url_clickable_cb', $ret); |
1356 $ret = preg_replace_callback('#(?<=[\s>])(\()?([\w]+?://(?:[\w\\x80-\\xff\#$%&~/=?@\[\](+-]|[.,;:](?![\s<]|(\))?([\s]|$))|(?(1)\)(?![\s<.,;:]|$)|\)))+)#is', '_make_url_clickable_cb', $ret); |
1286 $ret = preg_replace_callback('#([\s>])((www|ftp)\.[\w\\x80-\\xff\#$%&~/.\-;:=,?@\[\]+]+)#is', '_make_web_ftp_clickable_cb', $ret); |
1357 $ret = preg_replace_callback('#([\s>])((www|ftp)\.[\w\\x80-\\xff\#$%&~/.\-;:=,?@\[\]+]+)#is', '_make_web_ftp_clickable_cb', $ret); |
1287 $ret = preg_replace_callback('#([\s>])([.0-9a-z_+-]+)@(([0-9a-z-]+\.)+[0-9a-z]{2,})#i', '_make_email_clickable_cb', $ret); |
1358 $ret = preg_replace_callback('#([\s>])([.0-9a-z_+-]+)@(([0-9a-z-]+\.)+[0-9a-z]{2,})#i', '_make_email_clickable_cb', $ret); |
1288 // this one is not in an array because we need it to run last, for cleanup of accidental links within links |
1359 // this one is not in an array because we need it to run last, for cleanup of accidental links within links |
1289 $ret = preg_replace("#(<a( [^>]+?>|>))<a [^>]+?>([^>]+?)</a></a>#i", "$1$3</a>", $ret); |
1360 $ret = preg_replace("#(<a( [^>]+?>|>))<a [^>]+?>([^>]+?)</a></a>#i", "$1$3</a>", $ret); |
1290 $ret = trim($ret); |
1361 $ret = trim($ret); |
1302 function wp_rel_nofollow( $text ) { |
1373 function wp_rel_nofollow( $text ) { |
1303 global $wpdb; |
1374 global $wpdb; |
1304 // This is a pre save filter, so text is already escaped. |
1375 // This is a pre save filter, so text is already escaped. |
1305 $text = stripslashes($text); |
1376 $text = stripslashes($text); |
1306 $text = preg_replace_callback('|<a (.+?)>|i', 'wp_rel_nofollow_callback', $text); |
1377 $text = preg_replace_callback('|<a (.+?)>|i', 'wp_rel_nofollow_callback', $text); |
1307 $text = $wpdb->escape($text); |
1378 $text = esc_sql($text); |
1308 return $text; |
1379 return $text; |
1309 } |
1380 } |
1310 |
1381 |
1311 /** |
1382 /** |
1312 * Callback to used to add rel=nofollow string to HTML A element. |
1383 * Callback to used to add rel=nofollow string to HTML A element. |
1349 |
1420 |
1350 $smiley = trim(reset($smiley)); |
1421 $smiley = trim(reset($smiley)); |
1351 $img = $wpsmiliestrans[$smiley]; |
1422 $img = $wpsmiliestrans[$smiley]; |
1352 $smiley_masked = esc_attr($smiley); |
1423 $smiley_masked = esc_attr($smiley); |
1353 |
1424 |
1354 return " <img src='$siteurl/wp-includes/images/smilies/$img' alt='$smiley_masked' class='wp-smiley' /> "; |
1425 $srcurl = apply_filters('smilies_src', "$siteurl/wp-includes/images/smilies/$img", $img, $siteurl); |
1426 |
|
1427 return " <img src='$srcurl' alt='$smiley_masked' class='wp-smiley' /> "; |
|
1355 } |
1428 } |
1356 |
1429 |
1357 |
1430 |
1358 /** |
1431 /** |
1359 * Convert text equivalent of smilies to images. |
1432 * Convert text equivalent of smilies to images. |
1713 |
1786 |
1714 /** |
1787 /** |
1715 * Generates an excerpt from the content, if needed. |
1788 * Generates an excerpt from the content, if needed. |
1716 * |
1789 * |
1717 * The excerpt word amount will be 55 words and if the amount is greater than |
1790 * The excerpt word amount will be 55 words and if the amount is greater than |
1718 * that, then the string '[...]' will be appended to the excerpt. If the string |
1791 * that, then the string ' [...]' will be appended to the excerpt. If the string |
1719 * is less than 55 words, then the content will be returned as is. |
1792 * is less than 55 words, then the content will be returned as is. |
1720 * |
1793 * |
1794 * The 55 word limit can be modified by plugins/themes using the excerpt_length filter |
|
1795 * The ' [...]' string can be modified by plugins/themes using the excerpt_more filter |
|
1796 * |
|
1721 * @since 1.5.0 |
1797 * @since 1.5.0 |
1722 * |
1798 * |
1723 * @param string $text The exerpt. If set to empty an excerpt is generated. |
1799 * @param string $text The excerpt. If set to empty an excerpt is generated. |
1724 * @return string The excerpt. |
1800 * @return string The excerpt. |
1725 */ |
1801 */ |
1726 function wp_trim_excerpt($text) { |
1802 function wp_trim_excerpt($text) { |
1727 $raw_excerpt = $text; |
1803 $raw_excerpt = $text; |
1728 if ( '' == $text ) { |
1804 if ( '' == $text ) { |
1732 |
1808 |
1733 $text = apply_filters('the_content', $text); |
1809 $text = apply_filters('the_content', $text); |
1734 $text = str_replace(']]>', ']]>', $text); |
1810 $text = str_replace(']]>', ']]>', $text); |
1735 $text = strip_tags($text); |
1811 $text = strip_tags($text); |
1736 $excerpt_length = apply_filters('excerpt_length', 55); |
1812 $excerpt_length = apply_filters('excerpt_length', 55); |
1813 $excerpt_more = apply_filters('excerpt_more', ' ' . '[...]'); |
|
1737 $words = explode(' ', $text, $excerpt_length + 1); |
1814 $words = explode(' ', $text, $excerpt_length + 1); |
1738 if (count($words) > $excerpt_length) { |
1815 if (count($words) > $excerpt_length) { |
1739 array_pop($words); |
1816 array_pop($words); |
1740 array_push($words, '[...]'); |
|
1741 $text = implode(' ', $words); |
1817 $text = implode(' ', $words); |
1818 $text = $text . $excerpt_more; |
|
1742 } |
1819 } |
1743 } |
1820 } |
1744 return apply_filters('wp_trim_excerpt', $text, $raw_excerpt); |
1821 return apply_filters('wp_trim_excerpt', $text, $raw_excerpt); |
1745 } |
1822 } |
1746 |
1823 |
2103 return apply_filters('clean_url', $url, $original_url, $context); |
2180 return apply_filters('clean_url', $url, $original_url, $context); |
2104 } |
2181 } |
2105 |
2182 |
2106 /** |
2183 /** |
2107 * Perform a deep string replace operation to ensure the values in $search are no longer present |
2184 * Perform a deep string replace operation to ensure the values in $search are no longer present |
2108 * |
2185 * |
2109 * Repeats the replacement operation until it no longer replaces anything so as to remove "nested" values |
2186 * Repeats the replacement operation until it no longer replaces anything so as to remove "nested" values |
2110 * e.g. $subject = '%0%0%0DDD', $search ='%0D', $result ='' rather than the '%0%0DD' that |
2187 * e.g. $subject = '%0%0%0DDD', $search ='%0D', $result ='' rather than the '%0%0DD' that |
2111 * str_replace would return |
2188 * str_replace would return |
2112 * |
2189 * |
2113 * @since 2.8.1 |
2190 * @since 2.8.1 |
2114 * @access private |
2191 * @access private |
2115 * |
2192 * |
2116 * @param string|array $search |
2193 * @param string|array $search |
2117 * @param string $subject |
2194 * @param string $subject |
2118 * @return string The processed string |
2195 * @return string The processed string |
2119 */ |
2196 */ |
2120 function _deep_replace($search, $subject){ |
2197 function _deep_replace($search, $subject){ |
2126 $found = true; |
2203 $found = true; |
2127 $subject = str_replace($val, '', $subject); |
2204 $subject = str_replace($val, '', $subject); |
2128 } |
2205 } |
2129 } |
2206 } |
2130 } |
2207 } |
2131 |
2208 |
2132 return $subject; |
2209 return $subject; |
2133 } |
2210 } |
2134 |
2211 |
2135 /** |
2212 /** |
2136 * Escapes data for use in a MySQL query |
2213 * Escapes data for use in a MySQL query |
2215 $translation_table[chr(38)] = '&'; |
2292 $translation_table[chr(38)] = '&'; |
2216 return preg_replace( "/&(?![A-Za-z]{0,4}\w{2,3};|#[0-9]{2,3};)/", "&", strtr($myHTML, $translation_table) ); |
2293 return preg_replace( "/&(?![A-Za-z]{0,4}\w{2,3};|#[0-9]{2,3};)/", "&", strtr($myHTML, $translation_table) ); |
2217 } |
2294 } |
2218 |
2295 |
2219 /** |
2296 /** |
2220 * Escape single quotes, specialchar double quotes, and fix line endings. |
2297 * Escape single quotes, htmlspecialchar " < > &, and fix line endings. |
2221 * |
2298 * |
2299 * Escapes text strings for echoing in JS, both inline (for example in onclick="...") |
|
2300 * and inside <script> tag. Note that the strings have to be in single quotes. |
|
2222 * The filter 'js_escape' is also applied here. |
2301 * The filter 'js_escape' is also applied here. |
2223 * |
2302 * |
2224 * @since 2.8.0 |
2303 * @since 2.8.0 |
2225 * |
2304 * |
2226 * @param string $text The text to be escaped. |
2305 * @param string $text The text to be escaped. |
2228 */ |
2307 */ |
2229 function esc_js( $text ) { |
2308 function esc_js( $text ) { |
2230 $safe_text = wp_check_invalid_utf8( $text ); |
2309 $safe_text = wp_check_invalid_utf8( $text ); |
2231 $safe_text = _wp_specialchars( $safe_text, ENT_COMPAT ); |
2310 $safe_text = _wp_specialchars( $safe_text, ENT_COMPAT ); |
2232 $safe_text = preg_replace( '/&#(x)?0*(?(1)27|39);?/i', "'", stripslashes( $safe_text ) ); |
2311 $safe_text = preg_replace( '/&#(x)?0*(?(1)27|39);?/i', "'", stripslashes( $safe_text ) ); |
2233 $safe_text = preg_replace( "/\r?\n/", "\\n", addslashes( $safe_text ) ); |
2312 $safe_text = str_replace( "\r", '', $safe_text ); |
2313 $safe_text = str_replace( "\n", '\\n', addslashes( $safe_text ) ); |
|
2234 return apply_filters( 'js_escape', $safe_text, $text ); |
2314 return apply_filters( 'js_escape', $safe_text, $text ); |
2235 } |
2315 } |
2236 |
2316 |
2237 /** |
2317 /** |
2238 * Escape single quotes, specialchar double quotes, and fix line endings. |
2318 * Escape single quotes, specialchar double quotes, and fix line endings. |
2261 */ |
2341 */ |
2262 function esc_html( $text ) { |
2342 function esc_html( $text ) { |
2263 $safe_text = wp_check_invalid_utf8( $text ); |
2343 $safe_text = wp_check_invalid_utf8( $text ); |
2264 $safe_text = _wp_specialchars( $safe_text, ENT_QUOTES ); |
2344 $safe_text = _wp_specialchars( $safe_text, ENT_QUOTES ); |
2265 return apply_filters( 'esc_html', $safe_text, $text ); |
2345 return apply_filters( 'esc_html', $safe_text, $text ); |
2266 return $text; |
|
2267 } |
2346 } |
2268 |
2347 |
2269 /** |
2348 /** |
2270 * Escaping for HTML blocks |
2349 * Escaping for HTML blocks |
2271 * @deprecated 2.8.0 |
2350 * @deprecated 2.8.0 |
2372 case 'thumbnail_size_h': |
2451 case 'thumbnail_size_h': |
2373 case 'medium_size_w': |
2452 case 'medium_size_w': |
2374 case 'medium_size_h': |
2453 case 'medium_size_h': |
2375 case 'large_size_w': |
2454 case 'large_size_w': |
2376 case 'large_size_h': |
2455 case 'large_size_h': |
2456 case 'embed_size_h': |
|
2377 case 'default_post_edit_rows': |
2457 case 'default_post_edit_rows': |
2378 case 'mailserver_port': |
2458 case 'mailserver_port': |
2379 case 'comment_max_links': |
2459 case 'comment_max_links': |
2380 case 'page_on_front': |
2460 case 'page_on_front': |
2381 case 'rss_excerpt_length': |
2461 case 'rss_excerpt_length': |
2383 case 'default_email_category': |
2463 case 'default_email_category': |
2384 case 'default_link_category': |
2464 case 'default_link_category': |
2385 case 'close_comments_days_old': |
2465 case 'close_comments_days_old': |
2386 case 'comments_per_page': |
2466 case 'comments_per_page': |
2387 case 'thread_comments_depth': |
2467 case 'thread_comments_depth': |
2388 $value = abs((int) $value); |
2468 case 'users_can_register': |
2469 $value = absint( $value ); |
|
2470 break; |
|
2471 |
|
2472 case 'embed_size_w': |
|
2473 if ( '' !== $value ) |
|
2474 $value = absint( $value ); |
|
2389 break; |
2475 break; |
2390 |
2476 |
2391 case 'posts_per_page': |
2477 case 'posts_per_page': |
2392 case 'posts_per_rss': |
2478 case 'posts_per_rss': |
2393 $value = (int) $value; |
2479 $value = (int) $value; |
2616 * @param integer $str String to get the excerpt from. |
2702 * @param integer $str String to get the excerpt from. |
2617 * @param integer $count Maximum number of characters to take. |
2703 * @param integer $count Maximum number of characters to take. |
2618 * @return string The excerpt. |
2704 * @return string The excerpt. |
2619 */ |
2705 */ |
2620 function wp_html_excerpt( $str, $count ) { |
2706 function wp_html_excerpt( $str, $count ) { |
2621 $str = strip_tags( $str ); |
2707 $str = wp_strip_all_tags( $str, true ); |
2622 $str = mb_substr( $str, 0, $count ); |
2708 $str = mb_substr( $str, 0, $count ); |
2623 // remove part of an entity at the end |
2709 // remove part of an entity at the end |
2624 $str = preg_replace( '/&[^;\s]{0,6}$/', '', $str ); |
2710 $str = preg_replace( '/&[^;\s]{0,6}$/', '', $str ); |
2625 return $str; |
2711 return $str; |
2626 } |
2712 } |
2683 $tags = implode('|', (array)$tags); |
2769 $tags = implode('|', (array)$tags); |
2684 return preg_replace_callback("!<($tags)(.+?)>!i", |
2770 return preg_replace_callback("!<($tags)(.+?)>!i", |
2685 create_function('$m', 'return _links_add_target($m, "' . $target . '");'), |
2771 create_function('$m', 'return _links_add_target($m, "' . $target . '");'), |
2686 $content); |
2772 $content); |
2687 } |
2773 } |
2774 |
|
2688 /** |
2775 /** |
2689 * Callback to add a target attribute to all links in passed content. |
2776 * Callback to add a target attribute to all links in passed content. |
2690 * |
2777 * |
2691 * @since 2.7.0 |
2778 * @since 2.7.0 |
2692 * @access private |
2779 * @access private |
2707 $str = str_replace("\r", "\n", $str); |
2794 $str = str_replace("\r", "\n", $str); |
2708 $str = preg_replace( array( '/\n+/', '/[ \t]+/' ), array( "\n", ' ' ), $str ); |
2795 $str = preg_replace( array( '/\n+/', '/[ \t]+/' ), array( "\n", ' ' ), $str ); |
2709 return $str; |
2796 return $str; |
2710 } |
2797 } |
2711 |
2798 |
2799 /** |
|
2800 * Properly strip all HTML tags including script and style |
|
2801 * |
|
2802 * @since 2.9.0 |
|
2803 * |
|
2804 * @param string $string String containing HTML tags |
|
2805 * @param bool $remove_breaks optional Whether to remove left over line breaks and white space chars |
|
2806 * @return string The processed string. |
|
2807 */ |
|
2808 function wp_strip_all_tags($string, $remove_breaks = false) { |
|
2809 $string = preg_replace( '@<(script|style)[^>]*?>.*?</\\1>@si', '', $string ); |
|
2810 $string = strip_tags($string); |
|
2811 |
|
2812 if ( $remove_breaks ) |
|
2813 $string = preg_replace('/[\r\n\t ]+/', ' ', $string); |
|
2814 |
|
2815 return trim($string); |
|
2816 } |
|
2817 |
|
2818 /** |
|
2819 * Sanitize a string from user input or from the db |
|
2820 * |
|
2821 * check for invalid UTF-8, |
|
2822 * Convert single < characters to entity, |
|
2823 * strip all tags, |
|
2824 * remove line breaks, tabs and extra whitre space, |
|
2825 * strip octets. |
|
2826 * |
|
2827 * @since 2.9 |
|
2828 * |
|
2829 * @param string $str |
|
2830 * @return string |
|
2831 */ |
|
2832 function sanitize_text_field($str) { |
|
2833 $filtered = wp_check_invalid_utf8( $str ); |
|
2834 |
|
2835 if ( strpos($filtered, '<') !== false ) { |
|
2836 $filtered = wp_pre_kses_less_than( $filtered ); |
|
2837 $filtered = wp_strip_all_tags( $filtered, true ); |
|
2838 } else { |
|
2839 $filtered = trim( preg_replace('/[\r\n\t ]+/', ' ', $filtered) ); |
|
2840 } |
|
2841 |
|
2842 $match = array(); |
|
2843 while ( preg_match('/%[a-f0-9]{2}/i', $filtered, $match) ) |
|
2844 $filtered = str_replace($match[0], '', $filtered); |
|
2845 |
|
2846 return apply_filters('sanitize_text_field', $filtered, $str); |
|
2847 } |
|
2848 |
|
2712 ?> |
2849 ?> |