From 69914b0ae250dfd96b38f1bade47b60d1acbc865 Mon Sep 17 00:00:00 2001 From: ryan Date: Tue, 6 Jan 2009 18:20:47 +0000 Subject: [PATCH] Faster smilies. Props johanee. fixes #6464 git-svn-id: http://svn.automattic.com/wordpress/trunk@10322 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/formatting.php | 42 +++++++++++++++++++++++++++----- wp-includes/functions.php | 49 +++++++++++++++++++++++++++----------- 2 files changed, 71 insertions(+), 20 deletions(-) diff --git a/wp-includes/formatting.php b/wp-includes/formatting.php index dacef92276..4cc02b27ff 100644 --- a/wp-includes/formatting.php +++ b/wp-includes/formatting.php @@ -1218,29 +1218,59 @@ function wp_rel_nofollow_callback( $matches ) { return ""; } + +/** + * Convert one smiley code to the icon graphic file equivalent. + * + * Looks up one smiley code in the $wpsmiliestrans global array and returns an + * string for that smiley. + * + * @global array $wpsmiliestrans + * @since 2.8.0 + * + * @param string $smiley Smiley code to convert to image. + * @return string Image string for smiley. + */ +function translate_smiley($smiley) { + global $wpsmiliestrans; + + if (count($smiley) == 0) { + return ''; + } + + $siteurl = get_option( 'siteurl' ); + + $smiley = trim(reset($smiley)); + $img = $wpsmiliestrans[$smiley]; + $smiley_masked = attribute_escape($smiley); + + return " $smiley_masked "; +} + + /** * Convert text equivalent of smilies to images. * - * Will only convert smilies if the option 'use_smilies' is true and the globals - * used in the function aren't empty. + * Will only convert smilies if the option 'use_smilies' is true and the global + * used in the function isn't empty. * * @since 0.71 - * @uses $wp_smiliessearch, $wp_smiliesreplace Smiley replacement arrays. + * @uses $wp_smiliessearch * * @param string $text Content to convert smilies from text. * @return string Converted content with text smilies replaced with images. */ function convert_smilies($text) { - global $wp_smiliessearch, $wp_smiliesreplace; + global $wp_smiliessearch; $output = ''; - if ( get_option('use_smilies') && !empty($wp_smiliessearch) && !empty($wp_smiliesreplace) ) { + if ( get_option('use_smilies') && !empty($wp_smiliessearch) ) { // HTML loop taken from texturize function, could possible be consolidated $textarr = preg_split("/(<.*>)/U", $text, -1, PREG_SPLIT_DELIM_CAPTURE); // capture the tags as well as in between $stop = count($textarr);// loop stuff for ($i = 0; $i < $stop; $i++) { $content = $textarr[$i]; if ((strlen($content) > 0) && ('<' != $content{0})) { // If it's not a tag - $content = preg_replace($wp_smiliessearch, $wp_smiliesreplace, $content); + $content = preg_replace_callback($wp_smiliessearch, 'translate_smiley', $content); } $output .= $content; } diff --git a/wp-includes/functions.php b/wp-includes/functions.php index de8c89cbc9..2a10840126 100644 --- a/wp-includes/functions.php +++ b/wp-includes/functions.php @@ -2366,6 +2366,7 @@ function _mce_set_direction( $input ) { return $input; } + /** * Convert smiley code to the icon graphic file equivalent. * @@ -2376,25 +2377,19 @@ function _mce_set_direction( $input ) { * to an array, with the key the code the blogger types in and the value the * image file. * - * The $wp_smiliessearch global is for the regular expression array and is - * set each time the function is called. The $wp_smiliesreplace is the full - * replacement. Supposely, the $wp_smiliessearch array is looped over using - * preg_replace() or just setting the array of $wp_smiliessearch along with the - * array of $wp_smiliesreplace in the search and replace parameters of - * preg_replace(), which would be faster and less overhead. Either way, both are - * used with preg_replace() and can be defined after the function is called. + * The $wp_smiliessearch global is for the regular expression and is set each + * time the function is called. * * The full list of smilies can be found in the function and won't be listed in * the description. Probably should create a Codex page for it, so that it is * available. * * @global array $wpsmiliestrans - * @global array $wp_smiliesreplace * @global array $wp_smiliessearch * @since 2.2.0 */ function smilies_init() { - global $wpsmiliestrans, $wp_smiliessearch, $wp_smiliesreplace; + global $wpsmiliestrans, $wp_smiliessearch; // don't bother setting up smilies if they are disabled if ( !get_option( 'use_smilies' ) ) @@ -2449,12 +2444,38 @@ function smilies_init() { ); } - $siteurl = get_option( 'siteurl' ); - foreach ( (array) $wpsmiliestrans as $smiley => $img ) { - $wp_smiliessearch[] = '/(\s|^)' . preg_quote( $smiley, '/' ) . '(\s|$)/'; - $smiley_masked = attribute_escape( trim( $smiley ) ); - $wp_smiliesreplace[] = " $smiley_masked "; + if (count($wpsmiliestrans) == 0) { + return; } + + /* + * NOTE: we sort the smilies in reverse key order. This is to make sure + * we match the longest possible smilie (:???: vs :?) as the regular + * expression used below is first-match + */ + krsort($wpsmiliestrans); + + $wp_smiliessearch = '/(?:\s|^)'; + + $subchar = ''; + foreach ( (array) $wpsmiliestrans as $smiley => $img ) { + $firstchar = substr($smiley, 0, 1); + $rest = substr($smiley, 1); + + // new subpattern? + if ($firstchar != $subchar) { + if ($subchar != '') { + $wp_smiliessearch .= ')|'; + } + $subchar = $firstchar; + $wp_smiliessearch .= preg_quote($firstchar, '/') . '(?:'; + } else { + $wp_smiliessearch .= '|'; + } + $wp_smiliessearch .= preg_quote($rest); + } + + $wp_smiliessearch .= ')(?:\s|$)/'; } /**