Make WordPress Core

Ticket #6464: faster-smilies3.patch

File faster-smilies3.patch, 4.7 KB (added by johanee, 17 years ago)

Faster regexp and uses preg_replace_callback(), tested, fixed

  • wp-includes/functions.php

     
    23662366        return $input;
    23672367}
    23682368
     2369
    23692370/**
    23702371 * Convert smiley code to the icon graphic file equivalent.
    23712372 *
     
    23762377 * to an array, with the key the code the blogger types in and the value the
    23772378 * image file.
    23782379 *
    2379  * The $wp_smiliessearch global is for the regular expression array and is
    2380  * set each time the function is called. The $wp_smiliesreplace is the full
    2381  * replacement. Supposely, the $wp_smiliessearch array is looped over using
    2382  * preg_replace() or just setting the array of $wp_smiliessearch along with the
    2383  * array of $wp_smiliesreplace in the search and replace parameters of
    2384  * preg_replace(), which would be faster and less overhead. Either way, both are
    2385  * used with preg_replace() and can be defined after the function is called.
     2380 * The $wp_smiliessearch global is for the regular expression and is set each
     2381 * time the function is called.
    23862382 *
    23872383 * The full list of smilies can be found in the function and won't be listed in
    23882384 * the description. Probably should create a Codex page for it, so that it is
     
    24492445                );
    24502446        }
    24512447
    2452         $siteurl = get_option( 'siteurl' );
     2448        if (count($wpsmiliestrans) == 0) {
     2449                return;
     2450        }
     2451
     2452        /*
     2453         * NOTE: we sort the smilies in reverse key order. This is to make sure
     2454         * we match the longest possible smilie (:???: vs :?) as the regular
     2455         * expression used below is first-match
     2456         */
     2457        krsort($wpsmiliestrans);
     2458
     2459        $wp_smiliessearch = '/(?:\s|^)';
     2460
     2461        $subchar = '';
    24532462        foreach ( (array) $wpsmiliestrans as $smiley => $img ) {
    2454                 $wp_smiliessearch[] = '/(\s|^)' . preg_quote( $smiley, '/' ) . '(\s|$)/';
    2455                 $smiley_masked = attribute_escape( trim( $smiley ) );
    2456                 $wp_smiliesreplace[] = " <img src='$siteurl/wp-includes/images/smilies/$img' alt='$smiley_masked' class='wp-smiley' /> ";
     2463                $firstchar = substr($smiley, 0, 1);
     2464                $rest = substr($smiley, 1);
     2465
     2466                // new subpattern?
     2467                if ($firstchar != $subchar) {
     2468                        if ($subchar != '') {
     2469                                $wp_smiliessearch .= ')|';
     2470                        }
     2471                        $subchar = $firstchar;
     2472                        $wp_smiliessearch .= preg_quote($firstchar, '/') . '(?:';
     2473                } else {
     2474                        $wp_smiliessearch .= '|';
     2475                }
     2476                $wp_smiliessearch .= preg_quote($rest);
    24572477        }
     2478
     2479        $wp_smiliessearch .= ')(?:\s|$)/';
    24582480}
    24592481
    24602482/**
  • wp-includes/formatting.php

     
    12181218        return "<a $text rel=\"nofollow\">";
    12191219}
    12201220
     1221
    12211222/**
     1223 * Convert one smiley code to the icon graphic file equivalent.
     1224 *
     1225 * Looks up one smiley code in the $wpsmiliestrans global array and returns an
     1226 * <img> string for that smiley.
     1227 *
     1228 * @global array $wpsmiliestrans
     1229 * @since 2.8.0
     1230 *
     1231 * @param string $smiley Smiley code to convert to image.
     1232 * @return string Image string for smiley.
     1233 */
     1234function translate_smiley($smiley) {
     1235        global $wpsmiliestrans;
     1236
     1237        if (count($smiley) == 0) {
     1238                return '';
     1239        }
     1240
     1241        $siteurl = get_option( 'siteurl' );
     1242
     1243        $smiley = trim(reset($smiley));
     1244        $img = $wpsmiliestrans[$smiley];
     1245        $smiley_masked = attribute_escape($smiley);
     1246
     1247        return " <img src='$siteurl/wp-includes/images/smilies/$img' alt='$smiley_masked' class='wp-smiley' /> ";
     1248}
     1249
     1250
     1251/**
    12221252 * Convert text equivalent of smilies to images.
    12231253 *
    1224  * Will only convert smilies if the option 'use_smilies' is true and the globals
    1225  * used in the function aren't empty.
     1254 * Will only convert smilies if the option 'use_smilies' is true and the global
     1255 * used in the function isn't empty.
    12261256 *
    12271257 * @since 0.71
    1228  * @uses $wp_smiliessearch, $wp_smiliesreplace Smiley replacement arrays.
     1258 * @uses $wp_smiliessearch
    12291259 *
    12301260 * @param string $text Content to convert smilies from text.
    12311261 * @return string Converted content with text smilies replaced with images.
     
    12331263function convert_smilies($text) {
    12341264        global $wp_smiliessearch, $wp_smiliesreplace;
    12351265        $output = '';
    1236         if ( get_option('use_smilies') && !empty($wp_smiliessearch) && !empty($wp_smiliesreplace) ) {
     1266        if ( get_option('use_smilies') && !empty($wp_smiliessearch) ) {
    12371267                // HTML loop taken from texturize function, could possible be consolidated
    12381268                $textarr = preg_split("/(<.*>)/U", $text, -1, PREG_SPLIT_DELIM_CAPTURE); // capture the tags as well as in between
    12391269                $stop = count($textarr);// loop stuff
    12401270                for ($i = 0; $i < $stop; $i++) {
    12411271                        $content = $textarr[$i];
    12421272                        if ((strlen($content) > 0) && ('<' != $content{0})) { // If it's not a tag
    1243                                 $content = preg_replace($wp_smiliessearch, $wp_smiliesreplace, $content);
     1273                                $content = preg_replace_callback($wp_smiliessearch, 'translate_smiley', $content);
    12441274                        }
    12451275                        $output .= $content;
    12461276                }