wp_get_script_polyfill

函数
wp_get_script_polyfill ( $scripts, $tests )
参数
  • (WP_Scripts) $scripts WP_Scripts object.
    Required:
  • (string[]) $tests Features to detect.
    Required:
返回值
  • (string) Conditional polyfill inline script.
定义位置
相关方法
wp_get_script_tagwp_widget_descriptionwp_script_iswp_set_script_translationsget_edit_profile_url
引入
5.0.0
弃用
-

wp_get_script_polyfill: 这个函数用于检索polyfill脚本的URL,该脚本可用于在旧浏览器上提供现代功能。它接受脚本句柄作为参数,并返回polyfill脚本的URL。

返回一个内联脚本的内容,用于为未能通过所提供测试的浏览器添加polyfill脚本。提供的数组是一个从验证功能支持的条件到其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 && 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;
}

常见问题

FAQs
查看更多 >