value ],数组值自动 json 编码。 * @return string */ public static function render( $template, array $vars ) { $text = (string) $template; foreach ( $vars as $k => $v ) { if ( is_array( $v ) ) { $v = wp_json_encode( $v, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES ); } $text = preg_replace_callback( '/\{\{\s*' . preg_quote( $k, '/' ) . '\s*\}\}/iu', // 必须走 callback:待翻译文本里出现 $0、$1、\1 时, // preg_replace 会把它们当成反向引用替换掉,指令就被改坏了。 function ( $matches ) use ( $v ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter return (string) $v; }, $text ); } return $text; } }