wp_get_script_polyfill()
云策文档标注
概述
wp_get_script_polyfill() 函数用于生成内联脚本,为不通过指定功能测试的浏览器添加 polyfill 脚本。它基于提供的测试条件与脚本句柄映射来构建条件性 polyfill 脚本。
关键要点
- 函数返回一个字符串,包含条件性 polyfill 内联脚本。
- 参数 $scripts 必须是 WP_Scripts 对象,用于访问已注册的脚本信息。
- 参数 $tests 是一个数组,映射功能测试条件到对应的 polyfill 脚本句柄。
- 函数会检查脚本是否已注册,处理脚本 URL 和版本号,并应用 script_loader_src 过滤器。
- 生成的脚本使用 document.write 来同步写入 polyfill,确保在旧浏览器中正确加载。
代码示例
function wp_get_script_polyfill( $scripts, $tests ) {
$polyfill = '';
foreach ( $tests as $test => $handle ) {
if ( ! array_key_exists( $handle, $scripts->registered ) ) {
continue;
}
$src = $scripts->registered[$handle]->src;
$ver = $scripts->registered[$handle]->ver;
if ( ! preg_match( '|^(https?:)?//|', $src ) && ! ( $scripts->content_url && str_starts_with( $src, $scripts->content_url ) ) ) {
$src = $scripts->base_url . $src;
}
if ( ! empty( $ver ) ) {
$src = add_query_arg( 'ver', $ver, $src );
}
/** This filter is documented in wp-includes/class-wp-scripts.php */
$src = esc_url( apply_filters( 'script_loader_src', $src, $handle ) );
if ( ! $src ) {
continue;
}
$polyfill .= (
// Test presence of feature...
'( ' . $test . ' ) || ' .
/*
* ...appending polyfill on any failures. Cautious viewers may balk
* at the `document.write`. Its caveat of synchronous mid-stream
* blocking write is exactly the behavior we need though.
*/
'document.write( '<script src="' . $src . '"></script>' );'
);
}
return $polyfill;
}注意事项
- 函数依赖于 WP_Scripts 对象来获取脚本的 src 和 ver 属性,确保脚本已正确注册。
- 使用 document.write 可能在某些现代浏览器中引发性能问题,但在此场景下是必要的,以确保 polyfill 在旧浏览器中同步加载。
- 脚本 URL 会通过 script_loader_src 过滤器进行修改,开发者可以利用此过滤器自定义脚本加载行为。
原文内容
Returns contents of an inline script used in appending polyfill scripts for browsers which fail the provided tests. The provided array is a mapping from a condition to verify feature support to its polyfill script handle.
Parameters
$scriptsWP_Scriptsrequired- WP_Scripts object.
$testsstring[]required- Features to detect.
Source
function wp_get_script_polyfill( $scripts, $tests ) {
$polyfill = '';
foreach ( $tests as $test => $handle ) {
if ( ! array_key_exists( $handle, $scripts->registered ) ) {
continue;
}
$src = $scripts->registered[ $handle ]->src;
$ver = $scripts->registered[ $handle ]->ver;
if ( ! preg_match( '|^(https?:)?//|', $src ) && ! ( $scripts->content_url && str_starts_with( $src, $scripts->content_url ) ) ) {
$src = $scripts->base_url . $src;
}
if ( ! empty( $ver ) ) {
$src = add_query_arg( 'ver', $ver, $src );
}
/** This filter is documented in wp-includes/class-wp-scripts.php */
$src = esc_url( apply_filters( 'script_loader_src', $src, $handle ) );
if ( ! $src ) {
continue;
}
$polyfill .= (
// Test presence of feature...
'( ' . $test . ' ) || ' .
/*
* ...appending polyfill on any failures. Cautious viewers may balk
* at the `document.write`. Its caveat of synchronous mid-stream
* blocking write is exactly the behavior we need though.
*/
'document.write( '<script src="' .
$src .
'"></scr' + 'ipt>' );'
);
}
return $polyfill;
}
Hooks
- apply_filters( ‘script_loader_src’, string $src, string $handle )
Filters the script loader source.
Changelog
| Version | Description |
|---|---|
| 5.0.0 | Introduced. |
User Contributed Notes
You must log in before being able to contribute a note or feedback.