Make WordPress Core

Changeset 31752


Ignore:
Timestamp:
03/12/2015 01:16:34 PM (10 years ago)
Author:
pento
Message:

Emoji: There's a little tear in my eye as I remove DOMDocument from the Emoji staticizer. It was a beautiful dream, but it wasn't to be.

Instead, let's use the tried and trusted smiley replacement algorithm, which has stood the test of time.

See #31242

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/formatting.php

    r31734 r31752  
    41074107 * @since 4.2.0
    41084108 *
    4109  * @param string $content The content to encode.
     4109 * @param string $text The content to encode.
    41104110 * @return string The encoded content.
    41114111 */
    4112 function wp_staticize_emoji( $content ) {
    4113     $content = wp_encode_emoji( $content );
     4112function wp_staticize_emoji( $text ) {
     4113    $text = wp_encode_emoji( $text );
    41144114
    41154115    if ( ! class_exists( 'DOMDocument' ) ) {
    4116         return $content;
     4116        return $text;
    41174117    }
    41184118
     
    41224122    $ext = apply_filters( 'emoji_ext', '.png' );
    41234123
    4124     $html = '<!DOCTYPE html><html><head></head><body>' . $content . '</body></html>';
    4125 
    4126     $document = new DOMDocument;
    4127     if ( ! $document->loadHTML( $html ) ) {
    4128         return $content;
    4129     }
    4130 
    4131     $xpath = new DOMXPath( $document );
    4132     $textnodes = $xpath->query( '//text()' );
    4133 
    4134     foreach( $textnodes as $node ) {
    4135         $originalText = $text = wp_encode_emoji( $node->nodeValue );
    4136 
    4137         $matches = array();
    4138         if ( preg_match_all( '/(&#x1f1(e[6-9a-f]|f[0-9a-f]);){2}/', $text, $matches ) ) {
    4139             if ( ! empty( $matches[0] ) ) {
    4140                 foreach ( $matches[0] as $flag ) {
    4141                     $chars = str_replace( array( '&#x', ';'), '', $flag );
    4142 
    4143                     list( $char1, $char2 ) = str_split( $chars, 5 );
    4144                     $entity = '<img src="https:' . $cdn_url . $char1 . '-' . $char2 . $ext . '" class="wp-smiley" style="height: 1em;" />';
    4145 
    4146                     $text = str_replace( $flag, $entity, $text );
     4124    $output = '';
     4125    // HTML loop taken from smiley function, which was taking from texturize function. It'll never be consolidated.
     4126    $textarr = preg_split( '/(<.*>)/U', $text, -1, PREG_SPLIT_DELIM_CAPTURE ); // capture the tags as well as in between
     4127    $stop = count( $textarr );// loop stuff
     4128
     4129    // Ignore proessing of specific tags
     4130    $tags_to_ignore = 'code|pre|style|script|textarea';
     4131    $ignore_block_element = '';
     4132
     4133    for ( $i = 0; $i < $stop; $i++ ) {
     4134        $content = $textarr[$i];
     4135
     4136        // If we're in an ignore block, wait until we find its closing tag
     4137        if ( '' == $ignore_block_element && preg_match( '/^<(' . $tags_to_ignore . ')>/', $content, $matches ) )  {
     4138            $ignore_block_element = $matches[1];
     4139        }
     4140
     4141        // If it's not a tag and not in ignore block
     4142        if ( '' ==  $ignore_block_element && strlen( $content ) > 0 && '<' != $content[0] ) {
     4143            $matches = array();
     4144            if ( preg_match_all( '/(&#x1f1(e[6-9a-f]|f[0-9a-f]);){2}/', $content, $matches ) ) {
     4145                if ( ! empty( $matches[0] ) ) {
     4146                    foreach ( $matches[0] as $flag ) {
     4147                        $chars = str_replace( array( '&#x', ';'), '', $flag );
     4148
     4149                        list( $char1, $char2 ) = str_split( $chars, 5 );
     4150                        $entity = '<img src="https:' . $cdn_url . $char1 . '-' . $char2 . $ext . '" class="wp-smiley" style="height: 1em;" />';
     4151
     4152                        $content = str_replace( $flag, $entity, $content );
     4153                    }
    41474154                }
    41484155            }
    4149         }
    4150 
    4151         // Loosely match the Emoji Unicode range.
    4152         $regex = '/(&#x[2-3][0-9a-f]{3};|&#x1f[1-6][0-9a-f]{2};)/';
    4153 
    4154         $matches = array();
    4155         if ( preg_match_all( $regex, $text, $matches ) ) {
    4156             if ( ! empty( $matches[1] ) ) {
    4157                 foreach ( $matches[1] as $emoji ) {
    4158                     $char = str_replace( array( '&#x', ';'), '', $emoji );
    4159                     $entity = '<img src="https:' . $cdn_url . $char . $ext . '" class="wp-smiley" style="height: 1em;" />';
    4160 
    4161                     $text = str_replace( $emoji, $entity, $text );
     4156
     4157            // Loosely match the Emoji Unicode range.
     4158            $regex = '/(&#x[2-3][0-9a-f]{3};|&#x1f[1-6][0-9a-f]{2};)/';
     4159
     4160            $matches = array();
     4161            if ( preg_match_all( $regex, $content, $matches ) ) {
     4162                if ( ! empty( $matches[1] ) ) {
     4163                    foreach ( $matches[1] as $emoji ) {
     4164                        $char = str_replace( array( '&#x', ';'), '', $emoji );
     4165                        $entity = '<img src="https:' . $cdn_url . $char . $ext . '" class="wp-smiley" style="height: 1em;" />';
     4166
     4167                        $content = str_replace( $emoji, $entity, $content );
     4168                    }
    41624169                }
    41634170            }
    41644171        }
    41654172
    4166         if ( $originalText !== $text ) {
    4167             $content = str_replace( $originalText, $text, $content );
    4168         }
    4169     }
    4170 
    4171     return $content;
     4173        // did we exit ignore block
     4174        if ( '' != $ignore_block_element && '</' . $ignore_block_element . '>' == $content )  {
     4175            $ignore_block_element = '';
     4176        }
     4177
     4178        $output .= $content;
     4179    }
     4180
     4181    return $output;
    41724182}
    41734183
Note: See TracChangeset for help on using the changeset viewer.