smilies_init()
概述
smilies_init() 函数用于将表情符号代码转换为对应的图标图形文件。它通过全局变量 $wpsmiliestrans 和 $wp_smiliessearch 管理表情符号的映射和正则表达式匹配。
关键要点
- 函数检查 'use_smilies' 选项,若禁用则直接返回,不进行表情符号处理。
- 通过 $wpsmiliestrans 数组定义表情符号代码到图像文件的映射,插件可通过设置此数组覆盖默认列表。
- 使用 apply_filters('smilies', $wpsmiliestrans) 钩子允许过滤所有表情符号,需在 smilies_init() 运行前添加。
- 函数对 $wpsmiliestrans 进行反向键排序,以确保正则表达式优先匹配最长的表情符号代码。
- 生成 $wp_smiliessearch 正则表达式,用于在文本中查找并替换表情符号代码。
代码示例
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:' => "xf0x9fx98x90",
// ... 更多映射项
);
}
$wpsmiliestrans = apply_filters( 'smilies', $wpsmiliestrans );
if ( count( $wpsmiliestrans ) === 0 ) {
return;
}
krsort( $wpsmiliestrans );
$spaces = wp_spaces_regexp();
$wp_smiliessearch = '/(?<=' . $spaces . '|^)'; // 正则表达式构建逻辑
// ... 后续代码
}注意事项
- 表情符号功能可通过 WordPress 后台的撰写设置或设置 'use_smilies' 选项为 false 来禁用。
- 完整的表情符号列表在函数内部定义,未在文档中详细列出,建议参考相关 Codex 页面获取。
- 正则表达式构建时考虑了空格和边界条件,以确保准确匹配。
Converts smiley code to the icon graphic file equivalent.
Description
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.
Source
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:' => "xf0x9fx98x90",
':twisted:' => "xf0x9fx98x88",
':arrow:' => "xe2x9exa1",
':shock:' => "xf0x9fx98xaf",
':smile:' => "xf0x9fx99x82",
':???:' => "xf0x9fx98x95",
':cool:' => "xf0x9fx98x8e",
':evil:' => "xf0x9fx91xbf",
':grin:' => "xf0x9fx98x80",
':idea:' => "xf0x9fx92xa1",
':oops:' => "xf0x9fx98xb3",
':razz:' => "xf0x9fx98x9b",
':roll:' => "xf0x9fx99x84",
':wink:' => "xf0x9fx98x89",
':cry:' => "xf0x9fx98xa5",
':eek:' => "xf0x9fx98xae",
':lol:' => "xf0x9fx98x86",
':mad:' => "xf0x9fx98xa1",
':sad:' => "xf0x9fx99x81",
'8-)' => "xf0x9fx98x8e",
'8-O' => "xf0x9fx98xaf",
':-(' => "xf0x9fx99x81",
':-)' => "xf0x9fx99x82",
':-?' => "xf0x9fx98x95",
':-D' => "xf0x9fx98x80",
':-P' => "xf0x9fx98x9b",
':-o' => "xf0x9fx98xae",
':-x' => "xf0x9fx98xa1",
':-|' => "xf0x9fx98x90",
';-)' => "xf0x9fx98x89",
// This one transformation breaks regular text with frequency.
// '8)' => "xf0x9fx98x8e",
'8O' => "xf0x9fx98xaf",
':(' => "xf0x9fx99x81",
':)' => "xf0x9fx99x82",
':?' => "xf0x9fx98x95",
':D' => "xf0x9fx98x80",
':P' => "xf0x9fx98x9b",
':o' => "xf0x9fx98xae",
':x' => "xf0x9fx98xa1",
':|' => "xf0x9fx98x90",
';)' => "xf0x9fx98x89",
':!:' => "xe2x9dx97",
':?:' => "xe2x9dx93",
);
}
/**
* 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';
}
Hooks
- apply_filters( ‘smilies’, string[] $wpsmiliestrans )
-
Filters all the smilies.
Changelog
| Version | Description |
|---|---|
| 2.2.0 | Introduced. |