| 4063 | function print_emoji_detection_script() { |
| 4064 | global $wp_version; |
| 4065 | |
| 4066 | static $printed = false; |
| 4067 | if ( $printed ) { |
| 4068 | return; |
| 4069 | } |
| 4070 | $printed = true; |
| 4071 | |
| 4072 | $min = SCRIPT_DEBUG ? '' : '.min'; |
| 4073 | |
| 4074 | $settings = array( |
| 4075 | /** |
| 4076 | * Filter the URL where emoji images are hosted. |
| 4077 | * |
| 4078 | * @since 4.2.0 |
| 4079 | * |
| 4080 | * @param string The emoji base URL. |
| 4081 | */ |
| 4082 | 'baseUrl' => apply_filters( 'emoji_url', '//s0.wp.com/wp-content/mu-plugins/emoji/twemoji/72x72' ), |
| 4083 | |
| 4084 | /** |
| 4085 | * Filter the extension of the emoji files. |
| 4086 | * |
| 4087 | * @since 4.2.0 |
| 4088 | * |
| 4089 | * @param string The emoji extension. Default .png. |
| 4090 | */ |
| 4091 | 'ext' => apply_filters( 'emoji_ext', '.png' ), |
| 4092 | 'js' => array( |
| 4093 | 'wpemoji' => includes_url( "js/wp-emoji$min.js" ), |
| 4094 | 'twemoji' => includes_url( "js/twemoji$min.js" ), |
| 4095 | ), |
| 4096 | 'ver' => $wp_version, |
| 4097 | ); |
| 4098 | |
| 4099 | if ( SCRIPT_DEBUG ) { |
| 4100 | ?> |
| 4101 | <script type="text/javascript"> |
| 4102 | ( function( window ) { |
| 4103 | window._wpemojiSettings = <?php echo wp_json_encode( $settings ); ?>; |
| 4104 | /** |
| 4105 | * Detect if the browser supports rendering emoji or flag emoji. Flag emoji are a single glyph |
| 4106 | * made of two characters, so some browsers (notably, Firefox OS X) don't support them. |
| 4107 | * |
| 4108 | * @since 4.2.0 |
| 4109 | * |
| 4110 | * @param type {String} Whether to test for support of "simple" or "flag" emoji. |
| 4111 | * @return {Boolean} True if the browser can render emoji, false if it cannot. |
| 4112 | */ |
| 4113 | browserSupportsEmoji = function( type ) { |
| 4114 | var canvas = document.createElement( 'canvas' ), |
| 4115 | context = canvas.getContext && canvas.getContext( '2d' ); |
| 4116 | |
| 4117 | if ( ! context || ! context.fillText ) { |
| 4118 | return false; |
| 4119 | } |
| 4120 | |
| 4121 | /* |
| 4122 | * Chrome on OS X added native emoji rendering in M41. Unfortunately, |
| 4123 | * it doesn't work when the font is bolder than 500 weight. So, we |
| 4124 | * check for bold rendering support to avoid invisible emoji in Chrome. |
| 4125 | */ |
| 4126 | context.textBaseline = 'top'; |
| 4127 | context.font = '600 32px Arial'; |
| 4128 | |
| 4129 | if ( type === 'flag' ) { |
| 4130 | /* |
| 4131 | * This works because the image will be one of three things: |
| 4132 | * - Two empty squares, if the browser doesn't render emoji |
| 4133 | * - Two squares with 'G' and 'B' in them, if the browser doesn't render flag emoji |
| 4134 | * - The British flag |
| 4135 | * |
| 4136 | * The first two will encode to small images (1-2KB data URLs), the third will encode |
| 4137 | * to a larger image (4-5KB data URL). |
| 4138 | */ |
| 4139 | context.fillText( String.fromCharCode( 55356, 56812, 55356, 56807 ), 0, 0 ); |
| 4140 | return canvas.toDataURL().length > 3000; |
| 4141 | } else { |
| 4142 | /* |
| 4143 | * This creates a smiling emoji, and checks to see if there is any image data in the |
| 4144 | * center pixel. In browsers that don't support emoji, the character will be rendered |
| 4145 | * as an empty square, so the center pixel will be blank. |
| 4146 | */ |
| 4147 | context.fillText( String.fromCharCode( 55357, 56835 ), 0, 0 ); |
| 4148 | return context.getImageData( 16, 16, 1, 1 ).data[0] !== 0; |
| 4149 | } |
| 4150 | } |
| 4151 | |
| 4152 | if ( ! browserSupportsEmoji( 'simple' ) || ! browserSupportsEmoji( 'flag' ) ) { |
| 4153 | var script = document.createElement( 'script' ); |
| 4154 | script.src = window._wpemojiSettings.js.twemoji + '?ver=' + window._wpemojiSettings.ver; |
| 4155 | script.type = 'text/javascript'; |
| 4156 | document.getElementsByTagName( 'head' )[0].appendChild( script ); |
| 4157 | |
| 4158 | waitForTwemoji(); |
| 4159 | } |
| 4160 | |
| 4161 | function waitForTwemoji() { |
| 4162 | if ( ! window.twemoji ) { |
| 4163 | // Still waiting. |
| 4164 | window.setTimeout( waitForTwemoji, 50 ); |
| 4165 | } |
| 4166 | |
| 4167 | var script = document.createElement( 'script' ); |
| 4168 | script.src = window._wpemojiSettings.js.wpemoji + '?ver=' + window._wpemojiSettings.ver; |
| 4169 | script.type = 'text/javascript'; |
| 4170 | document.getElementsByTagName( 'head' )[0].appendChild( script ); |
| 4171 | } |
| 4172 | |
| 4173 | } )( window ); |
| 4174 | </script> |
| 4175 | <?php |
| 4176 | } else { // ! SCRIPT_DEBUG |
| 4177 | ?> |
| 4178 | <script type="text/javascript"> |
| 4179 | // TODO: Minified version of the emoji detection script |
| 4180 | </script> |
| 4181 | <?php |
| 4182 | } |
| 4183 | } |
| 4184 | |