WordPress Version: 6.4
/**
* Converts smiley code to the icon graphic file equivalent.
*
* You can turn off smilies, by going to the write setting screen and unchecking
* the box, or by setting 'use_smilies' option to false or removing the option.
*
* Plugins may override the default smiley list by setting the $wpsmiliestrans
* 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 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_smiliessearch
*
* @since 2.2.0
*/
function smilies_init()
{
global $wpsmiliestrans, $wp_smiliessearch;
// Don't bother setting up smilies if they are disabled.
if (!get_option('use_smilies')) {
return;
}
if (!isset($wpsmiliestrans)) {
$wpsmiliestrans = array(
':mrgreen:' => 'mrgreen.png',
':neutral:' => "😐",
':twisted:' => "😈",
':arrow:' => "➡",
':shock:' => "😯",
':smile:' => "🙂",
':???:' => "😕",
':cool:' => "😎",
':evil:' => "👿",
':grin:' => "😀",
':idea:' => "💡",
':oops:' => "😳",
':razz:' => "😛",
':roll:' => "🙄",
':wink:' => "😉",
':cry:' => "😥",
':eek:' => "😮",
':lol:' => "😆",
':mad:' => "😡",
':sad:' => "🙁",
'8-)' => "😎",
'8-O' => "😯",
':-(' => "🙁",
':-)' => "🙂",
':-?' => "😕",
':-D' => "😀",
':-P' => "😛",
':-o' => "😮",
':-x' => "😡",
':-|' => "😐",
';-)' => "😉",
// This one transformation breaks regular text with frequency.
// '8)' => "\xf0\x9f\x98\x8e",
'8O' => "😯",
':(' => "🙁",
':)' => "🙂",
':?' => "😕",
':D' => "😀",
':P' => "😛",
':o' => "😮",
':x' => "😡",
':|' => "😐",
';)' => "😉",
':!:' => "❗",
':?:' => "❓",
);
}
/**
* Filters all the smilies.
*
* This filter must be added before `smilies_init` is run, as
* it is normally only run once to setup the smilies regex.
*
* @since 4.7.0
*
* @param string[] $wpsmiliestrans List of the smilies' hexadecimal representations, keyed by their smily code.
*/
$wpsmiliestrans = apply_filters('smilies', $wpsmiliestrans);
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);
$spaces = wp_spaces_regexp();
// Begin first "subpattern".
$wp_smiliessearch = '/(?<=' . $spaces . '|^)';
$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 .= ')(?=' . $spaces . '|$)';
// End previous "subpattern".
$wp_smiliessearch .= '|(?<=' . $spaces . '|^)';
// Begin another "subpattern".
}
$subchar = $firstchar;
$wp_smiliessearch .= preg_quote($firstchar, '/') . '(?:';
} else {
$wp_smiliessearch .= '|';
}
$wp_smiliessearch .= preg_quote($rest, '/');
}
$wp_smiliessearch .= ')(?=' . $spaces . '|$)/m';
}