wp_staticize_emoji_for_email()
云策文档标注
概述
wp_staticize_emoji_for_email() 函数用于在电子邮件中将 emoji 转换为静态图像,以确保兼容性。它处理邮件数据数组,仅在内容类型为 'text/html' 时执行转换。
关键要点
- 函数接受一个邮件数据数组参数 $mail,并返回处理后的数组。
- 转换仅适用于 'text/html' 内容类型的邮件,通过检查邮件头和应用 'wp_mail_content_type' 过滤器来确定。
- 内部调用 wp_staticize_emoji() 函数来实际执行 emoji 到静态图像的转换。
- 如果邮件数组中没有 'message' 键,函数会直接返回原数组。
代码示例
function wp_staticize_emoji_for_email( $mail ) {
if ( ! isset( $mail['message'] ) ) {
return $mail;
}
$headers = array();
if ( isset( $mail['headers'] ) ) {
if ( is_array( $mail['headers'] ) ) {
$headers = $mail['headers'];
} else {
$headers = explode( "n", str_replace( "rn", "n", $mail['headers'] ) );
}
}
foreach ( $headers as $header ) {
if ( ! str_contains( $header, ':' ) ) {
continue;
}
list( $name, $content ) = explode( ':', trim( $header ), 2 );
$name = trim( $name );
$content = trim( $content );
if ( 'content-type' === strtolower( $name ) ) {
if ( str_contains( $content, ';' ) ) {
list( $type, $charset ) = explode( ';', $content );
$content_type = trim( $type );
} else {
$content_type = trim( $content );
}
break;
}
}
if ( ! isset( $content_type ) ) {
$content_type = 'text/plain';
}
$content_type = apply_filters( 'wp_mail_content_type', $content_type );
if ( 'text/html' === $content_type ) {
$mail['message'] = wp_staticize_emoji( $mail['message'] );
}
return $mail;
}注意事项
- 函数依赖于 wp_staticize_emoji() 来执行转换,确保该函数可用。
- 邮件头解析支持数组或字符串格式,并处理换行符。
- 使用 'wp_mail_content_type' 过滤器允许插件修改内容类型,影响转换逻辑。
原文内容
Converts emoji in emails into static images.
Parameters
$mailarrayrequired-
The email data array.
Source
function wp_staticize_emoji_for_email( $mail ) {
if ( ! isset( $mail['message'] ) ) {
return $mail;
}
/*
* We can only transform the emoji into images if it's a `text/html` email.
* To do that, here's a cut down version of the same process that happens
* in wp_mail() - get the `Content-Type` from the headers, if there is one,
* then pass it through the 'wp_mail_content_type' filter, in case
* a plugin is handling changing the `Content-Type`.
*/
$headers = array();
if ( isset( $mail['headers'] ) ) {
if ( is_array( $mail['headers'] ) ) {
$headers = $mail['headers'];
} else {
$headers = explode( "n", str_replace( "rn", "n", $mail['headers'] ) );
}
}
foreach ( $headers as $header ) {
if ( ! str_contains( $header, ':' ) ) {
continue;
}
// Explode them out.
list( $name, $content ) = explode( ':', trim( $header ), 2 );
// Cleanup crew.
$name = trim( $name );
$content = trim( $content );
if ( 'content-type' === strtolower( $name ) ) {
if ( str_contains( $content, ';' ) ) {
list( $type, $charset ) = explode( ';', $content );
$content_type = trim( $type );
} else {
$content_type = trim( $content );
}
break;
}
}
// Set Content-Type if we don't have a content-type from the input headers.
if ( ! isset( $content_type ) ) {
$content_type = 'text/plain';
}
/** This filter is documented in wp-includes/pluggable.php */
$content_type = apply_filters( 'wp_mail_content_type', $content_type );
if ( 'text/html' === $content_type ) {
$mail['message'] = wp_staticize_emoji( $mail['message'] );
}
return $mail;
}
Hooks
- apply_filters( ‘wp_mail_content_type’, string $content_type )
-
Filters the wp_mail() content type.
Changelog
| Version | Description |
|---|---|
| 4.2.0 | Introduced. |