返回内联脚本的内容,该内联脚本用于为未通过提供的测试的浏览器附加polyfill脚本。提供的数组是从条件到验证要素支持到其polyfill脚本句柄的映射。

原型

wp_get_script_polyfill( WP_Scripts $scripts, array $tests )

参数

$scripts (WP_Scripts) (Required) WP_Scripts对象。

$tests (array) (Required) 要检测的功能。

返回值

(string)  条件polyfill内联脚本。

源文件

路径:wp-includes/script-loader.php

<?php
...
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 && 0 === strpos( $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;
}
...
?>

其他

英文文档:https://developer.wordpress.org/reference/functions/wp_get_script_polyfill/